Skip to content

Commit 4448ced

Browse files
committed
[traits-controller] fix updating the traits status field unconditionally
before, traits wouldn't be updated if the condition has been the same (due to the "changed" check of setStatusCondition) also reworkes the getTraitCondition to make the code better readable
1 parent 5d77f36 commit 4448ced

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

internal/controller/traits_controller.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ func (tc *TraitsController) Reconcile(ctx context.Context, req ctrl.Request) (ct
9292
// fetch current traits, to ensure we don't add duplicates
9393
current, err := resourceproviders.GetTraits(ctx, tc.serviceClient, hv.Status.HypervisorID).Extract()
9494
if err != nil {
95-
return ctrl.Result{}, errors.Join(err, tc.UpdateStatusCondition(ctx, hv, err, "Failed to get current traits from placement"))
95+
if meta.SetStatusCondition(&hv.Status.Conditions,
96+
getTraitCondition(err, "Failed to get current traits from placement")) {
97+
err = errors.Join(tc.Status().Update(ctx, hv))
98+
}
99+
return ctrl.Result{}, err
96100
}
97101

98102
var targetTraits []string
@@ -119,17 +123,22 @@ func (tc *TraitsController) Reconcile(ctx context.Context, req ctrl.Request) (ct
119123

120124
if result.Err != nil {
121125
// set status condition
122-
return ctrl.Result{}, errors.Join(result.Err, tc.UpdateStatusCondition(ctx, hv, result.Err, "Failed to update traits in placement"))
126+
if meta.SetStatusCondition(&hv.Status.Conditions,
127+
getTraitCondition(err, "Failed to update traits in placement")) {
128+
err = errors.Join(tc.Status().Update(ctx, hv))
129+
}
130+
return ctrl.Result{}, err
123131
}
124132
}
125133

126-
// update status
134+
// update status unconditionally, since we want always to propagate the current traits
127135
hv.Status.Traits = targetTraits
128-
return ctrl.Result{}, tc.UpdateStatusCondition(ctx, hv, nil, "Traits successfully updated")
136+
meta.SetStatusCondition(&hv.Status.Conditions, getTraitCondition(nil, "Traits successfully updated"))
137+
return ctrl.Result{}, tc.Status().Update(ctx, hv)
129138
}
130139

131-
// UpdateStatusCondition updates the TraitsUpdated condition of the Hypervisor status and handles conflicts by retrying.
132-
func (tc *TraitsController) UpdateStatusCondition(ctx context.Context, hv *kvmv1.Hypervisor, err error, msg string) error {
140+
// getTraitCondition creates a Condition object for trait updates
141+
func getTraitCondition(err error, msg string) metav1.Condition {
133142
// set status condition
134143
var (
135144
reason = ConditionTraitsSuccess
@@ -147,16 +156,12 @@ func (tc *TraitsController) UpdateStatusCondition(ctx context.Context, hv *kvmv1
147156
}
148157
}
149158

150-
if meta.SetStatusCondition(&hv.Status.Conditions, metav1.Condition{
159+
return metav1.Condition{
151160
Type: ConditionTypeTraitsUpdated,
152161
Status: status,
153162
Reason: reason,
154163
Message: message,
155-
}) {
156-
return tc.Status().Update(ctx, hv)
157164
}
158-
159-
return nil
160165
}
161166

162167
// SetupWithManager sets up the controller with the Manager.

0 commit comments

Comments
 (0)