Skip to content

Commit

Permalink
Add support for managementPolicies
Browse files Browse the repository at this point in the history
Signed-off-by: Reza Nasiri-Toosi <[email protected]>
  • Loading branch information
r-nasiri committed Aug 16, 2024
1 parent 72a5615 commit a2778db
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 45 deletions.
6 changes: 3 additions & 3 deletions cmd/provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
26 changes: 18 additions & 8 deletions pkg/controller/applications/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ 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"
ctrl "sigs.k8s.io/controller-runtime"
"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"
Expand All @@ -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 (
Expand All @@ -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 {
Expand Down
23 changes: 15 additions & 8 deletions pkg/controller/applicationsets/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,21 @@ 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"

"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 (
Expand All @@ -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 {
Expand Down
9 changes: 4 additions & 5 deletions pkg/controller/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -31,16 +30,16 @@ 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,
cluster.SetupCluster,
applications.SetupApplication,
applicationsets.SetupApplicationSet,
} {
if err := setup(mgr, l); err != nil {
if err := setup(mgr, o); err != nil {
return err
}
}
Expand Down
18 changes: 12 additions & 6 deletions pkg/controller/cluster/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ 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"

"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 (
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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{
Expand All @@ -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)))))
}
19 changes: 14 additions & 5 deletions pkg/controller/projects/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ 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"

"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 (
Expand All @@ -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 {
Expand Down
17 changes: 12 additions & 5 deletions pkg/controller/repositories/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ 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"

"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 (
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

0 comments on commit a2778db

Please sign in to comment.