Skip to content

Commit

Permalink
Implement IgnoreAlreadyExists
Browse files Browse the repository at this point in the history
  • Loading branch information
rstefan1 committed Aug 17, 2022
1 parent f035121 commit c2c26e3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
26 changes: 26 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3403,6 +3403,32 @@ var _ = Describe("IgnoreNotFound", func() {
})
})

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

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

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

By("returning an error")
Expect(client.IgnoreAlreadyExists(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.IgnoreAlreadyExists(err)).To(HaveOccurred())
})
})

type fakeReader struct {
Called int
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/client/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,13 @@ func IgnoreNotFound(err error) error {
}
return err
}

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

return err
}

0 comments on commit c2c26e3

Please sign in to comment.