From cdbab43232fec613cfedb6aed596b7755001bb5d Mon Sep 17 00:00:00 2001 From: Tommy Hughes Date: Mon, 13 Feb 2023 12:41:37 -0600 Subject: [PATCH] make dbaaspolicy object optional Signed-off-by: Tommy Hughes --- api/v1beta1/dbaasprovider.go | 2 -- controllers/dbaas_controllers_common_test.go | 20 ------------- controllers/dbaas_reconciler.go | 10 +++---- controllers/dbaas_reconciler_test.go | 5 ++-- controllers/dbaasinventory_controller.go | 28 ------------------- controllers/dbaasinventory_controller_test.go | 26 ----------------- controllers/dbaasplatform_controller_test.go | 10 ++----- 7 files changed, 8 insertions(+), 93 deletions(-) diff --git a/api/v1beta1/dbaasprovider.go b/api/v1beta1/dbaasprovider.go index 494b15c4..f4cb2cf4 100644 --- a/api/v1beta1/dbaasprovider.go +++ b/api/v1beta1/dbaasprovider.go @@ -35,7 +35,6 @@ const ( // DBaaS condition reasons: Ready string = "Ready" - DBaaSPolicyNotFound string = "DBaaSPolicyNotFound" DBaaSPolicyNotReady string = "DBaaSPolicyNotReady" DBaaSProviderNotFound string = "DBaaSProviderNotFound" DBaaSInventoryNotFound string = "DBaaSInventoryNotFound" @@ -54,7 +53,6 @@ const ( MsgProviderCRReconcileInProgress string = "DBaaS Provider Custom Resource reconciliation in progress" MsgInventoryNotReady string = "Inventory discovery not done" MsgInventoryNotProvisionable string = "Inventory provisioning not allowed" - MsgPolicyNotFound string = "Failed to find an active Policy" MsgPolicyReady string = "Policy is active" MsgInvalidNamespace string = "Invalid connection namespace for the referenced inventory" MsgPolicyNotReady string = "Another active Policy already exists" diff --git a/controllers/dbaas_controllers_common_test.go b/controllers/dbaas_controllers_common_test.go index 89382498..84c89395 100644 --- a/controllers/dbaas_controllers_common_test.go +++ b/controllers/dbaas_controllers_common_test.go @@ -473,17 +473,6 @@ func assertInventoryStatus(inv *v1beta1.DBaaSInventory, condType string, dbaasSt } } -func assertInventoryStatusV1alpha1(inv *v1alpha1.DBaaSInventory, condType string, dbaasStatus metav1.ConditionStatus, providerResourceStatus interface{}) func() { - return func() { - status := inv.Status.DeepCopy() - dbaasConds, providerConds := splitStatusConditions(status.Conditions, condType) - Expect(len(dbaasConds)).Should(Equal(1)) - Expect(dbaasConds[0].Type).Should(Equal(condType)) - Expect(dbaasConds[0].Status).Should(Equal(dbaasStatus)) - status.Conditions = providerConds - Expect(status).Should(Equal(providerResourceStatus)) - } -} func assertConnectionStatus(conn *v1beta1.DBaaSConnection, condType string, providerResourceStatus interface{}) func() { return func() { assertConnectionDBaaSStatus(conn.Name, conn.Namespace, metav1.ConditionTrue)() @@ -526,15 +515,6 @@ func assertInstanceStatus(conn *v1beta1.DBaaSInstance, condType string, provider } } -func assertInstanceStatusV1alpha1(conn *v1alpha1.DBaaSInstance, condType string, providerResourceStatus interface{}) func() { - return func() { - assertInstanceDBaaSStatus(conn.Name, conn.Namespace, metav1.ConditionTrue)() - status := conn.Status.DeepCopy() - _, providerConds := splitStatusConditions(status.Conditions, condType) - status.Conditions = providerConds - Expect(status).Should(Equal(providerResourceStatus)) - } -} func assertProviderResourceSpecUpdated(object client.Object, groupVersion schema.GroupVersion, providerResourceKind string, DBaaSResourceSpec interface{}) func() { return func() { By("updating the DBaaS resource spec") diff --git a/controllers/dbaas_reconciler.go b/controllers/dbaas_reconciler.go index 5bfc11c9..648173ca 100644 --- a/controllers/dbaas_reconciler.go +++ b/controllers/dbaas_reconciler.go @@ -162,17 +162,15 @@ func (r *DBaaSReconciler) isValidConnectionNS(ctx context.Context, namespace str // check if provisioning is allowed against an inventory. inventory takes precedence over dbaaspolicy. func canProvision(inventory *v1beta1.DBaaSInventory, activePolicy *v1beta1.DBaaSPolicy) bool { - if activePolicy == nil { - // not an active namespace - return false - } if inventory.Spec.Policy != nil { if inventory.Spec.Policy.DisableProvisions != nil { return !*inventory.Spec.Policy.DisableProvisions } } else { - if activePolicy.Spec.DisableProvisions != nil { - return !*activePolicy.Spec.DisableProvisions + if activePolicy != nil { + if activePolicy.Spec.DisableProvisions != nil { + return !*activePolicy.Spec.DisableProvisions + } } } return true diff --git a/controllers/dbaas_reconciler_test.go b/controllers/dbaas_reconciler_test.go index 9e473d8b..6a081700 100644 --- a/controllers/dbaas_reconciler_test.go +++ b/controllers/dbaas_reconciler_test.go @@ -299,14 +299,13 @@ var _ = Describe("list policies by inventory namespace", func() { Expect(canProvision(inventory, activePolicy)).Should(BeTrue()) activePolicy.Spec.DisableProvisions = &isTrue Expect(canProvision(inventory, activePolicy)).Should(BeFalse()) + // check nil policy + Expect(canProvision(inventory, nil)).Should(BeTrue()) // override policy setting isFalse := false inventory.Spec.Policy = &v1beta1.DBaaSInventoryPolicy{DisableProvisions: &isFalse} Expect(canProvision(inventory, activePolicy)).Should(BeTrue()) - - // check nil policy - Expect(canProvision(inventory, nil)).Should(BeFalse()) }) It("should, upon deletion, make another policy active", func() { diff --git a/controllers/dbaasinventory_controller.go b/controllers/dbaasinventory_controller.go index 30ef0039..e2bcf298 100644 --- a/controllers/dbaasinventory_controller.go +++ b/controllers/dbaasinventory_controller.go @@ -72,34 +72,6 @@ func (r *DBaaSInventoryReconciler) Reconcile(ctx context.Context, req ctrl.Reque event = metrics.LabelEventValueCreate } - policyList, err := r.policyListByNS(ctx, req.Namespace) - if err != nil { - logger.Error(err, "unable to list policies") - metricLabelErrCdValue = metrics.LabelErrorCdValueUnableToListPolicies - return ctrl.Result{}, err - } - activePolicy := getActivePolicy(policyList) - if activePolicy == nil { - logger.Info("No DBaaSPolicy found for the target namespace", "Namespace", req.Namespace) - cond := metav1.Condition{ - Type: v1beta1.DBaaSInventoryReadyType, - Status: metav1.ConditionFalse, - Reason: v1beta1.DBaaSPolicyNotFound, - Message: v1beta1.MsgPolicyNotFound, - } - apimeta.SetStatusCondition(&inventory.Status.Conditions, cond) - if err := r.Client.Status().Update(ctx, &inventory); err != nil { - if errors.IsConflict(err) { - logger.V(1).Info("DBaaS Inventory resource modified, retry syncing status", "DBaaS Inventory", inventory) - return ctrl.Result{Requeue: true}, nil - } - logger.Error(err, "Error updating the DBaaS Inventory resource status", "DBaaS Inventory", inventory) - metricLabelErrCdValue = metrics.LabelErrorCdValueErrorUpdatingInventoryStatus - return ctrl.Result{}, err - } - return ctrl.Result{}, nil - } - if err := r.checkCredsRefLabel(ctx, inventory); err != nil { if errors.IsConflict(err) { return ctrl.Result{Requeue: true}, nil diff --git a/controllers/dbaasinventory_controller_test.go b/controllers/dbaasinventory_controller_test.go index ebeec9d5..5a5252eb 100644 --- a/controllers/dbaasinventory_controller_test.go +++ b/controllers/dbaasinventory_controller_test.go @@ -32,32 +32,6 @@ var _ = Describe("DBaaSInventory controller with errors", func() { ns := "testns-no-policy" nsSpec := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: ns}} BeforeEach(assertResourceCreationIfNotExists(nsSpec)) - Context("after creating DBaaSInventory without policy in the target namespace", func() { - inventoryName := "test-inventory-no-policy" - testSecret2 := testSecret.DeepCopy() - testSecret2.Namespace = ns - DBaaSInventorySpec := &v1beta1.DBaaSInventorySpec{ - CredentialsRef: &v1beta1.LocalObjectReference{ - Name: testSecret2.Name, - }, - } - testCreatedDBaaSInventory := &v1beta1.DBaaSInventory{ - ObjectMeta: metav1.ObjectMeta{ - Name: inventoryName, - Namespace: ns, - }, - Spec: v1beta1.DBaaSOperatorInventorySpec{ - ProviderRef: v1beta1.NamespacedName{ - Name: testProviderName, - }, - DBaaSInventorySpec: *DBaaSInventorySpec, - }, - } - BeforeEach(assertResourceCreationIfNotExists(testSecret2)) - BeforeEach(assertResourceCreationIfNotExists(testCreatedDBaaSInventory)) - It("reconcile with error", assertDBaaSResourceStatusUpdated(testCreatedDBaaSInventory, metav1.ConditionFalse, v1beta1.DBaaSPolicyNotFound)) - }) - Context("after creating DBaaSInventory without valid provider", func() { inventoryName := "test-inventory-no-provider" providerName := "provider-no-exist" diff --git a/controllers/dbaasplatform_controller_test.go b/controllers/dbaasplatform_controller_test.go index 4ca95ce9..8fedfd45 100644 --- a/controllers/dbaasplatform_controller_test.go +++ b/controllers/dbaasplatform_controller_test.go @@ -59,10 +59,7 @@ var _ = Describe("DBaaSPlatform controller", func() { }, } err := dRec.Get(ctx, client.ObjectKeyFromObject(secret), secret) - if err != nil { - return false - } - return true + return err == nil }, timeout).Should(BeTrue()) Eventually(func() bool { @@ -73,10 +70,7 @@ var _ = Describe("DBaaSPlatform controller", func() { }, } err := dRec.Get(ctx, client.ObjectKeyFromObject(cm), cm) - if err != nil { - return false - } - return true + return err == nil }, timeout).Should(BeTrue()) }) })