Skip to content

Commit dbd6735

Browse files
fwieselnotandy
authored andcommitted
Aggregates: Use Patch + FieldOwner
FieldOwner allows us to identify who maintains a field and detect if there is a conflict between multiple controllers.
1 parent bae3295 commit dbd6735

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

internal/controller/aggregates_controller.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,15 @@ func (ac *AggregatesController) Reconcile(ctx context.Context, req ctrl.Request)
114114
return ctrl.Result{}, err
115115
}
116116

117+
base := hv.DeepCopy()
117118
hv.Status.Aggregates = hv.Spec.Aggregates
118119
meta.SetStatusCondition(&hv.Status.Conditions, metav1.Condition{
119120
Type: kvmv1.ConditionTypeAggregatesUpdated,
120121
Status: metav1.ConditionTrue,
121122
Reason: kvmv1.ConditionReasonSucceeded,
122123
Message: "Aggregates updated successfully",
123124
})
124-
return ctrl.Result{}, ac.Status().Update(ctx, hv)
125+
return ctrl.Result{}, ac.Status().Patch(ctx, hv, k8sclient.MergeFromWithOptions(base, k8sclient.MergeFromWithOptimisticLock{}), k8sclient.FieldOwner(AggregatesControllerName))
125126
}
126127

127128
// setErrorCondition sets the error condition on the Hypervisor status, returns error if update fails
@@ -133,8 +134,9 @@ func (ac *AggregatesController) setErrorCondition(ctx context.Context, hv *kvmv1
133134
Message: msg,
134135
}
135136

137+
base := hv.DeepCopy()
136138
if meta.SetStatusCondition(&hv.Status.Conditions, condition) {
137-
if err := ac.Status().Update(ctx, hv); err != nil {
139+
if err := ac.Status().Patch(ctx, hv, k8sclient.MergeFromWithOptions(base, k8sclient.MergeFromWithOptimisticLock{}), k8sclient.FieldOwner(AggregatesControllerName)); err != nil {
138140
return err
139141
}
140142
}

0 commit comments

Comments
 (0)