@@ -100,15 +100,6 @@ func (h *imageRegistriesPatchHandler) Mutate(
100
100
return globalMirrorErr
101
101
}
102
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
-
112
103
registriesWithOptionalCredentials := make ([]providerConfig , 0 , len (imageRegistries ))
113
104
for _ , imageRegistry := range imageRegistries {
114
105
registryWithOptionalCredentials , generateErr := registryWithOptionalCredentialsFromImageRegistryCredentials (
@@ -125,23 +116,6 @@ func (h *imageRegistriesPatchHandler) Mutate(
125
116
registriesWithOptionalCredentials ,
126
117
registryWithOptionalCredentials ,
127
118
)
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 .EnsureOwnerReferenceForSecret (
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
- }
145
119
}
146
120
147
121
if globalMirrorErr == nil {
@@ -158,23 +132,6 @@ func (h *imageRegistriesPatchHandler) Mutate(
158
132
registriesWithOptionalCredentials ,
159
133
mirrorCredentials ,
160
134
)
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 .EnsureOwnerReferenceForSecret (
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
- }
178
135
}
179
136
180
137
needCredentials , err := needImageRegistryCredentialsConfiguration (
@@ -217,9 +174,23 @@ func (h *imageRegistriesPatchHandler) Mutate(
217
174
commands ... ,
218
175
)
219
176
220
- generateErr = createSecretIfNeeded (ctx , h .client , registriesWithOptionalCredentials , cluster )
221
- if generateErr != nil {
222
- return generateErr
177
+ cluster , err := clusterGetter (ctx )
178
+ if err != nil {
179
+ log .Error (
180
+ err ,
181
+ "failed to get cluster from Image Registry Credentials mutation handler" ,
182
+ )
183
+ return err
184
+ }
185
+
186
+ err = ensureOwnerReferenceOnCredentialsSecrets (ctx , h .client , imageRegistries , globalMirror , cluster )
187
+ if err != nil {
188
+ return err
189
+ }
190
+
191
+ err = createSecretIfNeeded (ctx , h .client , registriesWithOptionalCredentials , cluster )
192
+ if err != nil {
193
+ return err
223
194
}
224
195
225
196
initConfiguration := obj .Spec .Template .Spec .KubeadmConfigSpec .InitConfiguration
@@ -261,9 +232,23 @@ func (h *imageRegistriesPatchHandler) Mutate(
261
232
).Info ("adding PreKubeadmCommands to worker node kubeadm config template" )
262
233
obj .Spec .Template .Spec .PreKubeadmCommands = append (obj .Spec .Template .Spec .PreKubeadmCommands , commands ... )
263
234
264
- generateErr := createSecretIfNeeded (ctx , h .client , registriesWithOptionalCredentials , cluster )
265
- if generateErr != nil {
266
- return generateErr
235
+ cluster , err := clusterGetter (ctx )
236
+ if err != nil {
237
+ log .Error (
238
+ err ,
239
+ "failed to get cluster from Image Registry Credentials mutation handler" ,
240
+ )
241
+ return err
242
+ }
243
+
244
+ err = ensureOwnerReferenceOnCredentialsSecrets (ctx , h .client , imageRegistries , globalMirror , cluster )
245
+ if err != nil {
246
+ return err
247
+ }
248
+
249
+ err = createSecretIfNeeded (ctx , h .client , registriesWithOptionalCredentials , cluster )
250
+ if err != nil {
251
+ return err
267
252
}
268
253
269
254
joinConfiguration := obj .Spec .Template .Spec .JoinConfiguration
@@ -284,6 +269,45 @@ func (h *imageRegistriesPatchHandler) Mutate(
284
269
return nil
285
270
}
286
271
272
+ func ensureOwnerReferenceOnCredentialsSecrets (
273
+ ctx context.Context ,
274
+ c ctrlclient.Client ,
275
+ imageRegistries []v1alpha1.ImageRegistry ,
276
+ globalMirror v1alpha1.GlobalImageRegistryMirror ,
277
+ cluster * clusterv1.Cluster ,
278
+ ) error {
279
+ var credentials []* v1alpha1.RegistryCredentials
280
+ for _ , imageRegistry := range imageRegistries {
281
+ if imageRegistry .Credentials != nil {
282
+ credentials = append (credentials , imageRegistry .Credentials )
283
+ }
284
+ }
285
+ if globalMirror .Credentials != nil {
286
+ credentials = append (credentials , globalMirror .Credentials )
287
+ }
288
+
289
+ for _ , credential := range credentials {
290
+ if secretName := secretNameForImageRegistryCredentials (credential ); secretName != "" {
291
+ // Ensure the Secret is owned by the Cluster so it is correctly moved and deleted with the Cluster.
292
+ // This code assumes that Secret exists and that was validated before calling this function.
293
+ err := handlersutils .EnsureOwnerReferenceForSecret (
294
+ ctx ,
295
+ c ,
296
+ secretName ,
297
+ cluster ,
298
+ )
299
+ if err != nil {
300
+ return fmt .Errorf (
301
+ "error updating owner references on image registry Secret: %w" ,
302
+ err ,
303
+ )
304
+ }
305
+ }
306
+ }
307
+
308
+ return nil
309
+ }
310
+
287
311
func registryWithOptionalCredentialsFromImageRegistryCredentials (
288
312
ctx context.Context ,
289
313
c ctrlclient.Client ,
0 commit comments