Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle policy recreation race condition #221

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions controllers/configurationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2982,6 +2982,7 @@ func (r *ConfigurationPolicyReconciler) updatePolicyStatus(
"Updating configurationPolicy status", "status", policy.Status.ComplianceState, "policy", policy.GetName(),
)

evaluatedUID := policy.UID
updatedStatus := policy.Status

maxRetries := 3
Expand All @@ -2991,6 +2992,19 @@ func (r *ConfigurationPolicyReconciler) updatePolicyStatus(
log.Info(fmt.Sprintf("Failed to refresh policy; using previously fetched version: %s", err))
} else {
policy.Status = updatedStatus

// If the UID has changed, then the policy has been deleted and created again. Do not update the status,
// because it was calculated based on a previous version. If sendEvent is true, that event might be useful
// and it can be emitted. By leaving the status blank, the policy will be reevaluated and send a new event.
if evaluatedUID != policy.UID {
log.Info("The ConfigurationPolicy was recreated after it was evaluated. Skipping the status update.")

// Reset the original UID so that if there are more status updates (i.e. batches), the status on the
// API server is never updated.
policy.UID = evaluatedUID

break
}
}

err = r.Status().Update(context.TODO(), policy)
Expand Down