Browse Source

fix: cached uid after deleting a store (#6138)

Gergely Bräutigam 2 months ago
parent
commit
6ddc59ffb6
2 changed files with 32 additions and 0 deletions
  1. 15 0
      pkg/controllers/secretstore/common.go
  2. 17 0
      pkg/controllers/secretstore/common_test.go

+ 15 - 0
pkg/controllers/secretstore/common.go

@@ -24,6 +24,7 @@ import (
 
 	"github.com/go-logr/logr"
 	v1 "k8s.io/api/core/v1"
+	apierrors "k8s.io/apimachinery/pkg/api/errors"
 	"k8s.io/apimachinery/pkg/fields"
 	"k8s.io/apimachinery/pkg/types"
 	"k8s.io/client-go/tools/record"
@@ -95,7 +96,21 @@ func reconcile(ctx context.Context, req ctrl.Request, ss esapi.GenericStore, cl
 
 	// patch status when done processing
 	p := client.MergeFrom(ss.Copy())
+	storeUID := ss.GetObjectMeta().UID
 	defer func() {
+		current := ss.Copy()
+		if getErr := cl.Get(ctx, req.NamespacedName, current); getErr != nil {
+			if apierrors.IsNotFound(getErr) {
+				log.V(1).Info("store was deleted, skipping status patch")
+				return
+			}
+			log.Error(getErr, "unable to get store for status patch")
+			return
+		}
+		if current.GetObjectMeta().UID != storeUID {
+			log.V(1).Info("store was replaced, skipping status patch")
+			return
+		}
 		err := cl.Status().Patch(ctx, ss, p)
 		if err != nil {
 			log.Error(err, errPatchStatus)

+ 17 - 0
pkg/controllers/secretstore/common_test.go

@@ -87,6 +87,23 @@ var _ = Describe("SecretStore Controller", func() {
 			spc := tc.store.GetSpec()
 			spc.Controller = "something-else"
 			tc.assert = func() {
+				Eventually(func() bool {
+					ss := tc.store.Copy()
+					err := k8sClient.Get(context.Background(), types.NamespacedName{
+						Name:      defaultStoreName,
+						Namespace: ss.GetObjectMeta().Namespace,
+					}, ss)
+					if err != nil {
+						return false
+					}
+					return ss.GetObjectMeta().GetResourceVersion() != "" &&
+						ss.GetSpec().Controller == "something-else" &&
+						len(ss.GetStatus().Conditions) == 0
+				}).
+					WithTimeout(time.Second*10).
+					WithPolling(time.Millisecond*250).
+					Should(BeTrue(), "cache should reflect the new store with no conditions")
+
 				Consistently(func() bool {
 					ss := tc.store.Copy()
 					err := k8sClient.Get(context.Background(), types.NamespacedName{