Skip to content

Commit 209d4dd

Browse files
fwieselnotandy
authored andcommitted
Hypervisor: Use Patch + FieldOwner
FieldOwner allows us to identify who maintains a field and detect if there is a conflict between multiple controllers.
1 parent 59d34f2 commit 209d4dd

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

internal/controller/hypervisor_controller.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,12 @@ func (hv *HypervisorController) Reconcile(ctx context.Context, req ctrl.Request)
9898
// continue with creation
9999
} else {
100100
// update Status if needed
101-
changed := false
101+
base := hypervisor.DeepCopy()
102102

103103
// transfer internal IP
104104
for _, address := range node.Status.Addresses {
105105
if address.Type == corev1.NodeInternalIP && hypervisor.Status.InternalIP != address.Address {
106106
hypervisor.Status.InternalIP = address.Address
107-
changed = true
108107
break
109108
}
110109
}
@@ -113,31 +112,30 @@ func (hv *HypervisorController) Reconcile(ctx context.Context, req ctrl.Request)
113112
nodeTerminationCondition := FindNodeStatusCondition(node.Status.Conditions, "Terminating")
114113
if nodeTerminationCondition != nil && nodeTerminationCondition.Status == corev1.ConditionTrue {
115114
// Node might be terminating, propagate condition to hypervisor
116-
changed = meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
115+
meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
117116
Type: kvmv1.ConditionTypeReady,
118117
Status: metav1.ConditionFalse,
119118
Reason: nodeTerminationCondition.Reason,
120119
Message: nodeTerminationCondition.Message,
121-
}) || changed
122-
changed = meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
120+
})
121+
meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
123122
Type: kvmv1.ConditionTypeTerminating,
124123
Status: metav1.ConditionStatus(nodeTerminationCondition.Status),
125124
Reason: nodeTerminationCondition.Reason,
126125
Message: nodeTerminationCondition.Message,
127-
}) || changed
126+
})
128127
}
129128

130-
if changed {
131-
return ctrl.Result{}, hv.Status().Update(ctx, hypervisor)
129+
if !equality.Semantic.DeepEqual(hypervisor, base) {
130+
return ctrl.Result{}, hv.Status().Patch(ctx, hypervisor, k8sclient.MergeFromWithOptions(base, k8sclient.MergeFromWithOptimisticLock{}), k8sclient.FieldOwner(HypervisorControllerName))
132131
}
133132

134-
before := hypervisor.DeepCopy()
135133
syncLabelsAndAnnotations(nodeLabels, hypervisor, node)
136-
if equality.Semantic.DeepEqual(hypervisor, before) {
134+
if equality.Semantic.DeepEqual(hypervisor, base) {
137135
return ctrl.Result{}, nil
138136
}
139137

140-
return ctrl.Result{}, hv.Patch(ctx, hypervisor, k8sclient.MergeFromWithOptions(before, k8sclient.MergeFromWithOptimisticLock{}))
138+
return ctrl.Result{}, hv.Patch(ctx, hypervisor, k8sclient.MergeFromWithOptions(base, k8sclient.MergeFromWithOptimisticLock{}), k8sclient.FieldOwner(HypervisorControllerName))
141139
}
142140

143141
syncLabelsAndAnnotations(nodeLabels, hypervisor, node)

0 commit comments

Comments
 (0)