...

Source file src/sigs.k8s.io/controller-runtime/pkg/client/typed_client.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  	"context"
    21  
    22  	"k8s.io/apimachinery/pkg/runtime"
    23  )
    24  
    25  var _ Reader = &typedClient{}
    26  var _ Writer = &typedClient{}
    27  
    28  type typedClient struct {
    29  	resources  *clientRestResources
    30  	paramCodec runtime.ParameterCodec
    31  }
    32  
    33  // Create implements client.Client.
    34  func (c *typedClient) Create(ctx context.Context, obj Object, opts ...CreateOption) error {
    35  	o, err := c.resources.getObjMeta(obj)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	createOpts := &CreateOptions{}
    41  	createOpts.ApplyOptions(opts)
    42  
    43  	return o.Post().
    44  		NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
    45  		Resource(o.resource()).
    46  		Body(obj).
    47  		VersionedParams(createOpts.AsCreateOptions(), c.paramCodec).
    48  		Do(ctx).
    49  		Into(obj)
    50  }
    51  
    52  // Update implements client.Client.
    53  func (c *typedClient) Update(ctx context.Context, obj Object, opts ...UpdateOption) error {
    54  	o, err := c.resources.getObjMeta(obj)
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	updateOpts := &UpdateOptions{}
    60  	updateOpts.ApplyOptions(opts)
    61  
    62  	return o.Put().
    63  		NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
    64  		Resource(o.resource()).
    65  		Name(o.GetName()).
    66  		Body(obj).
    67  		VersionedParams(updateOpts.AsUpdateOptions(), c.paramCodec).
    68  		Do(ctx).
    69  		Into(obj)
    70  }
    71  
    72  // Delete implements client.Client.
    73  func (c *typedClient) Delete(ctx context.Context, obj Object, opts ...DeleteOption) error {
    74  	o, err := c.resources.getObjMeta(obj)
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	deleteOpts := DeleteOptions{}
    80  	deleteOpts.ApplyOptions(opts)
    81  
    82  	return o.Delete().
    83  		NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
    84  		Resource(o.resource()).
    85  		Name(o.GetName()).
    86  		Body(deleteOpts.AsDeleteOptions()).
    87  		Do(ctx).
    88  		Error()
    89  }
    90  
    91  // DeleteAllOf implements client.Client.
    92  func (c *typedClient) DeleteAllOf(ctx context.Context, obj Object, opts ...DeleteAllOfOption) error {
    93  	o, err := c.resources.getObjMeta(obj)
    94  	if err != nil {
    95  		return err
    96  	}
    97  
    98  	deleteAllOfOpts := DeleteAllOfOptions{}
    99  	deleteAllOfOpts.ApplyOptions(opts)
   100  
   101  	return o.Delete().
   102  		NamespaceIfScoped(deleteAllOfOpts.ListOptions.Namespace, o.isNamespaced()).
   103  		Resource(o.resource()).
   104  		VersionedParams(deleteAllOfOpts.AsListOptions(), c.paramCodec).
   105  		Body(deleteAllOfOpts.AsDeleteOptions()).
   106  		Do(ctx).
   107  		Error()
   108  }
   109  
   110  // Patch implements client.Client.
   111  func (c *typedClient) Patch(ctx context.Context, obj Object, patch Patch, opts ...PatchOption) error {
   112  	o, err := c.resources.getObjMeta(obj)
   113  	if err != nil {
   114  		return err
   115  	}
   116  
   117  	data, err := patch.Data(obj)
   118  	if err != nil {
   119  		return err
   120  	}
   121  
   122  	patchOpts := &PatchOptions{}
   123  	patchOpts.ApplyOptions(opts)
   124  
   125  	return o.Patch(patch.Type()).
   126  		NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
   127  		Resource(o.resource()).
   128  		Name(o.GetName()).
   129  		VersionedParams(patchOpts.AsPatchOptions(), c.paramCodec).
   130  		Body(data).
   131  		Do(ctx).
   132  		Into(obj)
   133  }
   134  
   135  // Get implements client.Client.
   136  func (c *typedClient) Get(ctx context.Context, key ObjectKey, obj Object, opts ...GetOption) error {
   137  	r, err := c.resources.getResource(obj)
   138  	if err != nil {
   139  		return err
   140  	}
   141  	getOpts := GetOptions{}
   142  	getOpts.ApplyOptions(opts)
   143  	return r.Get().
   144  		NamespaceIfScoped(key.Namespace, r.isNamespaced()).
   145  		Resource(r.resource()).
   146  		VersionedParams(getOpts.AsGetOptions(), c.paramCodec).
   147  		Name(key.Name).Do(ctx).Into(obj)
   148  }
   149  
   150  // List implements client.Client.
   151  func (c *typedClient) List(ctx context.Context, obj ObjectList, opts ...ListOption) error {
   152  	r, err := c.resources.getResource(obj)
   153  	if err != nil {
   154  		return err
   155  	}
   156  
   157  	listOpts := ListOptions{}
   158  	listOpts.ApplyOptions(opts)
   159  
   160  	return r.Get().
   161  		NamespaceIfScoped(listOpts.Namespace, r.isNamespaced()).
   162  		Resource(r.resource()).
   163  		VersionedParams(listOpts.AsListOptions(), c.paramCodec).
   164  		Do(ctx).
   165  		Into(obj)
   166  }
   167  
   168  func (c *typedClient) GetSubResource(ctx context.Context, obj, subResourceObj Object, subResource string, opts ...SubResourceGetOption) error {
   169  	o, err := c.resources.getObjMeta(obj)
   170  	if err != nil {
   171  		return err
   172  	}
   173  
   174  	if subResourceObj.GetName() == "" {
   175  		subResourceObj.SetName(obj.GetName())
   176  	}
   177  
   178  	getOpts := &SubResourceGetOptions{}
   179  	getOpts.ApplyOptions(opts)
   180  
   181  	return o.Get().
   182  		NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
   183  		Resource(o.resource()).
   184  		Name(o.GetName()).
   185  		SubResource(subResource).
   186  		VersionedParams(getOpts.AsGetOptions(), c.paramCodec).
   187  		Do(ctx).
   188  		Into(subResourceObj)
   189  }
   190  
   191  func (c *typedClient) CreateSubResource(ctx context.Context, obj Object, subResourceObj Object, subResource string, opts ...SubResourceCreateOption) error {
   192  	o, err := c.resources.getObjMeta(obj)
   193  	if err != nil {
   194  		return err
   195  	}
   196  
   197  	if subResourceObj.GetName() == "" {
   198  		subResourceObj.SetName(obj.GetName())
   199  	}
   200  
   201  	createOpts := &SubResourceCreateOptions{}
   202  	createOpts.ApplyOptions(opts)
   203  
   204  	return o.Post().
   205  		NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
   206  		Resource(o.resource()).
   207  		Name(o.GetName()).
   208  		SubResource(subResource).
   209  		Body(subResourceObj).
   210  		VersionedParams(createOpts.AsCreateOptions(), c.paramCodec).
   211  		Do(ctx).
   212  		Into(subResourceObj)
   213  }
   214  
   215  // UpdateSubResource used by SubResourceWriter to write status.
   216  func (c *typedClient) UpdateSubResource(ctx context.Context, obj Object, subResource string, opts ...SubResourceUpdateOption) error {
   217  	o, err := c.resources.getObjMeta(obj)
   218  	if err != nil {
   219  		return err
   220  	}
   221  	// TODO(droot): examine the returned error and check if it error needs to be
   222  	// wrapped to improve the UX ?
   223  	// It will be nice to receive an error saying the object doesn't implement
   224  	// status subresource and check CRD definition
   225  	updateOpts := &SubResourceUpdateOptions{}
   226  	updateOpts.ApplyOptions(opts)
   227  
   228  	body := obj
   229  	if updateOpts.SubResourceBody != nil {
   230  		body = updateOpts.SubResourceBody
   231  	}
   232  	if body.GetName() == "" {
   233  		body.SetName(obj.GetName())
   234  	}
   235  	if body.GetNamespace() == "" {
   236  		body.SetNamespace(obj.GetNamespace())
   237  	}
   238  
   239  	return o.Put().
   240  		NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
   241  		Resource(o.resource()).
   242  		Name(o.GetName()).
   243  		SubResource(subResource).
   244  		Body(body).
   245  		VersionedParams(updateOpts.AsUpdateOptions(), c.paramCodec).
   246  		Do(ctx).
   247  		Into(body)
   248  }
   249  
   250  // PatchSubResource used by SubResourceWriter to write subresource.
   251  func (c *typedClient) PatchSubResource(ctx context.Context, obj Object, subResource string, patch Patch, opts ...SubResourcePatchOption) error {
   252  	o, err := c.resources.getObjMeta(obj)
   253  	if err != nil {
   254  		return err
   255  	}
   256  
   257  	patchOpts := &SubResourcePatchOptions{}
   258  	patchOpts.ApplyOptions(opts)
   259  
   260  	body := obj
   261  	if patchOpts.SubResourceBody != nil {
   262  		body = patchOpts.SubResourceBody
   263  	}
   264  
   265  	data, err := patch.Data(body)
   266  	if err != nil {
   267  		return err
   268  	}
   269  
   270  	return o.Patch(patch.Type()).
   271  		NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
   272  		Resource(o.resource()).
   273  		Name(o.GetName()).
   274  		SubResource(subResource).
   275  		Body(data).
   276  		VersionedParams(patchOpts.AsPatchOptions(), c.paramCodec).
   277  		Do(ctx).
   278  		Into(body)
   279  }
   280  

View as plain text