@@ -17,6 +17,7 @@ import (
17
17
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
18
18
ctrl "sigs.k8s.io/controller-runtime"
19
19
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
20
+ "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
20
21
21
22
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
22
23
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
@@ -69,7 +70,7 @@ func (h *imageRegistriesPatchHandler) Mutate(
69
70
vars map [string ]apiextensionsv1.JSON ,
70
71
holderRef runtimehooksv1.HolderReference ,
71
72
clusterKey ctrlclient.ObjectKey ,
72
- _ mutation.ClusterGetter ,
73
+ clusterGetter mutation.ClusterGetter ,
73
74
) error {
74
75
log := ctrl .LoggerFrom (ctx ).WithValues (
75
76
"holderRef" , holderRef ,
@@ -151,6 +152,33 @@ func (h *imageRegistriesPatchHandler) Mutate(
151
152
return generateErr
152
153
}
153
154
155
+ credentialsSecret , generateErr := generateCredentialsSecret (
156
+ registriesWithOptionalCredentials ,
157
+ clusterKey .Name ,
158
+ clusterKey .Namespace ,
159
+ )
160
+ if generateErr != nil {
161
+ return fmt .Errorf (
162
+ "error generating credentials Secret for Image Registry Credentials variable: %w" ,
163
+ err ,
164
+ )
165
+ }
166
+
167
+ cluster , err := clusterGetter (ctx )
168
+ if err != nil {
169
+ log .Error (
170
+ err ,
171
+ "failed to get cluster from Image Registry Credentials mutation handler" ,
172
+ )
173
+ return err
174
+ }
175
+ if err = controllerutil .SetOwnerReference (cluster , credentialsSecret , h .client .Scheme ()); err != nil {
176
+ return fmt .Errorf (
177
+ "failed to set owner reference on Image Registry Credentials Secret: %w" ,
178
+ err ,
179
+ )
180
+ }
181
+
154
182
if err := patches .MutateIfApplicable (
155
183
obj , vars , & holderRef , selectors .ControlPlane (), log ,
156
184
func (obj * controlplanev1.KubeadmControlPlaneTemplate ) error {
@@ -172,9 +200,8 @@ func (h *imageRegistriesPatchHandler) Mutate(
172
200
commands ... ,
173
201
)
174
202
175
- generateErr = createSecretIfNeeded (ctx , h .client , registriesWithOptionalCredentials , clusterKey )
176
- if generateErr != nil {
177
- return generateErr
203
+ if err := client .ServerSideApply (ctx , h .client , credentialsSecret , client .ForceOwnership ); err != nil {
204
+ return fmt .Errorf ("failed to apply Image Registry Credentials Secret: %w" , err )
178
205
}
179
206
180
207
initConfiguration := obj .Spec .Template .Spec .KubeadmConfigSpec .InitConfiguration
@@ -216,9 +243,8 @@ func (h *imageRegistriesPatchHandler) Mutate(
216
243
).Info ("adding PreKubeadmCommands to worker node kubeadm config template" )
217
244
obj .Spec .Template .Spec .PreKubeadmCommands = append (obj .Spec .Template .Spec .PreKubeadmCommands , commands ... )
218
245
219
- generateErr := createSecretIfNeeded (ctx , h .client , registriesWithOptionalCredentials , clusterKey )
220
- if generateErr != nil {
221
- return generateErr
246
+ if err := client .ServerSideApply (ctx , h .client , credentialsSecret , client .ForceOwnership ); err != nil {
247
+ return fmt .Errorf ("failed to apply Image Registry Credentials Secret: %w" , err )
222
248
}
223
249
224
250
joinConfiguration := obj .Spec .Template .Spec .JoinConfiguration
@@ -331,32 +357,6 @@ func generateFilesAndCommands(
331
357
return files , commands , err
332
358
}
333
359
334
- func createSecretIfNeeded (
335
- ctx context.Context ,
336
- c ctrlclient.Client ,
337
- registriesWithOptionalCredentials []providerConfig ,
338
- clusterKey ctrlclient.ObjectKey ,
339
- ) error {
340
- credentialsSecret , err := generateCredentialsSecret (
341
- registriesWithOptionalCredentials ,
342
- clusterKey .Name ,
343
- clusterKey .Namespace ,
344
- )
345
- if err != nil {
346
- return fmt .Errorf (
347
- "error generating credentials Secret for Image Registry Credentials variable: %w" ,
348
- err ,
349
- )
350
- }
351
- if credentialsSecret != nil {
352
- if err := client .ServerSideApply (ctx , c , credentialsSecret , client .ForceOwnership ); err != nil {
353
- return fmt .Errorf ("failed to apply Image Registry Credentials Secret: %w" , err )
354
- }
355
- }
356
-
357
- return nil
358
- }
359
-
360
360
// secretForImageRegistryCredentials returns the Secret for the given ImageRegistryCredentials.
361
361
// Returns nil if the secret field is empty.
362
362
func secretForImageRegistryCredentials (
0 commit comments