Repository: kubernetes-sigs/security-profiles-operator
Pull request #1710 (2023-05-26 09:16:23)

Fix string comparison on not found resources

#### What type of PR is this?


/kind cleanup


#### What this PR does / why we need it:
This converts the errors to the correct types and then checks if the resources are actually not found.


#### Which issue(s) this PR fixes:

<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.

Fixes #

or

None
-->
Refers to https://github.com/kubernetes-sigs/controller-runtime/issues/2354
#### Does this PR have test?
None
<!--
If tests aren't applicable just write N/A.
-->

#### Special notes for your reviewer:
None
#### Does this PR introduce a user-facing change?

<!--
If no, just write "NONE" in the release-note block below.
If yes, a release note is required:
Enter your extended release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required".

For more information on release notes see: https://git.k8s.io/community/contributors/guide/release-notes.md
-->

```release-note
None
```


k8s-ci-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubernetes-sigs/security-profiles-operator/pull/1710#" title="Author self-approved">saschagrunert</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubernetes-sigs%2Fsecurity-profiles-operator).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/kubernetes-sigs/security-profiles-operator/blob/main/OWNERS)~~ [saschagrunert]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
codecov-commenter: ## [Codecov](https://app.codecov.io/gh/kubernetes-sigs/security-profiles-operator/pull/1710?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubernetes-sigs) Report
> Merging [#1710](https://app.codecov.io/gh/kubernetes-sigs/security-profiles-operator/pull/1710?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubernetes-sigs) (b2a8d06) into [main](https://app.codecov.io/gh/kubernetes-sigs/security-profiles-operator/commit/75f83ca3da8c864b6364abc1035a0f8a04050a82?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubernetes-sigs) (75f83ca) will **not change** coverage.
> The diff coverage is `n/a`.

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main    #1710   +/-   ##
=======================================
  Coverage   49.23%   49.23%           
=======================================
  Files          71       71           
  Lines        6946     6946           
=======================================
  Hits         3420     3420           
  Misses       3396     3396           
  Partials      130      130           
```

</details>

Patch:
diff --git a/internal/pkg/manager/spod/bindata/ca.go b/internal/pkg/manager/spod/bindata/ca.go
index 7c168cbbb..7dffebb18 100644
--- a/internal/pkg/manager/spod/bindata/ca.go
+++ b/internal/pkg/manager/spod/bindata/ca.go
@@ -18,6 +18,7 @@ package bindata
 
 import (
 	"context"
+	"errors"
 	"fmt"
 	"strings"
 
@@ -25,11 +26,12 @@ import (
 	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"
@@ -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
@@ -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)


Test patch:
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 911e0a92b..b76449842 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -19,11 +19,11 @@ jobs:
     runs-on: ubuntu-22.04
     steps:
       - uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
-      - uses: github/codeql-action/init@f0e3dfb30302f8a0881bb509b044e0de4f6ef589
+      - uses: github/codeql-action/init@0225834cc549ee0ca93cb085b92954821a145866
         with:
           languages: go
-      - uses: github/codeql-action/autobuild@f0e3dfb30302f8a0881bb509b044e0de4f6ef589
-      - uses: github/codeql-action/analyze@f0e3dfb30302f8a0881bb509b044e0de4f6ef589
+      - uses: github/codeql-action/autobuild@0225834cc549ee0ca93cb085b92954821a145866
+      - uses: github/codeql-action/analyze@0225834cc549ee0ca93cb085b92954821a145866
 
   coverage:
     runs-on: ubuntu-22.04