Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / sigs.k8s.io / controller-runtime / pkg / client / typed_client.go
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 // 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 {
28         cache      clientCache
29         paramCodec runtime.ParameterCodec
30 }
31
32 // Create implements client.Client
33 func (c *typedClient) Create(ctx context.Context, obj runtime.Object) error {
34         o, err := c.cache.getObjMeta(obj)
35         if err != nil {
36                 return err
37         }
38         return o.Post().
39                 NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
40                 Resource(o.resource()).
41                 Body(obj).
42                 Context(ctx).
43                 Do().
44                 Into(obj)
45 }
46
47 // Update implements client.Client
48 func (c *typedClient) Update(ctx context.Context, obj runtime.Object) error {
49         o, err := c.cache.getObjMeta(obj)
50         if err != nil {
51                 return err
52         }
53         return o.Put().
54                 NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
55                 Resource(o.resource()).
56                 Name(o.GetName()).
57                 Body(obj).
58                 Context(ctx).
59                 Do().
60                 Into(obj)
61 }
62
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)
66         if err != nil {
67                 return err
68         }
69
70         deleteOpts := DeleteOptions{}
71         return o.Delete().
72                 NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
73                 Resource(o.resource()).
74                 Name(o.GetName()).
75                 Body(deleteOpts.ApplyOptions(opts).AsDeleteOptions()).
76                 Context(ctx).
77                 Do().
78                 Error()
79 }
80
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)
84         if err != nil {
85                 return err
86         }
87         return r.Get().
88                 NamespaceIfScoped(key.Namespace, r.isNamespaced()).
89                 Resource(r.resource()).
90                 Context(ctx).
91                 Name(key.Name).Do().Into(obj)
92 }
93
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)
97         if err != nil {
98                 return err
99         }
100         namespace := ""
101         if opts != nil {
102                 namespace = opts.Namespace
103         }
104         return r.Get().
105                 NamespaceIfScoped(namespace, r.isNamespaced()).
106                 Resource(r.resource()).
107                 VersionedParams(opts.AsListOptions(), c.paramCodec).
108                 Context(ctx).
109                 Do().
110                 Into(obj)
111 }
112
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)
116         if err != nil {
117                 return err
118         }
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
123         return o.Put().
124                 NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
125                 Resource(o.resource()).
126                 Name(o.GetName()).
127                 SubResource("status").
128                 Body(obj).
129                 Context(ctx).
130                 Do().
131                 Into(obj)
132 }