Skip to content

Commit

Permalink
Detect the correct error for resource not found
Browse files Browse the repository at this point in the history
Updating controller-runtime seems to have changed the error.

Relates:
https://issues.redhat.com/browse/ACM-11498

Signed-off-by: mprahl <mprahl@users.noreply.github.com>
(cherry picked from commit 6125983)
  • Loading branch information
mprahl authored and magic-mirror-bot[bot] committed May 7, 2024
1 parent b26d25d commit 20ed8d1
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions controllers/templatesync/template_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
extensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/apimachinery/pkg/api/equality"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
Expand All @@ -38,7 +39,6 @@ import (
"open-cluster-management.io/governance-policy-propagator/controllers/common"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand All @@ -52,10 +52,7 @@ const (
ControllerName string = "policy-template-sync"
)

var (
log = ctrl.Log.WithName(ControllerName)
errResourceDiscoveryFailed *apiutil.ErrResourceDiscoveryFailed
)
var log = ctrl.Log.WithName(ControllerName)

//+kubebuilder:rbac:groups=policy.open-cluster-management.io,resources=*,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=templates.gatekeeper.sh,resources=constrainttemplates,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -1033,7 +1030,7 @@ func (r *PolicyReconciler) cleanUpExcessTemplates(
namespaced: false,
})
}
} else if errors.As(err, &errResourceDiscoveryFailed) {
} else if apimeta.IsNoMatchError(err) {
// If there's no v1 ConstraintTemplate, try the v1beta1 version
gkConstraintTemplateListv1beta1 := gktemplatesv1beta1.ConstraintTemplateList{}
err := r.List(ctx, &gkConstraintTemplateListv1beta1, &client.ListOptions{})
Expand Down Expand Up @@ -1066,7 +1063,7 @@ func (r *PolicyReconciler) cleanUpExcessTemplates(
})
}
// Log and ignore other errors to allow cleanup to continue since Gatekeeper may not be installed
} else if errors.As(err, &errResourceDiscoveryFailed) {
} else if apimeta.IsNoMatchError(err) {
reqLogger.Info("The ConstraintTemplate CRD is not installed")
r.setCreatedGkConstraint(false)
} else {
Expand Down Expand Up @@ -1101,7 +1098,7 @@ func (r *PolicyReconciler) cleanUpExcessTemplates(
})
}
}
} else if errors.As(err, &errResourceDiscoveryFailed) {
} else if apimeta.IsNoMatchError(err) {
crdsv1beta1 := extensionsv1beta1.CustomResourceDefinitionList{}
err := r.List(ctx, &crdsv1beta1, &crdQuery)
if err != nil {
Expand Down Expand Up @@ -1557,7 +1554,7 @@ func (r *PolicyReconciler) hasPolicyTemplateLabel(
err := r.Get(ctx, crdName, &crd)
if err == nil {
return crd.GetLabels()[utils.PolicyTypeLabel] == "template", nil
} else if errors.As(err, &errResourceDiscoveryFailed) {
} else if apimeta.IsNoMatchError(err) {
betaCrd := extensionsv1beta1.CustomResourceDefinition{}

err = r.Get(ctx, crdName, &betaCrd)
Expand Down

0 comments on commit 20ed8d1

Please sign in to comment.