Skip to content

Commit 320b6b6

Browse files
committed
Add field manager & dryrun to other opts
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.
1 parent 68d92bb commit 320b6b6

File tree

2 files changed

+57
-11
lines changed

2 files changed

+57
-11
lines changed

pkg/client/client_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,6 +2005,12 @@ var _ = Describe("Client", func() {
20052005
Expect(co.AsCreateOptions().DryRun).To(Equal(all))
20062006
})
20072007

2008+
It("should allow setting the field manager", func() {
2009+
po := &client.CreateOptions{}
2010+
client.FieldOwner("some-owner").ApplyToCreate(po)
2011+
Expect(po.AsCreateOptions().FieldManager).To(Equal("some-owner"))
2012+
})
2013+
20082014
It("should produce empty metav1.CreateOptions if nil", func() {
20092015
var co *client.CreateOptions
20102016
Expect(co.AsCreateOptions()).To(Equal(&metav1.CreateOptions{}))
@@ -2036,6 +2042,13 @@ var _ = Describe("Client", func() {
20362042
Expect(do.AsDeleteOptions().PropagationPolicy).To(Equal(&dp))
20372043
})
20382044

2045+
It("should allow setting DryRun", func() {
2046+
do := &client.DeleteOptions{}
2047+
client.DryRunAll.ApplyToDelete(do)
2048+
all := []string{metav1.DryRunAll}
2049+
Expect(do.AsDeleteOptions().DryRun).To(Equal(all))
2050+
})
2051+
20392052
It("should produce empty metav1.DeleteOptions if nil", func() {
20402053
var do *client.DeleteOptions
20412054
Expect(do.AsDeleteOptions()).To(Equal(&metav1.DeleteOptions{}))
@@ -2102,6 +2115,12 @@ var _ = Describe("Client", func() {
21022115
Expect(uo.AsUpdateOptions().DryRun).To(Equal(all))
21032116
})
21042117

2118+
It("should allow setting the field manager", func() {
2119+
po := &client.UpdateOptions{}
2120+
client.FieldOwner("some-owner").ApplyToUpdate(po)
2121+
Expect(po.AsUpdateOptions().FieldManager).To(Equal("some-owner"))
2122+
})
2123+
21052124
It("should produce empty metav1.UpdateOptions if nil", func() {
21062125
var co *client.UpdateOptions
21072126
Expect(co.AsUpdateOptions()).To(Equal(&metav1.UpdateOptions{}))

pkg/client/options.go

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,21 @@ func (dryRunAll) ApplyToUpdate(opts *UpdateOptions) {
7373
func (dryRunAll) ApplyToPatch(opts *PatchOptions) {
7474
opts.DryRun = []string{metav1.DryRunAll}
7575
}
76+
func (dryRunAll) ApplyToDelete(opts *DeleteOptions) {
77+
opts.DryRun = []string{metav1.DryRunAll}
78+
}
7679

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

8083
func (f FieldOwner) ApplyToPatch(opts *PatchOptions) {
8184
opts.FieldManager = string(f)
8285
}
83-
84-
// ForceOwnership indicates that in case of conflicts with server-side apply,
85-
// the client should acquire ownership of the conflicting field. Most
86-
// controllers should use this.
87-
var ForceOwnership = forceOwnership{}
88-
89-
type forceOwnership struct{}
90-
91-
func (forceOwnership) ApplyToPatch(opts *PatchOptions) {
92-
definitelyTrue := true
93-
opts.Force = &definitelyTrue
86+
func (f FieldOwner) ApplyToCreate(opts *CreateOptions) {
87+
opts.FieldManager = string(f)
88+
}
89+
func (f FieldOwner) ApplyToUpdate(opts *UpdateOptions) {
90+
opts.FieldManager = string(f)
9491
}
9592

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

107+
// FieldManager is the name of the user or component submitting
108+
// this request. It must be set with server-side apply.
109+
FieldManager string
110+
110111
// Raw represents raw CreateOptions, as passed to the API server.
111112
Raw *metav1.CreateOptions
112113
}
@@ -122,6 +123,7 @@ func (o *CreateOptions) AsCreateOptions() *metav1.CreateOptions {
122123
}
123124

124125
o.Raw.DryRun = o.DryRun
126+
o.Raw.FieldManager = o.FieldManager
125127
return o.Raw
126128
}
127129

@@ -168,6 +170,13 @@ type DeleteOptions struct {
168170

169171
// Raw represents raw DeleteOptions, as passed to the API server.
170172
Raw *metav1.DeleteOptions
173+
174+
// When present, indicates that modifications should not be
175+
// persisted. An invalid or unrecognized dryRun directive will
176+
// result in an error response and no further processing of the
177+
// request. Valid values are:
178+
// - All: all dry run stages will be processed
179+
DryRun []string
171180
}
172181

173182
// AsDeleteOptions returns these options as a metav1.DeleteOptions.
@@ -183,6 +192,7 @@ func (o *DeleteOptions) AsDeleteOptions() *metav1.DeleteOptions {
183192
o.Raw.GracePeriodSeconds = o.GracePeriodSeconds
184193
o.Raw.Preconditions = o.Preconditions
185194
o.Raw.PropagationPolicy = o.PropagationPolicy
195+
o.Raw.DryRun = o.DryRun
186196
return o.Raw
187197
}
188198

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

333+
// FieldManager is the name of the user or component submitting
334+
// this request. It must be set with server-side apply.
335+
FieldManager string
336+
323337
// Raw represents raw UpdateOptions, as passed to the API server.
324338
Raw *metav1.UpdateOptions
325339
}
@@ -335,6 +349,7 @@ func (o *UpdateOptions) AsUpdateOptions() *metav1.UpdateOptions {
335349
}
336350

337351
o.Raw.DryRun = o.DryRun
352+
o.Raw.FieldManager = o.FieldManager
338353
return o.Raw
339354
}
340355

@@ -404,6 +419,18 @@ func (o *PatchOptions) AsPatchOptions() *metav1.PatchOptions {
404419
return o.Raw
405420
}
406421

422+
// ForceOwnership indicates that in case of conflicts with server-side apply,
423+
// the client should acquire ownership of the conflicting field. Most
424+
// controllers should use this.
425+
var ForceOwnership = forceOwnership{}
426+
427+
type forceOwnership struct{}
428+
429+
func (forceOwnership) ApplyToPatch(opts *PatchOptions) {
430+
definitelyTrue := true
431+
opts.Force = &definitelyTrue
432+
}
433+
407434
// PatchDryRunAll sets the "dry run" option to "all".
408435
//
409436
// Deprecated: Use DryRunAll

0 commit comments

Comments
 (0)