Skip to content

Commit

Permalink
Retry policy evaluation when object changed during evaluation
Browse files Browse the repository at this point in the history
This is important for the dry-run to not send a noncompliant error
because the dry-run failed due to an object conflict. On the normal
update case, it saves the time it takes for the policy to be evaluated
again.

Signed-off-by: mprahl <mprahl@users.noreply.github.com>
  • Loading branch information
mprahl authored and openshift-ci[bot] committed Nov 3, 2023
1 parent ffc115c commit d2a4e04
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions controllers/configurationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2631,6 +2631,18 @@ func (r *ConfigurationPolicyReconciler) checkAndUpdateResource(
return true, "", false, false
}

// If it's a conflict, refetch the object and try again.
if k8serrors.IsConflict(err) {
log.Info("The object updating during the evaluation. Trying again.")

rv, getErr := res.Get(context.TODO(), obj.existingObj.GetName(), metav1.GetOptions{})
if getErr == nil {
obj.existingObj = rv

return r.checkAndUpdateResource(obj, complianceType, mdComplianceType, remediation)
}
}

message := getUpdateErrorMsg(err, obj.existingObj.GetKind(), obj.name)
if message == "" {
message = fmt.Sprintf(
Expand Down Expand Up @@ -2668,6 +2680,17 @@ func (r *ConfigurationPolicyReconciler) checkAndUpdateResource(
FieldValidation: metav1.FieldValidationStrict,
})
if err != nil {
if k8serrors.IsConflict(err) {
log.Info("The object updating during the evaluation. Trying again.")

rv, getErr := res.Get(context.TODO(), obj.existingObj.GetName(), metav1.GetOptions{})
if getErr == nil {
obj.existingObj = rv

return r.checkAndUpdateResource(obj, complianceType, mdComplianceType, remediation)
}
}

message := getUpdateErrorMsg(err, obj.existingObj.GetKind(), obj.name)
if message == "" {
message = fmt.Sprintf("Error updating the object `%v`, the error is `%v`", obj.name, err)
Expand Down

0 comments on commit d2a4e04

Please sign in to comment.