Skip to content

Commit 24ce9a4

Browse files
committed
Implement IgnoreNotFound
implement `IgnoreNotFound` that ignores `NotFound` errors create test cases
1 parent d6324a4 commit 24ce9a4

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

pkg/client/client_test.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
. "github.com/onsi/gomega"
2929
appsv1 "k8s.io/api/apps/v1"
3030
corev1 "k8s.io/api/core/v1"
31-
"k8s.io/apimachinery/pkg/api/errors"
31+
apierrors "k8s.io/apimachinery/pkg/api/errors"
3232
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3333
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3434
"k8s.io/apimachinery/pkg/runtime"
@@ -232,7 +232,7 @@ var _ = Describe("Client", func() {
232232
By("creating the object a second time")
233233
err = cl.Create(context.TODO(), old)
234234
Expect(err).To(HaveOccurred())
235-
Expect(errors.IsAlreadyExists(err)).To(BeTrue())
235+
Expect(apierrors.IsAlreadyExists(err)).To(BeTrue())
236236

237237
close(done)
238238
})
@@ -281,7 +281,7 @@ var _ = Describe("Client", func() {
281281

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

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

376376
close(done)
377377
})
@@ -420,7 +420,7 @@ var _ = Describe("Client", func() {
420420

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

426426
close(done)
@@ -2255,6 +2255,32 @@ var _ = Describe("Patch", func() {
22552255
})
22562256
})
22572257

2258+
var _ = Describe("IgnoreNotFound", func() {
2259+
It("should return nil on a 'NotFound' error", func() {
2260+
By("creating a NotFound error")
2261+
err := apierrors.NewNotFound(schema.GroupResource{}, "")
2262+
2263+
By("returning no error")
2264+
Expect(client.IgnoreNotFound(err)).To(Succeed())
2265+
})
2266+
2267+
It("should return the error on a status other than not found", func() {
2268+
By("creating a BadRequest error")
2269+
err := apierrors.NewBadRequest("")
2270+
2271+
By("returning an error")
2272+
Expect(client.IgnoreNotFound(err)).To(HaveOccurred())
2273+
})
2274+
2275+
It("should return the error on a non-status error", func() {
2276+
By("creating an fmt error")
2277+
err := fmt.Errorf("arbitrary error")
2278+
2279+
By("returning an error")
2280+
Expect(client.IgnoreNotFound(err)).To(HaveOccurred())
2281+
})
2282+
})
2283+
22582284
type fakeReader struct {
22592285
Called int
22602286
}

pkg/client/interfaces.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package client
1919
import (
2020
"context"
2121

22+
apierrors "k8s.io/apimachinery/pkg/api/errors"
23+
2224
"k8s.io/apimachinery/pkg/api/meta"
2325
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2426
"k8s.io/apimachinery/pkg/fields"
@@ -471,3 +473,12 @@ func UpdatePatchWith(optFuncs ...UpdateOptionFunc) PatchOptionFunc {
471473
}
472474
}
473475
}
476+
477+
// IgnoreNotFound returns nil on NotFound errors.
478+
// All other values that are not NotFound errors or nil are returned unmodified.
479+
func IgnoreNotFound(err error) error {
480+
if apierrors.IsNotFound(err) {
481+
return nil
482+
}
483+
return err
484+
}

0 commit comments

Comments
 (0)