diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index ff4f17f2e6..9e613d56d6 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -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{})) @@ -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{})) @@ -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{})) diff --git a/pkg/client/options.go b/pkg/client/options.go index 85d772e1fe..3a14c5eaf2 100644 --- a/pkg/client/options.go +++ b/pkg/client/options.go @@ -73,6 +73,9 @@ 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 @@ -80,17 +83,11 @@ 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) } // }}} @@ -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 } @@ -122,6 +123,7 @@ func (o *CreateOptions) AsCreateOptions() *metav1.CreateOptions { } o.Raw.DryRun = o.DryRun + o.Raw.FieldManager = o.FieldManager return o.Raw } @@ -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. @@ -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 } @@ -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 } @@ -335,6 +349,7 @@ func (o *UpdateOptions) AsUpdateOptions() *metav1.UpdateOptions { } o.Raw.DryRun = o.DryRun + o.Raw.FieldManager = o.FieldManager return o.Raw } @@ -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