Skip to content

Commit 3c5894c

Browse files
authored
Merge pull request #5525 from fabriziopandini/Remove-template-cleanup-func-in-topology-controller
🌱 Remove template cleanup func in Topology controller
2 parents 30bbb52 + 826326c commit 3c5894c

File tree

1 file changed

+19
-41
lines changed

1 file changed

+19
-41
lines changed

controllers/topology/reconcile_state.go

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
corev1 "k8s.io/api/core/v1"
2525
apierrors "k8s.io/apimachinery/pkg/api/errors"
2626
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
27-
kerrors "k8s.io/apimachinery/pkg/util/errors"
2827
"k8s.io/apiserver/pkg/storage/names"
2928
"sigs.k8s.io/cluster-api/controllers/topology/internal/check"
3029
"sigs.k8s.io/cluster-api/controllers/topology/internal/contract"
@@ -70,9 +69,6 @@ func (r *ClusterReconciler) reconcileInfrastructureCluster(ctx context.Context,
7069
// reconcileControlPlane works to bring the current state of a managed topology in line with the desired state. This involves
7170
// updating the cluster where needed.
7271
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-
7672
// If the clusterClass mandates the controlPlane has infrastructureMachines, reconcile it.
7773
if s.Blueprint.HasControlPlaneInfrastructureMachine() {
7874
ctx, _ := tlog.LoggerFrom(ctx).WithObject(s.Desired.ControlPlane.InfrastructureMachineTemplate).Into(ctx)
@@ -83,7 +79,7 @@ func (r *ClusterReconciler) reconcileControlPlane(ctx context.Context, s *scope.
8379
}
8480

8581
// Create or update the MachineInfrastructureTemplate of the control plane.
86-
cleanup, err = r.reconcileReferencedTemplate(ctx, reconcileReferencedTemplateInput{
82+
err = r.reconcileReferencedTemplate(ctx, reconcileReferencedTemplateInput{
8783
ref: cpInfraRef,
8884
current: s.Current.ControlPlane.InfrastructureMachineTemplate,
8985
desired: s.Desired.ControlPlane.InfrastructureMachineTemplate,
@@ -98,25 +94,17 @@ func (r *ClusterReconciler) reconcileControlPlane(ctx context.Context, s *scope.
9894
// The controlPlaneObject.Spec.machineTemplate.infrastructureRef has to be updated in the desired object
9995
err = contract.ControlPlane().MachineTemplate().InfrastructureRef().Set(s.Desired.ControlPlane.Object, refToUnstructured(cpInfraRef))
10096
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})
10598
}
10699
}
107100

108101
// Create or update the ControlPlaneObject for the ControlPlaneState.
109102
ctx, _ = tlog.LoggerFrom(ctx).WithObject(s.Desired.ControlPlane.Object).Into(ctx)
110103
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})
115105
}
116106

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
120108
}
121109

