@@ -57,7 +57,7 @@ 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
@@ -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
+ // 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
+
109
160
// DeleteOptions contains options for delete requests. It's generally a subset
110
161
// of metav1.DeleteOptions.
111
162
type DeleteOptions struct {
0 commit comments