...

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

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

     1  /*
     2  Copyright 2020 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  	"context"
    21  
    22  	"k8s.io/apimachinery/pkg/api/meta"
    23  	"k8s.io/apimachinery/pkg/runtime"
    24  	"k8s.io/apimachinery/pkg/runtime/schema"
    25  )
    26  
    27  // NewDryRunClient wraps an existing client and enforces DryRun mode
    28  // on all mutating api calls.
    29  func NewDryRunClient(c Client) Client {
    30  	return &dryRunClient{client: c}
    31  }
    32  
    33  var _ Client = &dryRunClient{}
    34  
    35  // dryRunClient is a Client that wraps another Client in order to enforce DryRun mode.
    36  type dryRunClient struct {
    37  	client Client
    38  }
    39  
    40  // Scheme returns the scheme this client is using.
    41  func (c *dryRunClient) Scheme() *runtime.Scheme {
    42  	return c.client.Scheme()
    43  }
    44  
    45  // RESTMapper returns the rest mapper this client is using.
    46  func (c *dryRunClient) RESTMapper() meta.RESTMapper {
    47  	return c.client.RESTMapper()
    48  }
    49  
    50  // GroupVersionKindFor returns the GroupVersionKind for the given object.
    51  func (c *dryRunClient) GroupVersionKindFor(obj runtime.Object) (schema.GroupVersionKind, error) {
    52  	return c.client.GroupVersionKindFor(obj)
    53  }
    54  
    55  // IsObjectNamespaced returns true if the GroupVersionKind of the object is namespaced.
    56  func (c *dryRunClient) IsObjectNamespaced(obj runtime.Object) (bool, error) {
    57  	return c.client.IsObjectNamespaced(obj)
    58  }
    59  
    60  // Create implements client.Client.
    61  func (c *dryRunClient) Create(ctx context.Context, obj Object, opts ...CreateOption) error {
    62  	return c.client.Create(ctx, obj, append(opts, DryRunAll)...)
    63  }
    64  
    65  // Update implements client.Client.
    66  func (c *dryRunClient) Update(ctx context.Context, obj Object, opts ...UpdateOption) error {
    67  	return c.client.Update(ctx, obj, append(opts, DryRunAll)...)
    68  }
    69  
    70  // Delete implements client.Client.
    71  func (c *dryRunClient) Delete(ctx context.Context, obj Object, opts ...DeleteOption) error {
    72  	return c.client.Delete(ctx, obj, append(opts, DryRunAll)...)
    73  }
    74  
    75  // DeleteAllOf implements client.Client.
    76  func (c *dryRunClient) DeleteAllOf(ctx context.Context, obj Object, opts ...DeleteAllOfOption) error {
    77  	return c.client.DeleteAllOf(ctx, obj, append(opts, DryRunAll)...)
    78  }
    79  
    80  // Patch implements client.Client.
    81  func (c *dryRunClient) Patch(ctx context.Context, obj Object, patch Patch, opts ...PatchOption) error {
    82  	return c.client.Patch(ctx, obj, patch, append(opts, DryRunAll)...)
    83  }
    84  
    85  // Get implements client.Client.
    86  func (c *dryRunClient) Get(ctx context.Context, key ObjectKey, obj Object, opts ...GetOption) error {
    87  	return c.client.Get(ctx, key, obj, opts...)
    88  }
    89  
    90  // List implements client.Client.
    91  func (c *dryRunClient) List(ctx context.Context, obj ObjectList, opts ...ListOption) error {
    92  	return c.client.List(ctx, obj, opts...)
    93  }
    94  
    95  // Status implements client.StatusClient.
    96  func (c *dryRunClient) Status() SubResourceWriter {
    97  	return c.SubResource("status")
    98  }
    99  
   100  // SubResource implements client.SubResourceClient.
   101  func (c *dryRunClient) SubResource(subResource string) SubResourceClient {
   102  	return &dryRunSubResourceClient{client: c.client.SubResource(subResource)}
   103  }
   104  
   105  // ensure dryRunSubResourceWriter implements client.SubResourceWriter.
   106  var _ SubResourceWriter = &dryRunSubResourceClient{}
   107  
   108  // dryRunSubResourceClient is client.SubResourceWriter that writes status subresource with dryRun mode
   109  // enforced.
   110  type dryRunSubResourceClient struct {
   111  	client SubResourceClient
   112  }
   113  
   114  func (sw *dryRunSubResourceClient) Get(ctx context.Context, obj, subResource Object, opts ...SubResourceGetOption) error {
   115  	return sw.client.Get(ctx, obj, subResource, opts...)
   116  }
   117  
   118  func (sw *dryRunSubResourceClient) Create(ctx context.Context, obj, subResource Object, opts ...SubResourceCreateOption) error {
   119  	return sw.client.Create(ctx, obj, subResource, append(opts, DryRunAll)...)
   120  }
   121  
   122  // Update implements client.SubResourceWriter.
   123  func (sw *dryRunSubResourceClient) Update(ctx context.Context, obj Object, opts ...SubResourceUpdateOption) error {
   124  	return sw.client.Update(ctx, obj, append(opts, DryRunAll)...)
   125  }
   126  
   127  // Patch implements client.SubResourceWriter.
   128  func (sw *dryRunSubResourceClient) Patch(ctx context.Context, obj Object, patch Patch, opts ...SubResourcePatchOption) error {
   129  	return sw.client.Patch(ctx, obj, patch, append(opts, DryRunAll)...)
   130  }
   131  

View as plain text