Skip to content

Commit 28a96c7

Browse files
committed
Move options to their own file in pkg/client
This refactors options off to their own file in order to make each file comprehensibly long (pkg/client/interfaces.go was getting rather large with all the different types of options).
1 parent cd1f7a6 commit 28a96c7

File tree

2 files changed

+376
-357
lines changed

2 files changed

+376
-357
lines changed

pkg/client/interfaces.go

Lines changed: 0 additions & 357 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ import (
2020
"context"
2121

2222
"k8s.io/apimachinery/pkg/api/meta"
23-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24-
"k8s.io/apimachinery/pkg/fields"
25-
"k8s.io/apimachinery/pkg/labels"
2623
"k8s.io/apimachinery/pkg/runtime"
2724
"k8s.io/apimachinery/pkg/types"
2825
)
@@ -117,357 +114,3 @@ type FieldIndexer interface {
117114
// and supporting efficient all-namespace queries.
118115
IndexField(obj runtime.Object, field string, extractValue IndexerFunc) error
119116
}
120-
121-
// CreateOptions contains options for create requests. It's generally a subset
122-
// of metav1.CreateOptions.
123-
type CreateOptions struct {
124-
// When present, indicates that modifications should not be
125-
// persisted. An invalid or unrecognized dryRun directive will
126-
// result in an error response and no further processing of the
127-
// request. Valid values are:
128-
// - All: all dry run stages will be processed
129-
DryRun []string
130-
131-
// Raw represents raw CreateOptions, as passed to the API server.
132-
Raw *metav1.CreateOptions
133-
}
134-
135-
// AsCreateOptions returns these options as a metav1.CreateOptions.
136-
// This may mutate the Raw field.
137-
func (o *CreateOptions) AsCreateOptions() *metav1.CreateOptions {
138-
139-
if o == nil {
140-
return &metav1.CreateOptions{}
141-
}
142-
if o.Raw == nil {
143-
o.Raw = &metav1.CreateOptions{}
144-
}
145-
146-
o.Raw.DryRun = o.DryRun
147-
return o.Raw
148-
}
149-
150-
// ApplyOptions executes the given CreateOptionFuncs and returns the mutated
151-
// CreateOptions.
152-
func (o *CreateOptions) ApplyOptions(optFuncs []CreateOptionFunc) *CreateOptions {
153-
for _, optFunc := range optFuncs {
154-
optFunc(o)
155-
}
156-
return o
157-
}
158-
159-
// CreateOptionFunc is a function that mutates a CreateOptions struct. It implements
160-
// the functional options pattern. See
161-
// https://github.com/tmrts/go-patterns/blob/master/idiom/functional-options.md.
162-
type CreateOptionFunc func(*CreateOptions)
163-
164-
// CreateDryRunAll is a functional option that sets the DryRun
165-
// field of a CreateOptions struct to metav1.DryRunAll.
166-
func CreateDryRunAll() CreateOptionFunc {
167-
return func(opts *CreateOptions) {
168-
opts.DryRun = []string{metav1.DryRunAll}
169-
}
170-
}
171-
172-
// DeleteOptions contains options for delete requests. It's generally a subset
173-
// of metav1.DeleteOptions.
174-
type DeleteOptions struct {
175-
// GracePeriodSeconds is the duration in seconds before the object should be
176-
// deleted. Value must be non-negative integer. The value zero indicates
177-
// delete immediately. If this value is nil, the default grace period for the
178-
// specified type will be used.
179-
GracePeriodSeconds *int64
180-
181-
// Preconditions must be fulfilled before a deletion is carried out. If not
182-
// possible, a 409 Conflict status will be returned.
183-
Preconditions *metav1.Preconditions
184-
185-
// PropagationPolicy determined whether and how garbage collection will be
186-
// performed. Either this field or OrphanDependents may be set, but not both.
187-
// The default policy is decided by the existing finalizer set in the
188-
// metadata.finalizers and the resource-specific default policy.
189-
// Acceptable values are: 'Orphan' - orphan the dependents; 'Background' -
190-
// allow the garbage collector to delete the dependents in the background;
191-
// 'Foreground' - a cascading policy that deletes all dependents in the
192-
// foreground.
193-
PropagationPolicy *metav1.DeletionPropagation
194-
195-
// Raw represents raw DeleteOptions, as passed to the API server.
196-
Raw *metav1.DeleteOptions
197-
}
198-
199-
// AsDeleteOptions returns these options as a metav1.DeleteOptions.
200-
// This may mutate the Raw field.
201-
func (o *DeleteOptions) AsDeleteOptions() *metav1.DeleteOptions {
202-
203-
if o == nil {
204-
return &metav1.DeleteOptions{}
205-
}
206-
if o.Raw == nil {
207-
o.Raw = &metav1.DeleteOptions{}
208-
}
209-
210-
o.Raw.GracePeriodSeconds = o.GracePeriodSeconds
211-
o.Raw.Preconditions = o.Preconditions
212-
o.Raw.PropagationPolicy = o.PropagationPolicy
213-
return o.Raw
214-
}
215-
216-
// ApplyOptions executes the given DeleteOptionFuncs and returns the mutated
217-
// DeleteOptions.
218-
func (o *DeleteOptions) ApplyOptions(optFuncs []DeleteOptionFunc) *DeleteOptions {
219-
for _, optFunc := range optFuncs {
220-
optFunc(o)
221-
}
222-
return o
223-
}
224-
225-
// DeleteOptionFunc is a function that mutates a DeleteOptions struct. It implements
226-
// the functional options pattern. See
227-
// https://github.com/tmrts/go-patterns/blob/master/idiom/functional-options.md.
228-
type DeleteOptionFunc func(*DeleteOptions)
229-
230-
// GracePeriodSeconds is a functional option that sets the GracePeriodSeconds
231-
// field of a DeleteOptions struct.
232-
func GracePeriodSeconds(gp int64) DeleteOptionFunc {
233-
return func(opts *DeleteOptions) {
234-
opts.GracePeriodSeconds = &gp
235-
}
236-
}
237-
238-
// Preconditions is a functional option that sets the Preconditions field of a
239-
// DeleteOptions struct.
240-
func Preconditions(p *metav1.Preconditions) DeleteOptionFunc {
241-
return func(opts *DeleteOptions) {
242-
opts.Preconditions = p
243-
}
244-
}
245-
246-
// PropagationPolicy is a functional option that sets the PropagationPolicy
247-
// field of a DeleteOptions struct.
248-
func PropagationPolicy(p metav1.DeletionPropagation) DeleteOptionFunc {
249-
return func(opts *DeleteOptions) {
250-
opts.PropagationPolicy = &p
251-
}
252-
}
253-
254-
// ListOptions contains options for limiting or filtering results.
255-
// It's generally a subset of metav1.ListOptions, with support for
256-
// pre-parsed selectors (since generally, selectors will be executed
257-
// against the cache).
258-
type ListOptions struct {
259-
// LabelSelector filters results by label. Use SetLabelSelector to
260-
// set from raw string form.
261-
LabelSelector labels.Selector
262-
// FieldSelector filters results by a particular field. In order
263-
// to use this with cache-based implementations, restrict usage to
264-
// a single field-value pair that's been added to the indexers.
265-
FieldSelector fields.Selector
266-
267-
// Namespace represents the namespace to list for, or empty for
268-
// non-namespaced objects, or to list across all namespaces.
269-
Namespace string
270-
271-
// Raw represents raw ListOptions, as passed to the API server. Note
272-
// that these may not be respected by all implementations of interface,
273-
// and the LabelSelector and FieldSelector fields are ignored.
274-
Raw *metav1.ListOptions
275-
}
276-
277-
// SetLabelSelector sets this the label selector of these options
278-
// from a string form of the selector.
279-
func (o *ListOptions) SetLabelSelector(selRaw string) error {
280-
sel, err := labels.Parse(selRaw)
281-
if err != nil {
282-
return err
283-
}
284-
o.LabelSelector = sel
285-
return nil
286-
}
287-
288-
// SetFieldSelector sets this the label selector of these options
289-
// from a string form of the selector.
290-
func (o *ListOptions) SetFieldSelector(selRaw string) error {
291-
sel, err := fields.ParseSelector(selRaw)
292-
if err != nil {
293-
return err
294-
}
295-
o.FieldSelector = sel
296-
return nil
297-
}
298-
299-
// AsListOptions returns these options as a flattened metav1.ListOptions.
300-
// This may mutate the Raw field.
301-
func (o *ListOptions) AsListOptions() *metav1.ListOptions {
302-
if o == nil {
303-
return &metav1.ListOptions{}
304-
}
305-
if o.Raw == nil {
306-
o.Raw = &metav1.ListOptions{}
307-
}
308-
if o.LabelSelector != nil {
309-
o.Raw.LabelSelector = o.LabelSelector.String()
310-
}
311-
if o.FieldSelector != nil {
312-
o.Raw.FieldSelector = o.FieldSelector.String()
313-
}
314-
return o.Raw
315-
}
316-
317-
// ApplyOptions executes the given ListOptionFuncs and returns the mutated
318-
// ListOptions.
319-
func (o *ListOptions) ApplyOptions(optFuncs []ListOptionFunc) *ListOptions {
320-
for _, optFunc := range optFuncs {
321-
optFunc(o)
322-
}
323-
return o
324-
}
325-
326-
// ListOptionFunc is a function that mutates a ListOptions struct. It implements
327-
// the functional options pattern. See
328-
// https://github.com/tmrts/go-patterns/blob/master/idiom/functional-options.md.
329-
type ListOptionFunc func(*ListOptions)
330-
331-
// MatchingLabels is a convenience function that sets the label selector
332-
// to match the given labels, and then returns the options.
333-
// It mutates the list options.
334-
func (o *ListOptions) MatchingLabels(lbls map[string]string) *ListOptions {
335-
sel := labels.SelectorFromSet(lbls)
336-
o.LabelSelector = sel
337-
return o
338-
}
339-
340-
// MatchingField is a convenience function that sets the field selector
341-
// to match the given field, and then returns the options.
342-
// It mutates the list options.
343-
func (o *ListOptions) MatchingField(name, val string) *ListOptions {
344-
sel := fields.SelectorFromSet(fields.Set{name: val})
345-
o.FieldSelector = sel
346-
return o
347-
}
348-
349-
// InNamespace is a convenience function that sets the namespace,
350-
// and then returns the options. It mutates the list options.
351-
func (o *ListOptions) InNamespace(ns string) *ListOptions {
352-
o.Namespace = ns
353-
return o
354-
}
355-
356-
// MatchingLabels is a functional option that sets the LabelSelector field of
357-
// a ListOptions struct.
358-
func MatchingLabels(lbls map[string]string) ListOptionFunc {
359-
sel := labels.SelectorFromSet(lbls)
360-
return func(opts *ListOptions) {
361-
opts.LabelSelector = sel
362-
}
363-
}
364-
365-
// MatchingField is a functional option that sets the FieldSelector field of
366-
// a ListOptions struct.
367-
func MatchingField(name, val string) ListOptionFunc {
368-
sel := fields.SelectorFromSet(fields.Set{name: val})
369-
return func(opts *ListOptions) {
370-
opts.FieldSelector = sel
371-
}
372-
}
373-
374-
// InNamespace is a functional option that sets the Namespace field of
375-
// a ListOptions struct.
376-
func InNamespace(ns string) ListOptionFunc {
377-
return func(opts *ListOptions) {
378-
opts.Namespace = ns
379-
}
380-
}
381-
382-
// UseListOptions is a functional option that replaces the fields of a
383-
// ListOptions struct with those of a different ListOptions struct.
384-
//
385-
// Example:
386-
// cl.List(ctx, list, client.UseListOptions(lo.InNamespace(ns).MatchingLabels(labels)))
387-
func UseListOptions(newOpts *ListOptions) ListOptionFunc {
388-
return func(opts *ListOptions) {
389-
*opts = *newOpts
390-
}
391-
}
392-
393-
// UpdateOptions contains options for create requests. It's generally a subset
394-
// of metav1.UpdateOptions.
395-
type UpdateOptions struct {
396-
// When present, indicates that modifications should not be
397-
// persisted. An invalid or unrecognized dryRun directive will
398-
// result in an error response and no further processing of the
399-
// request. Valid values are:
400-
// - All: all dry run stages will be processed
401-
DryRun []string
402-
403-
// Raw represents raw UpdateOptions, as passed to the API server.
404-
Raw *metav1.UpdateOptions
405-
}
406-
407-
// AsUpdateOptions returns these options as a metav1.UpdateOptions.
408-
// This may mutate the Raw field.
409-
func (o *UpdateOptions) AsUpdateOptions() *metav1.UpdateOptions {
410-
411-
if o == nil {
412-
return &metav1.UpdateOptions{}
413-
}
414-
if o.Raw == nil {
415-
o.Raw = &metav1.UpdateOptions{}
416-
}
417-
418-
o.Raw.DryRun = o.DryRun
419-
return o.Raw
420-
}
421-
422-
// ApplyOptions executes the given UpdateOptionFuncs and returns the mutated
423-
// UpdateOptions.
424-
func (o *UpdateOptions) ApplyOptions(optFuncs []UpdateOptionFunc) *UpdateOptions {
425-
for _, optFunc := range optFuncs {
426-
optFunc(o)
427-
}
428-
return o
429-
}
430-
431-
// UpdateOptionFunc is a function that mutates a UpdateOptions struct. It implements
432-
// the functional options pattern. See
433-
// https://github.com/tmrts/go-patterns/blob/master/idiom/functional-options.md.
434-
type UpdateOptionFunc func(*UpdateOptions)
435-
436-
// UpdateDryRunAll is a functional option that sets the DryRun
437-
// field of a UpdateOptions struct to metav1.DryRunAll.
438-
func UpdateDryRunAll() UpdateOptionFunc {
439-
return func(opts *UpdateOptions) {
440-
opts.DryRun = []string{metav1.DryRunAll}
441-
}
442-
}
443-
444-
// PatchOptions contains options for patch requests.
445-
type PatchOptions struct {
446-
UpdateOptions
447-
}
448-
449-
// ApplyOptions executes the given PatchOptionFuncs, mutating these PatchOptions.
450-
// It returns the mutated PatchOptions for convenience.
451-
func (o *PatchOptions) ApplyOptions(optFuncs []PatchOptionFunc) *PatchOptions {
452-
for _, optFunc := range optFuncs {
453-
optFunc(o)
454-
}
455-
return o
456-
}
457-
458-
// PatchOptionFunc is a function that mutates a PatchOptions struct. It implements
459-
// the functional options pattern. See
460-
// https://github.com/tmrts/go-patterns/blob/master/idiom/functional-options.md.
461-
type PatchOptionFunc func(*PatchOptions)
462-
463-
// Sadly, we need a separate function to "adapt" PatchOptions to the constituent
464-
// update options, since there's no way to write a function that works for both.
465-
466-
// UpdatePatchWith adapts the given UpdateOptionFuncs to be a PatchOptionFunc.
467-
func UpdatePatchWith(optFuncs ...UpdateOptionFunc) PatchOptionFunc {
468-
return func(opts *PatchOptions) {
469-
for _, optFunc := range optFuncs {
470-
optFunc(&opts.UpdateOptions)
471-
}
472-
}
473-
}

0 commit comments

Comments
 (0)