From 22ee51817b45639e834ed90910f8137c1ec3f0de Mon Sep 17 00:00:00 2001 From: Tom Pantelis Date: Fri, 3 May 2024 13:26:08 -0400 Subject: [PATCH 1/2] Add IsMissingNamespaceErr function Checks if the error indicates the namespace for a resource is missing when trying to create it. In this case the error is NotFound and the status details reflects the "namespaces" Kind. For example: { "ErrStatus": { "status": "Failure", "message": "namespaces \"foo\" not found", "reason": "NotFound", "details": { "name": "foo", "kind": "namespaces" }, "code": 404 } } Signed-off-by: Tom Pantelis --- pkg/resource/errors.go | 13 +++++++++++++ pkg/resource/errors_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/pkg/resource/errors.go b/pkg/resource/errors.go index c4b7cdfd..8e08f22e 100644 --- a/pkg/resource/errors.go +++ b/pkg/resource/errors.go @@ -42,3 +42,16 @@ func IsNotFoundErr(err error) bool { return false } + +func IsMissingNamespaceErr(err error) (bool, string) { + if !apierrors.IsNotFound(err) { + return false, "" + } + + var status apierrors.APIStatus + _ = errors.As(err, &status) + + d := status.Status().Details + + return d != nil && d.Kind == "namespaces" && d.Group == "", d.Name +} diff --git a/pkg/resource/errors_test.go b/pkg/resource/errors_test.go index f416f361..a27a60b3 100644 --- a/pkg/resource/errors_test.go +++ b/pkg/resource/errors_test.go @@ -67,3 +67,31 @@ var _ = Describe("IsNotFoundErr", func() { }) }) }) + +var _ = Describe("IsMissingNamespaceErr", func() { + When("the error isn't NotFound", func() { + It("should return false", func() { + ok, _ := resource.IsMissingNamespaceErr(apierrors.NewBadRequest("")) + Expect(ok).To(BeFalse()) + }) + }) + + When("the error details specify a namespace", func() { + It("should return true and the name", func() { + ok, name := resource.IsMissingNamespaceErr(apierrors.NewNotFound(schema.GroupResource{ + Resource: "namespaces", + }, "missing-ns")) + Expect(ok).To(BeTrue()) + Expect(name).To(Equal("missing-ns")) + }) + }) + + When("the error details does not specify a namespace", func() { + It("should return false", func() { + ok, _ := resource.IsMissingNamespaceErr(apierrors.NewNotFound(schema.GroupResource{ + Resource: "pods", + }, "missing")) + Expect(ok).To(BeFalse()) + }) + }) +}) From 2ac52729abd035e4cfc32e78b3f50754b0d3f83d Mon Sep 17 00:00:00 2001 From: Tom Pantelis Date: Mon, 6 May 2024 08:28:47 -0400 Subject: [PATCH 2/2] Disable unnamedResult et al in gocritic ...as is done in other projects. Signed-off-by: Tom Pantelis --- .golangci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.golangci.yml b/.golangci.yml index 222a1838..862804e1 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -6,6 +6,9 @@ linters-settings: - opinionated - performance - style + disabled-checks: + - ifElseChain + - unnamedResult gocyclo: min-complexity: 15 goheader: