Skip to content

Commit 699ecbe

Browse files
authored
Merge pull request #11415 from Patryk-Stefanski/cleanup-disable-machine-create-annotation-rolling-machine-deployment
🐛 remove disableMachineCreate annotation from new machinesets during rolling machine deployment reconciliation
2 parents 96679ff + e5fc401 commit 699ecbe

File tree

3 files changed

+69
-16
lines changed

3 files changed

+69
-16
lines changed

internal/controllers/machinedeployment/machinedeployment_rolling.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ import (
2121
"sort"
2222

2323
"github.com/pkg/errors"
24+
"k8s.io/klog/v2"
2425
ctrl "sigs.k8s.io/controller-runtime"
2526
"sigs.k8s.io/controller-runtime/pkg/client"
2627

2728
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2829
"sigs.k8s.io/cluster-api/internal/controllers/machinedeployment/mdutil"
30+
"sigs.k8s.io/cluster-api/util/patch"
2931
)
3032

3133
// rolloutRolling implements the logic for rolling a new MachineSet.
@@ -72,6 +74,10 @@ func (r *Reconciler) rolloutRolling(ctx context.Context, md *clusterv1.MachineDe
7274
}
7375

7476
func (r *Reconciler) reconcileNewMachineSet(ctx context.Context, allMSs []*clusterv1.MachineSet, newMS *clusterv1.MachineSet, deployment *clusterv1.MachineDeployment) error {
77+
if err := r.cleanupDisableMachineCreateAnnotation(ctx, newMS); err != nil {
78+
return err
79+
}
80+
7581
if deployment.Spec.Replicas == nil {
7682
return errors.Errorf("spec.replicas for MachineDeployment %v is nil, this is unexpected", client.ObjectKeyFromObject(deployment))
7783
}
@@ -293,3 +299,25 @@ func (r *Reconciler) scaleDownOldMachineSetsForRollingUpdate(ctx context.Context
293299

294300
return totalScaledDown, nil
295301
}
302+
303+
// cleanupDisableMachineCreateAnnotation will remove the disable machine create annotation from new MachineSets that were created during reconcileOldMachineSetsOnDelete.
304+
func (r *Reconciler) cleanupDisableMachineCreateAnnotation(ctx context.Context, newMS *clusterv1.MachineSet) error {
305+
log := ctrl.LoggerFrom(ctx, "MachineSet", klog.KObj(newMS))
306+
307+
if newMS.Annotations != nil {
308+
if _, ok := newMS.Annotations[clusterv1.DisableMachineCreateAnnotation]; ok {
309+
log.V(4).Info("removing annotation on latest MachineSet to enable machine creation")
310+
patchHelper, err := patch.NewHelper(newMS, r.Client)
311+
if err != nil {
312+
return err
313+
}
314+
delete(newMS.Annotations, clusterv1.DisableMachineCreateAnnotation)
315+
err = patchHelper.Patch(ctx, newMS)
316+
if err != nil {
317+
return err
318+
}
319+
}
320+
}
321+
322+
return nil
323+
}

internal/controllers/machinedeployment/machinedeployment_rolling_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,41 @@ func TestReconcileNewMachineSet(t *testing.T) {
217217
},
218218
error: nil,
219219
},
220+
{
221+
name: "Rolling Updated Cleanup disable machine create annotation",
222+
machineDeployment: &clusterv1.MachineDeployment{
223+
ObjectMeta: metav1.ObjectMeta{
224+
Namespace: "foo",
225+
Name: "bar",
226+
},
227+
Spec: clusterv1.MachineDeploymentSpec{
228+
Strategy: &clusterv1.MachineDeploymentStrategy{
229+
Type: clusterv1.RollingUpdateMachineDeploymentStrategyType,
230+
RollingUpdate: &clusterv1.MachineRollingUpdateDeployment{
231+
MaxUnavailable: intOrStrPtr(0),
232+
MaxSurge: intOrStrPtr(0),
233+
},
234+
},
235+
Replicas: ptr.To[int32](2),
236+
},
237+
},
238+
newMachineSet: &clusterv1.MachineSet{
239+
ObjectMeta: metav1.ObjectMeta{
240+
Namespace: "foo",
241+
Name: "bar",
242+
Annotations: map[string]string{
243+
clusterv1.DisableMachineCreateAnnotation: "true",
244+
clusterv1.DesiredReplicasAnnotation: "2",
245+
clusterv1.MaxReplicasAnnotation: "2",
246+
},
247+
},
248+
Spec: clusterv1.MachineSetSpec{
249+
Replicas: ptr.To[int32](2),
250+
},
251+
},
252+
expectedNewMachineSetReplicas: 2,
253+
error: nil,
254+
},
220255
}
221256

222257
for _, tc := range testCases {
@@ -252,6 +287,9 @@ func TestReconcileNewMachineSet(t *testing.T) {
252287

253288
g.Expect(*freshNewMachineSet.Spec.Replicas).To(BeEquivalentTo(tc.expectedNewMachineSetReplicas))
254289

290+
_, ok := freshNewMachineSet.GetAnnotations()[clusterv1.DisableMachineCreateAnnotation]
291+
g.Expect(ok).To(BeFalse())
292+
255293
desiredReplicasAnnotation, ok := freshNewMachineSet.GetAnnotations()[clusterv1.DesiredReplicasAnnotation]
256294
g.Expect(ok).To(BeTrue())
257295
g.Expect(strconv.Atoi(desiredReplicasAnnotation)).To(BeEquivalentTo(*tc.machineDeployment.Spec.Replicas))

internal/controllers/machinedeployment/machinedeployment_rollout_ondelete.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -165,22 +165,9 @@ func (r *Reconciler) reconcileOldMachineSetsOnDelete(ctx context.Context, oldMSs
165165

166166
// reconcileNewMachineSetOnDelete handles reconciliation of the latest MachineSet associated with the MachineDeployment in the OnDelete MachineDeploymentStrategyType.
167167
func (r *Reconciler) reconcileNewMachineSetOnDelete(ctx context.Context, allMSs []*clusterv1.MachineSet, newMS *clusterv1.MachineSet, deployment *clusterv1.MachineDeployment) error {
168-
// logic same as reconcile logic for RollingUpdate
169-
log := ctrl.LoggerFrom(ctx, "MachineSet", klog.KObj(newMS))
170-
171-
if newMS.Annotations != nil {
172-
if _, ok := newMS.Annotations[clusterv1.DisableMachineCreateAnnotation]; ok {
173-
log.V(4).Info("removing annotation on latest MachineSet to enable machine creation")
174-
patchHelper, err := patch.NewHelper(newMS, r.Client)
175-
if err != nil {
176-
return err
177-
}
178-
delete(newMS.Annotations, clusterv1.DisableMachineCreateAnnotation)
179-
err = patchHelper.Patch(ctx, newMS)
180-
if err != nil {
181-
return err
182-
}
183-
}
168+
if err := r.cleanupDisableMachineCreateAnnotation(ctx, newMS); err != nil {
169+
return err
184170
}
171+
185172
return r.reconcileNewMachineSet(ctx, allMSs, newMS, deployment)
186173
}

0 commit comments

Comments
 (0)