diff --git a/.golangci.yml b/.golangci.yml index 42d79ff4..4797e73a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -9,6 +9,7 @@ linters: - durationcheck - errchkjson - errorlint + - forbidigo - gocheckcompilerdirectives - gochecksumtype - goconst @@ -46,6 +47,15 @@ linters: exclude-functions: - fmt:.* - io/ioutil:^Read.* + forbidigo: + forbid: + - pattern: ^managed\.NewReconciler$ + pkg: ^github\.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed$ + msg: Use common.NewReconciler so the controller applies the provider-wide reconciler options. + - pattern: ^statemetrics\.NewMRStateRecorder$ + pkg: ^github\.com/crossplane/crossplane-runtime/v2/pkg/statemetrics$ + msg: Use common.AddMRStateRecorder so the controller skips the recorder when no state metrics are configured. + analyze-types: true goconst: min-len: 3 min-occurrences: 5 @@ -92,6 +102,9 @@ linters: - gocritic path: _test\.go text: (unnamedResult|exitAfterDefer) + - linters: + - forbidigo + path: pkg/common/controller\.go - linters: - gocritic text: '(hugeParam|rangeValCopy):' diff --git a/README.md b/README.md index 19c7c69e..a5d35d3d 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,27 @@ spec: kubectl apply -f examples/providerconfig/provider.yaml ``` +### Poll intervals + +The provider checks every managed resource for drift on a timer. These provider +flags set the timer: + +| Flag | Default | Description | +| --- | --- | --- | +| `--poll` | `1m` | How often the provider checks each resource. | +| `--min-poll-interval` | `10s` | The shortest interval a resource can request with the `crossplane.io/poll-interval` annotation. The provider also reads it from `MIN_POLL_INTERVAL`. Set it to `0s` to remove the minimum. | +| `--sync` | `1h` | How often the provider reconciles all resources, whatever their poll interval. | + +To poll one resource at a different interval, set the +`crossplane.io/poll-interval` annotation to a Go duration such as `5m` or `24h`. +If the value is shorter than `--min-poll-interval`, the provider polls at +`--min-poll-interval`. Each poll makes at least one GitLab API call. Many +resources with a short interval can use up the rate limit of the ProviderConfig +token, and GitLab then throttles every resource that uses that ProviderConfig. + +To change a flag, add it to the `args` of the `package-runtime` container in a +`DeploymentRuntimeConfig`. + ### Self-rotating service account tokens The namespaced `groups.gitlab.m.crossplane.io/v1alpha1` `ServiceAccountAccessToken` diff --git a/cmd/provider/main.go b/cmd/provider/main.go index 34f090ed..1de738e1 100644 --- a/cmd/provider/main.go +++ b/cmd/provider/main.go @@ -47,6 +47,7 @@ import ( apisCluster "github.com/crossplane-contrib/provider-gitlab/apis/cluster" apisNamespaced "github.com/crossplane-contrib/provider-gitlab/apis/namespaced" controllerCluster "github.com/crossplane-contrib/provider-gitlab/pkg/cluster/controller" + "github.com/crossplane-contrib/provider-gitlab/pkg/common" controllerNamespaced "github.com/crossplane-contrib/provider-gitlab/pkg/namespaced/controller" ) @@ -56,6 +57,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("1m").Duration() + minPollInterval = app.Flag("min-poll-interval", "Minimum poll interval a resource may request with the crossplane.io/poll-interval annotation. Shorter values are raised to this minimum.").Default("10s").Envar("MIN_POLL_INTERVAL").Duration() leaderElection = app.Flag("leader-election", "Use leader election for the conroller manager.").Short('l').Default("false").OverrideDefaultFromEnvar("LEADER_ELECTION").Bool() maxReconcileRate = app.Flag("max-reconcile-rate", "The global maximum rate per second at which resources may checked for drift from the desired state.").Default("10").Int() @@ -76,7 +78,7 @@ func main() { ctrl.SetLogger(zl) } - log.Debug("Starting", "sync-period", syncInterval.String()) + log.Debug("Starting", "sync-period", syncInterval.String(), "poll-interval", pollInterval.String(), "min-poll-interval", minPollInterval.String()) cfg, err := ctrl.GetConfig() kingpin.FatalIfError(err, "Cannot get API server rest config") @@ -118,13 +120,16 @@ func main() { kingpin.FatalIfError(apisCluster.AddToScheme(mgr.GetScheme()), "Cannot add Gitlab legacy APIs to scheme") kingpin.FatalIfError(apisNamespaced.AddToScheme(mgr.GetScheme()), "Cannot add Gitlab legacy APIs to scheme") - o := xpcontroller.Options{ - Logger: log, - MaxConcurrentReconciles: *maxReconcileRate, - PollInterval: *pollInterval, - GlobalRateLimiter: ratelimiter.NewGlobal(*maxReconcileRate), - Features: &feature.Flags{}, - MetricOptions: &mo, + o := common.ControllerOptions{ + Options: xpcontroller.Options{ + Logger: log, + MaxConcurrentReconciles: *maxReconcileRate, + PollInterval: *pollInterval, + GlobalRateLimiter: ratelimiter.NewGlobal(*maxReconcileRate), + Features: &feature.Flags{}, + MetricOptions: &mo, + }, + MinPollInterval: *minPollInterval, } if *enableManagementPolicies { @@ -139,7 +144,7 @@ func main() { if canSafeStart { crdGate := new(gate.Gate[schema.GroupVersionKind]) o.Gate = crdGate - kingpin.FatalIfError(customresourcesgate.Setup(mgr, o), "Cannot setup CRD gate") + kingpin.FatalIfError(customresourcesgate.Setup(mgr, o.Options), "Cannot setup CRD gate") kingpin.FatalIfError(controllerCluster.SetupGated(mgr, o), "Cannot setup Gitlab legacy controllers") kingpin.FatalIfError(controllerNamespaced.SetupGated(mgr, o), "Cannot setup Gitlab modern controllers") } else { diff --git a/pkg/cluster/controller/config/zz_config.go b/pkg/cluster/controller/config/zz_config.go index 7d9e6f0c..4ed55b8b 100644 --- a/pkg/cluster/controller/config/zz_config.go +++ b/pkg/cluster/controller/config/zz_config.go @@ -19,18 +19,18 @@ limitations under the License. package config import ( - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/event" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/providerconfig" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" ctrl "sigs.k8s.io/controller-runtime" "github.com/crossplane-contrib/provider-gitlab/apis/cluster/v1beta1" + "github.com/crossplane-contrib/provider-gitlab/pkg/common" ) // Setup adds a controller that reconciles ProviderConfigs by accounting for // their current usage. -func SetupNamespaced(mgr ctrl.Manager, o controller.Options) error { +func SetupNamespaced(mgr ctrl.Manager, o common.ControllerOptions) error { name := providerconfig.ControllerName(v1beta1.ProviderConfigGroupKind) of := resource.ProviderConfigKinds{ @@ -49,7 +49,7 @@ func SetupNamespaced(mgr ctrl.Manager, o controller.Options) error { providerconfig.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))))) } -func SetupNamespacedGated(mgr ctrl.Manager, o controller.Options) error { +func SetupNamespacedGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupNamespaced(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconcilers", "gvk", v1beta1.ProviderConfigGroupVersionKind.String()) diff --git a/pkg/cluster/controller/config/zz_setup.go b/pkg/cluster/controller/config/zz_setup.go index 4190cb70..049f7185 100644 --- a/pkg/cluster/controller/config/zz_setup.go +++ b/pkg/cluster/controller/config/zz_setup.go @@ -19,14 +19,15 @@ limitations under the License. package config import ( - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" ctrl "sigs.k8s.io/controller-runtime" + + "github.com/crossplane-contrib/provider-gitlab/pkg/common" ) // Setup creates all Gitlab API controllers with the supplied logger and adds // them to the supplied manager. -func Setup(mgr ctrl.Manager, o controller.Options) error { - for _, setup := range []func(ctrl.Manager, controller.Options) error{ +func Setup(mgr ctrl.Manager, o common.ControllerOptions) error { + for _, setup := range []func(ctrl.Manager, common.ControllerOptions) error{ SetupNamespaced, } { if err := setup(mgr, o); err != nil { @@ -38,8 +39,8 @@ func Setup(mgr ctrl.Manager, o controller.Options) error { // SetupGated creates all Gitlab API controllers with the supplied logger and adds // them to the supplied manager with CRD gate support for SafeStart. -func SetupGated(mgr ctrl.Manager, o controller.Options) error { - for _, setup := range []func(ctrl.Manager, controller.Options) error{ +func SetupGated(mgr ctrl.Manager, o common.ControllerOptions) error { + for _, setup := range []func(ctrl.Manager, common.ControllerOptions) error{ SetupNamespacedGated, } { if err := setup(mgr, o); err != nil { diff --git a/pkg/cluster/controller/groups/accesstokens/zz_controller.go b/pkg/cluster/controller/groups/accesstokens/zz_controller.go index 37fda556..d994fe2f 100644 --- a/pkg/cluster/controller/groups/accesstokens/zz_controller.go +++ b/pkg/cluster/controller/groups/accesstokens/zz_controller.go @@ -22,13 +22,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/pkg/errors" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -92,27 +88,16 @@ func selfManagedCondition(self bool) v2.Condition { } // SetupAccessToken adds a controller that reconciles GroupAccessTokens. -func SetupAccessToken(mgr ctrl.Manager, o controller.Options) error { +func SetupAccessToken(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.AccessTokenGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.AccessTokenGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: groups.NewAccessTokenClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.AccessTokenGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.AccessTokenList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.AccessTokenList{}); err != nil { return err } @@ -124,7 +109,7 @@ func SetupAccessToken(mgr ctrl.Manager, o controller.Options) error { } // SetupAccessTokenGated adds a controller with CRD gate support. -func SetupAccessTokenGated(mgr ctrl.Manager, o controller.Options) error { +func SetupAccessTokenGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupAccessToken(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.AccessTokenGroupVersionKind.String()) diff --git a/pkg/cluster/controller/groups/badges/zz_controller.go b/pkg/cluster/controller/groups/badges/zz_controller.go index f4a161fa..8098565e 100644 --- a/pkg/cluster/controller/groups/badges/zz_controller.go +++ b/pkg/cluster/controller/groups/badges/zz_controller.go @@ -22,13 +22,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -52,27 +48,16 @@ const ( ) // SetupBadge adds a controller that reconciles GroupBadges. -func SetupBadge(mgr ctrl.Manager, o controller.Options) error { +func SetupBadge(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.BadgeGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.BadgeGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: groups.NewBadgeClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.BadgeGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.BadgeList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.BadgeList{}); err != nil { return err } @@ -84,7 +69,7 @@ func SetupBadge(mgr ctrl.Manager, o controller.Options) error { } // SetupBadgeGated adds a controller with CRD gate support. -func SetupBadgeGated(mgr ctrl.Manager, o controller.Options) error { +func SetupBadgeGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupBadge(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.BadgeGroupVersionKind.String()) diff --git a/pkg/cluster/controller/groups/deploytokens/zz_controller.go b/pkg/cluster/controller/groups/deploytokens/zz_controller.go index 814ae197..af49917c 100644 --- a/pkg/cluster/controller/groups/deploytokens/zz_controller.go +++ b/pkg/cluster/controller/groups/deploytokens/zz_controller.go @@ -22,13 +22,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -53,27 +49,16 @@ const ( ) // SetupDeployToken adds a controller that reconciles GroupDeployTokens. -func SetupDeployToken(mgr ctrl.Manager, o controller.Options) error { +func SetupDeployToken(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.DeployTokenGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.DeployTokenGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: groups.NewDeployTokenClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.DeployTokenGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.DeployTokenList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.DeployTokenList{}); err != nil { return err } @@ -85,7 +70,7 @@ func SetupDeployToken(mgr ctrl.Manager, o controller.Options) error { } // SetupDeployTokenGated adds a controller with CRD gate support. -func SetupDeployTokenGated(mgr ctrl.Manager, o controller.Options) error { +func SetupDeployTokenGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupDeployToken(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.DeployTokenGroupVersionKind.String()) diff --git a/pkg/cluster/controller/groups/groups/zz_group.go b/pkg/cluster/controller/groups/groups/zz_group.go index c2264a6b..1c162023 100644 --- a/pkg/cluster/controller/groups/groups/zz_group.go +++ b/pkg/cluster/controller/groups/groups/zz_group.go @@ -24,13 +24,9 @@ import ( "strings" "time" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -61,27 +57,16 @@ const ( ) // SetupGroup adds a controller that reconciles Groups. -func SetupGroup(mgr ctrl.Manager, o controller.Options) error { +func SetupGroup(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.GroupKubernetesGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.GroupKubernetesGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: groups.NewGroupClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.GroupKubernetesGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.GroupList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.GroupList{}); err != nil { return err } @@ -93,7 +78,7 @@ func SetupGroup(mgr ctrl.Manager, o controller.Options) error { } // SetupGroupGated adds a controller with CRD gate support. -func SetupGroupGated(mgr ctrl.Manager, o controller.Options) error { +func SetupGroupGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupGroup(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.GroupKubernetesGroupVersionKind.String()) diff --git a/pkg/cluster/controller/groups/hooks/zz_controller.go b/pkg/cluster/controller/groups/hooks/zz_controller.go index 742cddf5..f7038e14 100644 --- a/pkg/cluster/controller/groups/hooks/zz_controller.go +++ b/pkg/cluster/controller/groups/hooks/zz_controller.go @@ -22,13 +22,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -54,27 +50,16 @@ const ( ) // SetupHook adds a controller that reconciles Hooks. -func SetupHook(mgr ctrl.Manager, o controller.Options) error { +func SetupHook(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.HookGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.HookGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: groups.NewHookClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.HookGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.HookList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.HookList{}); err != nil { return err } @@ -86,7 +71,7 @@ func SetupHook(mgr ctrl.Manager, o controller.Options) error { } // SetupHookGated adds a controller with CRD gate support. -func SetupHookGated(mgr ctrl.Manager, o controller.Options) error { +func SetupHookGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupHook(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.HookGroupVersionKind.String()) diff --git a/pkg/cluster/controller/groups/integrationharbor/zz_controller.go b/pkg/cluster/controller/groups/integrationharbor/zz_controller.go index 67c22305..d5e1ef52 100644 --- a/pkg/cluster/controller/groups/integrationharbor/zz_controller.go +++ b/pkg/cluster/controller/groups/integrationharbor/zz_controller.go @@ -21,13 +21,9 @@ package integrationharbor import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" gitlab "gitlab.com/gitlab-org/api/client-go/v3" ctrl "sigs.k8s.io/controller-runtime" @@ -50,34 +46,16 @@ const ( ) // SetupIntegrationHarbor adds a controller that reconciles GitLab Group Harbor integrations. -func SetupIntegrationHarbor(mgr ctrl.Manager, o controller.Options) error { +func SetupIntegrationHarbor(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.IntegrationHarborGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.IntegrationHarborGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: groups.NewHarborClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler( - mgr, - resource.ManagedKind(v1alpha1.IntegrationHarborGroupVersionKind), - reconcilerOpts..., ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), - o.Logger, - o.MetricOptions.MRStateMetrics, - &v1alpha1.IntegrationHarborList{}, - o.MetricOptions.PollStateMetricInterval, - )); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.IntegrationHarborList{}); err != nil { return err } @@ -89,7 +67,7 @@ func SetupIntegrationHarbor(mgr ctrl.Manager, o controller.Options) error { } // SetupIntegrationHarborGated adds a controller with CRD gate support for SafeStart. -func SetupIntegrationHarborGated(mgr ctrl.Manager, o controller.Options) error { +func SetupIntegrationHarborGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupIntegrationHarbor(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.IntegrationHarborGroupVersionKind.String()) diff --git a/pkg/cluster/controller/groups/ldapgrouplinks/zz_controller.go b/pkg/cluster/controller/groups/ldapgrouplinks/zz_controller.go index f01dcfcd..759428e8 100644 --- a/pkg/cluster/controller/groups/ldapgrouplinks/zz_controller.go +++ b/pkg/cluster/controller/groups/ldapgrouplinks/zz_controller.go @@ -22,13 +22,9 @@ import ( "context" "fmt" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/pkg/errors" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -51,26 +47,16 @@ const ( ) // SetupLdapGroupLink adds a controller that reconciles ldapgrouplinks. -func SetupLdapGroupLink(mgr ctrl.Manager, o controller.Options) error { +func SetupLdapGroupLink(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.LdapGroupLinkGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.LdapGroupLinkGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: groups.NewLdapGroupLinkClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.LdapGroupLinkGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.LdapGroupLinkList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.LdapGroupLinkList{}); err != nil { return err } @@ -82,7 +68,7 @@ func SetupLdapGroupLink(mgr ctrl.Manager, o controller.Options) error { } // SetupLdapGroupLinkGated adds a controller with CRD gate support. -func SetupLdapGroupLinkGated(mgr ctrl.Manager, o controller.Options) error { +func SetupLdapGroupLinkGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupLdapGroupLink(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.LdapGroupLinkGroupVersionKind.String()) diff --git a/pkg/cluster/controller/groups/members/zz_controller.go b/pkg/cluster/controller/groups/members/zz_controller.go index dbdaf971..1923199a 100644 --- a/pkg/cluster/controller/groups/members/zz_controller.go +++ b/pkg/cluster/controller/groups/members/zz_controller.go @@ -18,12 +18,8 @@ package members import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -51,31 +47,20 @@ const ( ) // SetupMember adds a controller that reconciles Group Members. -func SetupMember(mgr ctrl.Manager, o controller.Options) error { +func SetupMember(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.MemberKubernetesGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.MemberKubernetesGroupVersionKind), managed.WithExternalConnector(&connector{ kube: mgr.GetClient(), newGitlabClientFn: groups.NewMemberClient, newUserClientFn: users.NewUserClient, }), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.MemberKubernetesGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.MemberList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.MemberList{}); err != nil { return err } @@ -87,7 +72,7 @@ func SetupMember(mgr ctrl.Manager, o controller.Options) error { } // SetupMemberGated adds a controller with CRD gate support. -func SetupMemberGated(mgr ctrl.Manager, o controller.Options) error { +func SetupMemberGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupMember(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.MemberKubernetesGroupVersionKind.String()) diff --git a/pkg/cluster/controller/groups/runners/zz_controller.go b/pkg/cluster/controller/groups/runners/zz_controller.go index 5817835b..7e699bd4 100644 --- a/pkg/cluster/controller/groups/runners/zz_controller.go +++ b/pkg/cluster/controller/groups/runners/zz_controller.go @@ -24,13 +24,9 @@ import ( "strconv" "time" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/pkg/errors" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -59,30 +55,20 @@ const ( ) // SetupRunner adds a controller that reconciles runners. -func SetupRunner(mgr ctrl.Manager, o controller.Options) error { +func SetupRunner(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.RunnerGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.RunnerGroupVersionKind), managed.WithExternalConnector(&connector{ kube: mgr.GetClient(), newGitlabClientFn: runners.NewRunnerClient, newRunnerClientFn: users.NewRunnerClient, }), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.RunnerGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.RunnerList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.RunnerList{}); err != nil { return err } @@ -94,7 +80,7 @@ func SetupRunner(mgr ctrl.Manager, o controller.Options) error { } // SetupRunnerGated adds a controller with CRD gate support. -func SetupRunnerGated(mgr ctrl.Manager, o controller.Options) error { +func SetupRunnerGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupRunner(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.RunnerGroupVersionKind.String()) diff --git a/pkg/cluster/controller/groups/samlgrouplinks/zz_controller.go b/pkg/cluster/controller/groups/samlgrouplinks/zz_controller.go index 672babb3..e31a5f3a 100644 --- a/pkg/cluster/controller/groups/samlgrouplinks/zz_controller.go +++ b/pkg/cluster/controller/groups/samlgrouplinks/zz_controller.go @@ -21,13 +21,9 @@ package samlgrouplinks import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -51,26 +47,16 @@ const ( ) // SetupSamlGroupLink adds a controller that reconciles samlgrouplinks. -func SetupSamlGroupLink(mgr ctrl.Manager, o controller.Options) error { +func SetupSamlGroupLink(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.SamlGroupLinkGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.SamlGroupLinkGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: groups.NewSamlGroupLinkClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.SamlGroupLinkGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.SamlGroupLinkList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.SamlGroupLinkList{}); err != nil { return err } @@ -82,7 +68,7 @@ func SetupSamlGroupLink(mgr ctrl.Manager, o controller.Options) error { } // SetupSamlGroupLinkGated adds a controller with CRD gate support. -func SetupSamlGroupLinkGated(mgr ctrl.Manager, o controller.Options) error { +func SetupSamlGroupLinkGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupSamlGroupLink(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.SamlGroupLinkGroupVersionKind.String()) diff --git a/pkg/cluster/controller/groups/serviceaccountaccesstokens/zz_controller.go b/pkg/cluster/controller/groups/serviceaccountaccesstokens/zz_controller.go index 0985f743..54c7d1fe 100644 --- a/pkg/cluster/controller/groups/serviceaccountaccesstokens/zz_controller.go +++ b/pkg/cluster/controller/groups/serviceaccountaccesstokens/zz_controller.go @@ -24,13 +24,9 @@ import ( "strconv" "strings" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/pkg/errors" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -96,27 +92,16 @@ func selfManagedCondition(self bool) v2.Condition { // SetupServiceAccountAccessToken adds a controller that reconciles // group ServiceAccountAccessTokens. -func SetupServiceAccountAccessToken(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccountAccessToken(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.ServiceAccountAccessTokenGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ServiceAccountAccessTokenGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: groups.NewServiceAccountAccessTokenClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ServiceAccountAccessTokenGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ServiceAccountAccessTokenList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ServiceAccountAccessTokenList{}); err != nil { return err } @@ -128,7 +113,7 @@ func SetupServiceAccountAccessToken(mgr ctrl.Manager, o controller.Options) erro } // SetupServiceAccountAccessTokenGated adds a controller with CRD gate support. -func SetupServiceAccountAccessTokenGated(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccountAccessTokenGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupServiceAccountAccessToken(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ServiceAccountAccessTokenGroupVersionKind.String()) diff --git a/pkg/cluster/controller/groups/serviceaccounts/zz_controller.go b/pkg/cluster/controller/groups/serviceaccounts/zz_controller.go index 406b3b98..263302fe 100644 --- a/pkg/cluster/controller/groups/serviceaccounts/zz_controller.go +++ b/pkg/cluster/controller/groups/serviceaccounts/zz_controller.go @@ -22,14 +22,10 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" gitlab "gitlab.com/gitlab-org/api/client-go/v3" ctrl "sigs.k8s.io/controller-runtime" @@ -53,27 +49,16 @@ const ( ) // SetupServiceAccount adds a controller that reconciles GitLab Service Accounts. -func SetupServiceAccount(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccount(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.ServiceAccountGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ServiceAccountGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: groups.NewServiceAccountClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ServiceAccountGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ServiceAccountList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ServiceAccountList{}); err != nil { return err } @@ -85,7 +70,7 @@ func SetupServiceAccount(mgr ctrl.Manager, o controller.Options) error { } // SetupServiceAccountGated adds a controller with CRD gate support. -func SetupServiceAccountGated(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccountGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupServiceAccount(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ServiceAccountGroupVersionKind.String()) diff --git a/pkg/cluster/controller/groups/variables/zz_controller.go b/pkg/cluster/controller/groups/variables/zz_controller.go index d7de702f..c22946b3 100644 --- a/pkg/cluster/controller/groups/variables/zz_controller.go +++ b/pkg/cluster/controller/groups/variables/zz_controller.go @@ -21,13 +21,9 @@ package variables import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -51,27 +47,16 @@ const ( ) // SetupVariable adds a controller that reconciles Variables. -func SetupVariable(mgr ctrl.Manager, o controller.Options) error { +func SetupVariable(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.VariableGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.VariableGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: groups.NewVariableClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.VariableGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.VariableList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.VariableList{}); err != nil { return err } @@ -83,7 +68,7 @@ func SetupVariable(mgr ctrl.Manager, o controller.Options) error { } // SetupVariableGated adds a controller with CRD gate support. -func SetupVariableGated(mgr ctrl.Manager, o controller.Options) error { +func SetupVariableGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupVariable(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.VariableGroupVersionKind.String()) diff --git a/pkg/cluster/controller/groups/zz_setup.go b/pkg/cluster/controller/groups/zz_setup.go index ac209597..3935f184 100644 --- a/pkg/cluster/controller/groups/zz_setup.go +++ b/pkg/cluster/controller/groups/zz_setup.go @@ -19,7 +19,6 @@ limitations under the License. package groups import ( - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" ctrl "sigs.k8s.io/controller-runtime" "github.com/crossplane-contrib/provider-gitlab/pkg/cluster/controller/groups/accesstokens" @@ -35,11 +34,12 @@ import ( "github.com/crossplane-contrib/provider-gitlab/pkg/cluster/controller/groups/serviceaccountaccesstokens" "github.com/crossplane-contrib/provider-gitlab/pkg/cluster/controller/groups/serviceaccounts" "github.com/crossplane-contrib/provider-gitlab/pkg/cluster/controller/groups/variables" + "github.com/crossplane-contrib/provider-gitlab/pkg/common" ) // Setup all group controllers -func Setup(mgr ctrl.Manager, o controller.Options) error { - for _, setup := range []func(ctrl.Manager, controller.Options) error{ +func Setup(mgr ctrl.Manager, o common.ControllerOptions) error { + for _, setup := range []func(ctrl.Manager, common.ControllerOptions) error{ groups.SetupGroup, members.SetupMember, accesstokens.SetupAccessToken, @@ -63,8 +63,8 @@ func Setup(mgr ctrl.Manager, o controller.Options) error { // SetupGated creates all Gitlab API controllers with the supplied logger and adds // them to the supplied manager with CRD gate support for SafeStart. -func SetupGated(mgr ctrl.Manager, o controller.Options) error { - for _, setup := range []func(ctrl.Manager, controller.Options) error{ +func SetupGated(mgr ctrl.Manager, o common.ControllerOptions) error { + for _, setup := range []func(ctrl.Manager, common.ControllerOptions) error{ groups.SetupGroupGated, members.SetupMemberGated, accesstokens.SetupAccessTokenGated, diff --git a/pkg/cluster/controller/instance/appearance/zz_controller.go b/pkg/cluster/controller/instance/appearance/zz_controller.go index bbe5590e..a49652d5 100644 --- a/pkg/cluster/controller/instance/appearance/zz_controller.go +++ b/pkg/cluster/controller/instance/appearance/zz_controller.go @@ -21,13 +21,9 @@ package appearance import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" gitlab "gitlab.com/gitlab-org/api/client-go/v3" ctrl "sigs.k8s.io/controller-runtime" @@ -47,27 +43,16 @@ const ( ) // SetupAppearance adds a controller that reconciles GitLab Instance Appearance. -func SetupAppearance(mgr ctrl.Manager, o controller.Options) error { +func SetupAppearance(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.AppearanceGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.AppearanceGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: instance.NewAppearanceClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.AppearanceGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.AppearanceList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.AppearanceList{}); err != nil { return err } @@ -79,7 +64,7 @@ func SetupAppearance(mgr ctrl.Manager, o controller.Options) error { } // SetupAppearanceGated adds a controller with CRD gate support. -func SetupAppearanceGated(mgr ctrl.Manager, o controller.Options) error { +func SetupAppearanceGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupAppearance(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.AppearanceGroupVersionKind.String()) diff --git a/pkg/cluster/controller/instance/applications/zz_controller.go b/pkg/cluster/controller/instance/applications/zz_controller.go index d9ae59af..43891e5c 100644 --- a/pkg/cluster/controller/instance/applications/zz_controller.go +++ b/pkg/cluster/controller/instance/applications/zz_controller.go @@ -22,14 +22,10 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" gitlab "gitlab.com/gitlab-org/api/client-go/v3" ctrl "sigs.k8s.io/controller-runtime" @@ -52,27 +48,16 @@ const ( ) // SetupApplication adds a controller that reconciles GitLab OAuth Applications. -func SetupApplication(mgr ctrl.Manager, o controller.Options) error { +func SetupApplication(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.ApplicationGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ApplicationGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: instance.NewApplicationClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ApplicationGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ApplicationList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ApplicationList{}); err != nil { return err } @@ -84,7 +69,7 @@ func SetupApplication(mgr ctrl.Manager, o controller.Options) error { } // SetupApplicationGated adds a controller with CRD gate support. -func SetupApplicationGated(mgr ctrl.Manager, o controller.Options) error { +func SetupApplicationGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupApplication(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ApplicationGroupVersionKind.String()) diff --git a/pkg/cluster/controller/instance/license/zz_controller.go b/pkg/cluster/controller/instance/license/zz_controller.go index fa6a6ec9..c488fbb6 100644 --- a/pkg/cluster/controller/instance/license/zz_controller.go +++ b/pkg/cluster/controller/instance/license/zz_controller.go @@ -22,13 +22,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/pkg/errors" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -57,29 +53,19 @@ const ( ) // SetupLicense adds a controller that reconciles instance licenses. -func SetupLicense(mgr ctrl.Manager, o controller.Options) error { +func SetupLicense(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.LicenseGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.LicenseGroupVersionKind), managed.WithExternalConnector(&connector{ kube: mgr.GetClient(), newGitlabClientFn: instance.NewLicenseClient, }), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.LicenseGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.LicenseList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.LicenseList{}); err != nil { return err } @@ -91,7 +77,7 @@ func SetupLicense(mgr ctrl.Manager, o controller.Options) error { } // SetupLicenseGated adds a controller with CRD gate support. -func SetupLicenseGated(mgr ctrl.Manager, o controller.Options) error { +func SetupLicenseGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupLicense(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.LicenseGroupVersionKind.String()) diff --git a/pkg/cluster/controller/instance/runners/zz_controller.go b/pkg/cluster/controller/instance/runners/zz_controller.go index 7cfe6127..e41724b3 100644 --- a/pkg/cluster/controller/instance/runners/zz_controller.go +++ b/pkg/cluster/controller/instance/runners/zz_controller.go @@ -23,13 +23,9 @@ import ( "strconv" "time" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/pkg/errors" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -57,30 +53,20 @@ const ( ) // SetupRunner adds a controller that reconciles instance runners. -func SetupRunner(mgr ctrl.Manager, o controller.Options) error { +func SetupRunner(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.RunnerGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.RunnerGroupVersionKind), managed.WithExternalConnector(&connector{ kube: mgr.GetClient(), newGitlabClientFn: runners.NewRunnerClient, newRunnerClientFn: users.NewRunnerClient, }), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.RunnerGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.RunnerList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.RunnerList{}); err != nil { return err } @@ -92,7 +78,7 @@ func SetupRunner(mgr ctrl.Manager, o controller.Options) error { } // SetupRunnerGated adds a controller with CRD gate support. -func SetupRunnerGated(mgr ctrl.Manager, o controller.Options) error { +func SetupRunnerGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupRunner(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.RunnerGroupVersionKind.String()) diff --git a/pkg/cluster/controller/instance/serviceaccountaccesstokens/zz_controller.go b/pkg/cluster/controller/instance/serviceaccountaccesstokens/zz_controller.go index 43d4adde..bfd080cd 100644 --- a/pkg/cluster/controller/instance/serviceaccountaccesstokens/zz_controller.go +++ b/pkg/cluster/controller/instance/serviceaccountaccesstokens/zz_controller.go @@ -24,13 +24,9 @@ import ( "strconv" "strings" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/pkg/errors" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -97,27 +93,16 @@ func selfManagedCondition(self bool) v2.Condition { // SetupServiceAccountAccessToken adds a controller that reconciles // instance ServiceAccountAccessTokens. -func SetupServiceAccountAccessToken(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccountAccessToken(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.ServiceAccountAccessTokenGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ServiceAccountAccessTokenGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: instance.NewServiceAccountAccessTokenClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ServiceAccountAccessTokenGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ServiceAccountAccessTokenList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ServiceAccountAccessTokenList{}); err != nil { return err } @@ -129,7 +114,7 @@ func SetupServiceAccountAccessToken(mgr ctrl.Manager, o controller.Options) erro } // SetupServiceAccountAccessTokenGated adds a controller with CRD gate support. -func SetupServiceAccountAccessTokenGated(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccountAccessTokenGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupServiceAccountAccessToken(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ServiceAccountAccessTokenGroupVersionKind.String()) diff --git a/pkg/cluster/controller/instance/serviceaccounts/zz_controller.go b/pkg/cluster/controller/instance/serviceaccounts/zz_controller.go index 4b0830f2..fdbdd6e5 100644 --- a/pkg/cluster/controller/instance/serviceaccounts/zz_controller.go +++ b/pkg/cluster/controller/instance/serviceaccounts/zz_controller.go @@ -23,14 +23,10 @@ import ( "strconv" "sync" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" gitlab "gitlab.com/gitlab-org/api/client-go/v3" "k8s.io/utils/ptr" @@ -74,27 +70,16 @@ const ( ) // SetupServiceAccount adds a controller that reconciles GitLab Service Accounts. -func SetupServiceAccount(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccount(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.ServiceAccountGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ServiceAccountGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: instance.NewServiceAccountClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ServiceAccountGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ServiceAccountList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ServiceAccountList{}); err != nil { return err } @@ -106,7 +91,7 @@ func SetupServiceAccount(mgr ctrl.Manager, o controller.Options) error { } // SetupServiceAccountGated adds a controller with CRD gate support. -func SetupServiceAccountGated(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccountGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupServiceAccount(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ServiceAccountGroupVersionKind.String()) diff --git a/pkg/cluster/controller/instance/settings/zz_controller.go b/pkg/cluster/controller/instance/settings/zz_controller.go index 56d42201..aaa21d82 100644 --- a/pkg/cluster/controller/instance/settings/zz_controller.go +++ b/pkg/cluster/controller/instance/settings/zz_controller.go @@ -21,13 +21,9 @@ package settings import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" gitlab "gitlab.com/gitlab-org/api/client-go/v3" ctrl "sigs.k8s.io/controller-runtime" @@ -48,27 +44,16 @@ const ( ) // SetupApplicationSettings adds a controller that reconciles GitLab Instance Settings. -func SetupApplicationSettings(mgr ctrl.Manager, o controller.Options) error { +func SetupApplicationSettings(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.ApplicationSettingsGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ApplicationSettingsGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: instance.NewApplicationSettingsClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ApplicationSettingsGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ApplicationSettingsList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ApplicationSettingsList{}); err != nil { return err } @@ -80,7 +65,7 @@ func SetupApplicationSettings(mgr ctrl.Manager, o controller.Options) error { } // SetupApplicationSettingsGated adds a controller with CRD gate support. -func SetupApplicationSettingsGated(mgr ctrl.Manager, o controller.Options) error { +func SetupApplicationSettingsGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupApplicationSettings(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ApplicationSettingsGroupVersionKind.String()) diff --git a/pkg/cluster/controller/instance/variables/zz_controller.go b/pkg/cluster/controller/instance/variables/zz_controller.go index 49327392..c99176ae 100644 --- a/pkg/cluster/controller/instance/variables/zz_controller.go +++ b/pkg/cluster/controller/instance/variables/zz_controller.go @@ -21,13 +21,9 @@ package variables import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -50,27 +46,16 @@ const ( ) // SetupVariable adds a controller that reconciles Instance Variables. -func SetupVariable(mgr ctrl.Manager, o controller.Options) error { +func SetupVariable(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.VariableGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.VariableGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: instance.NewVariableClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.VariableGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.VariableList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.VariableList{}); err != nil { return err } @@ -82,7 +67,7 @@ func SetupVariable(mgr ctrl.Manager, o controller.Options) error { } // SetupVariableGated adds a controller with CRD gate support. -func SetupVariableGated(mgr ctrl.Manager, o controller.Options) error { +func SetupVariableGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupVariable(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.VariableGroupVersionKind.String()) diff --git a/pkg/cluster/controller/instance/variables/zz_controller_test.go b/pkg/cluster/controller/instance/variables/zz_controller_test.go index 741ebdbe..17d4b168 100644 --- a/pkg/cluster/controller/instance/variables/zz_controller_test.go +++ b/pkg/cluster/controller/instance/variables/zz_controller_test.go @@ -204,7 +204,7 @@ func (m *fakeManager) GetConverterRegistry() conversion.Registry { return nil } func TestSetupVariableGated(t *testing.T) { g := &recordingGate{} - o := controller.Options{Gate: g} + o := common.ControllerOptions{Options: controller.Options{Gate: g}} // mgr is not used until the callback is invoked. if err := SetupVariableGated(nil, o); err != nil { @@ -230,14 +230,14 @@ func TestSetupVariableReturnsAddError(t *testing.T) { cl := fake.NewClientBuilder().WithScheme(s).Build() mgr := &fakeManager{cl: cl, scheme: s, addErr: errBoom} - o := controller.Options{ + o := common.ControllerOptions{Options: controller.Options{ Logger: logging.NewNopLogger(), PollInterval: time.Second, Features: &feature.Flags{}, MetricOptions: &controller.MetricOptions{ MRStateMetrics: &statemetrics.MRStateMetrics{}, }, - } + }} err := SetupVariable(mgr, o) if !stderrors.Is(err, errBoom) { diff --git a/pkg/cluster/controller/instance/zz_setup.go b/pkg/cluster/controller/instance/zz_setup.go index e986371a..4be89011 100644 --- a/pkg/cluster/controller/instance/zz_setup.go +++ b/pkg/cluster/controller/instance/zz_setup.go @@ -19,7 +19,6 @@ limitations under the License. package instance import ( - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" ctrl "sigs.k8s.io/controller-runtime" "github.com/crossplane-contrib/provider-gitlab/pkg/cluster/controller/instance/appearance" @@ -30,10 +29,11 @@ import ( "github.com/crossplane-contrib/provider-gitlab/pkg/cluster/controller/instance/serviceaccounts" "github.com/crossplane-contrib/provider-gitlab/pkg/cluster/controller/instance/settings" "github.com/crossplane-contrib/provider-gitlab/pkg/cluster/controller/instance/variables" + "github.com/crossplane-contrib/provider-gitlab/pkg/common" ) -func Setup(mgr ctrl.Manager, o controller.Options) error { - for _, setup := range []func(ctrl.Manager, controller.Options) error{ +func Setup(mgr ctrl.Manager, o common.ControllerOptions) error { + for _, setup := range []func(ctrl.Manager, common.ControllerOptions) error{ settings.SetupApplicationSettings, runners.SetupRunner, appearance.SetupAppearance, @@ -52,8 +52,8 @@ func Setup(mgr ctrl.Manager, o controller.Options) error { // SetupGated creates all Gitlab API controllers with the supplied logger and adds // them to the supplied manager with CRD gate support for SafeStart. -func SetupGated(mgr ctrl.Manager, o controller.Options) error { - for _, setup := range []func(ctrl.Manager, controller.Options) error{ +func SetupGated(mgr ctrl.Manager, o common.ControllerOptions) error { + for _, setup := range []func(ctrl.Manager, common.ControllerOptions) error{ settings.SetupApplicationSettingsGated, runners.SetupRunnerGated, appearance.SetupAppearanceGated, diff --git a/pkg/cluster/controller/projects/accesstokens/zz_controller.go b/pkg/cluster/controller/projects/accesstokens/zz_controller.go index f402afef..06bd0f89 100644 --- a/pkg/cluster/controller/projects/accesstokens/zz_controller.go +++ b/pkg/cluster/controller/projects/accesstokens/zz_controller.go @@ -22,13 +22,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/pkg/errors" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -92,27 +88,16 @@ func selfManagedCondition(self bool) v2.Condition { } // SetupAccessToken adds a controller that reconciles ProjectAccessTokens. -func SetupAccessToken(mgr ctrl.Manager, o controller.Options) error { +func SetupAccessToken(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.AccessTokenGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.AccessTokenGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewAccessTokenClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.AccessTokenGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.AccessTokenList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.AccessTokenList{}); err != nil { return err } @@ -124,7 +109,7 @@ func SetupAccessToken(mgr ctrl.Manager, o controller.Options) error { } // SetupAccessTokenGated adds a controller with CRD gate support. -func SetupAccessTokenGated(mgr ctrl.Manager, o controller.Options) error { +func SetupAccessTokenGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupAccessToken(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.AccessTokenGroupVersionKind.String()) diff --git a/pkg/cluster/controller/projects/approvalrules/zz_controller.go b/pkg/cluster/controller/projects/approvalrules/zz_controller.go index af61308c..77f75d03 100644 --- a/pkg/cluster/controller/projects/approvalrules/zz_controller.go +++ b/pkg/cluster/controller/projects/approvalrules/zz_controller.go @@ -19,13 +19,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -50,30 +46,19 @@ const ( ) // SetupRules adds a controller that reconciles Approval Rules. -func SetupRules(mgr ctrl.Manager, o controller.Options) error { +func SetupRules(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.ApprovalRuleKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ApprovalRuleGroupVersionKind), managed.WithExternalConnector(&connector{ kube: mgr.GetClient(), newGitlabClientFn: projects.NewApprovalRulesClient, }), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ApprovalRuleGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ApprovalRuleList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ApprovalRuleList{}); err != nil { return err } @@ -85,7 +70,7 @@ func SetupRules(mgr ctrl.Manager, o controller.Options) error { } // SetupRulesGated adds a controller with CRD gate support. -func SetupRulesGated(mgr ctrl.Manager, o controller.Options) error { +func SetupRulesGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupRules(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ApprovalRuleGroupVersionKind.String()) diff --git a/pkg/cluster/controller/projects/badges/zz_controller.go b/pkg/cluster/controller/projects/badges/zz_controller.go index 981545fe..70fc5278 100644 --- a/pkg/cluster/controller/projects/badges/zz_controller.go +++ b/pkg/cluster/controller/projects/badges/zz_controller.go @@ -22,13 +22,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -52,27 +48,16 @@ const ( ) // SetupBadge adds a controller that reconciles ProjectBadges. -func SetupBadge(mgr ctrl.Manager, o controller.Options) error { +func SetupBadge(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.BadgeGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.BadgeGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewBadgeClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.BadgeGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.BadgeList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.BadgeList{}); err != nil { return err } @@ -84,7 +69,7 @@ func SetupBadge(mgr ctrl.Manager, o controller.Options) error { } // SetupBadgeGated adds a controller with CRD gate support. -func SetupBadgeGated(mgr ctrl.Manager, o controller.Options) error { +func SetupBadgeGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupBadge(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.BadgeGroupVersionKind.String()) diff --git a/pkg/cluster/controller/projects/deploykeys/zz_controller.go b/pkg/cluster/controller/projects/deploykeys/zz_controller.go index efade55d..16f81a22 100644 --- a/pkg/cluster/controller/projects/deploykeys/zz_controller.go +++ b/pkg/cluster/controller/projects/deploykeys/zz_controller.go @@ -6,13 +6,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -50,27 +46,16 @@ type connector struct { } // SetupDeployKey adds a controller that reconciles ProjectDeployKey. -func SetupDeployKey(mgr ctrl.Manager, o controller.Options) error { +func SetupDeployKey(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.DeployKeyGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.DeployKeyGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: newDeployKeyClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.DeployKeyGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.DeployKeyList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.DeployKeyList{}); err != nil { return err } @@ -82,7 +67,7 @@ func SetupDeployKey(mgr ctrl.Manager, o controller.Options) error { } // SetupDeployKeyGated adds a controller with CRD gate support. -func SetupDeployKeyGated(mgr ctrl.Manager, o controller.Options) error { +func SetupDeployKeyGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupDeployKey(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.DeployKeyGroupVersionKind.String()) diff --git a/pkg/cluster/controller/projects/deploytokens/zz_controller.go b/pkg/cluster/controller/projects/deploytokens/zz_controller.go index d2c27171..d8d2e1fb 100644 --- a/pkg/cluster/controller/projects/deploytokens/zz_controller.go +++ b/pkg/cluster/controller/projects/deploytokens/zz_controller.go @@ -22,13 +22,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -53,27 +49,16 @@ const ( ) // SetupDeployToken adds a controller that reconciles ProjectDeployTokens. -func SetupDeployToken(mgr ctrl.Manager, o controller.Options) error { +func SetupDeployToken(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.DeployTokenGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.DeployTokenGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewDeployTokenClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.DeployTokenGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.DeployKeyList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.DeployKeyList{}); err != nil { return err } @@ -85,7 +70,7 @@ func SetupDeployToken(mgr ctrl.Manager, o controller.Options) error { } // SetupDeployTokenGated adds a controller with CRD gate support. -func SetupDeployTokenGated(mgr ctrl.Manager, o controller.Options) error { +func SetupDeployTokenGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupDeployToken(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.DeployTokenGroupVersionKind.String()) diff --git a/pkg/cluster/controller/projects/hooks/zz_controller.go b/pkg/cluster/controller/projects/hooks/zz_controller.go index 9e9898b4..f0ad37e9 100644 --- a/pkg/cluster/controller/projects/hooks/zz_controller.go +++ b/pkg/cluster/controller/projects/hooks/zz_controller.go @@ -22,13 +22,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -54,27 +50,16 @@ const ( ) // SetupHook adds a controller that reconciles Hooks. -func SetupHook(mgr ctrl.Manager, o controller.Options) error { +func SetupHook(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.HookGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.HookGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewHookClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.HookGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.HookList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.HookList{}); err != nil { return err } @@ -86,7 +71,7 @@ func SetupHook(mgr ctrl.Manager, o controller.Options) error { } // SetupHookGated adds a controller with CRD gate support. -func SetupHookGated(mgr ctrl.Manager, o controller.Options) error { +func SetupHookGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupHook(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.HookGroupVersionKind.String()) diff --git a/pkg/cluster/controller/projects/integrationharbor/zz_controller.go b/pkg/cluster/controller/projects/integrationharbor/zz_controller.go index 090c55c4..ac2bfa23 100644 --- a/pkg/cluster/controller/projects/integrationharbor/zz_controller.go +++ b/pkg/cluster/controller/projects/integrationharbor/zz_controller.go @@ -21,13 +21,9 @@ package integrationharbor import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -51,34 +47,16 @@ const ( ) // SetupIntegrationHarbor adds a controller that reconciles GitLab Project Harbor integrations. -func SetupIntegrationHarbor(mgr ctrl.Manager, o controller.Options) error { +func SetupIntegrationHarbor(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.IntegrationHarborGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.IntegrationHarborGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewHarborClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler( - mgr, - resource.ManagedKind(v1alpha1.IntegrationHarborGroupVersionKind), - reconcilerOpts..., ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), - o.Logger, - o.MetricOptions.MRStateMetrics, - &v1alpha1.IntegrationHarborList{}, - o.MetricOptions.PollStateMetricInterval, - )); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.IntegrationHarborList{}); err != nil { return err } @@ -90,7 +68,7 @@ func SetupIntegrationHarbor(mgr ctrl.Manager, o controller.Options) error { } // SetupIntegrationHarborGated adds a controller with CRD gate support for SafeStart. -func SetupIntegrationHarborGated(mgr ctrl.Manager, o controller.Options) error { +func SetupIntegrationHarborGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupIntegrationHarbor(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.IntegrationHarborGroupVersionKind.String()) diff --git a/pkg/cluster/controller/projects/integrationmattermost/zz_controller.go b/pkg/cluster/controller/projects/integrationmattermost/zz_controller.go index 7ae16c8d..914a08ea 100644 --- a/pkg/cluster/controller/projects/integrationmattermost/zz_controller.go +++ b/pkg/cluster/controller/projects/integrationmattermost/zz_controller.go @@ -21,14 +21,10 @@ package integrationmattermost import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -51,34 +47,16 @@ const ( ) // SetupIntegrationMattermost adds a controller that reconciles GitLab Integration Mattermost. -func SetupIntegrationMattermost(mgr ctrl.Manager, o controller.Options) error { +func SetupIntegrationMattermost(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.IntegrationMattermostGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.IntegrationMattermostGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewMattermostClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler( - mgr, - resource.ManagedKind(v1alpha1.IntegrationMattermostGroupVersionKind), - reconcilerOpts..., ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), - o.Logger, - o.MetricOptions.MRStateMetrics, - &v1alpha1.IntegrationMattermostList{}, - o.MetricOptions.PollStateMetricInterval, - )); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.IntegrationMattermostList{}); err != nil { return err } @@ -90,7 +68,7 @@ func SetupIntegrationMattermost(mgr ctrl.Manager, o controller.Options) error { } // SetupIntegrationMattermostGated adds a controller with CRD gate support. -func SetupIntegrationMattermostGated(mgr ctrl.Manager, o controller.Options) error { +func SetupIntegrationMattermostGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupIntegrationMattermost(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.IntegrationMattermostGroupVersionKind.String()) diff --git a/pkg/cluster/controller/projects/members/zz_controller.go b/pkg/cluster/controller/projects/members/zz_controller.go index beddae36..aabbb410 100644 --- a/pkg/cluster/controller/projects/members/zz_controller.go +++ b/pkg/cluster/controller/projects/members/zz_controller.go @@ -18,12 +18,8 @@ package members import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -50,31 +46,20 @@ const ( ) // SetupMember adds a controller that reconciles Project Members. -func SetupMember(mgr ctrl.Manager, o controller.Options) error { +func SetupMember(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.MemberGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.MemberGroupVersionKind), managed.WithExternalConnector(&connector{ kube: mgr.GetClient(), newGitlabClientFn: projects.NewMemberClient, newUserClientFn: users.NewUserClient, }), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.MemberGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.MemberList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.MemberList{}); err != nil { return err } @@ -86,7 +71,7 @@ func SetupMember(mgr ctrl.Manager, o controller.Options) error { } // SetupMemberGated adds a controller with CRD gate support. -func SetupMemberGated(mgr ctrl.Manager, o controller.Options) error { +func SetupMemberGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupMember(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.MemberGroupVersionKind.String()) diff --git a/pkg/cluster/controller/projects/pipelineschedules/zz_controller.go b/pkg/cluster/controller/projects/pipelineschedules/zz_controller.go index 83806b4a..c45e83f2 100644 --- a/pkg/cluster/controller/projects/pipelineschedules/zz_controller.go +++ b/pkg/cluster/controller/projects/pipelineschedules/zz_controller.go @@ -22,13 +22,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -58,27 +54,16 @@ const ( ) // SetupPipelineSchedule adds a controller that reconciles PipelineSchedule. -func SetupPipelineSchedule(mgr ctrl.Manager, o controller.Options) error { +func SetupPipelineSchedule(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.PipelineScheduleGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.PipelineScheduleGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: newPipelineScheduleClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.PipelineScheduleGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.PipelineScheduleList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.PipelineScheduleList{}); err != nil { return err } @@ -90,7 +75,7 @@ func SetupPipelineSchedule(mgr ctrl.Manager, o controller.Options) error { } // SetupPipelineScheduleGated adds a controller with CRD gate support. -func SetupPipelineScheduleGated(mgr ctrl.Manager, o controller.Options) error { +func SetupPipelineScheduleGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupPipelineSchedule(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.PipelineScheduleGroupVersionKind.String()) diff --git a/pkg/cluster/controller/projects/projects/zz_project.go b/pkg/cluster/controller/projects/projects/zz_project.go index 17beaf69..e48d6a7a 100644 --- a/pkg/cluster/controller/projects/projects/zz_project.go +++ b/pkg/cluster/controller/projects/projects/zz_project.go @@ -24,13 +24,9 @@ import ( "strconv" "strings" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" @@ -62,27 +58,16 @@ const ( ) // SetupProject adds a controller that reconciles Projects. -func SetupProject(mgr ctrl.Manager, o controller.Options) error { +func SetupProject(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.ProjectGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ProjectGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewProjectClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ProjectGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ProjectList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ProjectList{}); err != nil { return err } @@ -94,7 +79,7 @@ func SetupProject(mgr ctrl.Manager, o controller.Options) error { } // SetupProjectGated adds a controller with CRD gate support. -func SetupProjectGated(mgr ctrl.Manager, o controller.Options) error { +func SetupProjectGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupProject(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ProjectGroupVersionKind.String()) diff --git a/pkg/cluster/controller/projects/projectsharegroups/zz_controller.go b/pkg/cluster/controller/projects/projectsharegroups/zz_controller.go index 4ed09fa4..dedf0694 100644 --- a/pkg/cluster/controller/projects/projectsharegroups/zz_controller.go +++ b/pkg/cluster/controller/projects/projectsharegroups/zz_controller.go @@ -6,12 +6,8 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/pkg/errors" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -32,27 +28,16 @@ const ( ) // SetupProjectShareGroup adds a controller that reconciles ProjectShareGroups. -func SetupProjectShareGroup(mgr ctrl.Manager, o controller.Options) error { +func SetupProjectShareGroup(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.ProjectShareGroupGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ProjectShareGroupGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewProjectClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ProjectShareGroupGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ProjectShareGroupList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ProjectShareGroupList{}); err != nil { return err } @@ -63,7 +48,7 @@ func SetupProjectShareGroup(mgr ctrl.Manager, o controller.Options) error { } // SetupProjectShareGroupGated adds a controller with CRD gate support. -func SetupProjectShareGroupGated(mgr ctrl.Manager, o controller.Options) error { +func SetupProjectShareGroupGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupProjectShareGroup(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ProjectShareGroupGroupVersionKind.String()) diff --git a/pkg/cluster/controller/projects/protectedbranches/zz_controller.go b/pkg/cluster/controller/projects/protectedbranches/zz_controller.go index 927eb978..e997cb42 100644 --- a/pkg/cluster/controller/projects/protectedbranches/zz_controller.go +++ b/pkg/cluster/controller/projects/protectedbranches/zz_controller.go @@ -21,12 +21,8 @@ package protectedbranches import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -50,27 +46,16 @@ const ( ) // SetupProtectedBranch adds a controller that reconciles ProtectedBranches. -func SetupProtectedBranch(mgr ctrl.Manager, o controller.Options) error { +func SetupProtectedBranch(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.ProtectedBranchGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ProtectedBranchGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewProtectedBranchClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ProtectedBranchGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ProtectedBranchList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ProtectedBranchList{}); err != nil { return err } @@ -82,7 +67,7 @@ func SetupProtectedBranch(mgr ctrl.Manager, o controller.Options) error { } // SetupProtectedBranchGated adds a controller with CRD gate support. -func SetupProtectedBranchGated(mgr ctrl.Manager, o controller.Options) error { +func SetupProtectedBranchGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupProtectedBranch(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ProtectedBranchGroupVersionKind.String()) diff --git a/pkg/cluster/controller/projects/protectedenvironments/zz_controller.go b/pkg/cluster/controller/projects/protectedenvironments/zz_controller.go index c9215852..9710217a 100644 --- a/pkg/cluster/controller/projects/protectedenvironments/zz_controller.go +++ b/pkg/cluster/controller/projects/protectedenvironments/zz_controller.go @@ -15,12 +15,8 @@ package protectedenvironments import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -45,32 +41,16 @@ const ( ) // SetupProtectedEnvironment adds a controller that reconciles ProtectedEnvironments. -func SetupProtectedEnvironment(mgr ctrl.Manager, o controller.Options) error { +func SetupProtectedEnvironment(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.ProtectedEnvironmentGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ProtectedEnvironmentGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewProtectedEnvironmentClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } + ) - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ProtectedEnvironmentGroupVersionKind), - reconcilerOpts...) - - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), - o.Logger, - o.MetricOptions.MRStateMetrics, - &v1alpha1.ProtectedEnvironmentList{}, - o.MetricOptions.PollStateMetricInterval, - )); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ProtectedEnvironmentList{}); err != nil { return err } @@ -82,7 +62,7 @@ func SetupProtectedEnvironment(mgr ctrl.Manager, o controller.Options) error { } // SetupProtectedEnvironmentGated adds a controller with CRD gate support. -func SetupProtectedEnvironmentGated(mgr ctrl.Manager, o controller.Options) error { +func SetupProtectedEnvironmentGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupProtectedEnvironment(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ProtectedEnvironmentGroupVersionKind.String()) diff --git a/pkg/cluster/controller/projects/runners/zz_controller.go b/pkg/cluster/controller/projects/runners/zz_controller.go index 34d299ab..d888b57f 100644 --- a/pkg/cluster/controller/projects/runners/zz_controller.go +++ b/pkg/cluster/controller/projects/runners/zz_controller.go @@ -24,13 +24,9 @@ import ( "strconv" "time" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/pkg/errors" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -59,30 +55,20 @@ const ( ) // SetupRunner adds a controller that reconciles projects runners. -func SetupRunner(mgr ctrl.Manager, o controller.Options) error { +func SetupRunner(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.RunnerGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.RunnerGroupVersionKind), managed.WithExternalConnector(&connector{ kube: mgr.GetClient(), newGitlabClientFn: runners.NewRunnerClient, newRunnerClientFn: users.NewRunnerClient, }), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.RunnerGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.RunnerList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.RunnerList{}); err != nil { return err } @@ -94,7 +80,7 @@ func SetupRunner(mgr ctrl.Manager, o controller.Options) error { } // SetupRunnerGated adds a controller with CRD gate support. -func SetupRunnerGated(mgr ctrl.Manager, o controller.Options) error { +func SetupRunnerGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupRunner(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.RunnerGroupVersionKind.String()) diff --git a/pkg/cluster/controller/projects/serviceaccountaccesstokens/zz_controller.go b/pkg/cluster/controller/projects/serviceaccountaccesstokens/zz_controller.go index 2079e82a..1afab33a 100644 --- a/pkg/cluster/controller/projects/serviceaccountaccesstokens/zz_controller.go +++ b/pkg/cluster/controller/projects/serviceaccountaccesstokens/zz_controller.go @@ -24,13 +24,9 @@ import ( "strconv" "strings" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/pkg/errors" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -96,27 +92,16 @@ func selfManagedCondition(self bool) v2.Condition { // SetupServiceAccountAccessToken adds a controller that reconciles // project ServiceAccountAccessTokens. -func SetupServiceAccountAccessToken(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccountAccessToken(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.ServiceAccountAccessTokenGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ServiceAccountAccessTokenGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewServiceAccountAccessTokenClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ServiceAccountAccessTokenGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ServiceAccountAccessTokenList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ServiceAccountAccessTokenList{}); err != nil { return err } @@ -128,7 +113,7 @@ func SetupServiceAccountAccessToken(mgr ctrl.Manager, o controller.Options) erro } // SetupServiceAccountAccessTokenGated adds a controller with CRD gate support. -func SetupServiceAccountAccessTokenGated(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccountAccessTokenGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupServiceAccountAccessToken(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ServiceAccountAccessTokenGroupVersionKind.String()) diff --git a/pkg/cluster/controller/projects/serviceaccounts/zz_controller.go b/pkg/cluster/controller/projects/serviceaccounts/zz_controller.go index edda3f50..5f8c9ae9 100644 --- a/pkg/cluster/controller/projects/serviceaccounts/zz_controller.go +++ b/pkg/cluster/controller/projects/serviceaccounts/zz_controller.go @@ -22,14 +22,10 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" gitlab "gitlab.com/gitlab-org/api/client-go/v3" ctrl "sigs.k8s.io/controller-runtime" @@ -52,27 +48,16 @@ const ( ) // SetupServiceAccount adds a controller that reconciles GitLab project Service Accounts. -func SetupServiceAccount(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccount(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.ServiceAccountGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ServiceAccountGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewServiceAccountClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ServiceAccountGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ServiceAccountList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ServiceAccountList{}); err != nil { return err } @@ -84,7 +69,7 @@ func SetupServiceAccount(mgr ctrl.Manager, o controller.Options) error { } // SetupServiceAccountGated adds a controller with CRD gate support. -func SetupServiceAccountGated(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccountGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupServiceAccount(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ServiceAccountGroupVersionKind.String()) diff --git a/pkg/cluster/controller/projects/variables/zz_controller.go b/pkg/cluster/controller/projects/variables/zz_controller.go index 1bce467e..67c7a870 100644 --- a/pkg/cluster/controller/projects/variables/zz_controller.go +++ b/pkg/cluster/controller/projects/variables/zz_controller.go @@ -21,13 +21,9 @@ package variables import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -51,27 +47,16 @@ const ( ) // SetupVariable adds a controller that reconciles Variables. -func SetupVariable(mgr ctrl.Manager, o controller.Options) error { +func SetupVariable(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName("cluster." + v1alpha1.VariableGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.VariableGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewVariableClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.VariableGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.VariableList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.VariableList{}); err != nil { return err } @@ -83,7 +68,7 @@ func SetupVariable(mgr ctrl.Manager, o controller.Options) error { } // SetupVariableGated adds a controller with CRD gate support. -func SetupVariableGated(mgr ctrl.Manager, o controller.Options) error { +func SetupVariableGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupVariable(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.VariableGroupVersionKind.String()) diff --git a/pkg/cluster/controller/projects/zz_setup.go b/pkg/cluster/controller/projects/zz_setup.go index ffb5827d..cdcf430f 100644 --- a/pkg/cluster/controller/projects/zz_setup.go +++ b/pkg/cluster/controller/projects/zz_setup.go @@ -19,7 +19,6 @@ limitations under the License. package projects import ( - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" ctrl "sigs.k8s.io/controller-runtime" "github.com/crossplane-contrib/provider-gitlab/pkg/cluster/controller/projects/accesstokens" @@ -40,11 +39,12 @@ import ( "github.com/crossplane-contrib/provider-gitlab/pkg/cluster/controller/projects/serviceaccountaccesstokens" "github.com/crossplane-contrib/provider-gitlab/pkg/cluster/controller/projects/serviceaccounts" "github.com/crossplane-contrib/provider-gitlab/pkg/cluster/controller/projects/variables" + "github.com/crossplane-contrib/provider-gitlab/pkg/common" ) // Setup all project controllers -func Setup(mgr ctrl.Manager, o controller.Options) error { - for _, setup := range []func(ctrl.Manager, controller.Options) error{ +func Setup(mgr ctrl.Manager, o common.ControllerOptions) error { + for _, setup := range []func(ctrl.Manager, common.ControllerOptions) error{ projects.SetupProject, hooks.SetupHook, members.SetupMember, @@ -73,8 +73,8 @@ func Setup(mgr ctrl.Manager, o controller.Options) error { // SetupGated creates all Gitlab API controllers with the supplied logger and adds // them to the supplied manager with CRD gate support for SafeStart. -func SetupGated(mgr ctrl.Manager, o controller.Options) error { - for _, setup := range []func(ctrl.Manager, controller.Options) error{ +func SetupGated(mgr ctrl.Manager, o common.ControllerOptions) error { + for _, setup := range []func(ctrl.Manager, common.ControllerOptions) error{ projects.SetupProjectGated, hooks.SetupHookGated, members.SetupMemberGated, diff --git a/pkg/cluster/controller/zz_setup.go b/pkg/cluster/controller/zz_setup.go index 6f14a3f8..cf3906a8 100644 --- a/pkg/cluster/controller/zz_setup.go +++ b/pkg/cluster/controller/zz_setup.go @@ -19,19 +19,19 @@ limitations under the License. package controller import ( - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" ctrl "sigs.k8s.io/controller-runtime" "github.com/crossplane-contrib/provider-gitlab/pkg/cluster/controller/config" "github.com/crossplane-contrib/provider-gitlab/pkg/cluster/controller/groups" "github.com/crossplane-contrib/provider-gitlab/pkg/cluster/controller/instance" "github.com/crossplane-contrib/provider-gitlab/pkg/cluster/controller/projects" + "github.com/crossplane-contrib/provider-gitlab/pkg/common" ) // Setup creates all Gitlab API controllers with the supplied logger and adds // them to the supplied manager. -func Setup(mgr ctrl.Manager, o controller.Options) error { - for _, setup := range []func(ctrl.Manager, controller.Options) error{ +func Setup(mgr ctrl.Manager, o common.ControllerOptions) error { + for _, setup := range []func(ctrl.Manager, common.ControllerOptions) error{ config.Setup, groups.Setup, projects.Setup, @@ -46,8 +46,8 @@ func Setup(mgr ctrl.Manager, o controller.Options) error { // SetupGated creates all Gitlab API controllers with the supplied logger and adds // them to the supplied manager with CRD gate support for SafeStart. -func SetupGated(mgr ctrl.Manager, o controller.Options) error { - for _, setup := range []func(ctrl.Manager, controller.Options) error{ +func SetupGated(mgr ctrl.Manager, o common.ControllerOptions) error { + for _, setup := range []func(ctrl.Manager, common.ControllerOptions) error{ config.SetupGated, groups.SetupGated, projects.SetupGated, diff --git a/pkg/common/controller.go b/pkg/common/controller.go new file mode 100644 index 00000000..4a723bb4 --- /dev/null +++ b/pkg/common/controller.go @@ -0,0 +1,68 @@ +/* +Copyright 2021 The Crossplane Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package common + +import ( + "time" + + "github.com/crossplane/crossplane-runtime/v2/pkg/controller" + "github.com/crossplane/crossplane-runtime/v2/pkg/event" + "github.com/crossplane/crossplane-runtime/v2/pkg/feature" + "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" + "github.com/crossplane/crossplane-runtime/v2/pkg/resource" + "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" + ctrl "sigs.k8s.io/controller-runtime" +) + +// ControllerOptions configure every controller this provider sets up. +type ControllerOptions struct { + controller.Options + + // MinPollInterval is the shortest poll interval a managed resource may + // request with the crossplane.io/poll-interval annotation. Shorter + // requests are raised to this value. + MinPollInterval time.Duration +} + +// NewReconciler returns a managed resource reconciler for the supplied kind +// that uses the provider-wide poll intervals, logger, event recorder, metric +// recorder and feature flags. The supplied options are applied after these. +func NewReconciler(mgr ctrl.Manager, o ControllerOptions, name string, of resource.ManagedKind, opts ...managed.ReconcilerOption) *managed.Reconciler { + ro := []managed.ReconcilerOption{ + managed.WithPollInterval(o.PollInterval), + managed.WithMinPollInterval(o.MinPollInterval), + managed.WithLogger(o.Logger.WithValues("controller", name)), + managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), + } + if o.MetricOptions != nil && o.MetricOptions.MRMetrics != nil { + ro = append(ro, managed.WithMetricRecorder(o.MetricOptions.MRMetrics)) + } + if o.Features.Enabled(feature.EnableBetaManagementPolicies) { + ro = append(ro, managed.WithManagementPolicies()) + } + return managed.NewReconciler(mgr, of, append(ro, opts...)...) +} + +// AddMRStateRecorder adds a runnable to mgr that records the exists, ready and +// synced metrics of the managed resources in list. It adds nothing when the +// options carry no MRStateMetrics. +func AddMRStateRecorder(mgr ctrl.Manager, o ControllerOptions, list resource.ManagedList) error { + if o.MetricOptions == nil || o.MetricOptions.MRStateMetrics == nil { + return nil + } + return mgr.Add(statemetrics.NewMRStateRecorder(mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, list, o.MetricOptions.PollStateMetricInterval)) +} diff --git a/pkg/common/controller_test.go b/pkg/common/controller_test.go new file mode 100644 index 00000000..4891b8a2 --- /dev/null +++ b/pkg/common/controller_test.go @@ -0,0 +1,283 @@ +/* +Copyright 2021 The Crossplane Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package common + +import ( + "context" + "strings" + "testing" + "time" + + "github.com/crossplane/crossplane-runtime/v2/pkg/controller" + "github.com/crossplane/crossplane-runtime/v2/pkg/feature" + "github.com/crossplane/crossplane-runtime/v2/pkg/logging" + "github.com/crossplane/crossplane-runtime/v2/pkg/meta" + "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" + "github.com/crossplane/crossplane-runtime/v2/pkg/resource" + "github.com/crossplane/crossplane-runtime/v2/pkg/resource/fake" + "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" + "github.com/crossplane/crossplane-runtime/v2/pkg/test" + xpv2 "github.com/crossplane/crossplane/apis/v2/core/v2" + "github.com/google/go-cmp/cmp" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/tools/record" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/manager" + "sigs.k8s.io/controller-runtime/pkg/metrics" + "sigs.k8s.io/controller-runtime/pkg/reconcile" + + namespacedgroupsv1alpha1 "github.com/crossplane-contrib/provider-gitlab/apis/namespaced/groups/v1alpha1" +) + +type fakeManager struct { + fake.Manager + runnables []manager.Runnable +} + +func (m *fakeManager) GetEventRecorderFor(string) record.EventRecorder { + return record.NewFakeRecorder(8) +} + +func (m *fakeManager) Add(r manager.Runnable) error { + m.runnables = append(m.runnables, r) + return nil +} + +func firstReconciles(t *testing.T) uint64 { + t.Helper() + families, err := metrics.Registry.Gather() + if err != nil { + t.Fatalf("metrics.Registry.Gather(): %v", err) + } + var n uint64 + for _, f := range families { + if f.GetName() != "crossplane_managed_resource_first_time_to_reconcile_seconds" { + continue + } + for _, m := range f.GetMetric() { + n += m.GetHistogram().GetSampleCount() + } + } + return n +} + +func stateGauges(t *testing.T) map[string]float64 { + t.Helper() + families, err := metrics.Registry.Gather() + if err != nil { + t.Fatalf("metrics.Registry.Gather(): %v", err) + } + got := map[string]float64{} + for _, f := range families { + if !strings.HasPrefix(f.GetName(), "crossplane_managed_resource_") { + continue + } + for _, m := range f.GetMetric() { + for _, l := range m.GetLabel() { + if l.GetName() == "gvk" { + got[f.GetName()+"{"+l.GetValue()+"}"] = m.GetGauge().GetValue() + } + } + } + } + return got +} + +func TestAddMRStateRecorder(t *testing.T) { + cases := map[string]struct { + reason string + metricOptions func(*statemetrics.MRStateMetrics) *controller.MetricOptions + wantRunnables int + wantGauges map[string]float64 + }{ + "NilMetricOptions": { + reason: "Without metric options no recorder should be added.", + wantGauges: map[string]float64{}, + }, + "MetricOptionsWithoutStateMetrics": { + reason: "Metric options without MRStateMetrics should add no recorder.", + metricOptions: func(*statemetrics.MRStateMetrics) *controller.MetricOptions { + return &controller.MetricOptions{PollStateMetricInterval: time.Millisecond} + }, + wantGauges: map[string]float64{}, + }, + "StateMetrics": { + reason: "With MRStateMetrics one recorder should be added that records the supplied list into those metrics.", + metricOptions: func(sm *statemetrics.MRStateMetrics) *controller.MetricOptions { + return &controller.MetricOptions{MRStateMetrics: sm, PollStateMetricInterval: time.Millisecond} + }, + wantRunnables: 1, + wantGauges: map[string]float64{ + "crossplane_managed_resource_exists{groups.gitlab.m.crossplane.io/v1alpha1, Kind=Variable}": 2, + "crossplane_managed_resource_ready{groups.gitlab.m.crossplane.io/v1alpha1, Kind=Variable}": 1, + "crossplane_managed_resource_synced{groups.gitlab.m.crossplane.io/v1alpha1, Kind=Variable}": 1, + }, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + sm := statemetrics.NewMRStateMetrics() + if err := metrics.Registry.Register(sm); err != nil { + t.Fatalf("metrics.Registry.Register(...): %v", err) + } + t.Cleanup(func() { metrics.Registry.Unregister(sm) }) + + s := runtime.NewScheme() + if err := namespacedgroupsv1alpha1.SchemeBuilder.AddToScheme(s); err != nil { + t.Fatalf("AddToScheme(...): %v", err) + } + ready := namespacedgroupsv1alpha1.Variable{} + ready.SetConditions(xpv2.Available(), xpv2.ReconcileSuccess()) + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + list := &namespacedgroupsv1alpha1.VariableList{} + mgr := &fakeManager{Manager: fake.Manager{Client: &test.MockClient{ + MockScheme: test.NewMockSchemeFn(s), + MockList: test.NewMockListFn(nil, func(client.ObjectList) error { + list.Items = []namespacedgroupsv1alpha1.Variable{ready, {}} + cancel() + return nil + }), + }}} + o := ControllerOptions{Options: controller.Options{Logger: logging.NewNopLogger()}} + if tc.metricOptions != nil { + o.MetricOptions = tc.metricOptions(sm) + } + + if err := AddMRStateRecorder(mgr, o, list); err != nil { + t.Fatalf("\n%s\nAddMRStateRecorder(...): unexpected error: %v", tc.reason, err) + } + if diff := cmp.Diff(tc.wantRunnables, len(mgr.runnables)); diff != "" { + t.Fatalf("\n%s\nrunnables added: -want, +got:\n%s", tc.reason, diff) + } + for _, r := range mgr.runnables { + if err := r.Start(ctx); err != nil { + t.Fatalf("\n%s\nr.Start(...): unexpected error: %v", tc.reason, err) + } + } + if diff := cmp.Diff(tc.wantGauges, stateGauges(t)); diff != "" { + t.Errorf("\n%s\nstate gauges: -want, +got:\n%s", tc.reason, diff) + } + }) + } +} + +func TestNewReconciler(t *testing.T) { + cases := map[string]struct { + reason string + annotation string + managementPolicies bool + metricOptions func(managed.MetricRecorder) *controller.MetricOptions + want reconcile.Result + wantFirstReconciles uint64 + }{ + "AnnotationBelowMinimum": { + reason: "An annotation below MinPollInterval should poll at MinPollInterval.", + annotation: "2s", + want: reconcile.Result{RequeueAfter: 10 * time.Second}, + }, + "AnnotationAboveMinimum": { + reason: "An annotation above MinPollInterval should poll at the annotation value.", + annotation: "45s", + want: reconcile.Result{RequeueAfter: 45 * time.Second}, + }, + "NoAnnotation": { + reason: "A resource without the annotation should poll at PollInterval.", + want: reconcile.Result{RequeueAfter: 10 * time.Minute}, + }, + "ManagementPoliciesEnabled": { + reason: "With the management policies feature enabled, empty management policies should pause the resource.", + managementPolicies: true, + want: reconcile.Result{}, + }, + "MetricRecorder": { + reason: "The MRMetrics recorder from the metric options should record the first reconcile.", + metricOptions: func(mr managed.MetricRecorder) *controller.MetricOptions { + return &controller.MetricOptions{MRMetrics: mr} + }, + want: reconcile.Result{RequeueAfter: 10 * time.Minute}, + wantFirstReconciles: 1, + }, + "MetricOptionsWithoutRecorder": { + reason: "Metric options without an MRMetrics recorder should neither record nor panic.", + metricOptions: func(managed.MetricRecorder) *controller.MetricOptions { + return &controller.MetricOptions{} + }, + want: reconcile.Result{RequeueAfter: 10 * time.Minute}, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + mr := managed.NewMRMetricRecorder() + if err := metrics.Registry.Register(mr); err != nil { + t.Fatalf("metrics.Registry.Register(...): %v", err) + } + t.Cleanup(func() { metrics.Registry.Unregister(mr) }) + + mgr := &fakeManager{Manager: fake.Manager{ + Client: &test.MockClient{ + MockGet: test.NewMockGetFn(nil, func(obj client.Object) error { + if tc.annotation != "" { + obj.SetAnnotations(map[string]string{meta.AnnotationKeyPollInterval: tc.annotation}) + } + return nil + }), + MockStatusUpdate: test.NewMockSubResourceUpdateFn(nil), + }, + Scheme: fake.SchemeWith(&fake.ModernManaged{}), + }} + features := &feature.Flags{} + if tc.managementPolicies { + features.Enable(feature.EnableBetaManagementPolicies) + } + o := ControllerOptions{ + Options: controller.Options{Logger: logging.NewNopLogger(), PollInterval: 10 * time.Minute, Features: features}, + MinPollInterval: 10 * time.Second, + } + if tc.metricOptions != nil { + o.MetricOptions = tc.metricOptions(mr) + } + + r := NewReconciler(mgr, o, "test", resource.ManagedKind(fake.GVK(&fake.ModernManaged{})), + managed.WithInitializers(), + managed.WithFinalizer(resource.FinalizerFns{AddFinalizerFn: func(context.Context, resource.Object) error { return nil }}), + managed.WithExternalConnector(managed.ExternalConnectorFn(func(context.Context, resource.Managed) (managed.ExternalClient, error) { + return &managed.ExternalClientFns{ + ObserveFn: func(context.Context, resource.Managed) (managed.ExternalObservation, error) { + return managed.ExternalObservation{ResourceExists: true, ResourceUpToDate: true}, nil + }, + DisconnectFn: func(context.Context) error { return nil }, + }, nil + })), + ) + + got, err := r.Reconcile(context.Background(), reconcile.Request{}) + if err != nil { + t.Fatalf("\n%s\nr.Reconcile(...): unexpected error: %v", tc.reason, err) + } + if diff := cmp.Diff(tc.want, got); diff != "" { + t.Errorf("\n%s\nr.Reconcile(...): -want, +got:\n%s", tc.reason, diff) + } + if diff := cmp.Diff(tc.wantFirstReconciles, firstReconciles(t)); diff != "" { + t.Errorf("\n%s\nfirst reconciles recorded: -want, +got:\n%s", tc.reason, diff) + } + }) + } +} diff --git a/pkg/namespaced/controller/config/config.go b/pkg/namespaced/controller/config/config.go index 133f9e35..8db2b6aa 100644 --- a/pkg/namespaced/controller/config/config.go +++ b/pkg/namespaced/controller/config/config.go @@ -17,13 +17,13 @@ limitations under the License. package config import ( - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/event" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/providerconfig" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" ctrl "sigs.k8s.io/controller-runtime" "github.com/crossplane-contrib/provider-gitlab/apis/namespaced/v1beta1" + "github.com/crossplane-contrib/provider-gitlab/pkg/common" ) // Setup adds a controller that reconciles ProviderConfigs by accounting for @@ -38,7 +38,7 @@ import ( // the ProviderConfig reconciler to the deprecated version: when v1beta1 is // eventually removed, these references must move to the then-current version, // otherwise the gate never fires and this controller silently never starts. -func SetupNamespaced(mgr ctrl.Manager, o controller.Options) error { +func SetupNamespaced(mgr ctrl.Manager, o common.ControllerOptions) error { name := providerconfig.ControllerName(v1beta1.ProviderConfigGroupKind) of := resource.ProviderConfigKinds{ @@ -57,7 +57,7 @@ func SetupNamespaced(mgr ctrl.Manager, o controller.Options) error { providerconfig.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))))) } -func SetupNamespacedGated(mgr ctrl.Manager, o controller.Options) error { +func SetupNamespacedGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupNamespaced(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconcilers", "gvk", v1beta1.ProviderConfigGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/config/config_cluster.go b/pkg/namespaced/controller/config/config_cluster.go index a7303952..e7762d1d 100644 --- a/pkg/namespaced/controller/config/config_cluster.go +++ b/pkg/namespaced/controller/config/config_cluster.go @@ -19,18 +19,18 @@ limitations under the License. package config import ( - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/event" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/providerconfig" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" ctrl "sigs.k8s.io/controller-runtime" "github.com/crossplane-contrib/provider-gitlab/apis/namespaced/v1beta1" + "github.com/crossplane-contrib/provider-gitlab/pkg/common" ) // Setup adds a controller that reconciles ProviderConfigs by accounting for // their current usage. -func SetupCluster(mgr ctrl.Manager, o controller.Options) error { +func SetupCluster(mgr ctrl.Manager, o common.ControllerOptions) error { name := providerconfig.ControllerName(v1beta1.ClusterProviderConfigGroupKind) of := resource.ProviderConfigKinds{ @@ -49,7 +49,7 @@ func SetupCluster(mgr ctrl.Manager, o controller.Options) error { providerconfig.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))))) } -func SetupClusterGated(mgr ctrl.Manager, o controller.Options) error { +func SetupClusterGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := Setup(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconcilers", "gvk", v1beta1.ClusterProviderConfigGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/config/setup.go b/pkg/namespaced/controller/config/setup.go index be43643d..f5283767 100644 --- a/pkg/namespaced/controller/config/setup.go +++ b/pkg/namespaced/controller/config/setup.go @@ -17,14 +17,15 @@ limitations under the License. package config import ( - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" ctrl "sigs.k8s.io/controller-runtime" + + "github.com/crossplane-contrib/provider-gitlab/pkg/common" ) // Setup creates all Gitlab API controllers with the supplied logger and adds // them to the supplied manager. -func Setup(mgr ctrl.Manager, o controller.Options) error { - for _, setup := range []func(ctrl.Manager, controller.Options) error{ +func Setup(mgr ctrl.Manager, o common.ControllerOptions) error { + for _, setup := range []func(ctrl.Manager, common.ControllerOptions) error{ SetupNamespaced, // +cluster-scope:delete=1 SetupCluster, @@ -38,8 +39,8 @@ func Setup(mgr ctrl.Manager, o controller.Options) error { // SetupGated creates all Gitlab API controllers with the supplied logger and adds // them to the supplied manager with CRD gate support for SafeStart. -func SetupGated(mgr ctrl.Manager, o controller.Options) error { - for _, setup := range []func(ctrl.Manager, controller.Options) error{ +func SetupGated(mgr ctrl.Manager, o common.ControllerOptions) error { + for _, setup := range []func(ctrl.Manager, common.ControllerOptions) error{ SetupNamespacedGated, // +cluster-scope:delete=1 SetupClusterGated, diff --git a/pkg/namespaced/controller/groups/accesstokens/controller.go b/pkg/namespaced/controller/groups/accesstokens/controller.go index fe407908..3c2b2e5b 100644 --- a/pkg/namespaced/controller/groups/accesstokens/controller.go +++ b/pkg/namespaced/controller/groups/accesstokens/controller.go @@ -20,13 +20,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/pkg/errors" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -90,27 +86,16 @@ func selfManagedCondition(self bool) v2.Condition { } // SetupAccessToken adds a controller that reconciles GroupAccessTokens. -func SetupAccessToken(mgr ctrl.Manager, o controller.Options) error { +func SetupAccessToken(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.AccessTokenGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.AccessTokenGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: groups.NewAccessTokenClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.AccessTokenGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.AccessTokenList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.AccessTokenList{}); err != nil { return err } @@ -122,7 +107,7 @@ func SetupAccessToken(mgr ctrl.Manager, o controller.Options) error { } // SetupAccessTokenGated adds a controller with CRD gate support. -func SetupAccessTokenGated(mgr ctrl.Manager, o controller.Options) error { +func SetupAccessTokenGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupAccessToken(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.AccessTokenGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/groups/badges/controller.go b/pkg/namespaced/controller/groups/badges/controller.go index 382a5ade..46691fe2 100644 --- a/pkg/namespaced/controller/groups/badges/controller.go +++ b/pkg/namespaced/controller/groups/badges/controller.go @@ -20,13 +20,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -50,27 +46,16 @@ const ( ) // SetupBadge adds a controller that reconciles GroupBadges. -func SetupBadge(mgr ctrl.Manager, o controller.Options) error { +func SetupBadge(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.BadgeGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.BadgeGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: groups.NewBadgeClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.BadgeGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.BadgeList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.BadgeList{}); err != nil { return err } @@ -82,7 +67,7 @@ func SetupBadge(mgr ctrl.Manager, o controller.Options) error { } // SetupBadgeGated adds a controller with CRD gate support. -func SetupBadgeGated(mgr ctrl.Manager, o controller.Options) error { +func SetupBadgeGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupBadge(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.BadgeGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/groups/deploytokens/controller.go b/pkg/namespaced/controller/groups/deploytokens/controller.go index b77945c1..aae89ee2 100644 --- a/pkg/namespaced/controller/groups/deploytokens/controller.go +++ b/pkg/namespaced/controller/groups/deploytokens/controller.go @@ -20,13 +20,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -51,27 +47,16 @@ const ( ) // SetupDeployToken adds a controller that reconciles GroupDeployTokens. -func SetupDeployToken(mgr ctrl.Manager, o controller.Options) error { +func SetupDeployToken(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.DeployTokenGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.DeployTokenGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: groups.NewDeployTokenClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.DeployTokenGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.DeployTokenList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.DeployTokenList{}); err != nil { return err } @@ -83,7 +68,7 @@ func SetupDeployToken(mgr ctrl.Manager, o controller.Options) error { } // SetupDeployTokenGated adds a controller with CRD gate support. -func SetupDeployTokenGated(mgr ctrl.Manager, o controller.Options) error { +func SetupDeployTokenGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupDeployToken(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.DeployTokenGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/groups/groups/group.go b/pkg/namespaced/controller/groups/groups/group.go index 03b8bc1e..b5edc953 100644 --- a/pkg/namespaced/controller/groups/groups/group.go +++ b/pkg/namespaced/controller/groups/groups/group.go @@ -22,13 +22,9 @@ import ( "strings" "time" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -59,27 +55,16 @@ const ( ) // SetupGroup adds a controller that reconciles Groups. -func SetupGroup(mgr ctrl.Manager, o controller.Options) error { +func SetupGroup(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.GroupKubernetesGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.GroupKubernetesGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: groups.NewGroupClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.GroupKubernetesGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.GroupList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.GroupList{}); err != nil { return err } @@ -91,7 +76,7 @@ func SetupGroup(mgr ctrl.Manager, o controller.Options) error { } // SetupGroupGated adds a controller with CRD gate support. -func SetupGroupGated(mgr ctrl.Manager, o controller.Options) error { +func SetupGroupGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupGroup(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.GroupKubernetesGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/groups/hooks/controller.go b/pkg/namespaced/controller/groups/hooks/controller.go index 95ec216f..6479dc4d 100644 --- a/pkg/namespaced/controller/groups/hooks/controller.go +++ b/pkg/namespaced/controller/groups/hooks/controller.go @@ -20,13 +20,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -52,27 +48,16 @@ const ( ) // SetupHook adds a controller that reconciles Hooks. -func SetupHook(mgr ctrl.Manager, o controller.Options) error { +func SetupHook(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.HookGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.HookGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: groups.NewHookClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.HookGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.HookList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.HookList{}); err != nil { return err } @@ -84,7 +69,7 @@ func SetupHook(mgr ctrl.Manager, o controller.Options) error { } // SetupHookGated adds a controller with CRD gate support. -func SetupHookGated(mgr ctrl.Manager, o controller.Options) error { +func SetupHookGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupHook(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.HookGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/groups/integrationharbor/controller.go b/pkg/namespaced/controller/groups/integrationharbor/controller.go index 2d5f36d0..de96383d 100644 --- a/pkg/namespaced/controller/groups/integrationharbor/controller.go +++ b/pkg/namespaced/controller/groups/integrationharbor/controller.go @@ -19,13 +19,9 @@ package integrationharbor import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" gitlab "gitlab.com/gitlab-org/api/client-go/v3" ctrl "sigs.k8s.io/controller-runtime" @@ -48,34 +44,16 @@ const ( ) // SetupIntegrationHarbor adds a controller that reconciles GitLab Group Harbor integrations. -func SetupIntegrationHarbor(mgr ctrl.Manager, o controller.Options) error { +func SetupIntegrationHarbor(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.IntegrationHarborGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.IntegrationHarborGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: groups.NewHarborClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler( - mgr, - resource.ManagedKind(v1alpha1.IntegrationHarborGroupVersionKind), - reconcilerOpts..., ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), - o.Logger, - o.MetricOptions.MRStateMetrics, - &v1alpha1.IntegrationHarborList{}, - o.MetricOptions.PollStateMetricInterval, - )); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.IntegrationHarborList{}); err != nil { return err } @@ -87,7 +65,7 @@ func SetupIntegrationHarbor(mgr ctrl.Manager, o controller.Options) error { } // SetupIntegrationHarborGated adds a controller with CRD gate support for SafeStart. -func SetupIntegrationHarborGated(mgr ctrl.Manager, o controller.Options) error { +func SetupIntegrationHarborGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupIntegrationHarbor(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.IntegrationHarborGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/groups/ldapgrouplinks/controller.go b/pkg/namespaced/controller/groups/ldapgrouplinks/controller.go index 914a7b1e..ccdd3e03 100644 --- a/pkg/namespaced/controller/groups/ldapgrouplinks/controller.go +++ b/pkg/namespaced/controller/groups/ldapgrouplinks/controller.go @@ -20,13 +20,9 @@ import ( "context" "fmt" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/pkg/errors" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -49,26 +45,16 @@ const ( ) // SetupLdapGroupLink adds a controller that reconciles ldapgrouplinks. -func SetupLdapGroupLink(mgr ctrl.Manager, o controller.Options) error { +func SetupLdapGroupLink(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.LdapGroupLinkGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.LdapGroupLinkGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: groups.NewLdapGroupLinkClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.LdapGroupLinkGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.LdapGroupLinkList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.LdapGroupLinkList{}); err != nil { return err } @@ -80,7 +66,7 @@ func SetupLdapGroupLink(mgr ctrl.Manager, o controller.Options) error { } // SetupLdapGroupLinkGated adds a controller with CRD gate support. -func SetupLdapGroupLinkGated(mgr ctrl.Manager, o controller.Options) error { +func SetupLdapGroupLinkGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupLdapGroupLink(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.LdapGroupLinkGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/groups/members/controller.go b/pkg/namespaced/controller/groups/members/controller.go index 43fe3561..aa23fea7 100644 --- a/pkg/namespaced/controller/groups/members/controller.go +++ b/pkg/namespaced/controller/groups/members/controller.go @@ -16,12 +16,8 @@ package members import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -49,31 +45,20 @@ const ( ) // SetupMember adds a controller that reconciles Group Members. -func SetupMember(mgr ctrl.Manager, o controller.Options) error { +func SetupMember(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.MemberKubernetesGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.MemberKubernetesGroupVersionKind), managed.WithExternalConnector(&connector{ kube: mgr.GetClient(), newGitlabClientFn: groups.NewMemberClient, newUserClientFn: users.NewUserClient, }), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.MemberKubernetesGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.MemberList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.MemberList{}); err != nil { return err } @@ -85,7 +70,7 @@ func SetupMember(mgr ctrl.Manager, o controller.Options) error { } // SetupMemberGated adds a controller with CRD gate support. -func SetupMemberGated(mgr ctrl.Manager, o controller.Options) error { +func SetupMemberGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupMember(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.MemberKubernetesGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/groups/runners/controller.go b/pkg/namespaced/controller/groups/runners/controller.go index 2355f64e..3c8c6d93 100644 --- a/pkg/namespaced/controller/groups/runners/controller.go +++ b/pkg/namespaced/controller/groups/runners/controller.go @@ -22,13 +22,9 @@ import ( "strconv" "time" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/pkg/errors" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -57,30 +53,20 @@ const ( ) // SetupRunner adds a controller that reconciles runners. -func SetupRunner(mgr ctrl.Manager, o controller.Options) error { +func SetupRunner(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.RunnerGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.RunnerGroupVersionKind), managed.WithExternalConnector(&connector{ kube: mgr.GetClient(), newGitlabClientFn: runners.NewRunnerClient, newRunnerClientFn: users.NewRunnerClient, }), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.RunnerGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.RunnerList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.RunnerList{}); err != nil { return err } @@ -92,7 +78,7 @@ func SetupRunner(mgr ctrl.Manager, o controller.Options) error { } // SetupRunnerGated adds a controller with CRD gate support. -func SetupRunnerGated(mgr ctrl.Manager, o controller.Options) error { +func SetupRunnerGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupRunner(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.RunnerGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/groups/samlgrouplinks/controller.go b/pkg/namespaced/controller/groups/samlgrouplinks/controller.go index fe2c594d..5194ea78 100644 --- a/pkg/namespaced/controller/groups/samlgrouplinks/controller.go +++ b/pkg/namespaced/controller/groups/samlgrouplinks/controller.go @@ -19,13 +19,9 @@ package samlgrouplinks import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -49,26 +45,16 @@ const ( ) // SetupSamlGroupLink adds a controller that reconciles samlgrouplinks. -func SetupSamlGroupLink(mgr ctrl.Manager, o controller.Options) error { +func SetupSamlGroupLink(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.SamlGroupLinkGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.SamlGroupLinkGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: groups.NewSamlGroupLinkClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.SamlGroupLinkGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.SamlGroupLinkList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.SamlGroupLinkList{}); err != nil { return err } @@ -80,7 +66,7 @@ func SetupSamlGroupLink(mgr ctrl.Manager, o controller.Options) error { } // SetupSamlGroupLinkGated adds a controller with CRD gate support. -func SetupSamlGroupLinkGated(mgr ctrl.Manager, o controller.Options) error { +func SetupSamlGroupLinkGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupSamlGroupLink(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.SamlGroupLinkGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/groups/serviceaccountaccesstokens/controller.go b/pkg/namespaced/controller/groups/serviceaccountaccesstokens/controller.go index ef92db22..e8a1a3bb 100644 --- a/pkg/namespaced/controller/groups/serviceaccountaccesstokens/controller.go +++ b/pkg/namespaced/controller/groups/serviceaccountaccesstokens/controller.go @@ -22,13 +22,9 @@ import ( "strconv" "strings" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/pkg/errors" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -94,27 +90,16 @@ func selfManagedCondition(self bool) v2.Condition { // SetupServiceAccountAccessToken adds a controller that reconciles // group ServiceAccountAccessTokens. -func SetupServiceAccountAccessToken(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccountAccessToken(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.ServiceAccountAccessTokenGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ServiceAccountAccessTokenGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: groups.NewServiceAccountAccessTokenClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ServiceAccountAccessTokenGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ServiceAccountAccessTokenList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ServiceAccountAccessTokenList{}); err != nil { return err } @@ -126,7 +111,7 @@ func SetupServiceAccountAccessToken(mgr ctrl.Manager, o controller.Options) erro } // SetupServiceAccountAccessTokenGated adds a controller with CRD gate support. -func SetupServiceAccountAccessTokenGated(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccountAccessTokenGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupServiceAccountAccessToken(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ServiceAccountAccessTokenGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/groups/serviceaccounts/controller.go b/pkg/namespaced/controller/groups/serviceaccounts/controller.go index b20022e6..58ca08fe 100644 --- a/pkg/namespaced/controller/groups/serviceaccounts/controller.go +++ b/pkg/namespaced/controller/groups/serviceaccounts/controller.go @@ -20,14 +20,10 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" gitlab "gitlab.com/gitlab-org/api/client-go/v3" ctrl "sigs.k8s.io/controller-runtime" @@ -51,27 +47,16 @@ const ( ) // SetupServiceAccount adds a controller that reconciles GitLab Service Accounts. -func SetupServiceAccount(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccount(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.ServiceAccountGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ServiceAccountGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: groups.NewServiceAccountClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ServiceAccountGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ServiceAccountList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ServiceAccountList{}); err != nil { return err } @@ -83,7 +68,7 @@ func SetupServiceAccount(mgr ctrl.Manager, o controller.Options) error { } // SetupServiceAccountGated adds a controller with CRD gate support. -func SetupServiceAccountGated(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccountGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupServiceAccount(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ServiceAccountGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/groups/setup.go b/pkg/namespaced/controller/groups/setup.go index 13b656ad..46519f02 100644 --- a/pkg/namespaced/controller/groups/setup.go +++ b/pkg/namespaced/controller/groups/setup.go @@ -17,9 +17,9 @@ limitations under the License. package groups import ( - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" ctrl "sigs.k8s.io/controller-runtime" + "github.com/crossplane-contrib/provider-gitlab/pkg/common" "github.com/crossplane-contrib/provider-gitlab/pkg/namespaced/controller/groups/accesstokens" "github.com/crossplane-contrib/provider-gitlab/pkg/namespaced/controller/groups/badges" "github.com/crossplane-contrib/provider-gitlab/pkg/namespaced/controller/groups/deploytokens" @@ -36,8 +36,8 @@ import ( ) // Setup all group controllers -func Setup(mgr ctrl.Manager, o controller.Options) error { - for _, setup := range []func(ctrl.Manager, controller.Options) error{ +func Setup(mgr ctrl.Manager, o common.ControllerOptions) error { + for _, setup := range []func(ctrl.Manager, common.ControllerOptions) error{ groups.SetupGroup, members.SetupMember, accesstokens.SetupAccessToken, @@ -61,8 +61,8 @@ func Setup(mgr ctrl.Manager, o controller.Options) error { // SetupGated creates all Gitlab API controllers with the supplied logger and adds // them to the supplied manager with CRD gate support for SafeStart. -func SetupGated(mgr ctrl.Manager, o controller.Options) error { - for _, setup := range []func(ctrl.Manager, controller.Options) error{ +func SetupGated(mgr ctrl.Manager, o common.ControllerOptions) error { + for _, setup := range []func(ctrl.Manager, common.ControllerOptions) error{ groups.SetupGroupGated, members.SetupMemberGated, accesstokens.SetupAccessTokenGated, diff --git a/pkg/namespaced/controller/groups/variables/controller.go b/pkg/namespaced/controller/groups/variables/controller.go index ce2fe7c9..b254270d 100644 --- a/pkg/namespaced/controller/groups/variables/controller.go +++ b/pkg/namespaced/controller/groups/variables/controller.go @@ -19,13 +19,9 @@ package variables import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -49,27 +45,16 @@ const ( ) // SetupVariable adds a controller that reconciles Variables. -func SetupVariable(mgr ctrl.Manager, o controller.Options) error { +func SetupVariable(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.VariableGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.VariableGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: groups.NewVariableClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.VariableGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.VariableList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.VariableList{}); err != nil { return err } @@ -81,7 +66,7 @@ func SetupVariable(mgr ctrl.Manager, o controller.Options) error { } // SetupVariableGated adds a controller with CRD gate support. -func SetupVariableGated(mgr ctrl.Manager, o controller.Options) error { +func SetupVariableGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupVariable(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.VariableGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/instance/appearance/controller.go b/pkg/namespaced/controller/instance/appearance/controller.go index 1596ea5d..ea2564d3 100644 --- a/pkg/namespaced/controller/instance/appearance/controller.go +++ b/pkg/namespaced/controller/instance/appearance/controller.go @@ -19,13 +19,9 @@ package appearance import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" gitlab "gitlab.com/gitlab-org/api/client-go/v3" ctrl "sigs.k8s.io/controller-runtime" @@ -45,27 +41,16 @@ const ( ) // SetupAppearance adds a controller that reconciles GitLab Instance Appearance. -func SetupAppearance(mgr ctrl.Manager, o controller.Options) error { +func SetupAppearance(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.AppearanceGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.AppearanceGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: instance.NewAppearanceClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.AppearanceGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.AppearanceList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.AppearanceList{}); err != nil { return err } @@ -77,7 +62,7 @@ func SetupAppearance(mgr ctrl.Manager, o controller.Options) error { } // SetupAppearanceGated adds a controller with CRD gate support. -func SetupAppearanceGated(mgr ctrl.Manager, o controller.Options) error { +func SetupAppearanceGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupAppearance(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.AppearanceGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/instance/applications/controller.go b/pkg/namespaced/controller/instance/applications/controller.go index db48866c..a766abda 100644 --- a/pkg/namespaced/controller/instance/applications/controller.go +++ b/pkg/namespaced/controller/instance/applications/controller.go @@ -20,14 +20,10 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" gitlab "gitlab.com/gitlab-org/api/client-go/v3" ctrl "sigs.k8s.io/controller-runtime" @@ -50,27 +46,16 @@ const ( ) // SetupApplication adds a controller that reconciles GitLab OAuth Applications. -func SetupApplication(mgr ctrl.Manager, o controller.Options) error { +func SetupApplication(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.ApplicationGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ApplicationGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: instance.NewApplicationClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ApplicationGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ApplicationList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ApplicationList{}); err != nil { return err } @@ -82,7 +67,7 @@ func SetupApplication(mgr ctrl.Manager, o controller.Options) error { } // SetupApplicationGated adds a controller with CRD gate support. -func SetupApplicationGated(mgr ctrl.Manager, o controller.Options) error { +func SetupApplicationGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupApplication(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ApplicationGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/instance/license/controller.go b/pkg/namespaced/controller/instance/license/controller.go index fa1d0e33..b2284644 100644 --- a/pkg/namespaced/controller/instance/license/controller.go +++ b/pkg/namespaced/controller/instance/license/controller.go @@ -20,13 +20,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/pkg/errors" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -55,29 +51,19 @@ const ( ) // SetupLicense adds a controller that reconciles instance licenses. -func SetupLicense(mgr ctrl.Manager, o controller.Options) error { +func SetupLicense(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.LicenseGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.LicenseGroupVersionKind), managed.WithExternalConnector(&connector{ kube: mgr.GetClient(), newGitlabClientFn: instance.NewLicenseClient, }), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.LicenseGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.LicenseList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.LicenseList{}); err != nil { return err } @@ -89,7 +75,7 @@ func SetupLicense(mgr ctrl.Manager, o controller.Options) error { } // SetupLicenseGated adds a controller with CRD gate support. -func SetupLicenseGated(mgr ctrl.Manager, o controller.Options) error { +func SetupLicenseGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupLicense(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.LicenseGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/instance/runners/controller.go b/pkg/namespaced/controller/instance/runners/controller.go index 04b470ad..2cfb997f 100644 --- a/pkg/namespaced/controller/instance/runners/controller.go +++ b/pkg/namespaced/controller/instance/runners/controller.go @@ -21,13 +21,9 @@ import ( "strconv" "time" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/pkg/errors" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -55,30 +51,20 @@ const ( ) // SetupRunner adds a controller that reconciles instance runners. -func SetupRunner(mgr ctrl.Manager, o controller.Options) error { +func SetupRunner(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.RunnerGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.RunnerGroupVersionKind), managed.WithExternalConnector(&connector{ kube: mgr.GetClient(), newGitlabClientFn: runners.NewRunnerClient, newRunnerClientFn: users.NewRunnerClient, }), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.RunnerGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.RunnerList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.RunnerList{}); err != nil { return err } @@ -90,7 +76,7 @@ func SetupRunner(mgr ctrl.Manager, o controller.Options) error { } // SetupRunnerGated adds a controller with CRD gate support. -func SetupRunnerGated(mgr ctrl.Manager, o controller.Options) error { +func SetupRunnerGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupRunner(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.RunnerGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/instance/serviceaccountaccesstokens/controller.go b/pkg/namespaced/controller/instance/serviceaccountaccesstokens/controller.go index 978c018b..cda15ddd 100644 --- a/pkg/namespaced/controller/instance/serviceaccountaccesstokens/controller.go +++ b/pkg/namespaced/controller/instance/serviceaccountaccesstokens/controller.go @@ -22,13 +22,9 @@ import ( "strconv" "strings" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/pkg/errors" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -95,27 +91,16 @@ func selfManagedCondition(self bool) v2.Condition { // SetupServiceAccountAccessToken adds a controller that reconciles // instance ServiceAccountAccessTokens. -func SetupServiceAccountAccessToken(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccountAccessToken(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.ServiceAccountAccessTokenGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ServiceAccountAccessTokenGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: instance.NewServiceAccountAccessTokenClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ServiceAccountAccessTokenGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ServiceAccountAccessTokenList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ServiceAccountAccessTokenList{}); err != nil { return err } @@ -127,7 +112,7 @@ func SetupServiceAccountAccessToken(mgr ctrl.Manager, o controller.Options) erro } // SetupServiceAccountAccessTokenGated adds a controller with CRD gate support. -func SetupServiceAccountAccessTokenGated(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccountAccessTokenGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupServiceAccountAccessToken(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ServiceAccountAccessTokenGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/instance/serviceaccounts/controller.go b/pkg/namespaced/controller/instance/serviceaccounts/controller.go index 6780eb8c..d04fe529 100644 --- a/pkg/namespaced/controller/instance/serviceaccounts/controller.go +++ b/pkg/namespaced/controller/instance/serviceaccounts/controller.go @@ -21,14 +21,10 @@ import ( "strconv" "sync" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" gitlab "gitlab.com/gitlab-org/api/client-go/v3" "k8s.io/utils/ptr" @@ -72,27 +68,16 @@ const ( ) // SetupServiceAccount adds a controller that reconciles GitLab Service Accounts. -func SetupServiceAccount(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccount(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.ServiceAccountGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ServiceAccountGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: instance.NewServiceAccountClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ServiceAccountGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ServiceAccountList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ServiceAccountList{}); err != nil { return err } @@ -104,7 +89,7 @@ func SetupServiceAccount(mgr ctrl.Manager, o controller.Options) error { } // SetupServiceAccountGated adds a controller with CRD gate support. -func SetupServiceAccountGated(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccountGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupServiceAccount(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ServiceAccountGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/instance/settings/controller.go b/pkg/namespaced/controller/instance/settings/controller.go index c8882881..b9a447a6 100644 --- a/pkg/namespaced/controller/instance/settings/controller.go +++ b/pkg/namespaced/controller/instance/settings/controller.go @@ -22,13 +22,9 @@ package settings import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" gitlab "gitlab.com/gitlab-org/api/client-go/v3" ctrl "sigs.k8s.io/controller-runtime" @@ -49,27 +45,16 @@ const ( ) // SetupApplicationSettings adds a controller that reconciles GitLab Instance Settings. -func SetupApplicationSettings(mgr ctrl.Manager, o controller.Options) error { +func SetupApplicationSettings(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.ApplicationSettingsGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ApplicationSettingsGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: instance.NewApplicationSettingsClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ApplicationSettingsGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ApplicationSettingsList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ApplicationSettingsList{}); err != nil { return err } @@ -81,7 +66,7 @@ func SetupApplicationSettings(mgr ctrl.Manager, o controller.Options) error { } // SetupApplicationSettingsGated adds a controller with CRD gate support. -func SetupApplicationSettingsGated(mgr ctrl.Manager, o controller.Options) error { +func SetupApplicationSettingsGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupApplicationSettings(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ApplicationSettingsGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/instance/setup.go b/pkg/namespaced/controller/instance/setup.go index 68d6d4fc..2e305dfa 100644 --- a/pkg/namespaced/controller/instance/setup.go +++ b/pkg/namespaced/controller/instance/setup.go @@ -17,9 +17,9 @@ limitations under the License. package instance import ( - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" ctrl "sigs.k8s.io/controller-runtime" + "github.com/crossplane-contrib/provider-gitlab/pkg/common" "github.com/crossplane-contrib/provider-gitlab/pkg/namespaced/controller/instance/appearance" "github.com/crossplane-contrib/provider-gitlab/pkg/namespaced/controller/instance/applications" "github.com/crossplane-contrib/provider-gitlab/pkg/namespaced/controller/instance/license" @@ -30,8 +30,8 @@ import ( "github.com/crossplane-contrib/provider-gitlab/pkg/namespaced/controller/instance/variables" ) -func Setup(mgr ctrl.Manager, o controller.Options) error { - for _, setup := range []func(ctrl.Manager, controller.Options) error{ +func Setup(mgr ctrl.Manager, o common.ControllerOptions) error { + for _, setup := range []func(ctrl.Manager, common.ControllerOptions) error{ settings.SetupApplicationSettings, runners.SetupRunner, appearance.SetupAppearance, @@ -50,8 +50,8 @@ func Setup(mgr ctrl.Manager, o controller.Options) error { // SetupGated creates all Gitlab API controllers with the supplied logger and adds // them to the supplied manager with CRD gate support for SafeStart. -func SetupGated(mgr ctrl.Manager, o controller.Options) error { - for _, setup := range []func(ctrl.Manager, controller.Options) error{ +func SetupGated(mgr ctrl.Manager, o common.ControllerOptions) error { + for _, setup := range []func(ctrl.Manager, common.ControllerOptions) error{ settings.SetupApplicationSettingsGated, runners.SetupRunnerGated, appearance.SetupAppearanceGated, diff --git a/pkg/namespaced/controller/instance/variables/controller.go b/pkg/namespaced/controller/instance/variables/controller.go index 1f0072a6..ee09f722 100644 --- a/pkg/namespaced/controller/instance/variables/controller.go +++ b/pkg/namespaced/controller/instance/variables/controller.go @@ -19,13 +19,9 @@ package variables import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -48,27 +44,16 @@ const ( ) // SetupVariable adds a controller that reconciles Instance Variables. -func SetupVariable(mgr ctrl.Manager, o controller.Options) error { +func SetupVariable(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.VariableGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.VariableGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: instance.NewVariableClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.VariableGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.VariableList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.VariableList{}); err != nil { return err } @@ -80,7 +65,7 @@ func SetupVariable(mgr ctrl.Manager, o controller.Options) error { } // SetupVariableGated adds a controller with CRD gate support. -func SetupVariableGated(mgr ctrl.Manager, o controller.Options) error { +func SetupVariableGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupVariable(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.VariableGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/instance/variables/controller_test.go b/pkg/namespaced/controller/instance/variables/controller_test.go index 26e96f54..3c46368b 100644 --- a/pkg/namespaced/controller/instance/variables/controller_test.go +++ b/pkg/namespaced/controller/instance/variables/controller_test.go @@ -202,7 +202,7 @@ func (m *fakeManager) GetConverterRegistry() conversion.Registry { return nil } func TestSetupVariableGated(t *testing.T) { g := &recordingGate{} - o := controller.Options{Gate: g} + o := common.ControllerOptions{Options: controller.Options{Gate: g}} // mgr is not used until the callback is invoked. if err := SetupVariableGated(nil, o); err != nil { @@ -228,14 +228,14 @@ func TestSetupVariableReturnsAddError(t *testing.T) { cl := fake.NewClientBuilder().WithScheme(s).Build() mgr := &fakeManager{cl: cl, scheme: s, addErr: errBoom} - o := controller.Options{ + o := common.ControllerOptions{Options: controller.Options{ Logger: logging.NewNopLogger(), PollInterval: time.Second, Features: &feature.Flags{}, MetricOptions: &controller.MetricOptions{ MRStateMetrics: &statemetrics.MRStateMetrics{}, }, - } + }} err := SetupVariable(mgr, o) if !stderrors.Is(err, errBoom) { diff --git a/pkg/namespaced/controller/projects/accesstokens/controller.go b/pkg/namespaced/controller/projects/accesstokens/controller.go index 85cd90b0..49963e95 100644 --- a/pkg/namespaced/controller/projects/accesstokens/controller.go +++ b/pkg/namespaced/controller/projects/accesstokens/controller.go @@ -20,13 +20,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/pkg/errors" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -90,27 +86,16 @@ func selfManagedCondition(self bool) v2.Condition { } // SetupAccessToken adds a controller that reconciles ProjectAccessTokens. -func SetupAccessToken(mgr ctrl.Manager, o controller.Options) error { +func SetupAccessToken(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.AccessTokenGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.AccessTokenGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewAccessTokenClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.AccessTokenGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.AccessTokenList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.AccessTokenList{}); err != nil { return err } @@ -122,7 +107,7 @@ func SetupAccessToken(mgr ctrl.Manager, o controller.Options) error { } // SetupAccessTokenGated adds a controller with CRD gate support. -func SetupAccessTokenGated(mgr ctrl.Manager, o controller.Options) error { +func SetupAccessTokenGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupAccessToken(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.AccessTokenGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/projects/approvalrules/controller.go b/pkg/namespaced/controller/projects/approvalrules/controller.go index 280d9f80..4ab80857 100644 --- a/pkg/namespaced/controller/projects/approvalrules/controller.go +++ b/pkg/namespaced/controller/projects/approvalrules/controller.go @@ -17,13 +17,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -48,30 +44,19 @@ const ( ) // SetupRules adds a controller that reconciles Approval Rules. -func SetupRules(mgr ctrl.Manager, o controller.Options) error { +func SetupRules(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.ApprovalRuleKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ApprovalRuleGroupVersionKind), managed.WithExternalConnector(&connector{ kube: mgr.GetClient(), newGitlabClientFn: projects.NewApprovalRulesClient, }), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ApprovalRuleGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ApprovalRuleList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ApprovalRuleList{}); err != nil { return err } @@ -83,7 +68,7 @@ func SetupRules(mgr ctrl.Manager, o controller.Options) error { } // SetupRulesGated adds a controller with CRD gate support. -func SetupRulesGated(mgr ctrl.Manager, o controller.Options) error { +func SetupRulesGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupRules(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ApprovalRuleGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/projects/badges/controller.go b/pkg/namespaced/controller/projects/badges/controller.go index b29f15b5..336c4ed0 100644 --- a/pkg/namespaced/controller/projects/badges/controller.go +++ b/pkg/namespaced/controller/projects/badges/controller.go @@ -20,13 +20,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -50,27 +46,16 @@ const ( ) // SetupBadge adds a controller that reconciles ProjectBadges. -func SetupBadge(mgr ctrl.Manager, o controller.Options) error { +func SetupBadge(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.BadgeGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.BadgeGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewBadgeClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.BadgeGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.BadgeList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.BadgeList{}); err != nil { return err } @@ -82,7 +67,7 @@ func SetupBadge(mgr ctrl.Manager, o controller.Options) error { } // SetupBadgeGated adds a controller with CRD gate support. -func SetupBadgeGated(mgr ctrl.Manager, o controller.Options) error { +func SetupBadgeGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupBadge(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.BadgeGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/projects/deploykeys/controller.go b/pkg/namespaced/controller/projects/deploykeys/controller.go index c08e139a..1c18de08 100644 --- a/pkg/namespaced/controller/projects/deploykeys/controller.go +++ b/pkg/namespaced/controller/projects/deploykeys/controller.go @@ -4,13 +4,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -48,27 +44,16 @@ type connector struct { } // SetupDeployKey adds a controller that reconciles ProjectDeployKey. -func SetupDeployKey(mgr ctrl.Manager, o controller.Options) error { +func SetupDeployKey(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.DeployKeyGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.DeployKeyGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: newDeployKeyClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.DeployKeyGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.DeployKeyList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.DeployKeyList{}); err != nil { return err } @@ -80,7 +65,7 @@ func SetupDeployKey(mgr ctrl.Manager, o controller.Options) error { } // SetupDeployKeyGated adds a controller with CRD gate support. -func SetupDeployKeyGated(mgr ctrl.Manager, o controller.Options) error { +func SetupDeployKeyGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupDeployKey(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.DeployKeyGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/projects/deploytokens/controller.go b/pkg/namespaced/controller/projects/deploytokens/controller.go index d8dd9b5f..411d41f2 100644 --- a/pkg/namespaced/controller/projects/deploytokens/controller.go +++ b/pkg/namespaced/controller/projects/deploytokens/controller.go @@ -20,13 +20,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -51,27 +47,16 @@ const ( ) // SetupDeployToken adds a controller that reconciles ProjectDeployTokens. -func SetupDeployToken(mgr ctrl.Manager, o controller.Options) error { +func SetupDeployToken(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.DeployTokenGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.DeployTokenGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewDeployTokenClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.DeployTokenGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.DeployKeyList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.DeployKeyList{}); err != nil { return err } @@ -83,7 +68,7 @@ func SetupDeployToken(mgr ctrl.Manager, o controller.Options) error { } // SetupDeployTokenGated adds a controller with CRD gate support. -func SetupDeployTokenGated(mgr ctrl.Manager, o controller.Options) error { +func SetupDeployTokenGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupDeployToken(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.DeployTokenGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/projects/hooks/controller.go b/pkg/namespaced/controller/projects/hooks/controller.go index 395d34df..89b34ba0 100644 --- a/pkg/namespaced/controller/projects/hooks/controller.go +++ b/pkg/namespaced/controller/projects/hooks/controller.go @@ -20,13 +20,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -52,27 +48,16 @@ const ( ) // SetupHook adds a controller that reconciles Hooks. -func SetupHook(mgr ctrl.Manager, o controller.Options) error { +func SetupHook(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.HookGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.HookGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewHookClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.HookGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.HookList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.HookList{}); err != nil { return err } @@ -84,7 +69,7 @@ func SetupHook(mgr ctrl.Manager, o controller.Options) error { } // SetupHookGated adds a controller with CRD gate support. -func SetupHookGated(mgr ctrl.Manager, o controller.Options) error { +func SetupHookGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupHook(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.HookGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/projects/integrationharbor/controller.go b/pkg/namespaced/controller/projects/integrationharbor/controller.go index ff235b20..4e9651db 100644 --- a/pkg/namespaced/controller/projects/integrationharbor/controller.go +++ b/pkg/namespaced/controller/projects/integrationharbor/controller.go @@ -19,13 +19,9 @@ package integrationharbor import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -49,34 +45,16 @@ const ( ) // SetupIntegrationHarbor adds a controller that reconciles GitLab Project Harbor integrations. -func SetupIntegrationHarbor(mgr ctrl.Manager, o controller.Options) error { +func SetupIntegrationHarbor(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.IntegrationHarborGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.IntegrationHarborGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewHarborClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler( - mgr, - resource.ManagedKind(v1alpha1.IntegrationHarborGroupVersionKind), - reconcilerOpts..., ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), - o.Logger, - o.MetricOptions.MRStateMetrics, - &v1alpha1.IntegrationHarborList{}, - o.MetricOptions.PollStateMetricInterval, - )); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.IntegrationHarborList{}); err != nil { return err } @@ -88,7 +66,7 @@ func SetupIntegrationHarbor(mgr ctrl.Manager, o controller.Options) error { } // SetupIntegrationHarborGated adds a controller with CRD gate support for SafeStart. -func SetupIntegrationHarborGated(mgr ctrl.Manager, o controller.Options) error { +func SetupIntegrationHarborGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupIntegrationHarbor(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.IntegrationHarborGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/projects/integrationmattermost/controller.go b/pkg/namespaced/controller/projects/integrationmattermost/controller.go index d388a104..fd73b001 100644 --- a/pkg/namespaced/controller/projects/integrationmattermost/controller.go +++ b/pkg/namespaced/controller/projects/integrationmattermost/controller.go @@ -19,14 +19,10 @@ package integrationmattermost import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -49,34 +45,16 @@ const ( ) // SetupIntegrationMattermost adds a controller that reconciles GitLab Integration Mattermost. -func SetupIntegrationMattermost(mgr ctrl.Manager, o controller.Options) error { +func SetupIntegrationMattermost(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.IntegrationMattermostGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.IntegrationMattermostGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewMattermostClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler( - mgr, - resource.ManagedKind(v1alpha1.IntegrationMattermostGroupVersionKind), - reconcilerOpts..., ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), - o.Logger, - o.MetricOptions.MRStateMetrics, - &v1alpha1.IntegrationMattermostList{}, - o.MetricOptions.PollStateMetricInterval, - )); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.IntegrationMattermostList{}); err != nil { return err } @@ -88,7 +66,7 @@ func SetupIntegrationMattermost(mgr ctrl.Manager, o controller.Options) error { } // SetupIntegrationMattermostGated adds a controller with CRD gate support. -func SetupIntegrationMattermostGated(mgr ctrl.Manager, o controller.Options) error { +func SetupIntegrationMattermostGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupIntegrationMattermost(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.IntegrationMattermostGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/projects/members/controller.go b/pkg/namespaced/controller/projects/members/controller.go index 3939ffe7..3dd5023c 100644 --- a/pkg/namespaced/controller/projects/members/controller.go +++ b/pkg/namespaced/controller/projects/members/controller.go @@ -16,12 +16,8 @@ package members import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -48,31 +44,20 @@ const ( ) // SetupMember adds a controller that reconciles Project Members. -func SetupMember(mgr ctrl.Manager, o controller.Options) error { +func SetupMember(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.MemberGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.MemberGroupVersionKind), managed.WithExternalConnector(&connector{ kube: mgr.GetClient(), newGitlabClientFn: projects.NewMemberClient, newUserClientFn: users.NewUserClient, }), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.MemberGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.MemberList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.MemberList{}); err != nil { return err } @@ -84,7 +69,7 @@ func SetupMember(mgr ctrl.Manager, o controller.Options) error { } // SetupMemberGated adds a controller with CRD gate support. -func SetupMemberGated(mgr ctrl.Manager, o controller.Options) error { +func SetupMemberGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupMember(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.MemberGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/projects/pipelineschedules/controller.go b/pkg/namespaced/controller/projects/pipelineschedules/controller.go index 0226f839..f836f260 100644 --- a/pkg/namespaced/controller/projects/pipelineschedules/controller.go +++ b/pkg/namespaced/controller/projects/pipelineschedules/controller.go @@ -20,13 +20,9 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -56,27 +52,16 @@ const ( ) // SetupPipelineSchedule adds a controller that reconciles PipelineSchedule. -func SetupPipelineSchedule(mgr ctrl.Manager, o controller.Options) error { +func SetupPipelineSchedule(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.PipelineScheduleGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.PipelineScheduleGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: newPipelineScheduleClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.PipelineScheduleGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.PipelineScheduleList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.PipelineScheduleList{}); err != nil { return err } @@ -88,7 +73,7 @@ func SetupPipelineSchedule(mgr ctrl.Manager, o controller.Options) error { } // SetupPipelineScheduleGated adds a controller with CRD gate support. -func SetupPipelineScheduleGated(mgr ctrl.Manager, o controller.Options) error { +func SetupPipelineScheduleGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupPipelineSchedule(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.PipelineScheduleGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/projects/projects/project.go b/pkg/namespaced/controller/projects/projects/project.go index 1fa407bd..d0d87062 100644 --- a/pkg/namespaced/controller/projects/projects/project.go +++ b/pkg/namespaced/controller/projects/projects/project.go @@ -22,13 +22,9 @@ import ( "strconv" "strings" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" @@ -60,27 +56,16 @@ const ( ) // SetupProject adds a controller that reconciles Projects. -func SetupProject(mgr ctrl.Manager, o controller.Options) error { +func SetupProject(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.ProjectGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ProjectGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewProjectClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ProjectGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ProjectList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ProjectList{}); err != nil { return err } @@ -92,7 +77,7 @@ func SetupProject(mgr ctrl.Manager, o controller.Options) error { } // SetupProjectGated adds a controller with CRD gate support. -func SetupProjectGated(mgr ctrl.Manager, o controller.Options) error { +func SetupProjectGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupProject(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ProjectGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/projects/projectsharegroups/controller.go b/pkg/namespaced/controller/projects/projectsharegroups/controller.go index 6d155bc9..009c96b6 100644 --- a/pkg/namespaced/controller/projects/projectsharegroups/controller.go +++ b/pkg/namespaced/controller/projects/projectsharegroups/controller.go @@ -4,12 +4,8 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/pkg/errors" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -30,27 +26,16 @@ const ( ) // SetupProjectShareGroup adds a controller that reconciles ProjectShareGroups. -func SetupProjectShareGroup(mgr ctrl.Manager, o controller.Options) error { +func SetupProjectShareGroup(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.ProjectShareGroupGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ProjectShareGroupGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewProjectClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ProjectShareGroupGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ProjectShareGroupList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ProjectShareGroupList{}); err != nil { return err } @@ -61,7 +46,7 @@ func SetupProjectShareGroup(mgr ctrl.Manager, o controller.Options) error { } // SetupProjectShareGroupGated adds a controller with CRD gate support. -func SetupProjectShareGroupGated(mgr ctrl.Manager, o controller.Options) error { +func SetupProjectShareGroupGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupProjectShareGroup(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ProjectShareGroupGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/projects/protectedbranches/controller.go b/pkg/namespaced/controller/projects/protectedbranches/controller.go index f48ab23a..fc0f27fc 100644 --- a/pkg/namespaced/controller/projects/protectedbranches/controller.go +++ b/pkg/namespaced/controller/projects/protectedbranches/controller.go @@ -19,12 +19,8 @@ package protectedbranches import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -48,27 +44,16 @@ const ( ) // SetupProtectedBranch adds a controller that reconciles ProtectedBranches. -func SetupProtectedBranch(mgr ctrl.Manager, o controller.Options) error { +func SetupProtectedBranch(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.ProtectedBranchGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ProtectedBranchGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewProtectedBranchClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ProtectedBranchGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ProtectedBranchList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ProtectedBranchList{}); err != nil { return err } @@ -80,7 +65,7 @@ func SetupProtectedBranch(mgr ctrl.Manager, o controller.Options) error { } // SetupProtectedBranchGated adds a controller with CRD gate support. -func SetupProtectedBranchGated(mgr ctrl.Manager, o controller.Options) error { +func SetupProtectedBranchGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupProtectedBranch(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ProtectedBranchGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/projects/protectedenvironments/controller.go b/pkg/namespaced/controller/projects/protectedenvironments/controller.go index 71b8308a..04d9809f 100644 --- a/pkg/namespaced/controller/projects/protectedenvironments/controller.go +++ b/pkg/namespaced/controller/projects/protectedenvironments/controller.go @@ -13,12 +13,8 @@ package protectedenvironments import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -43,32 +39,16 @@ const ( ) // SetupProtectedEnvironment adds a controller that reconciles ProtectedEnvironments. -func SetupProtectedEnvironment(mgr ctrl.Manager, o controller.Options) error { +func SetupProtectedEnvironment(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.ProtectedEnvironmentGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ProtectedEnvironmentGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewProtectedEnvironmentClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } + ) - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ProtectedEnvironmentGroupVersionKind), - reconcilerOpts...) - - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), - o.Logger, - o.MetricOptions.MRStateMetrics, - &v1alpha1.ProtectedEnvironmentList{}, - o.MetricOptions.PollStateMetricInterval, - )); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ProtectedEnvironmentList{}); err != nil { return err } @@ -80,7 +60,7 @@ func SetupProtectedEnvironment(mgr ctrl.Manager, o controller.Options) error { } // SetupProtectedEnvironmentGated adds a controller with CRD gate support. -func SetupProtectedEnvironmentGated(mgr ctrl.Manager, o controller.Options) error { +func SetupProtectedEnvironmentGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupProtectedEnvironment(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ProtectedEnvironmentGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/projects/runners/controller.go b/pkg/namespaced/controller/projects/runners/controller.go index 869fd748..593884c8 100644 --- a/pkg/namespaced/controller/projects/runners/controller.go +++ b/pkg/namespaced/controller/projects/runners/controller.go @@ -22,13 +22,9 @@ import ( "strconv" "time" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/pkg/errors" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -57,30 +53,20 @@ const ( ) // SetupRunner adds a controller that reconciles projects runners. -func SetupRunner(mgr ctrl.Manager, o controller.Options) error { +func SetupRunner(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.RunnerGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.RunnerGroupVersionKind), managed.WithExternalConnector(&connector{ kube: mgr.GetClient(), newGitlabClientFn: runners.NewRunnerClient, newRunnerClientFn: users.NewRunnerClient, }), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.RunnerGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.RunnerList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.RunnerList{}); err != nil { return err } @@ -92,7 +78,7 @@ func SetupRunner(mgr ctrl.Manager, o controller.Options) error { } // SetupRunnerGated adds a controller with CRD gate support. -func SetupRunnerGated(mgr ctrl.Manager, o controller.Options) error { +func SetupRunnerGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupRunner(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.RunnerGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/projects/serviceaccountaccesstokens/controller.go b/pkg/namespaced/controller/projects/serviceaccountaccesstokens/controller.go index 39403df4..e71ccc06 100644 --- a/pkg/namespaced/controller/projects/serviceaccountaccesstokens/controller.go +++ b/pkg/namespaced/controller/projects/serviceaccountaccesstokens/controller.go @@ -22,13 +22,9 @@ import ( "strconv" "strings" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/pkg/errors" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -94,27 +90,16 @@ func selfManagedCondition(self bool) v2.Condition { // SetupServiceAccountAccessToken adds a controller that reconciles // project ServiceAccountAccessTokens. -func SetupServiceAccountAccessToken(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccountAccessToken(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.ServiceAccountAccessTokenGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ServiceAccountAccessTokenGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewServiceAccountAccessTokenClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ServiceAccountAccessTokenGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ServiceAccountAccessTokenList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ServiceAccountAccessTokenList{}); err != nil { return err } @@ -126,7 +111,7 @@ func SetupServiceAccountAccessToken(mgr ctrl.Manager, o controller.Options) erro } // SetupServiceAccountAccessTokenGated adds a controller with CRD gate support. -func SetupServiceAccountAccessTokenGated(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccountAccessTokenGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupServiceAccountAccessToken(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ServiceAccountAccessTokenGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/projects/serviceaccounts/controller.go b/pkg/namespaced/controller/projects/serviceaccounts/controller.go index 61a0e57a..d8e4e910 100644 --- a/pkg/namespaced/controller/projects/serviceaccounts/controller.go +++ b/pkg/namespaced/controller/projects/serviceaccounts/controller.go @@ -20,14 +20,10 @@ import ( "context" "strconv" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/meta" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" gitlab "gitlab.com/gitlab-org/api/client-go/v3" ctrl "sigs.k8s.io/controller-runtime" @@ -50,27 +46,16 @@ const ( ) // SetupServiceAccount adds a controller that reconciles GitLab project Service Accounts. -func SetupServiceAccount(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccount(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.ServiceAccountGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.ServiceAccountGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewServiceAccountClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.ServiceAccountGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.ServiceAccountList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.ServiceAccountList{}); err != nil { return err } @@ -82,7 +67,7 @@ func SetupServiceAccount(mgr ctrl.Manager, o controller.Options) error { } // SetupServiceAccountGated adds a controller with CRD gate support. -func SetupServiceAccountGated(mgr ctrl.Manager, o controller.Options) error { +func SetupServiceAccountGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupServiceAccount(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.ServiceAccountGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/projects/setup.go b/pkg/namespaced/controller/projects/setup.go index c0e786c1..98b184fe 100644 --- a/pkg/namespaced/controller/projects/setup.go +++ b/pkg/namespaced/controller/projects/setup.go @@ -17,9 +17,9 @@ limitations under the License. package projects import ( - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" ctrl "sigs.k8s.io/controller-runtime" + "github.com/crossplane-contrib/provider-gitlab/pkg/common" "github.com/crossplane-contrib/provider-gitlab/pkg/namespaced/controller/projects/accesstokens" "github.com/crossplane-contrib/provider-gitlab/pkg/namespaced/controller/projects/approvalrules" "github.com/crossplane-contrib/provider-gitlab/pkg/namespaced/controller/projects/badges" @@ -41,8 +41,8 @@ import ( ) // Setup all project controllers -func Setup(mgr ctrl.Manager, o controller.Options) error { - for _, setup := range []func(ctrl.Manager, controller.Options) error{ +func Setup(mgr ctrl.Manager, o common.ControllerOptions) error { + for _, setup := range []func(ctrl.Manager, common.ControllerOptions) error{ projects.SetupProject, hooks.SetupHook, members.SetupMember, @@ -71,8 +71,8 @@ func Setup(mgr ctrl.Manager, o controller.Options) error { // SetupGated creates all Gitlab API controllers with the supplied logger and adds // them to the supplied manager with CRD gate support for SafeStart. -func SetupGated(mgr ctrl.Manager, o controller.Options) error { - for _, setup := range []func(ctrl.Manager, controller.Options) error{ +func SetupGated(mgr ctrl.Manager, o common.ControllerOptions) error { + for _, setup := range []func(ctrl.Manager, common.ControllerOptions) error{ projects.SetupProjectGated, hooks.SetupHookGated, members.SetupMemberGated, diff --git a/pkg/namespaced/controller/projects/variables/controller.go b/pkg/namespaced/controller/projects/variables/controller.go index cf54ff70..9ab41921 100644 --- a/pkg/namespaced/controller/projects/variables/controller.go +++ b/pkg/namespaced/controller/projects/variables/controller.go @@ -19,13 +19,9 @@ package variables import ( "context" - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" - "github.com/crossplane/crossplane-runtime/v2/pkg/event" - "github.com/crossplane/crossplane-runtime/v2/pkg/feature" "github.com/crossplane/crossplane-runtime/v2/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/v2/pkg/resource" - "github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics" v2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/google/go-cmp/cmp" gitlab "gitlab.com/gitlab-org/api/client-go/v3" @@ -49,27 +45,16 @@ const ( ) // SetupVariable adds a controller that reconciles Variables. -func SetupVariable(mgr ctrl.Manager, o controller.Options) error { +func SetupVariable(mgr ctrl.Manager, o common.ControllerOptions) error { name := managed.ControllerName(v1alpha1.VariableGroupKind) - reconcilerOpts := []managed.ReconcilerOption{ + r := common.NewReconciler(mgr, o, name, + resource.ManagedKind(v1alpha1.VariableGroupVersionKind), managed.WithExternalConnector(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewVariableClient}), managed.WithInitializers(), - managed.WithPollInterval(o.PollInterval), - managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - } - - if o.Features.Enabled(feature.EnableBetaManagementPolicies) { - reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies()) - } - - r := managed.NewReconciler(mgr, - resource.ManagedKind(v1alpha1.VariableGroupVersionKind), - reconcilerOpts...) + ) - if err := mgr.Add(statemetrics.NewMRStateRecorder( - mgr.GetClient(), o.Logger, o.MetricOptions.MRStateMetrics, &v1alpha1.VariableList{}, o.MetricOptions.PollStateMetricInterval)); err != nil { + if err := common.AddMRStateRecorder(mgr, o, &v1alpha1.VariableList{}); err != nil { return err } @@ -81,7 +66,7 @@ func SetupVariable(mgr ctrl.Manager, o controller.Options) error { } // SetupVariableGated adds a controller with CRD gate support. -func SetupVariableGated(mgr ctrl.Manager, o controller.Options) error { +func SetupVariableGated(mgr ctrl.Manager, o common.ControllerOptions) error { o.Gate.Register(func() { if err := SetupVariable(mgr, o); err != nil { mgr.GetLogger().Error(err, "unable to setup reconciler", "gvk", v1alpha1.VariableGroupVersionKind.String()) diff --git a/pkg/namespaced/controller/setup.go b/pkg/namespaced/controller/setup.go index fe0a7c15..f001f967 100644 --- a/pkg/namespaced/controller/setup.go +++ b/pkg/namespaced/controller/setup.go @@ -17,9 +17,9 @@ limitations under the License. package controller import ( - "github.com/crossplane/crossplane-runtime/v2/pkg/controller" ctrl "sigs.k8s.io/controller-runtime" + "github.com/crossplane-contrib/provider-gitlab/pkg/common" "github.com/crossplane-contrib/provider-gitlab/pkg/namespaced/controller/config" "github.com/crossplane-contrib/provider-gitlab/pkg/namespaced/controller/groups" "github.com/crossplane-contrib/provider-gitlab/pkg/namespaced/controller/instance" @@ -28,8 +28,8 @@ import ( // Setup creates all Gitlab API controllers with the supplied logger and adds // them to the supplied manager. -func Setup(mgr ctrl.Manager, o controller.Options) error { - for _, setup := range []func(ctrl.Manager, controller.Options) error{ +func Setup(mgr ctrl.Manager, o common.ControllerOptions) error { + for _, setup := range []func(ctrl.Manager, common.ControllerOptions) error{ config.Setup, groups.Setup, projects.Setup, @@ -44,8 +44,8 @@ func Setup(mgr ctrl.Manager, o controller.Options) error { // SetupGated creates all Gitlab API controllers with the supplied logger and adds // them to the supplied manager with CRD gate support for SafeStart. -func SetupGated(mgr ctrl.Manager, o controller.Options) error { - for _, setup := range []func(ctrl.Manager, controller.Options) error{ +func SetupGated(mgr ctrl.Manager, o common.ControllerOptions) error { + for _, setup := range []func(ctrl.Manager, common.ControllerOptions) error{ config.SetupGated, groups.SetupGated, projects.SetupGated,