Skip to content

Commit

Permalink
Stop updating on every reconcile when pruneObjectBehavior is not set
Browse files Browse the repository at this point in the history
equivalentTemplates would always return false when a ConfigurationPolicy
was encountered without pruneObjectBehavior explicitly set since the
Kubernetes API returns "None" as the default value.

This isn't an ideal solution, but it's inexpensive compared to
performing API queries to see what defaults are and this is a common
case.

Signed-off-by: mprahl <[email protected]>
  • Loading branch information
mprahl authored and dhaiducek committed Apr 4, 2023
1 parent acc8c3d commit c383fcb
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion controllers/templatesync/template_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -821,8 +821,19 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
return reconcile.Result{}, resultError
}

// equivalentTemplates determines whether the template existing on the cluster and the policy template are the same
// equivalentTemplates determines whether the template existing on the cluster and the policy template are the same.
// Any missing defaults this function is aware of will be set on tObject.
func equivalentTemplates(eObject *unstructured.Unstructured, tObject *unstructured.Unstructured) bool {
if tObject.GetKind() == "ConfigurationPolicy" {
pruneObjectBehavior, _, _ := unstructured.NestedString(tObject.Object, "spec", "pruneObjectBehavior")
if pruneObjectBehavior == "" {
err := unstructured.SetNestedField(tObject.Object, "None", "spec", "pruneObjectBehavior")
if err != nil {
log.Error(err, "Failed to set the default value of pruneObjectBehavior for")
}
}
}

if !equality.Semantic.DeepEqual(eObject.UnstructuredContent()["spec"], tObject.Object["spec"]) {
return false
}
Expand Down

0 comments on commit c383fcb

Please sign in to comment.