@@ -24,7 +24,6 @@ import (
24
24
corev1 "k8s.io/api/core/v1"
25
25
apierrors "k8s.io/apimachinery/pkg/api/errors"
26
26
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
27
- kerrors "k8s.io/apimachinery/pkg/util/errors"
28
27
"k8s.io/apiserver/pkg/storage/names"
29
28
"sigs.k8s.io/cluster-api/controllers/topology/internal/check"
30
29
"sigs.k8s.io/cluster-api/controllers/topology/internal/contract"
@@ -70,9 +69,6 @@ func (r *ClusterReconciler) reconcileInfrastructureCluster(ctx context.Context,
70
69
// reconcileControlPlane works to bring the current state of a managed topology in line with the desired state. This involves
71
70
// updating the cluster where needed.
72
71
func (r * ClusterReconciler ) reconcileControlPlane (ctx context.Context , s * scope.Scope ) error {
73
- // Set a default nil return function for the cleanup operation.
74
- cleanup := func () error { return nil }
75
-
76
72
// If the clusterClass mandates the controlPlane has infrastructureMachines, reconcile it.
77
73
if s .Blueprint .HasControlPlaneInfrastructureMachine () {
78
74
ctx , _ := tlog .LoggerFrom (ctx ).WithObject (s .Desired .ControlPlane .InfrastructureMachineTemplate ).Into (ctx )
@@ -83,7 +79,7 @@ func (r *ClusterReconciler) reconcileControlPlane(ctx context.Context, s *scope.
83
79
}
84
80
85
81
// Create or update the MachineInfrastructureTemplate of the control plane.
86
- cleanup , err = r .reconcileReferencedTemplate (ctx , reconcileReferencedTemplateInput {
82
+ err = r .reconcileReferencedTemplate (ctx , reconcileReferencedTemplateInput {
87
83
ref : cpInfraRef ,
88
84
current : s .Current .ControlPlane .InfrastructureMachineTemplate ,
89
85
desired : s .Desired .ControlPlane .InfrastructureMachineTemplate ,
@@ -98,25 +94,17 @@ func (r *ClusterReconciler) reconcileControlPlane(ctx context.Context, s *scope.
98
94
// The controlPlaneObject.Spec.machineTemplate.infrastructureRef has to be updated in the desired object
99
95
err = contract .ControlPlane ().MachineTemplate ().InfrastructureRef ().Set (s .Desired .ControlPlane .Object , refToUnstructured (cpInfraRef ))
100
96
if err != nil {
101
- return kerrors .NewAggregate ([]error {
102
- errors .Wrapf (err , "failed to update %s" , tlog.KObj {Obj : s .Desired .ControlPlane .Object }),
103
- cleanup (),
104
- })
97
+ return errors .Wrapf (err , "failed to update %s" , tlog.KObj {Obj : s .Desired .ControlPlane .Object })
105
98
}
106
99
}
107
100
108
101
// Create or update the ControlPlaneObject for the ControlPlaneState.
109
102
ctx , _ = tlog .LoggerFrom (ctx ).WithObject (s .Desired .ControlPlane .Object ).Into (ctx )
110
103
if err := r .reconcileReferencedObject (ctx , s .Current .ControlPlane .Object , s .Desired .ControlPlane .Object ); err != nil {
111
- return kerrors .NewAggregate ([]error {
112
- errors .Wrapf (err , "failed to update %s" , tlog.KObj {Obj : s .Desired .ControlPlane .Object }),
113
- cleanup (),
114
- })
104
+ return errors .Wrapf (err , "failed to update %s" , tlog.KObj {Obj : s .Desired .ControlPlane .Object })
115
105
}
116
106
117
- // At this point we've updated the ControlPlane object and, where required, the ControlPlane InfrastructureMachineTemplate
118
- // without error. Run the cleanup in order to delete the old InfrastructureMachineTemplate if template rotation was done during update.
119
- return cleanup ()
107
+ return nil
120
108
}
121
109
122
110
// reconcileCluster reconciles the desired state of the Cluster object.
@@ -180,14 +168,14 @@ func (r *ClusterReconciler) createMachineDeployment(ctx context.Context, md *sco
180
168
log := tlog .LoggerFrom (ctx ).WithMachineDeployment (md .Object )
181
169
182
170
ctx , _ = log .WithObject (md .InfrastructureMachineTemplate ).Into (ctx )
183
- if _ , err := r .reconcileReferencedTemplate (ctx , reconcileReferencedTemplateInput {
171
+ if err := r .reconcileReferencedTemplate (ctx , reconcileReferencedTemplateInput {
184
172
desired : md .InfrastructureMachineTemplate ,
185
173
}); err != nil {
186
174
return errors .Wrapf (err , "failed to create %s" , tlog.KObj {Obj : md .Object })
187
175
}
188
176
189
177
ctx , _ = log .WithObject (md .BootstrapTemplate ).Into (ctx )
190
- if _ , err := r .reconcileReferencedTemplate (ctx , reconcileReferencedTemplateInput {
178
+ if err := r .reconcileReferencedTemplate (ctx , reconcileReferencedTemplateInput {
191
179
desired : md .BootstrapTemplate ,
192
180
}); err != nil {
193
181
return errors .Wrapf (err , "failed to create %s" , tlog.KObj {Obj : md .Object })
@@ -206,7 +194,7 @@ func (r *ClusterReconciler) updateMachineDeployment(ctx context.Context, cluster
206
194
log := tlog .LoggerFrom (ctx ).WithMachineDeployment (desiredMD .Object )
207
195
208
196
ctx , _ = log .WithObject (desiredMD .InfrastructureMachineTemplate ).Into (ctx )
209
- if _ , err := r .reconcileReferencedTemplate (ctx , reconcileReferencedTemplateInput {
197
+ if err := r .reconcileReferencedTemplate (ctx , reconcileReferencedTemplateInput {
210
198
ref : & desiredMD .Object .Spec .Template .Spec .InfrastructureRef ,
211
199
current : currentMD .InfrastructureMachineTemplate ,
212
200
desired : desiredMD .InfrastructureMachineTemplate ,
@@ -217,7 +205,7 @@ func (r *ClusterReconciler) updateMachineDeployment(ctx context.Context, cluster
217
205
}
218
206
219
207
ctx , _ = log .WithObject (desiredMD .BootstrapTemplate ).Into (ctx )
220
- if _ , err := r .reconcileReferencedTemplate (ctx , reconcileReferencedTemplateInput {
208
+ if err := r .reconcileReferencedTemplate (ctx , reconcileReferencedTemplateInput {
221
209
ref : desiredMD .Object .Spec .Template .Spec .Bootstrap .ConfigRef ,
222
210
current : currentMD .BootstrapTemplate ,
223
211
desired : desiredMD .BootstrapTemplate ,
@@ -337,49 +325,47 @@ type reconcileReferencedTemplateInput struct {
337
325
// This function specifically takes care of the first step and updates the reference locally. So the remaining steps
338
326
// can be executed afterwards.
339
327
// NOTE: This func has a side effect in case of template rotation, changing both the desired object and the object reference.
340
- func (r * ClusterReconciler ) reconcileReferencedTemplate (ctx context.Context , in reconcileReferencedTemplateInput ) ( func () error , error ) {
328
+ func (r * ClusterReconciler ) reconcileReferencedTemplate (ctx context.Context , in reconcileReferencedTemplateInput ) error {
341
329
log := tlog .LoggerFrom (ctx )
342
330
343
- cleanupFunc := func () error { return nil }
344
-
345
331
// If there is no current object, create the desired object.
346
332
if in .current == nil {
347
333
log .Infof ("Creating %s" , tlog.KObj {Obj : in .desired })
348
334
if err := r .Client .Create (ctx , in .desired .DeepCopy ()); err != nil {
349
- return nil , errors .Wrapf (err , "failed to create %s" , tlog.KObj {Obj : in .desired })
335
+ return errors .Wrapf (err , "failed to create %s" , tlog.KObj {Obj : in .desired })
350
336
}
351
- return cleanupFunc , nil
337
+ return nil
352
338
}
353
339
354
340
if in .ref == nil {
355
- return nil , errors .Errorf ("failed to rotate %s: ref should not be nil" , in .desired .GroupVersionKind ())
341
+ return errors .Errorf ("failed to rotate %s: ref should not be nil" , in .desired .GroupVersionKind ())
356
342
}
357
343
358
344
// Check if the current and desired referenced object are compatible.
359
345
if err := in .compatibilityChecker (in .current , in .desired ); err != nil {
360
- return nil , err
346
+ return err
361
347
}
362
348
363
349
// Check differences between current and desired objects, and if there are changes eventually start the template rotation.
364
350
patchHelper , err := mergepatch .NewHelper (in .current , in .desired , r .Client )
365
351
if err != nil {
366
- return nil , errors .Wrapf (err , "failed to create patch helper for %s" , tlog.KObj {Obj : in .current })
352
+ return errors .Wrapf (err , "failed to create patch helper for %s" , tlog.KObj {Obj : in .current })
367
353
}
368
354
369
355
// Return if no changes are detected.
370
356
if ! patchHelper .HasChanges () {
371
357
log .V (3 ).Infof ("No changes for %s" , tlog.KObj {Obj : in .desired })
372
- return cleanupFunc , nil
358
+ return nil
373
359
}
374
360
375
361
// If there are no changes in the spec, and thus only changes in metadata, instead of doing a full template
376
362
// rotation we patch the object in place. This avoids recreating machines.
377
363
if ! patchHelper .HasSpecChanges () {
378
364
log .Infof ("Patching %s" , tlog.KObj {Obj : in .desired })
379
365
if err := patchHelper .Patch (ctx ); err != nil {
380
- return nil , errors .Wrapf (err , "failed to patch %s" , tlog.KObj {Obj : in .desired })
366
+ return errors .Wrapf (err , "failed to patch %s" , tlog.KObj {Obj : in .desired })
381
367
}
382
- return cleanupFunc , nil
368
+ return nil
383
369
}
384
370
385
371
// Create the new template.
@@ -392,21 +378,13 @@ func (r *ClusterReconciler) reconcileReferencedTemplate(ctx context.Context, in
392
378
log .Infof ("Rotating %s, new name %s" , tlog.KObj {Obj : in .current }, newName )
393
379
log .Infof ("Creating %s" , tlog.KObj {Obj : in .desired })
394
380
if err := r .Client .Create (ctx , in .desired .DeepCopy ()); err != nil {
395
- return nil , errors .Wrapf (err , "failed to create %s" , tlog.KObj {Obj : in .desired })
381
+ return errors .Wrapf (err , "failed to create %s" , tlog.KObj {Obj : in .desired })
396
382
}
397
383
398
384
// Update the reference with the new name.
399
385
// NOTE: Updating the object hosting reference to the template is executed outside this func.
400
386
// TODO: find a way to make side effect more explicit
401
387
in .ref .Name = newName
402
388
403
- // Set up a cleanup func for removing the old template.
404
- // NOTE: This function must be called after updating the object containing the reference to the Template.
405
- return func () error {
406
- log .Infof ("Deleting %s" , tlog.KObj {Obj : in .current })
407
- if err := r .Client .Delete (ctx , in .current ); err != nil {
408
- return errors .Wrapf (err , "failed to delete %s" , tlog.KObj {Obj : in .desired })
409
- }
410
- return nil
411
- }, nil
389
+ return nil
412
390
}
0 commit comments