Skip to content

Commit

Permalink
Add field manager & dryrun to other opts
Browse files Browse the repository at this point in the history
For field manager, update and create also support fieldmanager, so we
allow using them there.

For dryrun, it's also supported by delete, so we support setting it
there.
  • Loading branch information
DirectXMan12 committed Jul 24, 2019
1 parent 68d92bb commit 320b6b6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
19 changes: 19 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2005,6 +2005,12 @@ var _ = Describe("Client", func() {
Expect(co.AsCreateOptions().DryRun).To(Equal(all))
})

It("should allow setting the field manager", func() {
po := &client.CreateOptions{}
client.FieldOwner("some-owner").ApplyToCreate(po)
Expect(po.AsCreateOptions().FieldManager).To(Equal("some-owner"))
})

It("should produce empty metav1.CreateOptions if nil", func() {
var co *client.CreateOptions
Expect(co.AsCreateOptions()).To(Equal(&metav1.CreateOptions{}))
Expand Down Expand Up @@ -2036,6 +2042,13 @@ var _ = Describe("Client", func() {
Expect(do.AsDeleteOptions().PropagationPolicy).To(Equal(&dp))
})

It("should allow setting DryRun", func() {
do := &client.DeleteOptions{}
client.DryRunAll.ApplyToDelete(do)
all := []string{metav1.DryRunAll}
Expect(do.AsDeleteOptions().DryRun).To(Equal(all))
})

It("should produce empty metav1.DeleteOptions if nil", func() {
var do *client.DeleteOptions
Expect(do.AsDeleteOptions()).To(Equal(&metav1.DeleteOptions{}))
Expand Down Expand Up @@ -2102,6 +2115,12 @@ var _ = Describe("Client", func() {
Expect(uo.AsUpdateOptions().DryRun).To(Equal(all))
})

It("should allow setting the field manager", func() {
po := &client.UpdateOptions{}
client.FieldOwner("some-owner").ApplyToUpdate(po)
Expect(po.AsUpdateOptions().FieldManager).To(Equal("some-owner"))
})

It("should produce empty metav1.UpdateOptions if nil", func() {
var co *client.UpdateOptions
Expect(co.AsUpdateOptions()).To(Equal(&metav1.UpdateOptions{}))
Expand Down
49 changes: 38 additions & 11 deletions pkg/client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,21 @@ func (dryRunAll) ApplyToUpdate(opts *UpdateOptions) {
func (dryRunAll) ApplyToPatch(opts *PatchOptions) {
opts.DryRun = []string{metav1.DryRunAll}
}
func (dryRunAll) ApplyToDelete(opts *DeleteOptions) {
opts.DryRun = []string{metav1.DryRunAll}
}

// FieldOwner set the field manager name for the given server-side apply patch.
type FieldOwner string

func (f FieldOwner) ApplyToPatch(opts *PatchOptions) {
opts.FieldManager = string(f)
}

// ForceOwnership indicates that in case of conflicts with server-side apply,
// the client should acquire ownership of the conflicting field. Most
// controllers should use this.
var ForceOwnership = forceOwnership{}

type forceOwnership struct{}

func (forceOwnership) ApplyToPatch(opts *PatchOptions) {
definitelyTrue := true
opts.Force = &definitelyTrue
func (f FieldOwner) ApplyToCreate(opts *CreateOptions) {
opts.FieldManager = string(f)
}
func (f FieldOwner) ApplyToUpdate(opts *UpdateOptions) {
opts.FieldManager = string(f)
}

// }}}
Expand All @@ -107,6 +104,10 @@ type CreateOptions struct {
// - All: all dry run stages will be processed
DryRun []string

// FieldManager is the name of the user or component submitting
// this request. It must be set with server-side apply.
FieldManager string

// Raw represents raw CreateOptions, as passed to the API server.
Raw *metav1.CreateOptions
}
Expand All @@ -122,6 +123,7 @@ func (o *CreateOptions) AsCreateOptions() *metav1.CreateOptions {
}

o.Raw.DryRun = o.DryRun
o.Raw.FieldManager = o.FieldManager
return o.Raw
}

Expand Down Expand Up @@ -168,6 +170,13 @@ type DeleteOptions struct {

// Raw represents raw DeleteOptions, as passed to the API server.
Raw *metav1.DeleteOptions

// When present, indicates that modifications should not be
// persisted. An invalid or unrecognized dryRun directive will
// result in an error response and no further processing of the
// request. Valid values are:
// - All: all dry run stages will be processed
DryRun []string
}

// AsDeleteOptions returns these options as a metav1.DeleteOptions.
Expand All @@ -183,6 +192,7 @@ func (o *DeleteOptions) AsDeleteOptions() *metav1.DeleteOptions {
o.Raw.GracePeriodSeconds = o.GracePeriodSeconds
o.Raw.Preconditions = o.Preconditions
o.Raw.PropagationPolicy = o.PropagationPolicy
o.Raw.DryRun = o.DryRun
return o.Raw
}

Expand Down Expand Up @@ -320,6 +330,10 @@ type UpdateOptions struct {
// - All: all dry run stages will be processed
DryRun []string

// FieldManager is the name of the user or component submitting
// this request. It must be set with server-side apply.
FieldManager string

// Raw represents raw UpdateOptions, as passed to the API server.
Raw *metav1.UpdateOptions
}
Expand All @@ -335,6 +349,7 @@ func (o *UpdateOptions) AsUpdateOptions() *metav1.UpdateOptions {
}

o.Raw.DryRun = o.DryRun
o.Raw.FieldManager = o.FieldManager
return o.Raw
}

Expand Down Expand Up @@ -404,6 +419,18 @@ func (o *PatchOptions) AsPatchOptions() *metav1.PatchOptions {
return o.Raw
}

// ForceOwnership indicates that in case of conflicts with server-side apply,
// the client should acquire ownership of the conflicting field. Most
// controllers should use this.
var ForceOwnership = forceOwnership{}

type forceOwnership struct{}

func (forceOwnership) ApplyToPatch(opts *PatchOptions) {
definitelyTrue := true
opts.Force = &definitelyTrue
}

// PatchDryRunAll sets the "dry run" option to "all".
//
// Deprecated: Use DryRunAll
Expand Down

0 comments on commit 320b6b6

Please sign in to comment.