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

Add IsMissingNamespaceErr function #905

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions pkg/resource/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,16 @@

return false
}

func IsMissingNamespaceErr(err error) (bool, string) {

Check failure on line 46 in pkg/resource/errors.go

View workflow job for this annotation

GitHub Actions / Go

unnamedResult: consider giving a name to these results (gocritic)
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
}
28 changes: 28 additions & 0 deletions pkg/resource/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
})
})
Loading