Skip to content

Commit

Permalink
Merge pull request #428 from adracus/feature.ignore-not-found
Browse files Browse the repository at this point in the history
✨ Implement `IgnoreNotFound`
  • Loading branch information
k8s-ci-robot authored May 17, 2019
2 parents 13bd9ee + 794ef6d commit 3511f3c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
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
}

0 comments on commit 3511f3c

Please sign in to comment.