From 171e61794e63fcc736a86cbc4fa4f939eefa5de0 Mon Sep 17 00:00:00 2001 From: Tanmay Satam Date: Thu, 16 Nov 2023 15:44:25 -0500 Subject: [PATCH] Extract duplicated predicates to common predicates pkg --- .../autosizednodes_controller.go | 20 +++------ .../controllers/banner/banner_controller.go | 7 +-- .../checkers/clusterdnschecker/controller.go | 7 +-- .../ingresscertificatechecker/controller.go | 13 ++---- .../checkers/internetchecker/controller.go | 8 +--- .../serviceprincipalchecker/controller.go | 7 +-- .../cloudproviderconfig_controller.go | 7 +-- .../clusteroperatoraro_controller.go | 8 +--- .../controllers/dnsmasq/cluster_controller.go | 8 +--- .../genevalogging/genevalogging_controller.go | 8 +--- .../guardrails/guardrails_controller.go | 7 +-- .../controllers/ingress/ingress_controller.go | 14 +++--- .../machinehealthcheck_controller.go | 14 ++---- .../machineset/machineset_controller.go | 9 +--- .../monitoring/monitoring_controller.go | 7 +-- .../controllers/muo/muo_controller.go | 13 ++---- .../pullsecret/pullsecret_controller.go | 14 ++---- .../routefix/routefix_controller.go | 8 +--- .../storageaccount_controller.go | 16 ++----- .../controllers/subnets/subnets_controller.go | 15 ++----- .../workaround/workaround_controller.go | 4 +- pkg/operator/predicates/predicates.go | 43 +++++++++++++++++++ 22 files changed, 101 insertions(+), 156 deletions(-) create mode 100644 pkg/operator/predicates/predicates.go diff --git a/pkg/operator/controllers/autosizednodes/autosizednodes_controller.go b/pkg/operator/controllers/autosizednodes/autosizednodes_controller.go index 60b7ce254e1..d767708275f 100644 --- a/pkg/operator/controllers/autosizednodes/autosizednodes_controller.go +++ b/pkg/operator/controllers/autosizednodes/autosizednodes_controller.go @@ -6,7 +6,6 @@ package autosizednodes import ( "context" "fmt" - "strings" "github.com/Azure/go-autorest/autorest/to" mcv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1" @@ -17,9 +16,9 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/predicate" arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" + "github.com/Azure/ARO-RP/pkg/operator/predicates" ) type Reconciler struct { @@ -102,19 +101,12 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl. // SetupWithManager prepares the controller with info who to watch func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { - clusterPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - name := o.GetName() - return strings.EqualFold(arov1alpha1.SingletonClusterName, name) - }) - - b := ctrl.NewControllerManagedBy(mgr). - For(&arov1alpha1.Cluster{}, builder.WithPredicates(clusterPredicate)) - - // Controller adds ControllerManagedBy to KubeletConfit created by this controller. - // Any changes will trigger reconcile, but only for that config. - return b. - Named(ControllerName). + // Controller adds ControllerManagedBy to KubeletConfit created by this controller. + // Any changes will trigger reconcile, but only for that config. + return ctrl.NewControllerManagedBy(mgr). + For(&arov1alpha1.Cluster{}, builder.WithPredicates(predicates.AROCluster)). Owns(&mcv1.KubeletConfig{}). + Named(ControllerName). Complete(r) } diff --git a/pkg/operator/controllers/banner/banner_controller.go b/pkg/operator/controllers/banner/banner_controller.go index e6622493fb7..9b5050b64b4 100644 --- a/pkg/operator/controllers/banner/banner_controller.go +++ b/pkg/operator/controllers/banner/banner_controller.go @@ -18,6 +18,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/source" arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" + "github.com/Azure/ARO-RP/pkg/operator/predicates" ) const ( @@ -60,16 +61,12 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl. // SetupWithManager creates the controller func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { - aroClusterPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - return o.GetName() == arov1alpha1.SingletonClusterName - }) - aroBannerPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { return o.GetName() == BannerName }) return ctrl.NewControllerManagedBy(mgr). - For(&arov1alpha1.Cluster{}, builder.WithPredicates(aroClusterPredicate)). + For(&arov1alpha1.Cluster{}, builder.WithPredicates(predicates.AROCluster)). // watching ConsoleNotifications in case a user edits it Watches(&source.Kind{Type: &consolev1.ConsoleNotification{}}, &handler.EnqueueRequestForObject{}, builder.WithPredicates(aroBannerPredicate)). Named(ControllerName). diff --git a/pkg/operator/controllers/checkers/clusterdnschecker/controller.go b/pkg/operator/controllers/checkers/clusterdnschecker/controller.go index 772f6f67b43..0a8071bae89 100644 --- a/pkg/operator/controllers/checkers/clusterdnschecker/controller.go +++ b/pkg/operator/controllers/checkers/clusterdnschecker/controller.go @@ -20,6 +20,7 @@ import ( arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" checkercommon "github.com/Azure/ARO-RP/pkg/operator/controllers/checkers/common" + "github.com/Azure/ARO-RP/pkg/operator/predicates" "github.com/Azure/ARO-RP/pkg/util/conditions" ) @@ -112,16 +113,12 @@ func (r *Reconciler) condition(checkErr error) *operatorv1.OperatorCondition { // SetupWithManager setup our manager func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { - aroClusterPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - return o.GetName() == arov1alpha1.SingletonClusterName - }) - defaultClusterDNSPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { return o.GetName() == "default" }) builder := ctrl.NewControllerManagedBy(mgr). - For(&arov1alpha1.Cluster{}, builder.WithPredicates(aroClusterPredicate)). + For(&arov1alpha1.Cluster{}, builder.WithPredicates(predicates.AROCluster)). Watches( &source.Kind{Type: &operatorv1.DNS{}}, &handler.EnqueueRequestForObject{}, diff --git a/pkg/operator/controllers/checkers/ingresscertificatechecker/controller.go b/pkg/operator/controllers/checkers/ingresscertificatechecker/controller.go index 115ae4136d2..69cbd3ca047 100644 --- a/pkg/operator/controllers/checkers/ingresscertificatechecker/controller.go +++ b/pkg/operator/controllers/checkers/ingresscertificatechecker/controller.go @@ -22,6 +22,7 @@ import ( arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" checkercommon "github.com/Azure/ARO-RP/pkg/operator/controllers/checkers/common" + "github.com/Azure/ARO-RP/pkg/operator/predicates" "github.com/Azure/ARO-RP/pkg/util/conditions" ) @@ -118,20 +119,12 @@ func (r *Reconciler) condition(checkErr error) *operatorv1.OperatorCondition { // SetupWithManager setup our manager func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { - aroClusterPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - return o.GetName() == arov1alpha1.SingletonClusterName - }) - defaultIngressControllerPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { return o.GetNamespace() == "openshift-ingress-operator" && o.GetName() == "default" }) - clusterVersionPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - return o.GetName() == "version" - }) - builder := ctrl.NewControllerManagedBy(mgr). - For(&arov1alpha1.Cluster{}, builder.WithPredicates(aroClusterPredicate)). + For(&arov1alpha1.Cluster{}, builder.WithPredicates(predicates.AROCluster)). Watches( &source.Kind{Type: &operatorv1.IngressController{}}, &handler.EnqueueRequestForObject{}, @@ -140,7 +133,7 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { Watches( &source.Kind{Type: &configv1.ClusterVersion{}}, &handler.EnqueueRequestForObject{}, - builder.WithPredicates(clusterVersionPredicate), + builder.WithPredicates(predicates.ClusterVersion), ) return builder.Named(ControllerName).Complete(r) diff --git a/pkg/operator/controllers/checkers/internetchecker/controller.go b/pkg/operator/controllers/checkers/internetchecker/controller.go index 5a5ec19c49a..29c733c966b 100644 --- a/pkg/operator/controllers/checkers/internetchecker/controller.go +++ b/pkg/operator/controllers/checkers/internetchecker/controller.go @@ -13,11 +13,11 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" checkercommon "github.com/Azure/ARO-RP/pkg/operator/controllers/checkers/common" + "github.com/Azure/ARO-RP/pkg/operator/predicates" "github.com/Azure/ARO-RP/pkg/util/conditions" ) @@ -122,12 +122,8 @@ func (r *Reconciler) conditionType() string { // SetupWithManager setup our manager func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { - aroClusterPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - return o.GetName() == arov1alpha1.SingletonClusterName - }) - builder := ctrl.NewControllerManagedBy(mgr). - For(&arov1alpha1.Cluster{}, builder.WithPredicates(aroClusterPredicate)) + For(&arov1alpha1.Cluster{}, builder.WithPredicates(predicates.AROCluster)) return builder.Named(ControllerName).Complete(r) } diff --git a/pkg/operator/controllers/checkers/serviceprincipalchecker/controller.go b/pkg/operator/controllers/checkers/serviceprincipalchecker/controller.go index b188633ffaf..8b9f3e0425a 100644 --- a/pkg/operator/controllers/checkers/serviceprincipalchecker/controller.go +++ b/pkg/operator/controllers/checkers/serviceprincipalchecker/controller.go @@ -21,6 +21,7 @@ import ( arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" checkercommon "github.com/Azure/ARO-RP/pkg/operator/controllers/checkers/common" + "github.com/Azure/ARO-RP/pkg/operator/predicates" "github.com/Azure/ARO-RP/pkg/util/clusterauthorizer" "github.com/Azure/ARO-RP/pkg/util/conditions" ) @@ -114,16 +115,12 @@ func (r *Reconciler) condition(checkErr error) *operatorv1.OperatorCondition { // SetupWithManager setup our manager func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { - aroClusterPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - return o.GetName() == arov1alpha1.SingletonClusterName - }) - clusterSPPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { return o.GetName() == clusterauthorizer.AzureCredentialSecretName && o.GetNamespace() == clusterauthorizer.AzureCredentialSecretNameSpace }) builder := ctrl.NewControllerManagedBy(mgr). - For(&arov1alpha1.Cluster{}, builder.WithPredicates(aroClusterPredicate)). + For(&arov1alpha1.Cluster{}, builder.WithPredicates(predicates.AROCluster)). Watches( &source.Kind{Type: &corev1.Secret{}}, &handler.EnqueueRequestForObject{}, diff --git a/pkg/operator/controllers/cloudproviderconfig/cloudproviderconfig_controller.go b/pkg/operator/controllers/cloudproviderconfig/cloudproviderconfig_controller.go index 13b92749c94..ace15d00f33 100644 --- a/pkg/operator/controllers/cloudproviderconfig/cloudproviderconfig_controller.go +++ b/pkg/operator/controllers/cloudproviderconfig/cloudproviderconfig_controller.go @@ -22,6 +22,7 @@ import ( arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" "github.com/Azure/ARO-RP/pkg/operator/controllers/base" + "github.com/Azure/ARO-RP/pkg/operator/predicates" ) const ( @@ -164,16 +165,12 @@ func (r *CloudProviderConfigReconciler) Reconcile(ctx context.Context, request c func (r *CloudProviderConfigReconciler) SetupWithManager(mgr ctrl.Manager) error { r.Log.Info("starting cloud-provider-config controller") - aroClusterPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - return o.GetName() == arov1alpha1.SingletonClusterName - }) - cloudProviderConfigPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { return o.GetName() == cloudProviderConfigName.Name && o.GetNamespace() == cloudProviderConfigName.Namespace }) return ctrl.NewControllerManagedBy(mgr). - For(&arov1alpha1.Cluster{}, builder.WithPredicates(aroClusterPredicate)). + For(&arov1alpha1.Cluster{}, builder.WithPredicates(predicates.AROCluster)). Watches( &source.Kind{Type: &corev1.ConfigMap{}}, &handler.EnqueueRequestForObject{}, diff --git a/pkg/operator/controllers/clusteroperatoraro/clusteroperatoraro_controller.go b/pkg/operator/controllers/clusteroperatoraro/clusteroperatoraro_controller.go index 1e01b7ea57f..7a8ce8ec69f 100644 --- a/pkg/operator/controllers/clusteroperatoraro/clusteroperatoraro_controller.go +++ b/pkg/operator/controllers/clusteroperatoraro/clusteroperatoraro_controller.go @@ -21,10 +21,10 @@ import ( "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" - "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" + "github.com/Azure/ARO-RP/pkg/operator/predicates" "github.com/Azure/ARO-RP/pkg/util/version" ) @@ -163,12 +163,8 @@ func (r *Reconciler) defaultOperator() *configv1.ClusterOperator { // SetupWithManager setup our manager func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { - aroClusterPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - return o.GetName() == arov1alpha1.SingletonClusterName - }) - return ctrl.NewControllerManagedBy(mgr). - For(&arov1alpha1.Cluster{}, builder.WithPredicates(aroClusterPredicate)). + For(&arov1alpha1.Cluster{}, builder.WithPredicates(predicates.AROCluster)). Owns(&configv1.ClusterOperator{}). Named(ControllerName). Complete(r) diff --git a/pkg/operator/controllers/dnsmasq/cluster_controller.go b/pkg/operator/controllers/dnsmasq/cluster_controller.go index 56dca6d1d8e..36bb0e52660 100644 --- a/pkg/operator/controllers/dnsmasq/cluster_controller.go +++ b/pkg/operator/controllers/dnsmasq/cluster_controller.go @@ -12,11 +12,11 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" "github.com/Azure/ARO-RP/pkg/operator/controllers/base" + "github.com/Azure/ARO-RP/pkg/operator/predicates" "github.com/Azure/ARO-RP/pkg/util/dynamichelper" ) @@ -83,12 +83,8 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, request ctrl.Request) // SetupWithManager setup our mananger func (r *ClusterReconciler) SetupWithManager(mgr ctrl.Manager) error { - aroClusterPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - return o.GetName() == arov1alpha1.SingletonClusterName - }) - return ctrl.NewControllerManagedBy(mgr). - For(&arov1alpha1.Cluster{}, builder.WithPredicates(aroClusterPredicate)). + For(&arov1alpha1.Cluster{}, builder.WithPredicates(predicates.AROCluster)). Named(ClusterControllerName). Complete(r) } diff --git a/pkg/operator/controllers/genevalogging/genevalogging_controller.go b/pkg/operator/controllers/genevalogging/genevalogging_controller.go index 15ee6626e50..f0ef3e472ac 100644 --- a/pkg/operator/controllers/genevalogging/genevalogging_controller.go +++ b/pkg/operator/controllers/genevalogging/genevalogging_controller.go @@ -14,12 +14,12 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" "github.com/Azure/ARO-RP/pkg/operator" arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" "github.com/Azure/ARO-RP/pkg/operator/controllers/base" + "github.com/Azure/ARO-RP/pkg/operator/predicates" "github.com/Azure/ARO-RP/pkg/util/dynamichelper" ) @@ -112,12 +112,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl. // SetupWithManager setup our manager func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { - aroClusterPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - return o.GetName() == arov1alpha1.SingletonClusterName - }) - return ctrl.NewControllerManagedBy(mgr). - For(&arov1alpha1.Cluster{}, builder.WithPredicates(aroClusterPredicate)). + For(&arov1alpha1.Cluster{}, builder.WithPredicates(predicates.AROCluster)). Owns(&appsv1.DaemonSet{}). Owns(&corev1.ConfigMap{}). Owns(&corev1.Namespace{}). diff --git a/pkg/operator/controllers/guardrails/guardrails_controller.go b/pkg/operator/controllers/guardrails/guardrails_controller.go index b097c1e57f5..fafd597e812 100644 --- a/pkg/operator/controllers/guardrails/guardrails_controller.go +++ b/pkg/operator/controllers/guardrails/guardrails_controller.go @@ -20,6 +20,7 @@ import ( arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" "github.com/Azure/ARO-RP/pkg/operator/controllers/guardrails/config" + "github.com/Azure/ARO-RP/pkg/operator/predicates" "github.com/Azure/ARO-RP/pkg/util/deployer" "github.com/Azure/ARO-RP/pkg/util/dynamichelper" ) @@ -147,12 +148,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl. // SetupWithManager setup our manager func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { - aroClusterPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - return o.GetName() == arov1alpha1.SingletonClusterName - }) - grBuilder := ctrl.NewControllerManagedBy(mgr). - For(&arov1alpha1.Cluster{}, builder.WithPredicates(aroClusterPredicate)) + For(&arov1alpha1.Cluster{}, builder.WithPredicates(predicates.AROCluster)) resources, err := r.deployer.Template(&config.GuardRailsDeploymentConfig{}, staticFiles) if err != nil { diff --git a/pkg/operator/controllers/ingress/ingress_controller.go b/pkg/operator/controllers/ingress/ingress_controller.go index 1f4b618ef64..dd4a371931d 100644 --- a/pkg/operator/controllers/ingress/ingress_controller.go +++ b/pkg/operator/controllers/ingress/ingress_controller.go @@ -13,11 +13,11 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" "github.com/Azure/ARO-RP/pkg/operator/controllers/base" + "github.com/Azure/ARO-RP/pkg/operator/predicates" ) const ( @@ -81,12 +81,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl. // SetupWithManager setup the mananger for openshift ingress controller resource func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { - aroClusterPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - return o.GetName() == arov1alpha1.SingletonClusterName - }) - - builder := ctrl.NewControllerManagedBy(mgr). - For(&arov1alpha1.Cluster{}, builder.WithPredicates(aroClusterPredicate)) - - return builder.Named(ControllerName).Complete(r) + return ctrl.NewControllerManagedBy(mgr). + For(&arov1alpha1.Cluster{}, builder.WithPredicates(predicates.AROCluster)). + Named(ControllerName). + Complete(r) } diff --git a/pkg/operator/controllers/machinehealthcheck/machinehealthcheck_controller.go b/pkg/operator/controllers/machinehealthcheck/machinehealthcheck_controller.go index 8b0f7e73cd4..06f3080800b 100644 --- a/pkg/operator/controllers/machinehealthcheck/machinehealthcheck_controller.go +++ b/pkg/operator/controllers/machinehealthcheck/machinehealthcheck_controller.go @@ -6,7 +6,6 @@ package machinehealthcheck import ( "context" _ "embed" - "strings" "time" configv1 "github.com/openshift/api/config/v1" @@ -20,12 +19,12 @@ import ( "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/handler" - "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" "sigs.k8s.io/controller-runtime/pkg/source" arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" "github.com/Azure/ARO-RP/pkg/operator/controllers/base" + "github.com/Azure/ARO-RP/pkg/operator/predicates" "github.com/Azure/ARO-RP/pkg/util/dynamichelper" ) @@ -167,22 +166,15 @@ func (r *Reconciler) isClusterUpgrading(ctx context.Context) (bool, error) { // SetupWithManager will manage only our MHC resource with our specific controller name func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { - aroClusterPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - return strings.EqualFold(arov1alpha1.SingletonClusterName, o.GetName()) - }) - clusterVersionPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - return o.GetName() == "version" - }) - return ctrl.NewControllerManagedBy(mgr). - For(&arov1alpha1.Cluster{}, builder.WithPredicates(aroClusterPredicate)). + For(&arov1alpha1.Cluster{}, builder.WithPredicates(predicates.AROCluster)). Named(ControllerName). Owns(&machinev1beta1.MachineHealthCheck{}). Owns(&monitoringv1.PrometheusRule{}). Watches( &source.Kind{Type: &configv1.ClusterVersion{}}, &handler.EnqueueRequestForObject{}, - builder.WithPredicates(clusterVersionPredicate), + builder.WithPredicates(predicates.ClusterVersion), ). Complete(r) } diff --git a/pkg/operator/controllers/machineset/machineset_controller.go b/pkg/operator/controllers/machineset/machineset_controller.go index 3aba44a5e25..eeedaa85585 100644 --- a/pkg/operator/controllers/machineset/machineset_controller.go +++ b/pkg/operator/controllers/machineset/machineset_controller.go @@ -15,10 +15,10 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" "github.com/Azure/ARO-RP/pkg/operator/controllers/base" + "github.com/Azure/ARO-RP/pkg/operator/predicates" ) const ( @@ -109,13 +109,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl. } func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { - machineSetPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - role := o.GetLabels()["machine.openshift.io/cluster-api-machine-role"] - return strings.EqualFold("worker", role) - }) - return ctrl.NewControllerManagedBy(mgr). - For(&machinev1beta1.MachineSet{}, builder.WithPredicates(machineSetPredicate)). + For(&machinev1beta1.MachineSet{}, builder.WithPredicates(predicates.MachineRoleWorker)). Named(ControllerName). Complete(r) } diff --git a/pkg/operator/controllers/monitoring/monitoring_controller.go b/pkg/operator/controllers/monitoring/monitoring_controller.go index 4d8a23940bb..22fed671de8 100644 --- a/pkg/operator/controllers/monitoring/monitoring_controller.go +++ b/pkg/operator/controllers/monitoring/monitoring_controller.go @@ -26,6 +26,7 @@ import ( "github.com/Azure/ARO-RP/pkg/api" arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" "github.com/Azure/ARO-RP/pkg/operator/controllers/base" + "github.com/Azure/ARO-RP/pkg/operator/predicates" ) const ( @@ -206,16 +207,12 @@ func (r *MonitoringReconciler) monitoringConfigMap(ctx context.Context) (*corev1 func (r *MonitoringReconciler) SetupWithManager(mgr ctrl.Manager) error { r.Log.Info("starting cluster monitoring controller") - aroClusterPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - return o.GetName() == arov1alpha1.SingletonClusterName - }) - monitoringConfigMapPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { return o.GetName() == monitoringName.Name && o.GetNamespace() == monitoringName.Namespace }) return ctrl.NewControllerManagedBy(mgr). - For(&arov1alpha1.Cluster{}, builder.WithPredicates(aroClusterPredicate)). + For(&arov1alpha1.Cluster{}, builder.WithPredicates(predicates.AROCluster)). // https://github.com/kubernetes-sigs/controller-runtime/issues/1173 // equivalent to For(&v1.ConfigMap{}, ...)., but can't call For multiple times on one builder Watches( diff --git a/pkg/operator/controllers/muo/muo_controller.go b/pkg/operator/controllers/muo/muo_controller.go index e62036d5055..62e10bea19b 100644 --- a/pkg/operator/controllers/muo/muo_controller.go +++ b/pkg/operator/controllers/muo/muo_controller.go @@ -24,6 +24,7 @@ import ( arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" "github.com/Azure/ARO-RP/pkg/operator/controllers/muo/config" + "github.com/Azure/ARO-RP/pkg/operator/predicates" "github.com/Azure/ARO-RP/pkg/util/deployer" "github.com/Azure/ARO-RP/pkg/util/dynamichelper" "github.com/Azure/ARO-RP/pkg/util/pullsecret" @@ -166,20 +167,12 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl. // SetupWithManager setup our manager func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { - pullSecretPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - return (o.GetName() == pullSecretName.Name && o.GetNamespace() == pullSecretName.Namespace) - }) - - aroClusterPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - return o.GetName() == arov1alpha1.SingletonClusterName - }) - muoBuilder := ctrl.NewControllerManagedBy(mgr). - For(&arov1alpha1.Cluster{}, builder.WithPredicates(aroClusterPredicate)). + For(&arov1alpha1.Cluster{}, builder.WithPredicates(predicates.AROCluster)). Watches( &source.Kind{Type: &corev1.Secret{}}, &handler.EnqueueRequestForObject{}, - builder.WithPredicates(pullSecretPredicate), + builder.WithPredicates(predicates.PullSecret), ) resources, err := r.deployer.Template(&config.MUODeploymentConfig{}, staticFiles) diff --git a/pkg/operator/controllers/pullsecret/pullsecret_controller.go b/pkg/operator/controllers/pullsecret/pullsecret_controller.go index 3cfc8402113..b4cde6b9a42 100644 --- a/pkg/operator/controllers/pullsecret/pullsecret_controller.go +++ b/pkg/operator/controllers/pullsecret/pullsecret_controller.go @@ -30,6 +30,7 @@ import ( "github.com/Azure/ARO-RP/pkg/operator" arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" + "github.com/Azure/ARO-RP/pkg/operator/predicates" "github.com/Azure/ARO-RP/pkg/util/pullsecret" ) @@ -115,23 +116,14 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl. // SetupWithManager setup our manager func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { - pullSecretPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - return (o.GetName() == pullSecretName.Name && o.GetNamespace() == pullSecretName.Namespace) || - (o.GetName() == operator.SecretName && o.GetNamespace() == operator.Namespace) - }) - - aroClusterPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - return o.GetName() == arov1alpha1.SingletonClusterName - }) - return ctrl.NewControllerManagedBy(mgr). - For(&arov1alpha1.Cluster{}, builder.WithPredicates(aroClusterPredicate)). + For(&arov1alpha1.Cluster{}, builder.WithPredicates(predicates.AROCluster)). // https://github.com/kubernetes-sigs/controller-runtime/issues/1173 // equivalent to For(&v1.Secret{})., but can't call For multiple times on one builder Watches( &source.Kind{Type: &corev1.Secret{}}, &handler.EnqueueRequestForObject{}, - builder.WithPredicates(pullSecretPredicate), + builder.WithPredicates(predicate.Or(predicates.PullSecret, predicates.BackupPullSecret)), ). Named(ControllerName). Complete(r) diff --git a/pkg/operator/controllers/routefix/routefix_controller.go b/pkg/operator/controllers/routefix/routefix_controller.go index 7e09caeaf99..dffe37457b9 100644 --- a/pkg/operator/controllers/routefix/routefix_controller.go +++ b/pkg/operator/controllers/routefix/routefix_controller.go @@ -18,10 +18,10 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" + "github.com/Azure/ARO-RP/pkg/operator/predicates" "github.com/Azure/ARO-RP/pkg/util/dynamichelper" "github.com/Azure/ARO-RP/pkg/util/version" ) @@ -154,12 +154,8 @@ func (r *Reconciler) remove(ctx context.Context, instance *arov1alpha1.Cluster) // SetupWithManager creates the controller func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { - aroClusterPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - return o.GetName() == arov1alpha1.SingletonClusterName - }) - return ctrl.NewControllerManagedBy(mgr). - For(&arov1alpha1.Cluster{}, builder.WithPredicates(aroClusterPredicate)). + For(&arov1alpha1.Cluster{}, builder.WithPredicates(predicates.AROCluster)). Owns(&corev1.Namespace{}). Owns(&appsv1.DaemonSet{}). Owns(&securityv1.SecurityContextConstraints{}). diff --git a/pkg/operator/controllers/storageaccounts/storageaccount_controller.go b/pkg/operator/controllers/storageaccounts/storageaccount_controller.go index b8b20d634fc..6529e0f8692 100644 --- a/pkg/operator/controllers/storageaccounts/storageaccount_controller.go +++ b/pkg/operator/controllers/storageaccounts/storageaccount_controller.go @@ -14,11 +14,11 @@ import ( "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/handler" - "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" "sigs.k8s.io/controller-runtime/pkg/source" arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" + "github.com/Azure/ARO-RP/pkg/operator/predicates" "github.com/Azure/ARO-RP/pkg/util/azureclient" "github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/storage" "github.com/Azure/ARO-RP/pkg/util/clusterauthorizer" @@ -112,18 +112,10 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl. // SetupWithManager creates the controller func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { - aroClusterPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - return o.GetName() == arov1alpha1.SingletonClusterName - }) - masterMachinePredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - role, ok := o.GetLabels()["machine.openshift.io/cluster-api-machine-role"] - return ok && role == "master" - }) - return ctrl.NewControllerManagedBy(mgr). - For(&arov1alpha1.Cluster{}, builder.WithPredicates(aroClusterPredicate)). - Watches(&source.Kind{Type: &machinev1beta1.Machine{}}, &handler.EnqueueRequestForObject{}, builder.WithPredicates(masterMachinePredicate)). // to reconcile on master machine replacement - Watches(&source.Kind{Type: &machinev1beta1.MachineSet{}}, &handler.EnqueueRequestForObject{}). // to reconcile on worker machines + For(&arov1alpha1.Cluster{}, builder.WithPredicates(predicates.AROCluster)). + Watches(&source.Kind{Type: &machinev1beta1.Machine{}}, &handler.EnqueueRequestForObject{}, builder.WithPredicates(predicates.MachineRoleMaster)). // to reconcile on master machine replacement + Watches(&source.Kind{Type: &machinev1beta1.MachineSet{}}, &handler.EnqueueRequestForObject{}, builder.WithPredicates(predicates.MachineRoleWorker)). // to reconcile on worker machines Named(ControllerName). Complete(r) } diff --git a/pkg/operator/controllers/subnets/subnets_controller.go b/pkg/operator/controllers/subnets/subnets_controller.go index 5c5ecff9324..bd1808a33af 100644 --- a/pkg/operator/controllers/subnets/subnets_controller.go +++ b/pkg/operator/controllers/subnets/subnets_controller.go @@ -16,11 +16,11 @@ import ( "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/handler" - "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" "sigs.k8s.io/controller-runtime/pkg/source" arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" + "github.com/Azure/ARO-RP/pkg/operator/predicates" "github.com/Azure/ARO-RP/pkg/util/azureclient" "github.com/Azure/ARO-RP/pkg/util/clusterauthorizer" "github.com/Azure/ARO-RP/pkg/util/subnet" @@ -151,18 +151,11 @@ func (r *reconcileManager) reconcileSubnets(ctx context.Context) error { // SetupWithManager creates the controller func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { - aroClusterPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - return o.GetName() == arov1alpha1.SingletonClusterName - }) - masterMachinePredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { - role, ok := o.GetLabels()["machine.openshift.io/cluster-api-machine-role"] - return ok && role == "master" - }) return ctrl.NewControllerManagedBy(mgr). - For(&arov1alpha1.Cluster{}, builder.WithPredicates(aroClusterPredicate)). - Watches(&source.Kind{Type: &machinev1beta1.Machine{}}, &handler.EnqueueRequestForObject{}, builder.WithPredicates(masterMachinePredicate)). // to reconcile on master machine replacement - Watches(&source.Kind{Type: &machinev1beta1.MachineSet{}}, &handler.EnqueueRequestForObject{}). // to reconcile on worker machines + For(&arov1alpha1.Cluster{}, builder.WithPredicates(predicates.AROCluster)). + Watches(&source.Kind{Type: &machinev1beta1.Machine{}}, &handler.EnqueueRequestForObject{}, builder.WithPredicates(predicates.MachineRoleMaster)). // to reconcile on master machine replacement + Watches(&source.Kind{Type: &machinev1beta1.MachineSet{}}, &handler.EnqueueRequestForObject{}, builder.WithPredicates(predicates.MachineRoleWorker)). // to reconcile on worker machines Named(ControllerName). Complete(r) } diff --git a/pkg/operator/controllers/workaround/workaround_controller.go b/pkg/operator/controllers/workaround/workaround_controller.go index a5f54c4ac6d..d5c1a40ee36 100644 --- a/pkg/operator/controllers/workaround/workaround_controller.go +++ b/pkg/operator/controllers/workaround/workaround_controller.go @@ -11,10 +11,12 @@ import ( "github.com/sirupsen/logrus" "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/reconcile" arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" + "github.com/Azure/ARO-RP/pkg/operator/predicates" "github.com/Azure/ARO-RP/pkg/util/version" ) @@ -87,7 +89,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl. // SetupWithManager setup our manager func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). - For(&arov1alpha1.Cluster{}). + For(&arov1alpha1.Cluster{}, builder.WithPredicates(predicates.AROCluster)). Named(ControllerName). Complete(r) } diff --git a/pkg/operator/predicates/predicates.go b/pkg/operator/predicates/predicates.go new file mode 100644 index 00000000000..c05758a25ea --- /dev/null +++ b/pkg/operator/predicates/predicates.go @@ -0,0 +1,43 @@ +package predicates + +// Copyright (c) Microsoft Corporation. +// Licensed under the Apache License 2.0. + +import ( + "strings" + + "k8s.io/apimachinery/pkg/types" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/predicate" + + "github.com/Azure/ARO-RP/pkg/operator" + arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" +) + +var AROCluster predicate.Predicate = predicate.NewPredicateFuncs(func(o client.Object) bool { + return strings.EqualFold(arov1alpha1.SingletonClusterName, o.GetName()) +}) + +var MachineRoleMaster predicate.Predicate = predicate.NewPredicateFuncs(func(o client.Object) bool { + role, ok := o.GetLabels()["machine.openshift.io/cluster-api-machine-role"] + return ok && strings.EqualFold("master", role) +}) + +var MachineRoleWorker predicate.Predicate = predicate.NewPredicateFuncs(func(o client.Object) bool { + role, ok := o.GetLabels()["machine.openshift.io/cluster-api-machine-role"] + return ok && strings.EqualFold("worker", role) +}) + +var ClusterVersion predicate.Predicate = predicate.NewPredicateFuncs(func(o client.Object) bool { + return o.GetName() == "version" +}) + +var pullSecretName = types.NamespacedName{Name: "pull-secret", Namespace: "openshift-config"} +var PullSecret predicate.Predicate = predicate.NewPredicateFuncs(func(o client.Object) bool { + return (o.GetName() == pullSecretName.Name && o.GetNamespace() == pullSecretName.Namespace) +}) + +var backupPullSecretName = types.NamespacedName{Name: operator.SecretName, Namespace: operator.Namespace} +var BackupPullSecret predicate.Predicate = predicate.NewPredicateFuncs(func(o client.Object) bool { + return (o.GetName() == backupPullSecretName.Name && o.GetNamespace() == backupPullSecretName.Namespace) +})