Skip to content

Commit ff9a942

Browse files
committed
fix: use patch for machine/machineset status
1 parent a82d5db commit ff9a942

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

pkg/controller/machine/controller_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,14 +756,15 @@ func TestUpdateStatus(t *testing.T) {
756756
t.Fatalf("error deleting machine: %v", err)
757757
}
758758
}()
759+
machineCopy := machine.DeepCopy()
759760

760761
if tc.existingProviderStatus != "" {
761762
machine.Status.ProviderStatus = &runtime.RawExtension{
762763
Raw: []byte(tc.existingProviderStatus),
763764
}
764765
}
765766

766-
gs.Expect(k8sClient.Status().Update(ctx, machine)).To(Succeed())
767+
gs.Expect(k8sClient.Status().Patch(ctx, machine, client.MergeFrom(machineCopy))).To(Succeed())
767768

768769
namespacedName := types.NamespacedName{
769770
Namespace: machine.Namespace,

pkg/controller/machine/drain_controller.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func (d *machineDrainController) Reconcile(ctx context.Context, request reconcil
7575
// Error reading the object - requeue the request.
7676
return reconcile.Result{}, err
7777
}
78+
machineCopy := m.DeepCopy()
7879

7980
existingDrainedCondition := conditions.Get(m, machinev1.MachineDrained)
8081
alreadyDrained := existingDrainedCondition != nil && existingDrainedCondition.Status == corev1.ConditionTrue
@@ -110,9 +111,9 @@ func (d *machineDrainController) Reconcile(ctx context.Context, request reconcil
110111
}
111112

112113
conditions.Set(m, drainFinishedCondition)
113-
// requeue request in case of failed update
114-
if err := d.Client.Status().Update(ctx, m); err != nil {
115-
return reconcile.Result{}, fmt.Errorf("could not update machine status: %w", err)
114+
// requeue request in case of failed patch
115+
if err := d.Client.Status().Patch(ctx, m, client.MergeFrom(machineCopy)); err != nil {
116+
return reconcile.Result{}, fmt.Errorf("could not patch machine status: %w", err)
116117
}
117118
return reconcile.Result{}, nil
118119
}

pkg/controller/machineset/status.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func (c *ReconcileMachineSet) calculateStatus(ms *machinev1.MachineSet, filtered
7272

7373
// updateMachineSetStatus attempts to update the Status.Replicas of the given MachineSet, with a single GET/PUT retry.
7474
func updateMachineSetStatus(c client.Client, ms *machinev1.MachineSet, newStatus machinev1.MachineSetStatus) (*machinev1.MachineSet, error) {
75+
machineSetCopy := ms.DeepCopy()
7576
// This is the steady state. It happens when the MachineSet doesn't have any expectations, since
7677
// we do a periodic relist every 30s. If the generations differ but the replicas are
7778
// the same, a caller might've resized to the same replica count.
@@ -90,7 +91,7 @@ func updateMachineSetStatus(c client.Client, ms *machinev1.MachineSet, newStatus
9091
// same status.
9192
newStatus.ObservedGeneration = ms.Generation
9293

93-
var getErr, updateErr error
94+
var getErr, patchErr error
9495
for i := 0; ; i++ {
9596
var replicas int32
9697
if ms.Spec.Replicas != nil {
@@ -105,8 +106,8 @@ func updateMachineSetStatus(c client.Client, ms *machinev1.MachineSet, newStatus
105106
fmt.Sprintf("conditions: %v->%v", ms.Status.Conditions, newStatus.Conditions))
106107

107108
ms.Status = newStatus
108-
updateErr = c.Status().Update(context.Background(), ms)
109-
if updateErr == nil {
109+
patchErr = c.Status().Patch(context.Background(), ms, client.MergeFrom(machineSetCopy))
110+
if patchErr == nil {
110111
return ms, nil
111112
}
112113
// Stop retrying if we exceed statusUpdateRetries - the machineSet will be requeued with a rate limit.
@@ -121,7 +122,7 @@ func updateMachineSetStatus(c client.Client, ms *machinev1.MachineSet, newStatus
121122
}
122123
}
123124

124-
return nil, updateErr
125+
return nil, patchErr
125126
}
126127

127128
func (c *ReconcileMachineSet) getMachineNode(machine *machinev1.Machine) (*corev1.Node, error) {

0 commit comments

Comments
 (0)