diff --git a/cmd/provider/main.go b/cmd/provider/main.go index aadcee5..28a3b76 100644 --- a/cmd/provider/main.go +++ b/cmd/provider/main.go @@ -85,12 +85,12 @@ func main() { } if *enableManagementPolicies { - o.Features.Enable(features.EnableAlphaManagementPolicies) - log.Info("Alpha feature enabled", "flag", features.EnableAlphaManagementPolicies) + o.Features.Enable(features.EnableBetaManagementPolicies) + log.Info("Beta feature enabled", "flag", features.EnableBetaManagementPolicies) } kingpin.FatalIfError(err, "Cannot create controller manager") kingpin.FatalIfError(apis.AddToScheme(mgr.GetScheme()), "Cannot add argocd APIs to scheme") - kingpin.FatalIfError(controller.Setup(mgr, log), "Cannot setup argocd controllers") + kingpin.FatalIfError(controller.Setup(mgr, o), "Cannot setup argocd controllers") kingpin.FatalIfError(mgr.Start(ctrl.SetupSignalHandler()), "Cannot start controller manager") } diff --git a/pkg/controller/applications/controller.go b/pkg/controller/applications/controller.go index 08cc7b2..e838d1d 100644 --- a/pkg/controller/applications/controller.go +++ b/pkg/controller/applications/controller.go @@ -23,7 +23,6 @@ import ( "github.com/argoproj/argo-cd/v2/pkg/apiclient/application" argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" "github.com/argoproj/argo-cd/v2/util/io" - "github.com/crossplane/crossplane-runtime/pkg/logging" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -31,6 +30,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" + xpcontroller "github.com/crossplane/crossplane-runtime/pkg/controller" "github.com/crossplane/crossplane-runtime/pkg/event" "github.com/crossplane/crossplane-runtime/pkg/meta" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" @@ -39,6 +39,7 @@ import ( "github.com/crossplane-contrib/provider-argocd/apis/applications/v1alpha1" "github.com/crossplane-contrib/provider-argocd/pkg/clients" "github.com/crossplane-contrib/provider-argocd/pkg/clients/applications" + "github.com/crossplane-contrib/provider-argocd/pkg/features" ) const ( @@ -51,21 +52,30 @@ const ( ) // SetupApplication adds a controller that reconciles applications. -func SetupApplication(mgr ctrl.Manager, l logging.Logger) error { +func SetupApplication(mgr ctrl.Manager, o xpcontroller.Options) error { name := managed.ControllerName(v1alpha1.ApplicationKind) cps := []managed.ConnectionPublisher{managed.NewAPISecretPublisher(mgr.GetClient(), mgr.GetScheme())} + + opts := []managed.ReconcilerOption{ + managed.WithExternalConnectDisconnecter(&connector{kube: mgr.GetClient(), newArgocdClientFn: applications.NewApplicationServiceClient}), + managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())), + managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), + managed.WithLogger(o.Logger.WithValues("controller", name)), + managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), + managed.WithConnectionPublishers(cps...), + } + + if o.Features.Enabled(features.EnableBetaManagementPolicies) { + opts = append(opts, managed.WithManagementPolicies()) + } + return ctrl.NewControllerManagedBy(mgr). Named(name). For(&v1alpha1.Application{}). Complete(managed.NewReconciler(mgr, resource.ManagedKind(v1alpha1.ApplicationGroupVersionKind), - managed.WithExternalConnectDisconnecter(&connector{kube: mgr.GetClient(), newArgocdClientFn: applications.NewApplicationServiceClient}), - managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())), - managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), - managed.WithLogger(l.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - managed.WithConnectionPublishers(cps...))) + opts...)) } type connector struct { diff --git a/pkg/controller/applicationsets/controller.go b/pkg/controller/applicationsets/controller.go index 0d11549..2cc641b 100644 --- a/pkg/controller/applicationsets/controller.go +++ b/pkg/controller/applicationsets/controller.go @@ -25,13 +25,13 @@ import ( "github.com/argoproj/argo-cd/v2/util/io" xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" "github.com/crossplane/crossplane-runtime/pkg/event" - "github.com/crossplane/crossplane-runtime/pkg/logging" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + xpcontroller "github.com/crossplane/crossplane-runtime/pkg/controller" "github.com/crossplane/crossplane-runtime/pkg/meta" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/pkg/resource" @@ -39,6 +39,7 @@ import ( "github.com/crossplane-contrib/provider-argocd/apis/applicationsets/v1alpha1" "github.com/crossplane-contrib/provider-argocd/pkg/clients" appsets "github.com/crossplane-contrib/provider-argocd/pkg/clients/applicationsets" + "github.com/crossplane-contrib/provider-argocd/pkg/features" ) const ( @@ -47,22 +48,28 @@ const ( ) // SetupApplicationSet adds a controller that reconciles ApplicationSet managed resources. -func SetupApplicationSet(mgr ctrl.Manager, l logging.Logger) error { +func SetupApplicationSet(mgr ctrl.Manager, o xpcontroller.Options) error { name := managed.ControllerName(v1alpha1.ApplicationSetGroupKind) cps := []managed.ConnectionPublisher{managed.NewAPISecretPublisher(mgr.GetClient(), mgr.GetScheme())} + opts := []managed.ReconcilerOption{ + managed.WithExternalConnectDisconnecter(&connector{kube: mgr.GetClient(), newArgocdClientFn: appsets.NewApplicationSetServiceClient}), + managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())), + managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), + managed.WithLogger(o.Logger.WithValues("controller", name)), + managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), + managed.WithConnectionPublishers(cps...), + } + if o.Features.Enabled(features.EnableBetaManagementPolicies) { + opts = append(opts, managed.WithManagementPolicies()) + } return ctrl.NewControllerManagedBy(mgr). Named(name). For(&v1alpha1.ApplicationSet{}). Complete(managed.NewReconciler(mgr, resource.ManagedKind(v1alpha1.ApplicationSetGroupVersionKind), - managed.WithExternalConnectDisconnecter(&connector{kube: mgr.GetClient(), newArgocdClientFn: appsets.NewApplicationSetServiceClient}), - managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())), - managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), - managed.WithLogger(l.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), - managed.WithConnectionPublishers(cps...))) + opts...)) } type connector struct { diff --git a/pkg/controller/argocd.go b/pkg/controller/argocd.go index 4867701..c689bc6 100644 --- a/pkg/controller/argocd.go +++ b/pkg/controller/argocd.go @@ -17,10 +17,9 @@ limitations under the License. package controller import ( + xpcontroller "github.com/crossplane/crossplane-runtime/pkg/controller" ctrl "sigs.k8s.io/controller-runtime" - "github.com/crossplane/crossplane-runtime/pkg/logging" - "github.com/crossplane-contrib/provider-argocd/pkg/controller/applications" "github.com/crossplane-contrib/provider-argocd/pkg/controller/applicationsets" "github.com/crossplane-contrib/provider-argocd/pkg/controller/cluster" @@ -31,8 +30,8 @@ import ( // Setup creates all argocd API controllers with the supplied logger and adds // them to the supplied manager. -func Setup(mgr ctrl.Manager, l logging.Logger) error { - for _, setup := range []func(ctrl.Manager, logging.Logger) error{ +func Setup(mgr ctrl.Manager, o xpcontroller.Options) error { + for _, setup := range []func(ctrl.Manager, xpcontroller.Options) error{ config.Setup, repositories.SetupRepository, projects.SetupProject, @@ -40,7 +39,7 @@ func Setup(mgr ctrl.Manager, l logging.Logger) error { applications.SetupApplication, applicationsets.SetupApplicationSet, } { - if err := setup(mgr, l); err != nil { + if err := setup(mgr, o); err != nil { return err } } diff --git a/pkg/controller/cluster/controller.go b/pkg/controller/cluster/controller.go index 1d53f56..87dbb1d 100644 --- a/pkg/controller/cluster/controller.go +++ b/pkg/controller/cluster/controller.go @@ -35,8 +35,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" + xpcontroller "github.com/crossplane/crossplane-runtime/pkg/controller" "github.com/crossplane/crossplane-runtime/pkg/event" - "github.com/crossplane/crossplane-runtime/pkg/logging" "github.com/crossplane/crossplane-runtime/pkg/meta" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/pkg/resource" @@ -44,6 +44,7 @@ import ( "github.com/crossplane-contrib/provider-argocd/apis/cluster/v1alpha1" "github.com/crossplane-contrib/provider-argocd/pkg/clients" "github.com/crossplane-contrib/provider-argocd/pkg/clients/cluster" + "github.com/crossplane-contrib/provider-argocd/pkg/features" ) const ( @@ -58,17 +59,22 @@ const ( ) // SetupCluster adds a controller that reconciles cluster. -func SetupCluster(mgr ctrl.Manager, l logging.Logger) error { +func SetupCluster(mgr ctrl.Manager, o xpcontroller.Options) error { name := managed.ControllerName(v1alpha1.ClusterKind) - + opts := []managed.ReconcilerOption{ + managed.WithExternalConnectDisconnecter(&connector{kube: mgr.GetClient(), newArgocdClientFn: cluster.NewClusterServiceClient}), + managed.WithLogger(o.Logger.WithValues("controller", name)), + managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), + } + if o.Features.Enabled(features.EnableBetaManagementPolicies) { + opts = append(opts, managed.WithManagementPolicies()) + } return ctrl.NewControllerManagedBy(mgr). Named(name). For(&v1alpha1.Cluster{}). Complete(managed.NewReconciler(mgr, resource.ManagedKind(v1alpha1.ClusterGroupVersionKind), - managed.WithExternalConnectDisconnecter(&connector{kube: mgr.GetClient(), newArgocdClientFn: cluster.NewClusterServiceClient}), - managed.WithLogger(l.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))))) + opts...)) } type connector struct { diff --git a/pkg/controller/config/config.go b/pkg/controller/config/config.go index 4a5e40a..3aafe13 100644 --- a/pkg/controller/config/config.go +++ b/pkg/controller/config/config.go @@ -17,10 +17,10 @@ limitations under the License. package config import ( + xpcontroller "github.com/crossplane/crossplane-runtime/pkg/controller" ctrl "sigs.k8s.io/controller-runtime" "github.com/crossplane/crossplane-runtime/pkg/event" - "github.com/crossplane/crossplane-runtime/pkg/logging" "github.com/crossplane/crossplane-runtime/pkg/reconciler/providerconfig" "github.com/crossplane/crossplane-runtime/pkg/resource" @@ -29,7 +29,7 @@ import ( // Setup adds a controller that reconciles ProviderConfigs by accounting for // their current usage. -func Setup(mgr ctrl.Manager, l logging.Logger) error { +func Setup(mgr ctrl.Manager, o xpcontroller.Options) error { name := providerconfig.ControllerName(v1alpha1.ProviderConfigGroupKind) of := resource.ProviderConfigKinds{ @@ -42,6 +42,6 @@ func Setup(mgr ctrl.Manager, l logging.Logger) error { For(&v1alpha1.ProviderConfig{}). Watches(&v1alpha1.ProviderConfigUsage{}, &resource.EnqueueRequestForProviderConfig{}). Complete(providerconfig.NewReconciler(mgr, of, - providerconfig.WithLogger(l.WithValues("controller", name)), + providerconfig.WithLogger(o.Logger.WithValues("controller", name)), providerconfig.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))))) } diff --git a/pkg/controller/projects/controller.go b/pkg/controller/projects/controller.go index ee4a292..c32535d 100644 --- a/pkg/controller/projects/controller.go +++ b/pkg/controller/projects/controller.go @@ -31,8 +31,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" + xpcontroller "github.com/crossplane/crossplane-runtime/pkg/controller" "github.com/crossplane/crossplane-runtime/pkg/event" - "github.com/crossplane/crossplane-runtime/pkg/logging" "github.com/crossplane/crossplane-runtime/pkg/meta" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/pkg/resource" @@ -40,6 +40,7 @@ import ( "github.com/crossplane-contrib/provider-argocd/apis/projects/v1alpha1" "github.com/crossplane-contrib/provider-argocd/pkg/clients" "github.com/crossplane-contrib/provider-argocd/pkg/clients/projects" + "github.com/crossplane-contrib/provider-argocd/pkg/features" ) const ( @@ -52,17 +53,25 @@ const ( ) // SetupProject adds a controller that reconciles projects. -func SetupProject(mgr ctrl.Manager, l logging.Logger) error { +func SetupProject(mgr ctrl.Manager, o xpcontroller.Options) error { name := managed.ControllerName(v1alpha1.ProjectKind) + opts := []managed.ReconcilerOption{ + managed.WithExternalConnectDisconnecter(&connector{kube: mgr.GetClient(), newArgocdClientFn: projects.NewProjectServiceClient}), + managed.WithLogger(o.Logger.WithValues("controller", name)), + managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), + } + + if o.Features.Enabled(features.EnableBetaManagementPolicies) { + opts = append(opts, managed.WithManagementPolicies()) + } + return ctrl.NewControllerManagedBy(mgr). Named(name). For(&v1alpha1.Project{}). Complete(managed.NewReconciler(mgr, resource.ManagedKind(v1alpha1.ProjectGroupVersionKind), - managed.WithExternalConnectDisconnecter(&connector{kube: mgr.GetClient(), newArgocdClientFn: projects.NewProjectServiceClient}), - managed.WithLogger(l.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))))) + opts...)) } type connector struct { diff --git a/pkg/controller/repositories/controller.go b/pkg/controller/repositories/controller.go index 20d0e20..140a1df 100644 --- a/pkg/controller/repositories/controller.go +++ b/pkg/controller/repositories/controller.go @@ -32,8 +32,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" + xpcontroller "github.com/crossplane/crossplane-runtime/pkg/controller" "github.com/crossplane/crossplane-runtime/pkg/event" - "github.com/crossplane/crossplane-runtime/pkg/logging" "github.com/crossplane/crossplane-runtime/pkg/meta" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/pkg/resource" @@ -41,6 +41,7 @@ import ( "github.com/crossplane-contrib/provider-argocd/apis/repositories/v1alpha1" "github.com/crossplane-contrib/provider-argocd/pkg/clients" "github.com/crossplane-contrib/provider-argocd/pkg/clients/repositories" + "github.com/crossplane-contrib/provider-argocd/pkg/features" ) const ( @@ -55,17 +56,23 @@ const ( ) // SetupRepository adds a controller that reconciles repositories. -func SetupRepository(mgr ctrl.Manager, l logging.Logger) error { +func SetupRepository(mgr ctrl.Manager, o xpcontroller.Options) error { name := managed.ControllerName(v1alpha1.RepositoryKind) + opts := []managed.ReconcilerOption{ + managed.WithExternalConnectDisconnecter(&connector{kube: mgr.GetClient(), newArgocdClientFn: repositories.NewRepositoryServiceClient}), + managed.WithLogger(o.Logger.WithValues("controller", name)), + managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), + } + if o.Features.Enabled(features.EnableBetaManagementPolicies) { + opts = append(opts, managed.WithManagementPolicies()) + } return ctrl.NewControllerManagedBy(mgr). Named(name). For(&v1alpha1.Repository{}). Complete(managed.NewReconciler(mgr, resource.ManagedKind(v1alpha1.RepositoryGroupVersionKind), - managed.WithExternalConnectDisconnecter(&connector{kube: mgr.GetClient(), newArgocdClientFn: repositories.NewRepositoryServiceClient}), - managed.WithLogger(l.WithValues("controller", name)), - managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))))) + opts...)) } type connector struct { diff --git a/pkg/features/features.go b/pkg/features/features.go index 9a6a59f..38f5ff0 100644 --- a/pkg/features/features.go +++ b/pkg/features/features.go @@ -20,8 +20,8 @@ import "github.com/crossplane/crossplane-runtime/pkg/feature" // Feature flags. const ( - // EnableAlphaManagementPolicies enables alpha support for + // EnableBetaManagementPolicies enables alpha support for // Management Policies. See the below design for more details. // https://github.com/crossplane/crossplane/pull/3531 - EnableAlphaManagementPolicies feature.Flag = "EnableAlphaManagementPolicies" + EnableBetaManagementPolicies feature.Flag = "EnableBetaManagementPolicies" )