@@ -23,7 +23,7 @@ import (
2323
2424 "k8s.io/apimachinery/pkg/util/rand"
2525
26- v1 "k8s.io/api/authentication/v1"
26+ authv1 "k8s.io/api/authentication/v1"
2727)
2828
2929const (
@@ -80,7 +80,7 @@ func NormalizeCredentials(credentialsJSON json.RawMessage) (map[string][]byte, [
8080 return normalized , metadata , nil
8181}
8282
83- func BuildUserInfo (ctx context.Context , userInfo * v1 .UserInfo ) string {
83+ func BuildUserInfo (ctx context.Context , userInfo * authv1 .UserInfo ) string {
8484 log := logutils .GetLogger (ctx )
8585 if userInfo == nil {
8686 return ""
@@ -177,13 +177,25 @@ func RemoveAnnotations(ctx context.Context, k8sClient client.Client, object comm
177177
178178func AddWatchForSecretIfNeeded (ctx context.Context , k8sClient client.Client , secret * corev1.Secret , instanceUID string ) error {
179179 log := logutils .GetLogger (ctx )
180+ updateRequired := false
180181 if secret .Annotations == nil {
181182 secret .Annotations = make (map [string ]string )
182183 }
183184 if len (secret .Annotations [common .WatchSecretAnnotation + string (instanceUID )]) == 0 {
184- log .Info (fmt .Sprintf ("adding secret watch for secret %s" , secret .Name ))
185+ log .Info (fmt .Sprintf ("adding secret watch annotation for instance %s on secret %s" , instanceUID , secret .Name ))
185186 secret .Annotations [common .WatchSecretAnnotation + instanceUID ] = "true"
187+ updateRequired = true
188+ }
189+ if secret .Labels == nil {
190+ secret .Labels = make (map [string ]string )
191+ }
192+ if secret .Labels [common .WatchSecretLabel ] != "true" {
193+ log .Info (fmt .Sprintf ("adding watch label for secret %s" , secret .Name ))
194+ secret .Labels [common .WatchSecretLabel ] = "true"
186195 controllerutil .AddFinalizer (secret , common .FinalizerName )
196+ updateRequired = true
197+ }
198+ if updateRequired {
187199 return k8sClient .Update (ctx , secret )
188200 }
189201
@@ -197,19 +209,22 @@ func RemoveWatchForSecret(ctx context.Context, k8sClient client.Client, secretKe
197209 }
198210
199211 delete (secret .Annotations , common .WatchSecretAnnotation + instanceUID )
200- if ! IsSecretWatched (secret .Annotations ) {
212+ existInstanceAnnotation := false
213+ for key := range secret .Annotations {
214+ if strings .HasPrefix (key , common .WatchSecretAnnotation ) {
215+ existInstanceAnnotation = true
216+ break
217+ }
218+ }
219+ if ! existInstanceAnnotation {
220+ delete (secret .Labels , common .WatchSecretLabel )
201221 controllerutil .RemoveFinalizer (secret , common .FinalizerName )
202222 }
203223 return k8sClient .Update (ctx , secret )
204224}
205225
206- func IsSecretWatched (secretAnnotations map [string ]string ) bool {
207- for key := range secretAnnotations {
208- if strings .HasPrefix (key , common .WatchSecretAnnotation ) {
209- return true
210- }
211- }
212- return false
226+ func IsSecretWatched (secret client.Object ) bool {
227+ return secret .GetLabels () != nil && secret .GetLabels ()[common .WatchSecretLabel ] == "true"
213228}
214229
215230func GetLabelKeyForInstanceSecret (secretName string ) string {
0 commit comments