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

✨ Implement IgnoreNotFound #428

Merged
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
36 changes: 31 additions & 5 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -232,7 +232,7 @@ var _ = Describe("Client", func() {
By("creating the object a second time")
err = cl.Create(context.TODO(), old)
Expect(err).To(HaveOccurred())
Expect(errors.IsAlreadyExists(err)).To(BeTrue())
Expect(apierrors.IsAlreadyExists(err)).To(BeTrue())

close(done)
})
Expand Down Expand Up @@ -281,7 +281,7 @@ var _ = Describe("Client", func() {

actual, err := clientset.AppsV1().Deployments(ns).Get(dep.Name, metav1.GetOptions{})
Expect(err).To(HaveOccurred())
Expect(errors.IsNotFound(err)).To(BeTrue())
Expect(apierrors.IsNotFound(err)).To(BeTrue())
Expect(actual).To(Equal(&appsv1.Deployment{}))

close(done)
Expand Down Expand Up @@ -371,7 +371,7 @@ var _ = Describe("Client", func() {
By("creating the object a second time")
err = cl.Create(context.TODO(), u)
Expect(err).To(HaveOccurred())
Expect(errors.IsAlreadyExists(err)).To(BeTrue())
Expect(apierrors.IsAlreadyExists(err)).To(BeTrue())

close(done)
})
Expand Down Expand Up @@ -420,7 +420,7 @@ var _ = Describe("Client", func() {

actual, err := clientset.AppsV1().Deployments(ns).Get(dep.Name, metav1.GetOptions{})
Expect(err).To(HaveOccurred())
Expect(errors.IsNotFound(err)).To(BeTrue())
Expect(apierrors.IsNotFound(err)).To(BeTrue())
Expect(actual).To(Equal(&appsv1.Deployment{}))

close(done)
Expand Down Expand Up @@ -2279,6 +2279,32 @@ var _ = Describe("Patch", func() {
})
})

var _ = Describe("IgnoreNotFound", func() {
It("should return nil on a 'NotFound' error", func() {
By("creating a NotFound error")
err := apierrors.NewNotFound(schema.GroupResource{}, "")

By("returning no error")
Expect(client.IgnoreNotFound(err)).To(Succeed())
})

It("should return the error on a status other than not found", func() {
By("creating a BadRequest error")
err := apierrors.NewBadRequest("")

By("returning an error")
Expect(client.IgnoreNotFound(err)).To(HaveOccurred())
})

It("should return the error on a non-status error", func() {
By("creating an fmt error")
err := fmt.Errorf("arbitrary error")

By("returning an error")
Expect(client.IgnoreNotFound(err)).To(HaveOccurred())
})
})

type fakeReader struct {
Called int
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/client/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package client
import (
"context"

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/fields"
Expand Down Expand Up @@ -504,3 +506,12 @@ func PatchWithForce() PatchOptionFunc {
opts.Force = &force
}
}

// IgnoreNotFound returns nil on NotFound errors.
// All other values that are not NotFound errors or nil are returned unmodified.
func IgnoreNotFound(err error) error {
if apierrors.IsNotFound(err) {
return nil
}
return err
}