From 5e93ee2ba9800f6a1b03ff81299ba604a598af91 Mon Sep 17 00:00:00 2001 From: KubeKyrie Date: Mon, 2 Dec 2024 18:07:51 +0800 Subject: [PATCH] fix set secret OwnerReferences Signed-off-by: KubeKyrie --- pkg/util/cluster.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/pkg/util/cluster.go b/pkg/util/cluster.go index fc8296b3c..37316a29f 100644 --- a/pkg/util/cluster.go +++ b/pkg/util/cluster.go @@ -84,8 +84,15 @@ func UpdateOwnReference(client kubernetes.Interface, configMapList []*apis.Confi } return err // not ignore } - if len(cm.OwnerReferences) != 0 { - continue // do nothing + hasSet := false + for _, ownerRef := range cm.OwnerReferences { + if ownerRef.UID == belongToReference.UID { + hasSet = true + break + } + } + if hasSet { + continue } // cm belongs to `Cluster` cm.OwnerReferences = append(cm.OwnerReferences, belongToReference) @@ -104,8 +111,15 @@ func UpdateOwnReference(client kubernetes.Interface, configMapList []*apis.Confi } return err // not ignore } - if len(secret.OwnerReferences) != 0 { - continue // do nothing + hasSet := false + for _, ownerRef := range secret.OwnerReferences { + if ownerRef.UID == belongToReference.UID { + hasSet = true + break + } + } + if hasSet { + continue } secret.OwnerReferences = append(secret.OwnerReferences, belongToReference) if _, err := client.CoreV1().Secrets(ref.NameSpace).Update(context.Background(), secret, metav1.UpdateOptions{}); err != nil {