@@ -57,14 +57,14 @@ type Reader interface {
57
57
// Writer knows how to create, delete, and update Kubernetes objects.
58
58
type Writer interface {
59
59
// Create saves the object obj in the Kubernetes cluster.
60
- Create (ctx context.Context , obj runtime.Object ) error
60
+ Create (ctx context.Context , obj runtime.Object , opts ... CreateOptionFunc ) error
61
61
62
62
// Delete deletes the given obj from Kubernetes cluster.
63
63
Delete (ctx context.Context , obj runtime.Object , opts ... DeleteOptionFunc ) error
64
64
65
65
// Update updates the given obj in the Kubernetes cluster. obj must be a
66
66
// struct pointer so that obj can be updated with the content returned by the Server.
67
- Update (ctx context.Context , obj runtime.Object ) error
67
+ Update (ctx context.Context , obj runtime.Object , opts ... UpdateOptionFunc ) error
68
68
}
69
69
70
70
// StatusClient knows how to create a client which can update status subresource
@@ -106,6 +106,57 @@ type FieldIndexer interface {
106
106
IndexField (obj runtime.Object , field string , extractValue IndexerFunc ) error
107
107
}
108
108
109
+ // CreateOptions contains options for create requests. It's generally a subset
110
+ // of metav1.CreateOptions.
111
+ type CreateOptions struct {
112
+ // When present, indicates that modifications should not be
113
+ // persisted. An invalid or unrecognized dryRun directive will
114
+ // result in an error response and no further processing of the
115
+ // request. Valid values are:
116
+ // - All: all dry run stages will be processed
117
+ DryRun []string
118
+
119
+ // Raw represents raw CreateOptions, as passed to the API server.
120
+ Raw * metav1.CreateOptions
121
+ }
122
+
123
+ // AsCreateOptions returns these options as a metav1.CreateOptions.
124
+ // This may mutate the Raw field.
125
+ func (o * CreateOptions ) AsCreateOptions () * metav1.CreateOptions {
126
+
127
+ if o == nil {
128
+ return & metav1.CreateOptions {}
129
+ }
130
+ if o .Raw == nil {
131
+ o .Raw = & metav1.CreateOptions {}
132
+ }
133
+
134
+ o .Raw .DryRun = o .DryRun
135
+ return o .Raw
136
+ }
137
+
138
+ // ApplyOptions executes the given CreateOptionFuncs and returns the mutated
139
+ // CreateOptions.
140
+ func (o * CreateOptions ) ApplyOptions (optFuncs []CreateOptionFunc ) * CreateOptions {
141
+ for _ , optFunc := range optFuncs {
142
+ optFunc (o )
143
+ }
144
+ return o
145
+ }
146
+
147
+ // CreateOptionFunc is a function that mutates a CreateOptions struct. It implements
148
+ // the functional options pattern. See
149
+ // https://github.com/tmrts/go-patterns/blob/master/idiom/functional-options.md.
150
+ type CreateOptionFunc func (* CreateOptions )
151
+
152
+ // CreateDryRunAll is a functional option that sets the DryRun
153
+ // field of a CreateOptions struct to metav1.DryRunAll.
154
+ func CreateDryRunAll () CreateOptionFunc {
155
+ return func (opts * CreateOptions ) {
156
+ opts .DryRun = []string {metav1 .DryRunAll }
157
+ }
158
+ }
159
+
109
160
// DeleteOptions contains options for delete requests. It's generally a subset
110
161
// of metav1.DeleteOptions.
111
162
type DeleteOptions struct {
@@ -326,3 +377,54 @@ func UseListOptions(newOpts *ListOptions) ListOptionFunc {
326
377
* opts = * newOpts
327
378
}
328
379
}
380
+
381
+ // UpdateOptions contains options for create requests. It's generally a subset
382
+ // of metav1.UpdateOptions.
383
+ type UpdateOptions struct {
384
+ // When present, indicates that modifications should not be
385
+ // persisted. An invalid or unrecognized dryRun directive will
386
+ // result in an error response and no further processing of the
387
+ // request. Valid values are:
388
+ // - All: all dry run stages will be processed
389
+ DryRun []string
390
+
391
+ // Raw represents raw UpdateOptions, as passed to the API server.
392
+ Raw * metav1.UpdateOptions
393
+ }
394
+
395
+ // AsUpdateOptions returns these options as a metav1.UpdateOptions.
396
+ // This may mutate the Raw field.
397
+ func (o * UpdateOptions ) AsUpdateOptions () * metav1.UpdateOptions {
398
+
399
+ if o == nil {
400
+ return & metav1.UpdateOptions {}
401
+ }
402
+ if o .Raw == nil {
403
+ o .Raw = & metav1.UpdateOptions {}
404
+ }
405
+
406
+ o .Raw .DryRun = o .DryRun
407
+ return o .Raw
408
+ }
409
+
410
+ // ApplyOptions executes the given UpdateOptionFuncs and returns the mutated
411
+ // UpdateOptions.
412
+ func (o * UpdateOptions ) ApplyOptions (optFuncs []UpdateOptionFunc ) * UpdateOptions {
413
+ for _ , optFunc := range optFuncs {
414
+ optFunc (o )
415
+ }
416
+ return o
417
+ }
418
+
419
+ // UpdateOptionFunc is a function that mutates a UpdateOptions struct. It implements
420
+ // the functional options pattern. See
421
+ // https://github.com/tmrts/go-patterns/blob/master/idiom/functional-options.md.
422
+ type UpdateOptionFunc func (* UpdateOptions )
423
+
424
+ // UpdateDryRunAll is a functional option that sets the DryRun
425
+ // field of a UpdateOptions struct to metav1.DryRunAll.
426
+ func UpdateDryRunAll () UpdateOptionFunc {
427
+ return func (opts * UpdateOptions ) {
428
+ opts .DryRun = []string {metav1 .DryRunAll }
429
+ }
430
+ }
0 commit comments