From 5e4ed8a6d5d639071f317a37ed54e48daf0b3624 Mon Sep 17 00:00:00 2001 From: Nitin Goyal Date: Fri, 1 Dec 2023 10:47:06 +0530 Subject: [PATCH] skip NooBaa reconcile unless requested from same namespace as operator currently noobaa supports the installation of noobaa instance within the operator namespace only. It does not make sense to deploy the noobaa in the different namespace or via the storagecluster from the different namespace. Signed-off-by: Nitin Goyal --- .../noobaa_system_reconciler.go | 10 +++ .../noobaa_system_reconciler_test.go | 61 +++++++++++++++++-- 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/controllers/storagecluster/noobaa_system_reconciler.go b/controllers/storagecluster/noobaa_system_reconciler.go index 76fcdea975..23f72a71a5 100644 --- a/controllers/storagecluster/noobaa_system_reconciler.go +++ b/controllers/storagecluster/noobaa_system_reconciler.go @@ -28,6 +28,11 @@ const ( type ocsNoobaaSystem struct{} func (obj *ocsNoobaaSystem) ensureCreated(r *StorageClusterReconciler, sc *ocsv1.StorageCluster) (reconcile.Result, error) { + // skip noobaa reconcile if it is not requested from same namespace as operator + if sc.Namespace != r.OperatorNamespace { + return reconcile.Result{}, nil + } + var err error var reconcileStrategy ReconcileStrategy @@ -240,6 +245,11 @@ func (r *StorageClusterReconciler) setNooBaaDesiredState(nb *nbv1.NooBaa, sc *oc // ensureDeleted Delete noobaa system in the namespace func (obj *ocsNoobaaSystem) ensureDeleted(r *StorageClusterReconciler, sc *ocsv1.StorageCluster) (reconcile.Result, error) { + // skip noobaa reconcile if it is not requested from same namespace as operator + if sc.Namespace != r.OperatorNamespace { + return reconcile.Result{}, nil + } + // Delete only if this is being managed by the OCS operator if sc.Spec.MultiCloudGateway != nil { reconcileStrategy := ReconcileStrategy(sc.Spec.MultiCloudGateway.ReconcileStrategy) diff --git a/controllers/storagecluster/noobaa_system_reconciler_test.go b/controllers/storagecluster/noobaa_system_reconciler_test.go index 047265acd6..9ce8c13841 100644 --- a/controllers/storagecluster/noobaa_system_reconciler_test.go +++ b/controllers/storagecluster/noobaa_system_reconciler_test.go @@ -32,7 +32,7 @@ var noobaaReconcileTestLogger = logf.Log.WithName("noobaa_system_reconciler_test func TestEnsureNooBaaSystem(t *testing.T) { namespacedName := types.NamespacedName{ Name: "noobaa", - Namespace: "test_ns", + Namespace: "openshift-storage", } sc := v1.StorageCluster{ ObjectMeta: metav1.ObjectMeta{ @@ -130,10 +130,60 @@ func TestEnsureNooBaaSystem(t *testing.T) { } } +func TestNooBaaSkipUnskip(t *testing.T) { + t.Run("Ensure noobaa is skipped in namespace other than operator namespace", func(t *testing.T) { + var obj ocsNoobaaSystem + reconciler := getReconciler(t, &nbv1.NooBaa{}) + sc := v1.StorageCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "ocs", + Namespace: "test-ns", + }, + } + _, err := obj.ensureCreated(&reconciler, &sc) + assert.NoError(t, err) + + noobaa := &nbv1.NooBaa{} + err = reconciler.Client.Get(context.TODO(), types.NamespacedName{Namespace: sc.Namespace, Name: "noobaa"}, noobaa) + assert.True(t, errors.IsNotFound(err)) + }) + + t.Run("Ensure noobaa is created in namespace same as operator namespace", func(t *testing.T) { + var obj ocsNoobaaSystem + reconciler := getReconciler(t, &nbv1.NooBaa{}) + sc := v1.StorageCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "ocs", + Namespace: "openshift-storage", + }, + Status: v1.StorageClusterStatus{ + Images: v1.ImagesStatus{ + NooBaaCore: &v1.ComponentImageStatus{}, + NooBaaDB: &v1.ComponentImageStatus{}, + }, + }, + } + + cephCluster := cephv1.CephCluster{} + cephCluster.Name = generateNameForCephClusterFromString(sc.Name) + cephCluster.Namespace = sc.Namespace + cephCluster.Status.State = cephv1.ClusterStateCreated + err := reconciler.Client.Create(context.TODO(), &cephCluster) + assert.NoError(t, err) + + _, err = obj.ensureCreated(&reconciler, &sc) + assert.NoError(t, err) + + noobaa := &nbv1.NooBaa{} + err = reconciler.Client.Get(context.TODO(), types.NamespacedName{Namespace: sc.Namespace, Name: "noobaa"}, noobaa) + assert.NoError(t, err) + }) +} + func TestNooBaaReconcileStrategy(t *testing.T) { namespacedName := types.NamespacedName{ Name: "noobaa", - Namespace: "test_ns", + Namespace: "openshift-storage", } cases := []struct { @@ -404,9 +454,10 @@ func getReconciler(t *testing.T, objs ...runtime.Object) StorageClusterReconcile client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(registerObjs...).WithStatusSubresource(sc).Build() return StorageClusterReconciler{ - Scheme: scheme, - Client: client, - platform: &Platform{}, + Scheme: scheme, + Client: client, + platform: &Platform{}, + OperatorNamespace: "openshift-storage", } }