Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix string comparison on not found resources #1710

Merged
merged 1 commit into from
May 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading