Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/kcp-dev/kcp-operator/internal/controller/kubeconfig"
"github.com/kcp-dev/kcp-operator/internal/controller/rootshard"
"github.com/kcp-dev/kcp-operator/internal/controller/shard"
"github.com/kcp-dev/kcp-operator/internal/metrics"
"github.com/kcp-dev/kcp-operator/internal/reconciling"
operatorv1alpha1 "github.com/kcp-dev/kcp-operator/sdk/apis/operator/v1alpha1"
)
Expand Down Expand Up @@ -190,6 +191,12 @@ func main() {
}
// +kubebuilder:scaffold:builder

metrics.RegisterMetrics()

metricsCollector := metrics.NewMetricsCollector(mgr.GetClient())
ctx := ctrl.SetupSignalHandler()
go metricsCollector.Start(ctx)

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
Expand All @@ -200,7 +207,7 @@ func main() {
}

setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
if err := mgr.Start(ctx); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/kcp-dev/kcp-operator/sdk v0.0.0-00010101000000-000000000000
github.com/kcp-dev/kcp/sdk v0.27.1
github.com/kcp-dev/logicalcluster/v3 v3.0.5
github.com/prometheus/client_golang v1.20.5
github.com/stretchr/testify v1.10.0
go.uber.org/zap v1.27.0
k8c.io/reconciler v0.5.0
Expand Down Expand Up @@ -68,7 +69,6 @@ require (
github.com/onsi/gomega v1.35.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.61.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
Expand Down
17 changes: 17 additions & 0 deletions internal/controller/frontproxy/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package frontproxy
import (
"context"
"fmt"
"time"

certmanagerv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"

Expand All @@ -38,6 +39,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/kcp-dev/kcp-operator/internal/controller/util"
"github.com/kcp-dev/kcp-operator/internal/metrics"
"github.com/kcp-dev/kcp-operator/internal/resources"
"github.com/kcp-dev/kcp-operator/internal/resources/frontproxy"
operatorv1alpha1 "github.com/kcp-dev/kcp-operator/sdk/apis/operator/v1alpha1"
Expand Down Expand Up @@ -89,12 +91,19 @@ func (r *FrontProxyReconciler) SetupWithManager(mgr ctrl.Manager) error {
// +kubebuilder:rbac:groups=core,resources=services;configmaps;secrets,verbs=get;list;watch;create;update;patch;delete

func (r *FrontProxyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res ctrl.Result, recErr error) {
startTime := time.Now()
defer func() {
duration := time.Since(startTime)
metrics.RecordReconciliationMetrics(metrics.FrontProxyResourceType, duration.Seconds(), recErr)
}()

logger := log.FromContext(ctx)
logger.V(4).Info("Reconciling")

var frontProxy operatorv1alpha1.FrontProxy
if err := r.Get(ctx, req.NamespacedName, &frontProxy); err != nil {
if ctrlruntimeclient.IgnoreNotFound(err) != nil {
metrics.RecordReconciliationError(metrics.FrontProxyResourceType, err.Error())
return ctrl.Result{}, fmt.Errorf("failed to get FrontProxy object: %w", err)
}

Expand All @@ -108,6 +117,14 @@ func (r *FrontProxyReconciler) Reconcile(ctx context.Context, req ctrl.Request)
recErr = kerrors.NewAggregate([]error{recErr, err})
}

metrics.RecordObjectMetrics(
metrics.FrontProxyResourceType,
frontProxy.Name,
req.Namespace,
string(frontProxy.Status.Phase),
frontProxy.Status.Conditions,
)

return ctrl.Result{}, recErr
}

Expand Down
16 changes: 16 additions & 0 deletions internal/controller/kubeconfig/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/kcp-dev/kcp-operator/internal/controller/util"
"github.com/kcp-dev/kcp-operator/internal/metrics"
"github.com/kcp-dev/kcp-operator/internal/reconciling"
"github.com/kcp-dev/kcp-operator/internal/resources"
"github.com/kcp-dev/kcp-operator/internal/resources/kubeconfig"
Expand Down Expand Up @@ -74,6 +75,12 @@ func (r *KubeconfigReconciler) SetupWithManager(mgr ctrl.Manager) error {
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
func (r *KubeconfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
startTime := time.Now()
defer func() {
duration := time.Since(startTime)
metrics.RecordReconciliationMetrics(metrics.KubeconfigResourceType, duration.Seconds(), nil)
}()

logger := log.FromContext(ctx)
logger.V(4).Info("Reconciling")

Expand All @@ -83,6 +90,7 @@ func (r *KubeconfigReconciler) Reconcile(ctx context.Context, req ctrl.Request)
if apierrors.IsNotFound(err) {
return ctrl.Result{}, nil
}
metrics.RecordReconciliationError(metrics.KubeconfigResourceType, err.Error())
return ctrl.Result{}, err
}

Expand Down Expand Up @@ -113,6 +121,14 @@ func (r *KubeconfigReconciler) Reconcile(ctx context.Context, req ctrl.Request)
recErr = kerrors.NewAggregate([]error{recErr, err})
}

metrics.RecordObjectMetrics(
metrics.KubeconfigResourceType,
kc.Name,
req.Namespace,
string(kc.Status.Phase),
kc.Status.Conditions,
)

return ctrl.Result{}, recErr
}

Expand Down
17 changes: 17 additions & 0 deletions internal/controller/rootshard/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"sort"
"time"

certmanagerv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
k8creconciling "k8c.io/reconciler/pkg/reconciling"
Expand All @@ -40,6 +41,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/kcp-dev/kcp-operator/internal/controller/util"
"github.com/kcp-dev/kcp-operator/internal/metrics"
"github.com/kcp-dev/kcp-operator/internal/reconciling"
"github.com/kcp-dev/kcp-operator/internal/resources"
"github.com/kcp-dev/kcp-operator/internal/resources/frontproxy"
Expand Down Expand Up @@ -99,12 +101,19 @@ func (r *RootShardReconciler) SetupWithManager(mgr ctrl.Manager) error {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *RootShardReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res ctrl.Result, recErr error) {
startTime := time.Now()
defer func() {
duration := time.Since(startTime)
metrics.RecordReconciliationMetrics(metrics.RootShardResourceType, duration.Seconds(), recErr)
}()

logger := log.FromContext(ctx)
logger.V(4).Info("Reconciling")

var rootShard operatorv1alpha1.RootShard
if err := r.Get(ctx, req.NamespacedName, &rootShard); err != nil {
if ctrlruntimeclient.IgnoreNotFound(err) != nil {
metrics.RecordReconciliationError(metrics.RootShardResourceType, err.Error())
return ctrl.Result{}, fmt.Errorf("failed to find %s/%s: %w", req.Namespace, req.Name, err)
}

Expand All @@ -118,6 +127,14 @@ func (r *RootShardReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
recErr = kerrors.NewAggregate([]error{recErr, err})
}

metrics.RecordObjectMetrics(
metrics.RootShardResourceType,
rootShard.Name,
req.Namespace,
string(rootShard.Status.Phase),
rootShard.Status.Conditions,
)

return ctrl.Result{}, recErr
}

Expand Down
17 changes: 17 additions & 0 deletions internal/controller/shard/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package shard
import (
"context"
"fmt"
"time"

certmanagerv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
k8creconciling "k8c.io/reconciler/pkg/reconciling"
Expand All @@ -39,6 +40,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/kcp-dev/kcp-operator/internal/controller/util"
"github.com/kcp-dev/kcp-operator/internal/metrics"
"github.com/kcp-dev/kcp-operator/internal/reconciling"
"github.com/kcp-dev/kcp-operator/internal/resources"
"github.com/kcp-dev/kcp-operator/internal/resources/shard"
Expand Down Expand Up @@ -90,12 +92,19 @@ func (r *ShardReconciler) SetupWithManager(mgr ctrl.Manager) error {
// +kubebuilder:rbac:groups=core,resources=secrets;services,verbs=get;list;watch;create;update;patch;delete

func (r *ShardReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res ctrl.Result, recErr error) {
startTime := time.Now()
defer func() {
duration := time.Since(startTime)
metrics.RecordReconciliationMetrics(metrics.ShardResourceType, duration.Seconds(), recErr)
}()

logger := log.FromContext(ctx)
logger.V(4).Info("Reconciling Shard object")

var s operatorv1alpha1.Shard
if err := r.Get(ctx, req.NamespacedName, &s); err != nil {
if ctrlruntimeclient.IgnoreNotFound(err) != nil {
metrics.RecordReconciliationError(metrics.ShardResourceType, err.Error())
return ctrl.Result{}, fmt.Errorf("failed to get shard: %w", err)
}

Expand All @@ -108,6 +117,14 @@ func (r *ShardReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
recErr = kerrors.NewAggregate([]error{recErr, err})
}

metrics.RecordObjectMetrics(
metrics.ShardResourceType,
s.Name,
req.Namespace,
string(s.Status.Phase),
s.Status.Conditions,
)

return ctrl.Result{}, recErr
}

Expand Down
Loading