@@ -65,76 +65,53 @@ func (r *S3UserReconciler) addPoliciesToUser(
6565func (r * S3UserReconciler ) getUserLinkedSecrets (
6666 ctx context.Context ,
6767 userResource * s3v1alpha1.S3User ,
68- ) ([]corev1.Secret , error ) {
68+ ) ([]corev1.Secret , * corev1. Secret , error ) {
6969 logger := log .FromContext (ctx )
7070
7171 // Listing every secrets in the S3User's namespace, as a first step
7272 // to get the actual secret matching the S3User proper.
7373 // TODO : proper label matching ?
7474 secretsList := & corev1.SecretList {}
7575
76- userSecretList := []corev1.Secret {}
76+ userOwnedSecretList := []corev1.Secret {}
7777
7878 err := r .List (ctx , secretsList , client .InNamespace (userResource .Namespace ))
7979 if err != nil {
8080 logger .Error (err , "An error occurred while listing the secrets in user's namespace" )
81- return userSecretList , fmt .Errorf ("SecretListingFailed" )
81+ return userOwnedSecretList , nil , fmt .Errorf ("SecretListingFailed" )
8282 }
8383
8484 if len (secretsList .Items ) == 0 {
8585 logger .Info ("The user's namespace doesn't appear to contain any secret" )
86- return userSecretList , nil
86+ return userOwnedSecretList , nil , nil
8787 }
8888 // In all the secrets inside the S3User's namespace, one should have an owner reference
8989 // pointing to the S3User. For that specific secret, we check if its name matches the one from
9090 // the S3User, whether explicit (userResource.Spec.SecretName) or implicit (userResource.Name)
9191 // In case of mismatch, that secret is deleted (and will be recreated) ; if there is a match,
9292 // it will be used for state comparison.
93+ // We also check for secret not owned by the resource but with a name matching the configured
94+ // or default one. If such a secret is found it will be returned separately as it is to be
95+ // handled differently.
9396 uid := userResource .GetUID ()
9497
98+ var secretConfiguredName string = userResource .Spec .SecretName
99+ var secretDefaultName string = userResource .Name
100+ var notOwnedConfiguredSecret * corev1.Secret
95101 // cmp.Or takes the first non "zero" value, see https://pkg.go.dev/cmp#Or
96102 for _ , secret := range secretsList .Items {
97103 for _ , ref := range secret .OwnerReferences {
98104 if ref .UID == uid {
99- userSecretList = append (userSecretList , secret )
105+ userOwnedSecretList = append (userOwnedSecretList , secret )
106+ } else if secret .Name == secretConfiguredName {
107+ notOwnedConfiguredSecret = & secret
108+ } else if secret .Name == secretDefaultName && notOwnedConfiguredSecret == nil {
109+ notOwnedConfiguredSecret = & secret
100110 }
101111 }
102112 }
103113
104- return userSecretList , nil
105- }
106-
107-
108- func (r * S3UserReconciler ) getUserUnlinkedSecret (
109- ctx context.Context ,
110- namespace string ,
111- secretNameA string ,
112- secretNameB string ,
113- ) (* corev1.Secret , error ) {
114- logger := log .FromContext (ctx )
115- // Listing every secrets in the S3User's namespace, as a first step
116- // to get the actual secret matching the S3User proper.
117- // TODO : proper label matching ?
118- secretsList := & corev1.SecretList {}
119- err := r .List (ctx , secretsList , client .InNamespace (namespace ))
120- if err != nil {
121- logger .Error (err , "An error occurred while listing the secrets in user's namespace" )
122- return nil , fmt .Errorf ("SecretListingFailed" )
123- }
124- if len (secretsList .Items ) == 0 {
125- logger .Info ("The user's namespace doesn't appear to contain any secret" )
126- return nil , nil
127- }
128-
129- var secretB * corev1.Secret
130- for _ , secret := range secretsList .Items {
131- if secret .Name == secretNameA {
132- return & secret , nil
133- } else if secret .Name == secretNameB {
134- secretB = & secret
135- }
136- }
137- return secretB , nil
114+ return userOwnedSecretList , notOwnedConfiguredSecret , nil
138115}
139116
140117func (r * S3UserReconciler ) deleteSecret (ctx context.Context , secret * corev1.Secret ) error {
0 commit comments