diff --git a/controllers/dpa_controller.go b/controllers/dpa_controller.go index 3c0eb588de..6a7ee3abb0 100644 --- a/controllers/dpa_controller.go +++ b/controllers/dpa_controller.go @@ -51,6 +51,7 @@ type DPAReconciler struct { NamespacedName types.NamespacedName EventRecorder record.EventRecorder dpa *oadpv1alpha1.DataProtectionApplication + OADPNamespace string } var debugMode = os.Getenv("DEBUG") == "true" @@ -148,7 +149,7 @@ func (r *DPAReconciler) SetupWithManager(mgr ctrl.Manager) error { Owns(&routev1.Route{}). Owns(&corev1.ConfigMap{}). Watches(&corev1.Secret{}, &labelHandler{}). - WithEventFilter(veleroPredicate(r.Scheme)). + WithEventFilter(veleroPredicate(r.Scheme, r.OADPNamespace)). Complete(r) } diff --git a/controllers/nonadmin_controller.go b/controllers/nonadmin_controller.go index a53f1aa59f..76249b9709 100644 --- a/controllers/nonadmin_controller.go +++ b/controllers/nonadmin_controller.go @@ -10,7 +10,6 @@ import ( corev1 "k8s.io/api/core/v1" k8serror "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/fields" "k8s.io/apimachinery/pkg/types" "k8s.io/utils/ptr" "sigs.k8s.io/controller-runtime/pkg/client" @@ -85,17 +84,15 @@ func (r *DPAReconciler) ReconcileNonAdminController(log logr.Logger) (bool, erro return true, nil } - selector, err := fields.ParseSelector(fmt.Sprintf("metadata.namespace!=%s", r.NamespacedName.Namespace)) - if err != nil { - return false, err - } dpaList := &oadpv1alpha1.DataProtectionApplicationList{} - err = r.List(r.Context, dpaList, &client.ListOptions{FieldSelector: selector}) + err := r.List(r.Context, dpaList) if err != nil { return false, err } + r.Log.Info("number of DPAs fetched: ", "number of DPAs", len(dpaList.Items)) + r.Log.Info("DPA list fetched:\n", "DPAs", dpaList.Items) for _, dpa := range dpaList.Items { - if (&DPAReconciler{dpa: &dpa}).checkNonAdminEnabled() { + if dpa.Namespace != r.NamespacedName.Namespace && (&DPAReconciler{dpa: &dpa}).checkNonAdminEnabled() { return false, fmt.Errorf("only a single instance of Non-Admin Controller can be installed across the entire cluster. Non-Admin controller is also configured to be installed in %s namespace", dpa.Namespace) } } diff --git a/controllers/predicate.go b/controllers/predicate.go index 4ed1adad0b..456bf974ec 100644 --- a/controllers/predicate.go +++ b/controllers/predicate.go @@ -9,22 +9,22 @@ import ( oadpv1alpha1 "github.com/openshift/oadp-operator/api/v1alpha1" ) -func veleroPredicate(scheme *runtime.Scheme) predicate.Predicate { +func veleroPredicate(scheme *runtime.Scheme, namespace string) predicate.Predicate { return predicate.Funcs{ // Update returns true if the Update event should be processed UpdateFunc: func(e event.UpdateEvent) bool { if e.ObjectOld.GetGeneration() == e.ObjectNew.GetGeneration() { return false } - return isObjectOurs(scheme, e.ObjectOld) + return isObjectOurs(scheme, e.ObjectOld, namespace) }, // Create returns true if the Create event should be processed CreateFunc: func(e event.CreateEvent) bool { - return isObjectOurs(scheme, e.Object) + return isObjectOurs(scheme, e.Object, namespace) }, // Delete returns true if the Delete event should be processed DeleteFunc: func(e event.DeleteEvent) bool { - return !e.DeleteStateUnknown && isObjectOurs(scheme, e.Object) + return !e.DeleteStateUnknown && isObjectOurs(scheme, e.Object, namespace) }, } } @@ -32,7 +32,10 @@ func veleroPredicate(scheme *runtime.Scheme) predicate.Predicate { // isObjectOurs returns true if the object is ours. // it first checks if the object has our group, version, and kind // else it will check for non empty OadpOperatorlabel labels -func isObjectOurs(scheme *runtime.Scheme, object client.Object) bool { +func isObjectOurs(scheme *runtime.Scheme, object client.Object, namespace string) bool { + if object.GetNamespace() != namespace { + return false + } objGVKs, _, err := scheme.ObjectKinds(object) if err != nil { return false diff --git a/main.go b/main.go index 7cd24df03f..21fb661997 100644 --- a/main.go +++ b/main.go @@ -44,7 +44,7 @@ import ( _ "k8s.io/client-go/plugin/pkg/client/auth" "k8s.io/client-go/rest" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/cache" + // "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/healthz" "sigs.k8s.io/controller-runtime/pkg/log/zap" @@ -171,11 +171,11 @@ func main() { RenewDeadline: &leConfig.RenewDeadline.Duration, RetryPeriod: &leConfig.RetryPeriod.Duration, LeaderElectionID: "oadp.openshift.io", - Cache: cache.Options{ - DefaultNamespaces: map[string]cache.Config{ - watchNamespace: {}, - }, - }, + // Cache: cache.Options{ + // DefaultNamespaces: map[string]cache.Config{ + // watchNamespace: {}, + // }, + // }, }) if err != nil { setupLog.Error(err, "unable to start manager") @@ -222,6 +222,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), EventRecorder: mgr.GetEventRecorderFor("DPA-controller"), + OADPNamespace: watchNamespace, }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "DataProtectionApplication") os.Exit(1)