@@ -28,6 +28,7 @@ import (
28
28
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/k8s/client"
29
29
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/imageregistries"
30
30
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/mirrors"
31
+ handlersutils "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/utils"
31
32
)
32
33
33
34
type imageRegistriesPatchHandler struct {
@@ -99,6 +100,15 @@ func (h *imageRegistriesPatchHandler) Mutate(
99
100
return globalMirrorErr
100
101
}
101
102
103
+ cluster , err := clusterGetter (ctx )
104
+ if err != nil {
105
+ log .Error (
106
+ err ,
107
+ "failed to get cluster from Image Registry Credentials mutation handler" ,
108
+ )
109
+ return err
110
+ }
111
+
102
112
registriesWithOptionalCredentials := make ([]providerConfig , 0 , len (imageRegistries ))
103
113
for _ , imageRegistry := range imageRegistries {
104
114
registryWithOptionalCredentials , generateErr := registryWithOptionalCredentialsFromImageRegistryCredentials (
@@ -115,6 +125,23 @@ func (h *imageRegistriesPatchHandler) Mutate(
115
125
registriesWithOptionalCredentials ,
116
126
registryWithOptionalCredentials ,
117
127
)
128
+
129
+ if secretName := secretNameForImageRegistryCredentials (imageRegistry .Credentials ); secretName != "" {
130
+ // Ensure the Secret is owned by the Cluster so it is correctly moved and deleted with the Cluster.
131
+ // This code assumes that Secret exists and that was validated before calling this function.
132
+ err := handlersutils .EnsureOwnerRefForSecret (
133
+ ctx ,
134
+ h .client ,
135
+ secretName ,
136
+ cluster ,
137
+ )
138
+ if err != nil {
139
+ return fmt .Errorf (
140
+ "error updating owner references on image registry Secret: %w" ,
141
+ err ,
142
+ )
143
+ }
144
+ }
118
145
}
119
146
120
147
if globalMirrorErr == nil {
@@ -131,6 +158,23 @@ func (h *imageRegistriesPatchHandler) Mutate(
131
158
registriesWithOptionalCredentials ,
132
159
mirrorCredentials ,
133
160
)
161
+
162
+ if secretName := secretNameForImageRegistryCredentials (globalMirror .Credentials ); secretName != "" {
163
+ // Ensure the Secret is owned by the Cluster so it is correctly moved and deleted with the Cluster.
164
+ // This code assumes that Secret exists and that was validated before calling this function.
165
+ err := handlersutils .EnsureOwnerRefForSecret (
166
+ ctx ,
167
+ h .client ,
168
+ secretName ,
169
+ cluster ,
170
+ )
171
+ if err != nil {
172
+ return fmt .Errorf (
173
+ "error updating owner references on global mirror Secret: %w" ,
174
+ err ,
175
+ )
176
+ }
177
+ }
134
178
}
135
179
136
180
needCredentials , err := needImageRegistryCredentialsConfiguration (
@@ -173,15 +217,6 @@ func (h *imageRegistriesPatchHandler) Mutate(
173
217
commands ... ,
174
218
)
175
219
176
- cluster , err := clusterGetter (ctx )
177
- if err != nil {
178
- log .Error (
179
- err ,
180
- "failed to get cluster from Image Registry Credentials mutation handler" ,
181
- )
182
- return err
183
- }
184
-
185
220
generateErr = createSecretIfNeeded (ctx , h .client , registriesWithOptionalCredentials , cluster )
186
221
if generateErr != nil {
187
222
return generateErr
@@ -226,15 +261,6 @@ func (h *imageRegistriesPatchHandler) Mutate(
226
261
).Info ("adding PreKubeadmCommands to worker node kubeadm config template" )
227
262
obj .Spec .Template .Spec .PreKubeadmCommands = append (obj .Spec .Template .Spec .PreKubeadmCommands , commands ... )
228
263
229
- cluster , err := clusterGetter (ctx )
230
- if err != nil {
231
- log .Error (
232
- err ,
233
- "failed to get cluster from Image Registry Credentials mutation handler" ,
234
- )
235
- return err
236
- }
237
-
238
264
generateErr := createSecretIfNeeded (ctx , h .client , registriesWithOptionalCredentials , cluster )
239
265
if generateErr != nil {
240
266
return generateErr
@@ -390,12 +416,13 @@ func secretForImageRegistryCredentials(
390
416
credentials * v1alpha1.RegistryCredentials ,
391
417
objectNamespace string ,
392
418
) (* corev1.Secret , error ) {
393
- if credentials == nil || credentials .SecretRef == nil {
419
+ name := secretNameForImageRegistryCredentials (credentials )
420
+ if name == "" {
394
421
return nil , nil
395
422
}
396
423
397
424
key := ctrlclient.ObjectKey {
398
- Name : credentials . SecretRef . Name ,
425
+ Name : name ,
399
426
Namespace : objectNamespace ,
400
427
}
401
428
secret := & corev1.Secret {}
@@ -434,3 +461,12 @@ func needImageRegistryCredentialsConfiguration(configs []providerConfig) (bool,
434
461
435
462
return true , nil
436
463
}
464
+
465
+ // secretForImageRegistryCredentials returns the name of the Secret for the given RegistryCredentials.
466
+ // Returns an empty string if the credentials or secret field is empty.
467
+ func secretNameForImageRegistryCredentials (credentials * v1alpha1.RegistryCredentials ) string {
468
+ if credentials == nil || credentials .SecretRef == nil || credentials .SecretRef .Name == "" {
469
+ return ""
470
+ }
471
+ return credentials .SecretRef .Name
472
+ }
0 commit comments