Skip to content

Commit c808fc1

Browse files
committed
MD controller: remove redundant defaulting + fix overwrite of local changes
Signed-off-by: Stefan Büringer [email protected]
1 parent aef77c3 commit c808fc1

File tree

4 files changed

+74
-71
lines changed

4 files changed

+74
-71
lines changed

api/v1beta1/machinedeployment_webhook.go

Lines changed: 58 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,64 @@ var _ webhook.Validator = &MachineDeployment{}
4848

4949
// Default implements webhook.Defaulter so a webhook will be registered for the type.
5050
func (m *MachineDeployment) Default() {
51-
PopulateDefaultsMachineDeployment(m)
51+
if m.Labels == nil {
52+
m.Labels = make(map[string]string)
53+
}
54+
m.Labels[ClusterNameLabel] = m.Spec.ClusterName
55+
56+
if m.Spec.MinReadySeconds == nil {
57+
m.Spec.MinReadySeconds = pointer.Int32(0)
58+
}
59+
60+
if m.Spec.RevisionHistoryLimit == nil {
61+
m.Spec.RevisionHistoryLimit = pointer.Int32(1)
62+
}
63+
64+
if m.Spec.ProgressDeadlineSeconds == nil {
65+
m.Spec.ProgressDeadlineSeconds = pointer.Int32(600)
66+
}
67+
68+
if m.Spec.Selector.MatchLabels == nil {
69+
m.Spec.Selector.MatchLabels = make(map[string]string)
70+
}
71+
72+
if m.Spec.Strategy == nil {
73+
m.Spec.Strategy = &MachineDeploymentStrategy{}
74+
}
75+
76+
if m.Spec.Strategy.Type == "" {
77+
m.Spec.Strategy.Type = RollingUpdateMachineDeploymentStrategyType
78+
}
79+
80+
if m.Spec.Template.Labels == nil {
81+
m.Spec.Template.Labels = make(map[string]string)
82+
}
83+
84+
// Default RollingUpdate strategy only if strategy type is RollingUpdate.
85+
if m.Spec.Strategy.Type == RollingUpdateMachineDeploymentStrategyType {
86+
if m.Spec.Strategy.RollingUpdate == nil {
87+
m.Spec.Strategy.RollingUpdate = &MachineRollingUpdateDeployment{}
88+
}
89+
if m.Spec.Strategy.RollingUpdate.MaxSurge == nil {
90+
ios1 := intstr.FromInt(1)
91+
m.Spec.Strategy.RollingUpdate.MaxSurge = &ios1
92+
}
93+
if m.Spec.Strategy.RollingUpdate.MaxUnavailable == nil {
94+
ios0 := intstr.FromInt(0)
95+
m.Spec.Strategy.RollingUpdate.MaxUnavailable = &ios0
96+
}
97+
}
98+
99+
// If no selector has been provided, add label and selector for the
100+
// MachineDeployment's name as a default way of providing uniqueness.
101+
if len(m.Spec.Selector.MatchLabels) == 0 && len(m.Spec.Selector.MatchExpressions) == 0 {
102+
m.Spec.Selector.MatchLabels[MachineDeploymentNameLabel] = m.Name
103+
m.Spec.Template.Labels[MachineDeploymentNameLabel] = m.Name
104+
}
105+
// Make sure selector and template to be in the same cluster.
106+
m.Spec.Selector.MatchLabels[ClusterNameLabel] = m.Spec.ClusterName
107+
m.Spec.Template.Labels[ClusterNameLabel] = m.Spec.ClusterName
108+
52109
// tolerate version strings without a "v" prefix: prepend it if it's not there
53110
if m.Spec.Template.Spec.Version != nil && !strings.HasPrefix(*m.Spec.Template.Spec.Version, "v") {
54111
normalizedVersion := "v" + *m.Spec.Template.Spec.Version
@@ -156,65 +213,3 @@ func (m *MachineDeployment) validate(old *MachineDeployment) error {
156213

157214
return apierrors.NewInvalid(GroupVersion.WithKind("MachineDeployment").GroupKind(), m.Name, allErrs)
158215
}
159-
160-
// PopulateDefaultsMachineDeployment fills in default field values.
161-
// This is also called during MachineDeployment sync.
162-
func PopulateDefaultsMachineDeployment(d *MachineDeployment) {
163-
if d.Labels == nil {
164-
d.Labels = make(map[string]string)
165-
}
166-
d.Labels[ClusterNameLabel] = d.Spec.ClusterName
167-
168-
if d.Spec.MinReadySeconds == nil {
169-
d.Spec.MinReadySeconds = pointer.Int32(0)
170-
}
171-
172-
if d.Spec.RevisionHistoryLimit == nil {
173-
d.Spec.RevisionHistoryLimit = pointer.Int32(1)
174-
}
175-
176-
if d.Spec.ProgressDeadlineSeconds == nil {
177-
d.Spec.ProgressDeadlineSeconds = pointer.Int32(600)
178-
}
179-
180-
if d.Spec.Selector.MatchLabels == nil {
181-
d.Spec.Selector.MatchLabels = make(map[string]string)
182-
}
183-
184-
if d.Spec.Strategy == nil {
185-
d.Spec.Strategy = &MachineDeploymentStrategy{}
186-
}
187-
188-
if d.Spec.Strategy.Type == "" {
189-
d.Spec.Strategy.Type = RollingUpdateMachineDeploymentStrategyType
190-
}
191-
192-
if d.Spec.Template.Labels == nil {
193-
d.Spec.Template.Labels = make(map[string]string)
194-
}
195-
196-
// Default RollingUpdate strategy only if strategy type is RollingUpdate.
197-
if d.Spec.Strategy.Type == RollingUpdateMachineDeploymentStrategyType {
198-
if d.Spec.Strategy.RollingUpdate == nil {
199-
d.Spec.Strategy.RollingUpdate = &MachineRollingUpdateDeployment{}
200-
}
201-
if d.Spec.Strategy.RollingUpdate.MaxSurge == nil {
202-
ios1 := intstr.FromInt(1)
203-
d.Spec.Strategy.RollingUpdate.MaxSurge = &ios1
204-
}
205-
if d.Spec.Strategy.RollingUpdate.MaxUnavailable == nil {
206-
ios0 := intstr.FromInt(0)
207-
d.Spec.Strategy.RollingUpdate.MaxUnavailable = &ios0
208-
}
209-
}
210-
211-
// If no selector has been provided, add label and selector for the
212-
// MachineDeployment's name as a default way of providing uniqueness.
213-
if len(d.Spec.Selector.MatchLabels) == 0 && len(d.Spec.Selector.MatchExpressions) == 0 {
214-
d.Spec.Selector.MatchLabels[MachineDeploymentNameLabel] = d.Name
215-
d.Spec.Template.Labels[MachineDeploymentNameLabel] = d.Name
216-
}
217-
// Make sure selector and template to be in the same cluster.
218-
d.Spec.Selector.MatchLabels[ClusterNameLabel] = d.Spec.ClusterName
219-
d.Spec.Template.Labels[ClusterNameLabel] = d.Spec.ClusterName
220-
}

docs/book/src/developer/providers/v1.3-to-v1.4.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ maintainers of providers and consumers of our Go API.
3030
- `exp/runtime/server.NewServer` has been removed.
3131
- `--disable-no-echo` option from `clusterctl describe cluster` subcommand has been removed
3232
- `api/v1beta1.ClusterTopologyManagedFieldsAnnotation` field has been removed.
33+
- `api/v1beta1.PopulateDefaultsMachineDeployment` func has been removed.
3334

3435
### API Changes
3536

internal/controllers/machinedeployment/machinedeployment_controller_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,18 @@ func TestMachineDeploymentReconciler(t *testing.T) {
250250
//
251251
secondMachineSet := machineSets.Items[0]
252252
t.Log("Scaling the MachineDeployment to 3 replicas")
253-
modifyFunc := func(d *clusterv1.MachineDeployment) { d.Spec.Replicas = pointer.Int32(3) }
253+
desiredMachineDeploymentReplicas := int32(3)
254+
modifyFunc := func(d *clusterv1.MachineDeployment) {
255+
d.Spec.Replicas = pointer.Int32(desiredMachineDeploymentReplicas)
256+
}
254257
g.Expect(updateMachineDeployment(ctx, env, deployment, modifyFunc)).To(Succeed())
255258
g.Eventually(func() int {
256259
key := client.ObjectKey{Name: secondMachineSet.Name, Namespace: secondMachineSet.Namespace}
257260
if err := env.Get(ctx, key, &secondMachineSet); err != nil {
258261
return -1
259262
}
260263
return int(*secondMachineSet.Spec.Replicas)
261-
}, timeout).Should(BeEquivalentTo(3))
264+
}, timeout).Should(BeEquivalentTo(desiredMachineDeploymentReplicas))
262265

263266
//
264267
// Update a MachineDeployment, expect Reconcile to be called and a new MachineSet to appear.
@@ -393,7 +396,7 @@ func TestMachineDeploymentReconciler(t *testing.T) {
393396
if err := env.List(ctx, machineSets, listOpts); err != nil {
394397
return false
395398
}
396-
return machineSets.Items[0].Status.Replicas == *deployment.Spec.Replicas
399+
return machineSets.Items[0].Status.Replicas == desiredMachineDeploymentReplicas
397400
}, timeout*5).Should(BeTrue())
398401

