Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Implement IgnoreAlreadyExists #1965

Merged
merged 1 commit into from Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions pkg/client/client_test.go
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
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
}