2 Copyright 2018 The Kubernetes Authors.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
22 "k8s.io/apimachinery/pkg/runtime"
25 // client is a client.Client that reads and writes directly from/to an API server. It lazily initializes
26 // new clients at the time they are used, and caches the client.
27 type typedClient struct {
29 paramCodec runtime.ParameterCodec
32 // Create implements client.Client
33 func (c *typedClient) Create(ctx context.Context, obj runtime.Object) error {
34 o, err := c.cache.getObjMeta(obj)
39 NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
40 Resource(o.resource()).
47 // Update implements client.Client
48 func (c *typedClient) Update(ctx context.Context, obj runtime.Object) error {
49 o, err := c.cache.getObjMeta(obj)
54 NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
55 Resource(o.resource()).
63 // Delete implements client.Client
64 func (c *typedClient) Delete(ctx context.Context, obj runtime.Object, opts ...DeleteOptionFunc) error {
65 o, err := c.cache.getObjMeta(obj)
70 deleteOpts := DeleteOptions{}
72 NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
73 Resource(o.resource()).
75 Body(deleteOpts.ApplyOptions(opts).AsDeleteOptions()).
81 // Get implements client.Client
82 func (c *typedClient) Get(ctx context.Context, key ObjectKey, obj runtime.Object) error {
83 r, err := c.cache.getResource(obj)
88 NamespaceIfScoped(key.Namespace, r.isNamespaced()).
89 Resource(r.resource()).
91 Name(key.Name).Do().Into(obj)
94 // List implements client.Client
95 func (c *typedClient) List(ctx context.Context, opts *ListOptions, obj runtime.Object) error {
96 r, err := c.cache.getResource(obj)
102 namespace = opts.Namespace
105 NamespaceIfScoped(namespace, r.isNamespaced()).
106 Resource(r.resource()).
107 VersionedParams(opts.AsListOptions(), c.paramCodec).
113 // UpdateStatus used by StatusWriter to write status.
114 func (c *typedClient) UpdateStatus(ctx context.Context, obj runtime.Object) error {
115 o, err := c.cache.getObjMeta(obj)
119 // TODO(droot): examine the returned error and check if it error needs to be
120 // wrapped to improve the UX ?
121 // It will be nice to receive an error saying the object doesn't implement
122 // status subresource and check CRD definition
124 NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
125 Resource(o.resource()).
127 SubResource("status").