399402
t.Log("Verifying MachineSets with old labels are deleted")

internal/controllers/machinedeployment/machinedeployment_sync.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -532,16 +532,20 @@ func (r *Reconciler) updateMachineDeployment(ctx context.Context, d *clusterv1.M
532532

533533
// We have this as standalone variant to be able to use it from the tests.
534534
func updateMachineDeployment(ctx context.Context, c client.Client, d *clusterv1.MachineDeployment, modify func(*clusterv1.MachineDeployment)) error {
535+
mdObjectKey := util.ObjectKey(d)
535536
return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
536-
if err := c.Get(ctx, util.ObjectKey(d), d); err != nil {
537+
// Note: We intentionally don't re-use the passed in MachineDeployment d here as that would
538+
// overwrite any local changes we might have previously made to the MachineDeployment with the version
539+
// we get here from the apiserver.
540+
md := &clusterv1.MachineDeployment{}
541+
if err := c.Get(ctx, mdObjectKey, md); err != nil {
537542
return err
538543
}
539-
patchHelper, err := patch.NewHelper(d, c)
544+
patchHelper, err := patch.NewHelper(md, c)
540545
if err != nil {
541546
return err
542547
}
543-
clusterv1.PopulateDefaultsMachineDeployment(d)
544-
modify(d)
545-
return patchHelper.Patch(ctx, d)
548+
modify(md)
549+
return patchHelper.Patch(ctx, md)
546550
})
547551
}

0 commit comments

Comments
 (0)