Skip to content

Commit

Permalink
Adjust the error to check for the ConstraintTemplate CRD missing
Browse files Browse the repository at this point in the history
After updating to controller-runtime 0.15, it seems that a different
error is returned when a CRD is missing, so it caused an API server hit
every time the policy template sync controller reconciled. This uses the
new error.

Relates:
stolostron/backlog#27375

Signed-off-by: mprahl <mprahl@users.noreply.github.com>
  • Loading branch information
mprahl committed Sep 15, 2023
1 parent 03bd852 commit ebd2e16
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions controllers/templatesync/template_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,7 @@ func (r *PolicyReconciler) cleanUpExcessTemplates(
}

tmplGVRs := []gvrScoped{}
var discoveryErr *discovery.ErrGroupDiscoveryFailed

// Query for Constraints if one was already synced successfully or the boolean is not yet set
if r.createdGkConstraint == nil || *r.createdGkConstraint {
Expand Down Expand Up @@ -995,7 +996,7 @@ func (r *PolicyReconciler) cleanUpExcessTemplates(
namespaced: false,
})
}
} else if meta.IsNoMatchError(err) {
} else if errors.As(err, &discoveryErr) {
// If there's no v1 ConstraintTemplate, try the v1beta1 version
gkConstraintTemplateListv1beta1 := gktemplatesv1beta1.ConstraintTemplateList{}
err := r.List(ctx, &gkConstraintTemplateListv1beta1, &client.ListOptions{})
Expand Down Expand Up @@ -1026,7 +1027,7 @@ func (r *PolicyReconciler) cleanUpExcessTemplates(
})
}
// Log and ignore other errors to allow cleanup to continue since Gatekeeper may not be installed
} else if meta.IsNoMatchError(err) {
} else if errors.As(err, &discoveryErr) {
reqLogger.Info("The ConstraintTemplate CRD is not installed")
r.setCreatedGkConstraint(false)
} else {
Expand Down Expand Up @@ -1059,7 +1060,7 @@ func (r *PolicyReconciler) cleanUpExcessTemplates(
})
}
}
} else if meta.IsNoMatchError(err) {
} else if errors.As(err, &discoveryErr) {
crdsv1beta1 := extensionsv1beta1.CustomResourceDefinitionList{}
err := r.List(ctx, &crdsv1beta1, &crdQuery)
if err != nil {
Expand Down

0 comments on commit ebd2e16

Please sign in to comment.