...

Source file src/sigs.k8s.io/controller-runtime/pkg/client/options.go

Documentation: sigs.k8s.io/controller-runtime/pkg/client

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package client
    18  
    19  import (
    20  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    21  	"k8s.io/apimachinery/pkg/fields"
    22  	"k8s.io/apimachinery/pkg/labels"
    23  	"k8s.io/apimachinery/pkg/selection"
    24  )
    25  
    26  // {{{ "Functional" Option Interfaces
    27  
    28  // CreateOption is some configuration that modifies options for a create request.
    29  type CreateOption interface {
    30  	// ApplyToCreate applies this configuration to the given create options.
    31  	ApplyToCreate(*CreateOptions)
    32  }
    33  
    34  // DeleteOption is some configuration that modifies options for a delete request.
    35  type DeleteOption interface {
    36  	// ApplyToDelete applies this configuration to the given delete options.
    37  	ApplyToDelete(*DeleteOptions)
    38  }
    39  
    40  // GetOption is some configuration that modifies options for a get request.
    41  type GetOption interface {
    42  	// ApplyToGet applies this configuration to the given get options.
    43  	ApplyToGet(*GetOptions)
    44  }
    45  
    46  // ListOption is some configuration that modifies options for a list request.
    47  type ListOption interface {
    48  	// ApplyToList applies this configuration to the given list options.
    49  	ApplyToList(*ListOptions)
    50  }
    51  
    52  // UpdateOption is some configuration that modifies options for a update request.
    53  type UpdateOption interface {
    54  	// ApplyToUpdate applies this configuration to the given update options.
    55  	ApplyToUpdate(*UpdateOptions)
    56  }
    57  
    58  // PatchOption is some configuration that modifies options for a patch request.
    59  type PatchOption interface {
    60  	// ApplyToPatch applies this configuration to the given patch options.
    61  	ApplyToPatch(*PatchOptions)
    62  }
    63  
    64  // DeleteAllOfOption is some configuration that modifies options for a delete request.
    65  type DeleteAllOfOption interface {
    66  	// ApplyToDeleteAllOf applies this configuration to the given deletecollection options.
    67  	ApplyToDeleteAllOf(*DeleteAllOfOptions)
    68  }
    69  
    70  // SubResourceGetOption modifies options for a SubResource Get request.
    71  type SubResourceGetOption interface {
    72  	ApplyToSubResourceGet(*SubResourceGetOptions)
    73  }
    74  
    75  // SubResourceUpdateOption is some configuration that modifies options for a update request.
    76  type SubResourceUpdateOption interface {
    77  	// ApplyToSubResourceUpdate applies this configuration to the given update options.
    78  	ApplyToSubResourceUpdate(*SubResourceUpdateOptions)
    79  }
    80  
    81  // SubResourceCreateOption is some configuration that modifies options for a create request.
    82  type SubResourceCreateOption interface {
    83  	// ApplyToSubResourceCreate applies this configuration to the given create options.
    84  	ApplyToSubResourceCreate(*SubResourceCreateOptions)
    85  }
    86  
    87  // SubResourcePatchOption configures a subresource patch request.
    88  type SubResourcePatchOption interface {
    89  	// ApplyToSubResourcePatch applies the configuration on the given patch options.
    90  	ApplyToSubResourcePatch(*SubResourcePatchOptions)
    91  }
    92  
    93  // }}}
    94  
    95  // {{{ Multi-Type Options
    96  
    97  // DryRunAll sets the "dry run" option to "all", executing all
    98  // validation, etc without persisting the change to storage.
    99  var DryRunAll = dryRunAll{}
   100  
   101  type dryRunAll struct{}
   102  
   103  // ApplyToCreate applies this configuration to the given create options.
   104  func (dryRunAll) ApplyToCreate(opts *CreateOptions) {
   105  	opts.DryRun = []string{metav1.DryRunAll}
   106  }
   107  
   108  // ApplyToUpdate applies this configuration to the given update options.
   109  func (dryRunAll) ApplyToUpdate(opts *UpdateOptions) {
   110  	opts.DryRun = []string{metav1.DryRunAll}
   111  }
   112  
   113  // ApplyToPatch applies this configuration to the given patch options.
   114  func (dryRunAll) ApplyToPatch(opts *PatchOptions) {
   115  	opts.DryRun = []string{metav1.DryRunAll}
   116  }
   117  
   118  // ApplyToPatch applies this configuration to the given delete options.
   119  func (dryRunAll) ApplyToDelete(opts *DeleteOptions) {
   120  	opts.DryRun = []string{metav1.DryRunAll}
   121  }
   122  
   123  func (dryRunAll) ApplyToDeleteAllOf(opts *DeleteAllOfOptions) {
   124  	opts.DryRun = []string{metav1.DryRunAll}
   125  }
   126  
   127  func (dryRunAll) ApplyToSubResourceCreate(opts *SubResourceCreateOptions) {
   128  	opts.DryRun = []string{metav1.DryRunAll}
   129  }
   130  
   131  func (dryRunAll) ApplyToSubResourceUpdate(opts *SubResourceUpdateOptions) {
   132  	opts.DryRun = []string{metav1.DryRunAll}
   133  }
   134  
   135  func (dryRunAll) ApplyToSubResourcePatch(opts *SubResourcePatchOptions) {
   136  	opts.DryRun = []string{metav1.DryRunAll}
   137  }
   138  
   139  // FieldOwner set the field manager name for the given server-side apply patch.
   140  type FieldOwner string
   141  
   142  // ApplyToPatch applies this configuration to the given patch options.
   143  func (f FieldOwner) ApplyToPatch(opts *PatchOptions) {
   144  	opts.FieldManager = string(f)
   145  }
   146  
   147  // ApplyToCreate applies this configuration to the given create options.
   148  func (f FieldOwner) ApplyToCreate(opts *CreateOptions) {
   149  	opts.FieldManager = string(f)
   150  }
   151  
   152  // ApplyToUpdate applies this configuration to the given update options.
   153  func (f FieldOwner) ApplyToUpdate(opts *UpdateOptions) {
   154  	opts.FieldManager = string(f)
   155  }
   156  
   157  // ApplyToSubResourcePatch applies this configuration to the given patch options.
   158  func (f FieldOwner) ApplyToSubResourcePatch(opts *SubResourcePatchOptions) {
   159  	opts.FieldManager = string(f)
   160  }
   161  
   162  // ApplyToSubResourceCreate applies this configuration to the given create options.
   163  func (f FieldOwner) ApplyToSubResourceCreate(opts *SubResourceCreateOptions) {
   164  	opts.FieldManager = string(f)
   165  }
   166  
   167  // ApplyToSubResourceUpdate applies this configuration to the given update options.
   168  func (f FieldOwner) ApplyToSubResourceUpdate(opts *SubResourceUpdateOptions) {
   169  	opts.FieldManager = string(f)
   170  }
   171  
   172  // }}}
   173  
   174  // {{{ Create Options
   175  
   176  // CreateOptions contains options for create requests. It's generally a subset
   177  // of metav1.CreateOptions.
   178  type CreateOptions struct {
   179  	// When present, indicates that modifications should not be
   180  	// persisted. An invalid or unrecognized dryRun directive will
   181  	// result in an error response and no further processing of the
   182  	// request. Valid values are:
   183  	// - All: all dry run stages will be processed
   184  	DryRun []string
   185  
   186  	// FieldManager is the name of the user or component submitting
   187  	// this request.  It must be set with server-side apply.
   188  	FieldManager string
   189  
   190  	// Raw represents raw CreateOptions, as passed to the API server.
   191  	Raw *metav1.CreateOptions
   192  }
   193  
   194  // AsCreateOptions returns these options as a metav1.CreateOptions.
   195  // This may mutate the Raw field.
   196  func (o *CreateOptions) AsCreateOptions() *metav1.CreateOptions {
   197  	if o == nil {
   198  		return &metav1.CreateOptions{}
   199  	}
   200  	if o.Raw == nil {
   201  		o.Raw = &metav1.CreateOptions{}
   202  	}
   203  
   204  	o.Raw.DryRun = o.DryRun
   205  	o.Raw.FieldManager = o.FieldManager
   206  	return o.Raw
   207  }
   208  
   209  // ApplyOptions applies the given create options on these options,
   210  // and then returns itself (for convenient chaining).
   211  func (o *CreateOptions) ApplyOptions(opts []CreateOption) *CreateOptions {
   212  	for _, opt := range opts {
   213  		opt.ApplyToCreate(o)
   214  	}
   215  	return o
   216  }
   217  
   218  // ApplyToCreate implements CreateOption.
   219  func (o *CreateOptions) ApplyToCreate(co *CreateOptions) {
   220  	if o.DryRun != nil {
   221  		co.DryRun = o.DryRun
   222  	}
   223  	if o.FieldManager != "" {
   224  		co.FieldManager = o.FieldManager
   225  	}
   226  	if o.Raw != nil {
   227  		co.Raw = o.Raw
   228  	}
   229  }
   230  
   231  var _ CreateOption = &CreateOptions{}
   232  
   233  // }}}
   234  
   235  // {{{ Delete Options
   236  
   237  // DeleteOptions contains options for delete requests. It's generally a subset
   238  // of metav1.DeleteOptions.
   239  type DeleteOptions struct {
   240  	// GracePeriodSeconds is the duration in seconds before the object should be
   241  	// deleted. Value must be non-negative integer. The value zero indicates
   242  	// delete immediately. If this value is nil, the default grace period for the
   243  	// specified type will be used.
   244  	GracePeriodSeconds *int64
   245  
   246  	// Preconditions must be fulfilled before a deletion is carried out. If not
   247  	// possible, a 409 Conflict status will be returned.
   248  	Preconditions *metav1.Preconditions
   249  
   250  	// PropagationPolicy determined whether and how garbage collection will be
   251  	// performed. Either this field or OrphanDependents may be set, but not both.
   252  	// The default policy is decided by the existing finalizer set in the
   253  	// metadata.finalizers and the resource-specific default policy.
   254  	// Acceptable values are: 'Orphan' - orphan the dependents; 'Background' -
   255  	// allow the garbage collector to delete the dependents in the background;
   256  	// 'Foreground' - a cascading policy that deletes all dependents in the
   257  	// foreground.
   258  	PropagationPolicy *metav1.DeletionPropagation
   259  
   260  	// Raw represents raw DeleteOptions, as passed to the API server.
   261  	Raw *metav1.DeleteOptions
   262  
   263  	// When present, indicates that modifications should not be
   264  	// persisted. An invalid or unrecognized dryRun directive will
   265  	// result in an error response and no further processing of the
   266  	// request. Valid values are:
   267  	// - All: all dry run stages will be processed
   268  	DryRun []string
   269  }
   270  
   271  // AsDeleteOptions returns these options as a metav1.DeleteOptions.
   272  // This may mutate the Raw field.
   273  func (o *DeleteOptions) AsDeleteOptions() *metav1.DeleteOptions {
   274  	if o == nil {
   275  		return &metav1.DeleteOptions{}
   276  	}
   277  	if o.Raw == nil {
   278  		o.Raw = &metav1.DeleteOptions{}
   279  	}
   280  
   281  	o.Raw.GracePeriodSeconds = o.GracePeriodSeconds
   282  	o.Raw.Preconditions = o.Preconditions
   283  	o.Raw.PropagationPolicy = o.PropagationPolicy
   284  	o.Raw.DryRun = o.DryRun
   285  	return o.Raw
   286  }
   287  
   288  // ApplyOptions applies the given delete options on these options,
   289  // and then returns itself (for convenient chaining).
   290  func (o *DeleteOptions) ApplyOptions(opts []DeleteOption) *DeleteOptions {
   291  	for _, opt := range opts {
   292  		opt.ApplyToDelete(o)
   293  	}
   294  	return o
   295  }
   296  
   297  var _ DeleteOption = &DeleteOptions{}
   298  
   299  // ApplyToDelete implements DeleteOption.
   300  func (o *DeleteOptions) ApplyToDelete(do *DeleteOptions) {
   301  	if o.GracePeriodSeconds != nil {
   302  		do.GracePeriodSeconds = o.GracePeriodSeconds
   303  	}
   304  	if o.Preconditions != nil {
   305  		do.Preconditions = o.Preconditions
   306  	}
   307  	if o.PropagationPolicy != nil {
   308  		do.PropagationPolicy = o.PropagationPolicy
   309  	}
   310  	if o.Raw != nil {
   311  		do.Raw = o.Raw
   312  	}
   313  	if o.DryRun != nil {
   314  		do.DryRun = o.DryRun
   315  	}
   316  }
   317  
   318  // GracePeriodSeconds sets the grace period for the deletion
   319  // to the given number of seconds.
   320  type GracePeriodSeconds int64
   321  
   322  // ApplyToDelete applies this configuration to the given delete options.
   323  func (s GracePeriodSeconds) ApplyToDelete(opts *DeleteOptions) {
   324  	secs := int64(s)
   325  	opts.GracePeriodSeconds = &secs
   326  }
   327  
   328  // ApplyToDeleteAllOf applies this configuration to the given an List options.
   329  func (s GracePeriodSeconds) ApplyToDeleteAllOf(opts *DeleteAllOfOptions) {
   330  	s.ApplyToDelete(&opts.DeleteOptions)
   331  }
   332  
   333  // Preconditions must be fulfilled before an operation (update, delete, etc.) is carried out.
   334  type Preconditions metav1.Preconditions
   335  
   336  // ApplyToDelete applies this configuration to the given delete options.
   337  func (p Preconditions) ApplyToDelete(opts *DeleteOptions) {
   338  	preconds := metav1.Preconditions(p)
   339  	opts.Preconditions = &preconds
   340  }
   341  
   342  // ApplyToDeleteAllOf applies this configuration to the given an List options.
   343  func (p Preconditions) ApplyToDeleteAllOf(opts *DeleteAllOfOptions) {
   344  	p.ApplyToDelete(&opts.DeleteOptions)
   345  }
   346  
   347  // PropagationPolicy determined whether and how garbage collection will be
   348  // performed. Either this field or OrphanDependents may be set, but not both.
   349  // The default policy is decided by the existing finalizer set in the
   350  // metadata.finalizers and the resource-specific default policy.
   351  // Acceptable values are: 'Orphan' - orphan the dependents; 'Background' -
   352  // allow the garbage collector to delete the dependents in the background;
   353  // 'Foreground' - a cascading policy that deletes all dependents in the
   354  // foreground.
   355  type PropagationPolicy metav1.DeletionPropagation
   356  
   357  // ApplyToDelete applies the given delete options on these options.
   358  // It will propagate to the dependents of the object to let the garbage collector handle it.
   359  func (p PropagationPolicy) ApplyToDelete(opts *DeleteOptions) {
   360  	policy := metav1.DeletionPropagation(p)
   361  	opts.PropagationPolicy = &policy
   362  }
   363  
   364  // ApplyToDeleteAllOf applies this configuration to the given an List options.
   365  func (p PropagationPolicy) ApplyToDeleteAllOf(opts *DeleteAllOfOptions) {
   366  	p.ApplyToDelete(&opts.DeleteOptions)
   367  }
   368  
   369  // }}}
   370  
   371  // {{{ Get Options
   372  
   373  // GetOptions contains options for get operation.
   374  // Now it only has a Raw field, with support for specific resourceVersion.
   375  type GetOptions struct {
   376  	// Raw represents raw GetOptions, as passed to the API server.  Note
   377  	// that these may not be respected by all implementations of interface.
   378  	Raw *metav1.GetOptions
   379  }
   380  
   381  var _ GetOption = &GetOptions{}
   382  
   383  // ApplyToGet implements GetOption for GetOptions.
   384  func (o *GetOptions) ApplyToGet(lo *GetOptions) {
   385  	if o.Raw != nil {
   386  		lo.Raw = o.Raw
   387  	}
   388  }
   389  
   390  // AsGetOptions returns these options as a flattened metav1.GetOptions.
   391  // This may mutate the Raw field.
   392  func (o *GetOptions) AsGetOptions() *metav1.GetOptions {
   393  	if o == nil || o.Raw == nil {
   394  		return &metav1.GetOptions{}
   395  	}
   396  	return o.Raw
   397  }
   398  
   399  // ApplyOptions applies the given get options on these options,
   400  // and then returns itself (for convenient chaining).
   401  func (o *GetOptions) ApplyOptions(opts []GetOption) *GetOptions {
   402  	for _, opt := range opts {
   403  		opt.ApplyToGet(o)
   404  	}
   405  	return o
   406  }
   407  
   408  // }}}
   409  
   410  // {{{ List Options
   411  
   412  // ListOptions contains options for limiting or filtering results.
   413  // It's generally a subset of metav1.ListOptions, with support for
   414  // pre-parsed selectors (since generally, selectors will be executed
   415  // against the cache).
   416  type ListOptions struct {
   417  	// LabelSelector filters results by label. Use labels.Parse() to
   418  	// set from raw string form.
   419  	LabelSelector labels.Selector
   420  	// FieldSelector filters results by a particular field.  In order
   421  	// to use this with cache-based implementations, restrict usage to
   422  	// exact match field-value pair that's been added to the indexers.
   423  	FieldSelector fields.Selector
   424  
   425  	// Namespace represents the namespace to list for, or empty for
   426  	// non-namespaced objects, or to list across all namespaces.
   427  	Namespace string
   428  
   429  	// Limit specifies the maximum number of results to return from the server. The server may
   430  	// not support this field on all resource types, but if it does and more results remain it
   431  	// will set the continue field on the returned list object. This field is not supported if watch
   432  	// is true in the Raw ListOptions.
   433  	Limit int64
   434  	// Continue is a token returned by the server that lets a client retrieve chunks of results
   435  	// from the server by specifying limit. The server may reject requests for continuation tokens
   436  	// it does not recognize and will return a 410 error if the token can no longer be used because
   437  	// it has expired. This field is not supported if watch is true in the Raw ListOptions.
   438  	Continue string
   439  
   440  	// UnsafeDisableDeepCopy indicates not to deep copy objects during list objects.
   441  	// Be very careful with this, when enabled you must DeepCopy any object before mutating it,
   442  	// otherwise you will mutate the object in the cache.
   443  	// +optional
   444  	UnsafeDisableDeepCopy *bool
   445  
   446  	// Raw represents raw ListOptions, as passed to the API server.  Note
   447  	// that these may not be respected by all implementations of interface,
   448  	// and the LabelSelector, FieldSelector, Limit and Continue fields are ignored.
   449  	Raw *metav1.ListOptions
   450  }
   451  
   452  var _ ListOption = &ListOptions{}
   453  
   454  // ApplyToList implements ListOption for ListOptions.
   455  func (o *ListOptions) ApplyToList(lo *ListOptions) {
   456  	if o.LabelSelector != nil {
   457  		lo.LabelSelector = o.LabelSelector
   458  	}
   459  	if o.FieldSelector != nil {
   460  		lo.FieldSelector = o.FieldSelector
   461  	}
   462  	if o.Namespace != "" {
   463  		lo.Namespace = o.Namespace
   464  	}
   465  	if o.Raw != nil {
   466  		lo.Raw = o.Raw
   467  	}
   468  	if o.Limit > 0 {
   469  		lo.Limit = o.Limit
   470  	}
   471  	if o.Continue != "" {
   472  		lo.Continue = o.Continue
   473  	}
   474  	if o.UnsafeDisableDeepCopy != nil {
   475  		lo.UnsafeDisableDeepCopy = o.UnsafeDisableDeepCopy
   476  	}
   477  }
   478  
   479  // AsListOptions returns these options as a flattened metav1.ListOptions.
   480  // This may mutate the Raw field.
   481  func (o *ListOptions) AsListOptions() *metav1.ListOptions {
   482  	if o == nil {
   483  		return &metav1.ListOptions{}
   484  	}
   485  	if o.Raw == nil {
   486  		o.Raw = &metav1.ListOptions{}
   487  	}
   488  	if o.LabelSelector != nil {
   489  		o.Raw.LabelSelector = o.LabelSelector.String()
   490  	}
   491  	if o.FieldSelector != nil {
   492  		o.Raw.FieldSelector = o.FieldSelector.String()
   493  	}
   494  	if !o.Raw.Watch {
   495  		o.Raw.Limit = o.Limit
   496  		o.Raw.Continue = o.Continue
   497  	}
   498  	return o.Raw
   499  }
   500  
   501  // ApplyOptions applies the given list options on these options,
   502  // and then returns itself (for convenient chaining).
   503  func (o *ListOptions) ApplyOptions(opts []ListOption) *ListOptions {
   504  	for _, opt := range opts {
   505  		opt.ApplyToList(o)
   506  	}
   507  	return o
   508  }
   509  
   510  // MatchingLabels filters the list/delete operation on the given set of labels.
   511  type MatchingLabels map[string]string
   512  
   513  // ApplyToList applies this configuration to the given list options.
   514  func (m MatchingLabels) ApplyToList(opts *ListOptions) {
   515  	// TODO(directxman12): can we avoid reserializing this over and over?
   516  	if opts.LabelSelector == nil {
   517  		opts.LabelSelector = labels.SelectorFromValidatedSet(map[string]string(m))
   518  		return
   519  	}
   520  	// If there's already a selector, we need to AND the two together.
   521  	noValidSel := labels.SelectorFromValidatedSet(map[string]string(m))
   522  	reqs, _ := noValidSel.Requirements()
   523  	for _, req := range reqs {
   524  		opts.LabelSelector = opts.LabelSelector.Add(req)
   525  	}
   526  }
   527  
   528  // ApplyToDeleteAllOf applies this configuration to the given an List options.
   529  func (m MatchingLabels) ApplyToDeleteAllOf(opts *DeleteAllOfOptions) {
   530  	m.ApplyToList(&opts.ListOptions)
   531  }
   532  
   533  // HasLabels filters the list/delete operation checking if the set of labels exists
   534  // without checking their values.
   535  type HasLabels []string
   536  
   537  // ApplyToList applies this configuration to the given list options.
   538  func (m HasLabels) ApplyToList(opts *ListOptions) {
   539  	if opts.LabelSelector == nil {
   540  		opts.LabelSelector = labels.NewSelector()
   541  	}
   542  	// TODO: ignore invalid labels will result in an empty selector.
   543  	// This is inconsistent to the that of MatchingLabels.
   544  	for _, label := range m {
   545  		r, err := labels.NewRequirement(label, selection.Exists, nil)
   546  		if err == nil {
   547  			opts.LabelSelector = opts.LabelSelector.Add(*r)
   548  		}
   549  	}
   550  }
   551  
   552  // ApplyToDeleteAllOf applies this configuration to the given an List options.
   553  func (m HasLabels) ApplyToDeleteAllOf(opts *DeleteAllOfOptions) {
   554  	m.ApplyToList(&opts.ListOptions)
   555  }
   556  
   557  // MatchingLabelsSelector filters the list/delete operation on the given label
   558  // selector (or index in the case of cached lists). A struct is used because
   559  // labels.Selector is an interface, which cannot be aliased.
   560  type MatchingLabelsSelector struct {
   561  	labels.Selector
   562  }
   563  
   564  // ApplyToList applies this configuration to the given list options.
   565  func (m MatchingLabelsSelector) ApplyToList(opts *ListOptions) {
   566  	opts.LabelSelector = m
   567  }
   568  
   569  // ApplyToDeleteAllOf applies this configuration to the given an List options.
   570  func (m MatchingLabelsSelector) ApplyToDeleteAllOf(opts *DeleteAllOfOptions) {
   571  	m.ApplyToList(&opts.ListOptions)
   572  }
   573  
   574  // MatchingFields filters the list/delete operation on the given field Set
   575  // (or index in the case of cached lists).
   576  type MatchingFields fields.Set
   577  
   578  // ApplyToList applies this configuration to the given list options.
   579  func (m MatchingFields) ApplyToList(opts *ListOptions) {
   580  	// TODO(directxman12): can we avoid re-serializing this?
   581  	sel := fields.Set(m).AsSelector()
   582  	opts.FieldSelector = sel
   583  }
   584  
   585  // ApplyToDeleteAllOf applies this configuration to the given an List options.
   586  func (m MatchingFields) ApplyToDeleteAllOf(opts *DeleteAllOfOptions) {
   587  	m.ApplyToList(&opts.ListOptions)
   588  }
   589  
   590  // MatchingFieldsSelector filters the list/delete operation on the given field
   591  // selector (or index in the case of cached lists). A struct is used because
   592  // fields.Selector is an interface, which cannot be aliased.
   593  type MatchingFieldsSelector struct {
   594  	fields.Selector
   595  }
   596  
   597  // ApplyToList applies this configuration to the given list options.
   598  func (m MatchingFieldsSelector) ApplyToList(opts *ListOptions) {
   599  	opts.FieldSelector = m
   600  }
   601  
   602  // ApplyToDeleteAllOf applies this configuration to the given an List options.
   603  func (m MatchingFieldsSelector) ApplyToDeleteAllOf(opts *DeleteAllOfOptions) {
   604  	m.ApplyToList(&opts.ListOptions)
   605  }
   606  
   607  // InNamespace restricts the list/delete operation to the given namespace.
   608  type InNamespace string
   609  
   610  // ApplyToList applies this configuration to the given list options.
   611  func (n InNamespace) ApplyToList(opts *ListOptions) {
   612  	opts.Namespace = string(n)
   613  }
   614  
   615  // ApplyToDeleteAllOf applies this configuration to the given an List options.
   616  func (n InNamespace) ApplyToDeleteAllOf(opts *DeleteAllOfOptions) {
   617  	n.ApplyToList(&opts.ListOptions)
   618  }
   619  
   620  // AsSelector returns a selector that matches objects in the given namespace.
   621  func (n InNamespace) AsSelector() fields.Selector {
   622  	return fields.SelectorFromSet(fields.Set{"metadata.namespace": string(n)})
   623  }
   624  
   625  // Limit specifies the maximum number of results to return from the server.
   626  // Limit does not implement DeleteAllOfOption interface because the server
   627  // does not support setting it for deletecollection operations.
   628  type Limit int64
   629  
   630  // ApplyToList applies this configuration to the given an list options.
   631  func (l Limit) ApplyToList(opts *ListOptions) {
   632  	opts.Limit = int64(l)
   633  }
   634  
   635  // UnsafeDisableDeepCopyOption indicates not to deep copy objects during list objects.
   636  // Be very careful with this, when enabled you must DeepCopy any object before mutating it,
   637  // otherwise you will mutate the object in the cache.
   638  type UnsafeDisableDeepCopyOption bool
   639  
   640  // ApplyToList applies this configuration to the given an List options.
   641  func (d UnsafeDisableDeepCopyOption) ApplyToList(opts *ListOptions) {
   642  	definitelyTrue := true
   643  	definitelyFalse := false
   644  	if d {
   645  		opts.UnsafeDisableDeepCopy = &definitelyTrue
   646  	} else {
   647  		opts.UnsafeDisableDeepCopy = &definitelyFalse
   648  	}
   649  }
   650  
   651  // UnsafeDisableDeepCopy indicates not to deep copy objects during list objects.
   652  const UnsafeDisableDeepCopy = UnsafeDisableDeepCopyOption(true)
   653  
   654  // Continue sets a continuation token to retrieve chunks of results when using limit.
   655  // Continue does not implement DeleteAllOfOption interface because the server
   656  // does not support setting it for deletecollection operations.
   657  type Continue string
   658  
   659  // ApplyToList applies this configuration to the given an List options.
   660  func (c Continue) ApplyToList(opts *ListOptions) {
   661  	opts.Continue = string(c)
   662  }
   663  
   664  // }}}
   665  
   666  // {{{ Update Options
   667  
   668  // UpdateOptions contains options for create requests. It's generally a subset
   669  // of metav1.UpdateOptions.
   670  type UpdateOptions struct {
   671  	// When present, indicates that modifications should not be
   672  	// persisted. An invalid or unrecognized dryRun directive will
   673  	// result in an error response and no further processing of the
   674  	// request. Valid values are:
   675  	// - All: all dry run stages will be processed
   676  	DryRun []string
   677  
   678  	// FieldManager is the name of the user or component submitting
   679  	// this request.  It must be set with server-side apply.
   680  	FieldManager string
   681  
   682  	// Raw represents raw UpdateOptions, as passed to the API server.
   683  	Raw *metav1.UpdateOptions
   684  }
   685  
   686  // AsUpdateOptions returns these options as a metav1.UpdateOptions.
   687  // This may mutate the Raw field.
   688  func (o *UpdateOptions) AsUpdateOptions() *metav1.UpdateOptions {
   689  	if o == nil {
   690  		return &metav1.UpdateOptions{}
   691  	}
   692  	if o.Raw == nil {
   693  		o.Raw = &metav1.UpdateOptions{}
   694  	}
   695  
   696  	o.Raw.DryRun = o.DryRun
   697  	o.Raw.FieldManager = o.FieldManager
   698  	return o.Raw
   699  }
   700  
   701  // ApplyOptions applies the given update options on these options,
   702  // and then returns itself (for convenient chaining).
   703  func (o *UpdateOptions) ApplyOptions(opts []UpdateOption) *UpdateOptions {
   704  	for _, opt := range opts {
   705  		opt.ApplyToUpdate(o)
   706  	}
   707  	return o
   708  }
   709  
   710  var _ UpdateOption = &UpdateOptions{}
   711  
   712  // ApplyToUpdate implements UpdateOption.
   713  func (o *UpdateOptions) ApplyToUpdate(uo *UpdateOptions) {
   714  	if o.DryRun != nil {
   715  		uo.DryRun = o.DryRun
   716  	}
   717  	if o.FieldManager != "" {
   718  		uo.FieldManager = o.FieldManager
   719  	}
   720  	if o.Raw != nil {
   721  		uo.Raw = o.Raw
   722  	}
   723  }
   724  
   725  // }}}
   726  
   727  // {{{ Patch Options
   728  
   729  // PatchOptions contains options for patch requests.
   730  type PatchOptions struct {
   731  	// When present, indicates that modifications should not be
   732  	// persisted. An invalid or unrecognized dryRun directive will
   733  	// result in an error response and no further processing of the
   734  	// request. Valid values are:
   735  	// - All: all dry run stages will be processed
   736  	DryRun []string
   737  
   738  	// Force is going to "force" Apply requests. It means user will
   739  	// re-acquire conflicting fields owned by other people. Force
   740  	// flag must be unset for non-apply patch requests.
   741  	// +optional
   742  	Force *bool
   743  
   744  	// FieldManager is the name of the user or component submitting
   745  	// this request.  It must be set with server-side apply.
   746  	FieldManager string
   747  
   748  	// Raw represents raw PatchOptions, as passed to the API server.
   749  	Raw *metav1.PatchOptions
   750  }
   751  
   752  // ApplyOptions applies the given patch options on these options,
   753  // and then returns itself (for convenient chaining).
   754  func (o *PatchOptions) ApplyOptions(opts []PatchOption) *PatchOptions {
   755  	for _, opt := range opts {
   756  		opt.ApplyToPatch(o)
   757  	}
   758  	return o
   759  }
   760  
   761  // AsPatchOptions returns these options as a metav1.PatchOptions.
   762  // This may mutate the Raw field.
   763  func (o *PatchOptions) AsPatchOptions() *metav1.PatchOptions {
   764  	if o == nil {
   765  		return &metav1.PatchOptions{}
   766  	}
   767  	if o.Raw == nil {
   768  		o.Raw = &metav1.PatchOptions{}
   769  	}
   770  
   771  	o.Raw.DryRun = o.DryRun
   772  	o.Raw.Force = o.Force
   773  	o.Raw.FieldManager = o.FieldManager
   774  	return o.Raw
   775  }
   776  
   777  var _ PatchOption = &PatchOptions{}
   778  
   779  // ApplyToPatch implements PatchOptions.
   780  func (o *PatchOptions) ApplyToPatch(po *PatchOptions) {
   781  	if o.DryRun != nil {
   782  		po.DryRun = o.DryRun
   783  	}
   784  	if o.Force != nil {
   785  		po.Force = o.Force
   786  	}
   787  	if o.FieldManager != "" {
   788  		po.FieldManager = o.FieldManager
   789  	}
   790  	if o.Raw != nil {
   791  		po.Raw = o.Raw
   792  	}
   793  }
   794  
   795  // ForceOwnership indicates that in case of conflicts with server-side apply,
   796  // the client should acquire ownership of the conflicting field.  Most
   797  // controllers should use this.
   798  var ForceOwnership = forceOwnership{}
   799  
   800  type forceOwnership struct{}
   801  
   802  func (forceOwnership) ApplyToPatch(opts *PatchOptions) {
   803  	definitelyTrue := true
   804  	opts.Force = &definitelyTrue
   805  }
   806  
   807  func (forceOwnership) ApplyToSubResourcePatch(opts *SubResourcePatchOptions) {
   808  	definitelyTrue := true
   809  	opts.Force = &definitelyTrue
   810  }
   811  
   812  // }}}
   813  
   814  // {{{ DeleteAllOf Options
   815  
   816  // these are all just delete options and list options
   817  
   818  // DeleteAllOfOptions contains options for deletecollection (deleteallof) requests.
   819  // It's just list and delete options smooshed together.
   820  type DeleteAllOfOptions struct {
   821  	ListOptions
   822  	DeleteOptions
   823  }
   824  
   825  // ApplyOptions applies the given deleteallof options on these options,
   826  // and then returns itself (for convenient chaining).
   827  func (o *DeleteAllOfOptions) ApplyOptions(opts []DeleteAllOfOption) *DeleteAllOfOptions {
   828  	for _, opt := range opts {
   829  		opt.ApplyToDeleteAllOf(o)
   830  	}
   831  	return o
   832  }
   833  
   834  var _ DeleteAllOfOption = &DeleteAllOfOptions{}
   835  
   836  // ApplyToDeleteAllOf implements DeleteAllOfOption.
   837  func (o *DeleteAllOfOptions) ApplyToDeleteAllOf(do *DeleteAllOfOptions) {
   838  	o.ApplyToList(&do.ListOptions)
   839  	o.ApplyToDelete(&do.DeleteOptions)
   840  }
   841  
   842  // }}}
   843  

View as plain text