Skip to content

Commit 06239f6

Browse files
committed
Add CreateOptions to client
1 parent 68ae79e commit 06239f6

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

pkg/client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ type client struct {
104104
}
105105

106106
// Create implements client.Client
107-
func (c *client) Create(ctx context.Context, obj runtime.Object) error {
107+
func (c *client) Create(ctx context.Context, obj runtime.Object, opts ...CreateOptionFunc) error {
108108
_, ok := obj.(*unstructured.Unstructured)
109109
if ok {
110110
return c.unstructuredClient.Create(ctx, obj)

pkg/client/interfaces.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type Reader interface {
5757
// Writer knows how to create, delete, and update Kubernetes objects.
5858
type Writer interface {
5959
// 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
6161

6262
// Delete deletes the given obj from Kubernetes cluster.
6363
Delete(ctx context.Context, obj runtime.Object, opts ...DeleteOptionFunc) error
@@ -106,6 +106,57 @@ type FieldIndexer interface {
106106
IndexField(obj runtime.Object, field string, extractValue IndexerFunc) error
107107
}
108108

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+
// DryRunAll is a functional option that sets the DryRun
153+
// field of a CreateOptions struct to metav1.DryRunAll.
154+
func DryRunAll() CreateOptionFunc {
155+
return func(opts *CreateOptions) {
156+
opts.DryRun = []string{metav1.DryRunAll}
157+
}
158+
}
159+
109160
// DeleteOptions contains options for delete requests. It's generally a subset
110161
// of metav1.DeleteOptions.
111162
type DeleteOptions struct {

0 commit comments

Comments
 (0)