Skip to content

Commit

Permalink
Add refetch before updating status
Browse files Browse the repository at this point in the history
Especially when DeleteIfCreated is in use, if the status update fails,
the controller is no longer aware it created resources. A refetch
prevents the status update from failing due to conflicts.

Signed-off-by: Dale Haiducek <19750917+dhaiducek@users.noreply.github.com>
  • Loading branch information
dhaiducek authored and openshift-merge-robot committed Apr 10, 2023
1 parent 7248a00 commit f0e15dd
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions controllers/configurationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2756,9 +2756,27 @@ func (r *ConfigurationPolicyReconciler) updatePolicyStatus(
"Updating configurationPolicy status", "status", policy.Status.ComplianceState, "policy", policy.GetName(),
)

err := r.Status().Update(context.TODO(), policy)
if err != nil {
return err
updatedStatus := policy.Status

maxRetries := 3
for i := 1; i <= maxRetries; i++ {
err := r.Get(context.TODO(), types.NamespacedName{Namespace: policy.Namespace, Name: policy.Name}, policy)
if err != nil {
log.Info(fmt.Sprintf("Failed to refresh policy; using previously fetched version: %s", err))
} else {
policy.Status = updatedStatus
}

err = r.Status().Update(context.TODO(), policy)
if err != nil {
if i == maxRetries {
return err
}

log.Info(fmt.Sprintf("Failed to update policy status. Retrying (attempt %d/%d): %s", i, maxRetries, err))
} else {
break
}
}

if sendEvent {
Expand Down

0 comments on commit f0e15dd

Please sign in to comment.