122110
// reconcileCluster reconciles the desired state of the Cluster object.
@@ -180,14 +168,14 @@ func (r *ClusterReconciler) createMachineDeployment(ctx context.Context, md *sco
180168
log := tlog.LoggerFrom(ctx).WithMachineDeployment(md.Object)
181169

182170
ctx, _ = log.WithObject(md.InfrastructureMachineTemplate).Into(ctx)
183-
if _, err := r.reconcileReferencedTemplate(ctx, reconcileReferencedTemplateInput{
171+
if err := r.reconcileReferencedTemplate(ctx, reconcileReferencedTemplateInput{
184172
desired: md.InfrastructureMachineTemplate,
185173
}); err != nil {
186174
return errors.Wrapf(err, "failed to create %s", tlog.KObj{Obj: md.Object})
187175
}
188176

189177
ctx, _ = log.WithObject(md.BootstrapTemplate).Into(ctx)
190-
if _, err := r.reconcileReferencedTemplate(ctx, reconcileReferencedTemplateInput{
178+
if err := r.reconcileReferencedTemplate(ctx, reconcileReferencedTemplateInput{
191179
desired: md.BootstrapTemplate,
192180
}); err != nil {
193181
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
206194
log := tlog.LoggerFrom(ctx).WithMachineDeployment(desiredMD.Object)
207195

208196
ctx, _ = log.WithObject(desiredMD.InfrastructureMachineTemplate).Into(ctx)
209-
if _, err := r.reconcileReferencedTemplate(ctx, reconcileReferencedTemplateInput{
197+
if err := r.reconcileReferencedTemplate(ctx, reconcileReferencedTemplateInput{
210198
ref: &desiredMD.Object.Spec.Template.Spec.InfrastructureRef,
211199
current: currentMD.InfrastructureMachineTemplate,
212200
desired: desiredMD.InfrastructureMachineTemplate,
@@ -217,7 +205,7 @@ func (r *ClusterReconciler) updateMachineDeployment(ctx context.Context, cluster
217205
}
218206

219207
ctx, _ = log.WithObject(desiredMD.BootstrapTemplate).Into(ctx)
220-
if _, err := r.reconcileReferencedTemplate(ctx, reconcileReferencedTemplateInput{
208+
if err := r.reconcileReferencedTemplate(ctx, reconcileReferencedTemplateInput{
221209
ref: desiredMD.Object.Spec.Template.Spec.Bootstrap.ConfigRef,
222210
current: currentMD.BootstrapTemplate,
223211
desired: desiredMD.BootstrapTemplate,
@@ -337,49 +325,47 @@ type reconcileReferencedTemplateInput struct {
337325
// This function specifically takes care of the first step and updates the reference locally. So the remaining steps
338326
// can be executed afterwards.
339327
// 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 {
341329
log := tlog.LoggerFrom(ctx)
342330

343-
cleanupFunc := func() error { return nil }
344-
345331
// If there is no current object, create the desired object.
346332
if in.current == nil {
347333
log.Infof("Creating %s", tlog.KObj{Obj: in.desired})
348334
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})
350336
}
351-
return cleanupFunc, nil
337+
return nil
352338
}
353339

354340
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())
356342
}
357343

358344
// Check if the current and desired referenced object are compatible.
359345
if err := in.compatibilityChecker(in.current, in.desired); err != nil {
360-
return nil, err
346+
return err
361347
}
362348

363349
// Check differences between current and desired objects, and if there are changes eventually start the template rotation.
364350
patchHelper, err := mergepatch.NewHelper(in.current, in.desired, r.Client)
365351
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})
367353
}
368354

369355
// Return if no changes are detected.
370356
if !patchHelper.HasChanges() {
371357
log.V(3).Infof("No changes for %s", tlog.KObj{Obj: in.desired})
372-
return cleanupFunc, nil
358+
return nil
373359
}
374360

375361
// If there are no changes in the spec, and thus only changes in metadata, instead of doing a full template
376362
// rotation we patch the object in place. This avoids recreating machines.
377363
if !patchHelper.HasSpecChanges() {
378364
log.Infof("Patching %s", tlog.KObj{Obj: in.desired})
379365
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})
381367
}
382-
return cleanupFunc, nil
368+
return nil
383369
}
384370

385371
// Create the new template.
@@ -392,21 +378,13 @@ func (r *ClusterReconciler) reconcileReferencedTemplate(ctx context.Context, in
392378
log.Infof("Rotating %s, new name %s", tlog.KObj{Obj: in.current}, newName)
393379
log.Infof("Creating %s", tlog.KObj{Obj: in.desired})
394380
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})
396382
}
397383

398384
// Update the reference with the new name.
399385
// NOTE: Updating the object hosting reference to the template is executed outside this func.
400386
// TODO: find a way to make side effect more explicit
401387
in.ref.Name = newName
402388

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
412390
}

0 commit comments

Comments
 (0)