Skip to content

Commit 2d210d0

Browse files
authored
Merge pull request #1965 from rstefan1/implement-ignore-already-exists
✨ Implement IgnoreAlreadyExists
2 parents b792a7d + c2c26e3 commit 2d210d0

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

pkg/client/client_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3403,6 +3403,32 @@ var _ = Describe("IgnoreNotFound", func() {
34033403
})
34043404
})
34053405

3406+
var _ = Describe("IgnoreAlreadyExists", func() {
3407+
It("should return nil on a 'AlreadyExists' error", func() {
3408+
By("creating a AlreadyExists error")
3409+
err := apierrors.NewAlreadyExists(schema.GroupResource{}, "")
3410+
3411+
By("returning no error")
3412+
Expect(client.IgnoreAlreadyExists(err)).To(Succeed())
3413+
})
3414+
3415+
It("should return the error on a status other than already exists", func() {
3416+
By("creating a BadRequest error")
3417+
err := apierrors.NewBadRequest("")
3418+
3419+
By("returning an error")
3420+
Expect(client.IgnoreAlreadyExists(err)).To(HaveOccurred())
3421+
})
3422+
3423+
It("should return the error on a non-status error", func() {
3424+
By("creating an fmt error")
3425+
err := fmt.Errorf("arbitrary error")
3426+
3427+
By("returning an error")
3428+
Expect(client.IgnoreAlreadyExists(err)).To(HaveOccurred())
3429+
})
3430+
})
3431+
34063432
type fakeReader struct {
34073433
Called int
34083434
}

pkg/client/interfaces.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,13 @@ func IgnoreNotFound(err error) error {
143143
}
144144
return err
145145
}
146+
147+
// IgnoreAlreadyExists returns nil on AlreadyExists errors.
148+
// All other values that are not AlreadyExists errors or nil are returned unmodified.
149+
func IgnoreAlreadyExists(err error) error {
150+
if apierrors.IsAlreadyExists(err) {
151+
return nil
152+
}
153+
154+
return err
155+
}

0 commit comments

Comments
 (0)