Explorar o código

chore: refactor get-es-conditions func

Signed-off-by: Moritz Johner <beller.moritz@googlemail.com>
Moritz Johner hai 3 meses
pai
achega
b7d35f3bbc

+ 28 - 0
apis/externalsecrets/v1/conditions.go

@@ -0,0 +1,28 @@
+/*
+Copyright © 2025 ESO Maintainer Team
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    https://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package v1
+
+// GetExternalSecretCondition returns the condition with the provided type.
+func GetExternalSecretCondition(status ExternalSecretStatus, condType ExternalSecretConditionType) *ExternalSecretStatusCondition {
+	for _, c := range status.Conditions {
+		if c.Type == condType {
+			return &c
+		}
+	}
+
+	return nil
+}

+ 69 - 0
apis/externalsecrets/v1/conditions_test.go

@@ -0,0 +1,69 @@
+/*
+Copyright © 2025 ESO Maintainer Team
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    https://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package v1
+
+import (
+	"testing"
+
+	"github.com/google/go-cmp/cmp"
+	corev1 "k8s.io/api/core/v1"
+)
+
+func TestGetExternalSecretCondition(t *testing.T) {
+	status := ExternalSecretStatus{
+		Conditions: []ExternalSecretStatusCondition{
+			{
+				Type:   ExternalSecretReady,
+				Status: corev1.ConditionFalse,
+			},
+			{
+				Type:   ExternalSecretReady,
+				Status: corev1.ConditionTrue,
+			},
+		},
+	}
+
+	tests := []struct {
+		name     string
+		condType ExternalSecretConditionType
+		expected *ExternalSecretStatusCondition
+	}{
+		{
+			name:     "Status has a condition of the specified type",
+			condType: ExternalSecretReady,
+			expected: &ExternalSecretStatusCondition{
+				Type:   ExternalSecretReady,
+				Status: corev1.ConditionFalse,
+			},
+		},
+		{
+			name:     "Status does not have a condition of the specified type",
+			condType: ExternalSecretDeleted,
+			expected: nil,
+		},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			got := GetExternalSecretCondition(status, tt.condType)
+
+			if diff := cmp.Diff(tt.expected, got); diff != "" {
+				t.Errorf("(-got, +want)\n%s", diff)
+			}
+		})
+	}
+}

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

@@ -800,7 +800,7 @@ func (r *Reconciler) getRequeueResult(externalSecret *esv1.ExternalSecret) ctrl.
 }
 
 func (r *Reconciler) markAsDone(externalSecret *esv1.ExternalSecret, start time.Time, log logr.Logger, reason, msg string) {
-	oldReadyCondition := GetExternalSecretCondition(externalSecret.Status, esv1.ExternalSecretReady)
+	oldReadyCondition := esv1.GetExternalSecretCondition(externalSecret.Status, esv1.ExternalSecretReady)
 	newReadyCondition := NewExternalSecretCondition(esv1.ExternalSecretReady, v1.ConditionTrue, reason, msg)
 	SetExternalSecretCondition(externalSecret, *newReadyCondition)
 

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

@@ -280,7 +280,7 @@ var _ = Describe("ExternalSecret controller", Serial, func() {
 			// default condition: es should be ready
 			targetSecretName: ExternalSecretTargetSecretName,
 			checkCondition: func(es *esv1.ExternalSecret) bool {
-				cond := GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
+				cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
 				if cond == nil || cond.Status != v1.ConditionTrue {
 					return false
 				}
@@ -1659,14 +1659,14 @@ var _ = Describe("ExternalSecret controller", Serial, func() {
 			"bar/foo": []byte(BarValue),
 		}, nil)
 		tc.checkCondition = func(es *esv1.ExternalSecret) bool {
-			cond := GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
+			cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
 			if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
 				return false
 			}
 			return true
 		}
 		tc.checkCondition = func(es *esv1.ExternalSecret) bool {
-			cond := GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
+			cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
 			if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
 				return false
 			}
@@ -1708,14 +1708,14 @@ var _ = Describe("ExternalSecret controller", Serial, func() {
 			"bar/foo": []byte(BarValue),
 		}, nil)
 		tc.checkCondition = func(es *esv1.ExternalSecret) bool {
-			cond := GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
+			cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
 			if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
 				return false
 			}
 			return true
 		}
 		tc.checkCondition = func(es *esv1.ExternalSecret) bool {
-			cond := GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
+			cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
 			if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
 				return false
 			}
@@ -1847,7 +1847,7 @@ var _ = Describe("ExternalSecret controller", Serial, func() {
 		fakeProvider.WithGetSecret(nil, errors.New("boom"))
 		tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Millisecond * 100}
 		tc.checkCondition = func(es *esv1.ExternalSecret) bool {
-			cond := GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
+			cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
 			if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
 				return false
 			}
@@ -1871,7 +1871,7 @@ var _ = Describe("ExternalSecret controller", Serial, func() {
 					return false
 				}
 				// condition must now be true!
-				cond := GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
+				cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
 				if cond == nil && cond.Status != v1.ConditionTrue {
 					return false
 				}
@@ -1885,7 +1885,7 @@ var _ = Describe("ExternalSecret controller", Serial, func() {
 	storeMissingErrCondition := func(tc *testCase) {
 		tc.externalSecret.Spec.SecretStoreRef.Name = "nonexistent"
 		tc.checkCondition = func(es *esv1.ExternalSecret) bool {
-			cond := GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
+			cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
 			if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
 				return false
 			}
@@ -1911,7 +1911,7 @@ var _ = Describe("ExternalSecret controller", Serial, func() {
 		})
 		tc.checkCondition = func(es *esv1.ExternalSecret) bool {
 			// condition must be false
-			cond := GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
+			cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
 			if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
 				return false
 			}
@@ -1933,7 +1933,7 @@ var _ = Describe("ExternalSecret controller", Serial, func() {
 	ignoreMismatchController := func(tc *testCase) {
 		tc.secretStore.GetSpec().Controller = "nop"
 		tc.checkCondition = func(es *esv1.ExternalSecret) bool {
-			cond := GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
+			cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
 			return cond == nil
 		}
 		tc.checkExternalSecret = func(_ *esv1.ExternalSecret) {
@@ -1968,7 +1968,7 @@ var _ = Describe("ExternalSecret controller", Serial, func() {
 		)).To(BeTrue())
 
 		tc.checkCondition = func(es *esv1.ExternalSecret) bool {
-			cond := GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
+			cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
 			return cond == nil
 		}
 	}
@@ -2158,7 +2158,7 @@ var _ = Describe("ExternalSecret controller", Serial, func() {
 		}
 
 		tc.checkCondition = func(es *esv1.ExternalSecret) bool {
-			cond := GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
+			cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
 			if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
 				return false
 			}
@@ -2175,7 +2175,7 @@ var _ = Describe("ExternalSecret controller", Serial, func() {
 		}
 
 		tc.checkCondition = func(es *esv1.ExternalSecret) bool {
-			cond := GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
+			cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
 			if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
 				return false
 			}
@@ -2195,7 +2195,7 @@ var _ = Describe("ExternalSecret controller", Serial, func() {
 		}
 
 		tc.checkCondition = func(es *esv1.ExternalSecret) bool {
-			cond := GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
+			cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
 			if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
 				return false
 			}
@@ -2237,7 +2237,7 @@ var _ = Describe("ExternalSecret controller", Serial, func() {
 		}
 
 		tc.checkCondition = func(es *esv1.ExternalSecret) bool {
-			cond := GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
+			cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
 			if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
 				return false
 			}
@@ -2267,7 +2267,7 @@ var _ = Describe("ExternalSecret controller", Serial, func() {
 		}
 
 		tc.checkCondition = func(es *esv1.ExternalSecret) bool {
-			cond := GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
+			cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
 			if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
 				return false
 			}
@@ -2319,7 +2319,7 @@ var _ = Describe("ExternalSecret controller", Serial, func() {
 		}
 
 		tc.checkCondition = func(es *esv1.ExternalSecret) bool {
-			cond := GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
+			cond := esv1.GetExternalSecretCondition(es.Status, esv1.ExternalSecretReady)
 			if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1.ConditionReasonSecretSyncedError {
 				return false
 			}

+ 1 - 11
pkg/controllers/externalsecret/util.go

@@ -38,20 +38,10 @@ func NewExternalSecretCondition(condType esv1.ExternalSecretConditionType, statu
 	}
 }
 
-// GetExternalSecretCondition returns the condition with the provided type.
-func GetExternalSecretCondition(status esv1.ExternalSecretStatus, condType esv1.ExternalSecretConditionType) *esv1.ExternalSecretStatusCondition {
-	for _, c := range status.Conditions {
-		if c.Type == condType {
-			return &c
-		}
-	}
-	return nil
-}
-
 // SetExternalSecretCondition updates the external secret to include the provided
 // condition.
 func SetExternalSecretCondition(es *esv1.ExternalSecret, condition esv1.ExternalSecretStatusCondition) {
-	currentCond := GetExternalSecretCondition(es.Status, condition.Type)
+	currentCond := esv1.GetExternalSecretCondition(es.Status, condition.Type)
 
 	if currentCond != nil && currentCond.Status == condition.Status &&
 		currentCond.Reason == condition.Reason && currentCond.Message == condition.Message {

+ 0 - 45
pkg/controllers/externalsecret/util_test.go

@@ -27,51 +27,6 @@ import (
 	esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
 )
 
-func TestGetExternalSecretCondition(t *testing.T) {
-	status := esv1.ExternalSecretStatus{
-		Conditions: []esv1.ExternalSecretStatusCondition{
-			{
-				Type:   esv1.ExternalSecretReady,
-				Status: corev1.ConditionFalse,
-			},
-			{
-				Type:   esv1.ExternalSecretReady,
-				Status: corev1.ConditionTrue,
-			},
-		},
-	}
-
-	tests := []struct {
-		name     string
-		condType esv1.ExternalSecretConditionType
-		expected *esv1.ExternalSecretStatusCondition
-	}{
-		{
-			name:     "Status has a condition of the specified type",
-			condType: esv1.ExternalSecretReady,
-			expected: &esv1.ExternalSecretStatusCondition{
-				Type:   esv1.ExternalSecretReady,
-				Status: corev1.ConditionFalse,
-			},
-		},
-		{
-			name:     "Status does not have a condition of the specified type",
-			condType: esv1.ExternalSecretDeleted,
-			expected: nil,
-		},
-	}
-
-	for _, tt := range tests {
-		t.Run(tt.name, func(t *testing.T) {
-			got := GetExternalSecretCondition(status, tt.condType)
-
-			if diff := cmp.Diff(tt.expected, got); diff != "" {
-				t.Errorf("(-got, +want)\n%s", diff)
-			}
-		})
-	}
-}
-
 func TestSetExternalSecretCondition(t *testing.T) {
 	now := time.Now()