diff --git a/cmd/provider/main.go b/cmd/provider/main.go index fca2412a..2c426fb7 100644 --- a/cmd/provider/main.go +++ b/cmd/provider/main.go @@ -25,6 +25,8 @@ import ( "github.com/crossplane/crossplane-runtime/pkg/certificates" "github.com/crossplane/crossplane-runtime/pkg/controller" "github.com/crossplane/crossplane-runtime/pkg/feature" + "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" + "github.com/crossplane/crossplane-runtime/pkg/statemetrics" "go.uber.org/zap/zapcore" "k8s.io/client-go/tools/leaderelection/resourcelock" @@ -35,6 +37,7 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/log/zap" + "sigs.k8s.io/controller-runtime/pkg/metrics" xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" "github.com/crossplane/crossplane-runtime/pkg/logging" @@ -53,6 +56,7 @@ func main() { debug = app.Flag("debug", "Run with debug logging.").Short('d').Bool() syncInterval = app.Flag("sync", "Sync interval controls how often all resources will be double checked for drift.").Short('s').Default("1h").Duration() pollInterval = app.Flag("poll", "Poll interval controls how often an individual resource should be checked for drift.").Default("10m").Duration() + pollStateMetricInterval = app.Flag("poll-state-metric", "State metric recording interval").Default("5s").Duration() pollJitter = app.Flag("poll-jitter", "If non-zero, varies the poll interval by a random amount up to plus-or-minus this value.").Default("1m").Duration() timeout = app.Flag("timeout", "Controls how long Terraform processes may run before they are killed.").Default("20m").Duration() leaderElection = app.Flag("leader-election", "Use leader election for the controller manager.").Short('l').Default("false").Envar("LEADER_ELECTION").Bool() @@ -101,12 +105,23 @@ func main() { kingpin.FatalIfError(apis.AddToScheme(mgr.GetScheme()), "Cannot add terraform APIs to scheme") + metricRecorder := managed.NewMRMetricRecorder() + stateMetrics := statemetrics.NewMRStateMetrics() + + metrics.Registry.MustRegister(metricRecorder) + metrics.Registry.MustRegister(stateMetrics) + o := controller.Options{ Logger: log, MaxConcurrentReconciles: *maxReconcileRate, PollInterval: *pollInterval, GlobalRateLimiter: ratelimiter.NewGlobal(*maxReconcileRate), Features: &feature.Flags{}, + MetricOptions: &controller.MetricOptions{ + PollStateMetricInterval: *pollStateMetricInterval, + MRMetrics: metricRecorder, + MRStateMetrics: stateMetrics, + }, } if *enableManagementPolicies { diff --git a/internal/controller/workspace/workspace.go b/internal/controller/workspace/workspace.go index 6543af47..e292d3f6 100644 --- a/internal/controller/workspace/workspace.go +++ b/internal/controller/workspace/workspace.go @@ -26,6 +26,7 @@ import ( "time" "github.com/crossplane/crossplane-runtime/pkg/logging" + "github.com/crossplane/crossplane-runtime/pkg/statemetrics" "github.com/pkg/errors" "github.com/spf13/afero" corev1 "k8s.io/api/core/v1" @@ -145,12 +146,18 @@ func Setup(mgr ctrl.Manager, o controller.Options, timeout, pollJitter time.Dura managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), managed.WithTimeout(timeout), managed.WithConnectionPublishers(cps...), + managed.WithMetricRecorder(o.MetricOptions.MRMetrics), } if o.Features.Enabled(features.EnableBetaManagementPolicies) { opts = append(opts, managed.WithManagementPolicies()) } + if err := mgr.Add(statemetrics.NewMRStateRecorder( + mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1beta1.WorkspaceList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + return err + } + r := managed.NewReconciler(mgr, resource.ManagedKind(v1beta1.WorkspaceGroupVersionKind), opts...)