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

Automated backport of #905: Add IsMissingNamespaceErr function #908

Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ linters-settings:
- opinionated
- performance
- style
disabled-checks:
- ifElseChain
- unnamedResult
gocyclo:
min-complexity: 15
goheader:
Expand Down
38 changes: 38 additions & 0 deletions pkg/resource/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
SPDX-License-Identifier: Apache-2.0

Copyright Contributors to the Submariner project.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package resource

import (
"errors"

apierrors "k8s.io/apimachinery/pkg/api/errors"
)

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
}
55 changes: 55 additions & 0 deletions pkg/resource/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
SPDX-License-Identifier: Apache-2.0

Copyright Contributors to the Submariner project.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package resource_test

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/submariner-io/admiral/pkg/resource"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
)

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