Skip to content

Commit 794ef6d

Browse files
committed
Implement IgnoreNotFound
implement `IgnoreNotFound` that ignores `NotFound` errors create test cases
1 parent 13bd9ee commit 794ef6d

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)
@@ -2279,6 +2279,32 @@ var _ = Describe("Patch", func() {
22792279
})
22802280
})
22812281

2282+
var _ = Describe("IgnoreNotFound", func() {
2283+
It("should return nil on a 'NotFound' error", func() {
2284+
By("creating a NotFound error")
2285+
err := apierrors.NewNotFound(schema.GroupResource{}, "")
2286+
2287+
By("returning no error")
2288+
Expect(client.IgnoreNotFound(err)).To(Succeed())
2289+
})
2290+
2291+
It("should return the error on a status other than not found", func() {
2292+
By("creating a BadRequest error")
2293+
err := apierrors.NewBadRequest("")
2294+
2295+
By("returning an error")
2296+
Expect(client.IgnoreNotFound(err)).To(HaveOccurred())
2297+
})
2298+
2299+
It("should return the error on a non-status error", func() {
2300+
By("creating an fmt error")
2301+
err := fmt.Errorf("arbitrary error")
2302+
2303+
By("returning an error")
2304+
Expect(client.IgnoreNotFound(err)).To(HaveOccurred())
2305+
})
2306+
})
2307+
22822308
type fakeReader struct {
22832309
Called int
22842310
}

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"
@@ -504,3 +506,12 @@ func PatchWithForce() PatchOptionFunc {
504506
opts.Force = &force
505507
}
506508
}
509+
510+
// IgnoreNotFound returns nil on NotFound errors.
511+
// All other values that are not NotFound errors or nil are returned unmodified.
512+
func IgnoreNotFound(err error) error {
513+
if apierrors.IsNotFound(err) {
514+
return nil
515+
}
516+
return err
517+
}

0 commit comments

Comments
 (0)