Skip to content

Commit

Permalink
Fix string comparison on not found resources
Browse files Browse the repository at this point in the history
This converts the errors to the correct types and then checks if the
resources are actually not found.

Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
  • Loading branch information
saschagrunert authored and k8s-ci-robot committed May 26, 2023
1 parent 5fb4149 commit d09257a
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions internal/pkg/manager/spod/bindata/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ package bindata

import (
"context"
"errors"
"fmt"
"strings"

certmanagerv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
certmanagermetav1 "github.com/cert-manager/cert-manager/pkg/apis/meta/v1"
"github.com/go-logr/logr"
configv1 "github.com/openshift/api/config/v1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/discovery"
"sigs.k8s.io/controller-runtime/pkg/client"

"sigs.k8s.io/security-profiles-operator/internal/pkg/config"
Expand Down Expand Up @@ -69,14 +71,24 @@ func GetCAInjectType(
func IsNotFound(err error) bool {
if runtime.IsNotRegisteredError(err) ||
meta.IsNoMatchError(err) ||
errors.IsNotFound(err) {
apierrors.IsNotFound(err) {
return true
}

// Introduced in controller-runtime v0.15.0, which makes a simple
// `apierrors.IsNotFound(err)` not work any more.
groupErr := &discovery.ErrGroupDiscoveryFailed{}
if errors.As(err, &groupErr) {
for _, err := range groupErr.Groups {
if apierrors.IsNotFound(err) {
return true
}
}
}

// Fallback
for _, msg := range []string{
"failed to get restmapping",
"could not find the requested resource",
} {
if strings.Contains(err.Error(), msg) {
return true
Expand Down Expand Up @@ -116,7 +128,7 @@ func GetCertManagerResources(namespace string) *CertManagerResources {
func (c *CertManagerResources) Create(ctx context.Context, cl client.Client) error {
for k, o := range c.objectMap() {
if err := cl.Create(ctx, o); err != nil {
if errors.IsAlreadyExists(err) {
if apierrors.IsAlreadyExists(err) {
continue
}
return fmt.Errorf("creating %s: %w", k, err)
Expand Down

0 comments on commit d09257a

Please sign in to comment.