Skip to content

Commit

Permalink
✨ Add additional SubResource* functions for FieldOwner (#2153)
Browse files Browse the repository at this point in the history
* Add additional SubResource* functions for FieldOwner

This enhances #2072 to allow FieldOwner work with SubResources to set the
FieldManager option.

* Typos

* Apply gofmt -s

* Add tests for FieldOwner

---------

Co-authored-by: David Schmitt <david.schmitt@overmind.tech>
  • Loading branch information
k8s-infra-cherrypick-robot and DavidS-ovm committed Jan 29, 2023
1 parent 7a4c6ec commit 8ad99d8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 12 deletions.
15 changes: 15 additions & 0 deletions pkg/client/options.go
Expand Up @@ -154,6 +154,21 @@ func (f FieldOwner) ApplyToUpdate(opts *UpdateOptions) {
opts.FieldManager = string(f)
}

// ApplyToSubResourcePatch applies this configuration to the given patch options.
func (f FieldOwner) ApplyToSubResourcePatch(opts *SubResourcePatchOptions) {
opts.FieldManager = string(f)
}

// ApplyToSubResourceCreate applies this configuration to the given create options.
func (f FieldOwner) ApplyToSubResourceCreate(opts *SubResourceCreateOptions) {
opts.FieldManager = string(f)
}

// ApplyToSubResourceUpdate applies this configuration to the given update options.
func (f FieldOwner) ApplyToSubResourceUpdate(opts *SubResourceUpdateOptions) {
opts.FieldManager = string(f)
}

// }}}

// {{{ Create Options
Expand Down
63 changes: 51 additions & 12 deletions pkg/client/options_test.go
Expand Up @@ -86,27 +86,27 @@ var _ = Describe("GetOptions", func() {
var _ = Describe("CreateOptions", func() {
It("Should set DryRun", func() {
o := &client.CreateOptions{DryRun: []string{"Hello", "Theodore"}}
newCreatOpts := &client.CreateOptions{}
o.ApplyToCreate(newCreatOpts)
Expect(newCreatOpts).To(Equal(o))
newCreateOpts := &client.CreateOptions{}
o.ApplyToCreate(newCreateOpts)
Expect(newCreateOpts).To(Equal(o))
})
It("Should set FieldManager", func() {
o := &client.CreateOptions{FieldManager: "FieldManager"}
newCreatOpts := &client.CreateOptions{}
o.ApplyToCreate(newCreatOpts)
Expect(newCreatOpts).To(Equal(o))
newCreateOpts := &client.CreateOptions{}
o.ApplyToCreate(newCreateOpts)
Expect(newCreateOpts).To(Equal(o))
})
It("Should set Raw", func() {
o := &client.CreateOptions{Raw: &metav1.CreateOptions{DryRun: []string{"Bye", "Theodore"}}}
newCreatOpts := &client.CreateOptions{}
o.ApplyToCreate(newCreatOpts)
Expect(newCreatOpts).To(Equal(o))
newCreateOpts := &client.CreateOptions{}
o.ApplyToCreate(newCreateOpts)
Expect(newCreateOpts).To(Equal(o))
})
It("Should not set anything", func() {
o := &client.CreateOptions{}
newCreatOpts := &client.CreateOptions{}
o.ApplyToCreate(newCreatOpts)
Expect(newCreatOpts).To(Equal(o))
newCreateOpts := &client.CreateOptions{}
o.ApplyToCreate(newCreateOpts)
Expect(newCreateOpts).To(Equal(o))
})
})

Expand Down Expand Up @@ -238,3 +238,42 @@ var _ = Describe("MatchingLabels", func() {
Expect(err.Error()).To(Equal(expectedErrMsg))
})
})

var _ = Describe("FieldOwner", func() {
It("Should apply to PatchOptions", func() {
o := &client.PatchOptions{FieldManager: "bar"}
t := client.FieldOwner("foo")
t.ApplyToPatch(o)
Expect(o.FieldManager).To(Equal("foo"))
})
It("Should apply to CreateOptions", func() {
o := &client.CreateOptions{FieldManager: "bar"}
t := client.FieldOwner("foo")
t.ApplyToCreate(o)
Expect(o.FieldManager).To(Equal("foo"))
})
It("Should apply to UpdateOptions", func() {
o := &client.UpdateOptions{FieldManager: "bar"}
t := client.FieldOwner("foo")
t.ApplyToUpdate(o)
Expect(o.FieldManager).To(Equal("foo"))
})
It("Should apply to SubResourcePatchOptions", func() {
o := &client.SubResourcePatchOptions{PatchOptions: client.PatchOptions{FieldManager: "bar"}}
t := client.FieldOwner("foo")
t.ApplyToSubResourcePatch(o)
Expect(o.FieldManager).To(Equal("foo"))
})
It("Should apply to SubResourceCreateOptions", func() {
o := &client.SubResourceCreateOptions{CreateOptions: client.CreateOptions{FieldManager: "bar"}}
t := client.FieldOwner("foo")
t.ApplyToSubResourceCreate(o)
Expect(o.FieldManager).To(Equal("foo"))
})
It("Should apply to SubResourceUpdateOptions", func() {
o := &client.SubResourceUpdateOptions{UpdateOptions: client.UpdateOptions{FieldManager: "bar"}}
t := client.FieldOwner("foo")
t.ApplyToSubResourceUpdate(o)
Expect(o.FieldManager).To(Equal("foo"))
})
})

0 comments on commit 8ad99d8

Please sign in to comment.