Skip to content

Commit e2c92af

Browse files
committed
Add UpdateOptions to client
1 parent 06239f6 commit e2c92af

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

pkg/client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (c *client) Create(ctx context.Context, obj runtime.Object, opts ...CreateO
113113
}
114114

115115
// Update implements client.Client
116-
func (c *client) Update(ctx context.Context, obj runtime.Object) error {
116+
func (c *client) Update(ctx context.Context, obj runtime.Object, opts ...UpdateOptionFunc) error {
117117
_, ok := obj.(*unstructured.Unstructured)
118118
if ok {
119119
return c.unstructuredClient.Update(ctx, obj)

pkg/client/interfaces.go

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ type Writer interface {
6464

6565
// Update updates the given obj in the Kubernetes cluster. obj must be a
6666
// 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
6868
}
6969

7070
// StatusClient knows how to create a client which can update status subresource
@@ -149,9 +149,9 @@ func (o *CreateOptions) ApplyOptions(optFuncs []CreateOptionFunc) *CreateOptions
149149
// https://github.com/tmrts/go-patterns/blob/master/idiom/functional-options.md.
150150
type CreateOptionFunc func(*CreateOptions)
151151

152-
// DryRunAll is a functional option that sets the DryRun
152+
// CreateDryRunAll is a functional option that sets the DryRun
153153
// field of a CreateOptions struct to metav1.DryRunAll.
154-
func DryRunAll() CreateOptionFunc {
154+
func CreateDryRunAll() CreateOptionFunc {
155155
return func(opts *CreateOptions) {
156156
opts.DryRun = []string{metav1.DryRunAll}
157157
}
@@ -377,3 +377,54 @@ func UseListOptions(newOpts *ListOptions) ListOptionFunc {
377377
*opts = *newOpts
378378
}
379379
}
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

Comments
 (0)