Browse Source

refactor and add unit test for skipping cluster secret store

Eric Chan 4 years ago
parent
commit
4055e7d186

+ 5 - 1
pkg/controllers/externalsecret/externalsecret_controller.go

@@ -109,7 +109,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
 		return ctrl.Result{}, nil
 	}
 
-	if !r.ClusterSecretStoreEnabled && externalSecret.Spec.SecretStoreRef.Kind == esv1beta1.ClusterSecretStoreKind {
+	if shouldSkipClusterSecretStore(r, externalSecret) {
 		log.Info("skipping cluster secret store as it is disabled")
 		return ctrl.Result{}, nil
 	}
@@ -326,6 +326,10 @@ func hashMeta(m metav1.ObjectMeta) string {
 	})
 }
 
+func shouldSkipClusterSecretStore(r *Reconciler, es esv1beta1.ExternalSecret) bool {
+	return !r.ClusterSecretStoreEnabled && es.Spec.SecretStoreRef.Kind == esv1beta1.ClusterSecretStoreKind
+}
+
 func shouldRefresh(es esv1beta1.ExternalSecret) bool {
 	// refresh if resource version changed
 	if es.Status.SyncedResourceVersion != getResourceVersion(es) {

+ 17 - 0
pkg/controllers/externalsecret/externalsecret_controller_test.go

@@ -971,6 +971,22 @@ var _ = Describe("ExternalSecret controller", func() {
 		}
 	}
 
+	ignoreClusterSecretStoreWhenDisabled := func(tc *testCase) {
+		tc.externalSecret.Spec.SecretStoreRef.Kind = esv1beta1.ClusterSecretStoreKind
+
+		Expect(shouldSkipClusterSecretStore(
+			&Reconciler{
+				ClusterSecretStoreEnabled: false,
+			},
+			*tc.externalSecret,
+		)).To(BeTrue())
+
+		tc.checkCondition = func(es *esv1beta1.ExternalSecret) bool {
+			cond := GetExternalSecretCondition(es.Status, esv1beta1.ExternalSecretReady)
+			return cond == nil
+		}
+	}
+
 	// When the ownership is set to owner, and we delete a dependent child kind=secret
 	// it should be recreated without waiting for refresh interval
 	checkDeletion := func(tc *testCase) {
@@ -1113,6 +1129,7 @@ var _ = Describe("ExternalSecret controller", func() {
 		Entry("should set an error condition when store does not exist", storeMissingErrCondition),
 		Entry("should set an error condition when store provider constructor fails", storeConstructErrCondition),
 		Entry("should not process store with mismatching controller field", ignoreMismatchController),
+		Entry("should not process cluster secret store when it is disabled", ignoreClusterSecretStoreWhenDisabled),
 	)
 })