Skip to content

Commit

Permalink
certmanager-user: k8sutil: corrects IsAlreadyOwnedError (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
mheers authored Apr 17, 2024
1 parent 8118f60 commit f4ce2cf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Changed

- [PR #415](https://github.com/konpyutaika/nifikop/pull/415) - **[Operator]** Upgrade golang to 1.22.2.
- [PR #416](https://github.com/konpyutaika/nifikop/pull/416) - **[Operator]** Certmanager-user: k8sutil: corrects IsAlreadyOwnedError.

### Fixed Bugs

Expand Down
5 changes: 3 additions & 2 deletions pkg/k8sutil/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

v1 "github.com/konpyutaika/nifikop/api/v1"
)

// IsAlreadyOwnedError checks if a controller already own the instance.
func IsAlreadyOwnedError(err error) bool {
return errors.Is(err, &controllerutil.AlreadyOwnedError{})
errString := err.Error()
// check if "Object */* is already owned by another * controller" is in the error message
return strings.Contains(errString, "Object") && strings.Contains(errString, "is already owned by another") && strings.Contains(errString, "controller")
}

// IsMarkedForDeletion determines if the object is marked for deletion.
Expand Down
22 changes: 22 additions & 0 deletions pkg/k8sutil/status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package k8sutil

import (
"testing"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

func TestErrorIs(t *testing.T) {
err := &controllerutil.AlreadyOwnedError{
Object: &v1.ObjectMeta{},
Owner: v1.OwnerReference{
Name: "test",
},
}

is := IsAlreadyOwnedError(err)
if !is {
t.Errorf("Error is not AlreadyOwnedError")
}
}

0 comments on commit f4ce2cf

Please sign in to comment.