Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / k8s.io / client-go / dynamic / simple.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 dynamic
18
19 import (
20         "io"
21
22         "k8s.io/apimachinery/pkg/api/meta"
23         metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24         "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
25         "k8s.io/apimachinery/pkg/runtime"
26         "k8s.io/apimachinery/pkg/runtime/schema"
27         "k8s.io/apimachinery/pkg/runtime/serializer/streaming"
28         "k8s.io/apimachinery/pkg/types"
29         "k8s.io/apimachinery/pkg/watch"
30         "k8s.io/client-go/rest"
31 )
32
33 type dynamicClient struct {
34         client *rest.RESTClient
35 }
36
37 var _ Interface = &dynamicClient{}
38
39 // NewForConfigOrDie creates a new Interface for the given config and
40 // panics if there is an error in the config.
41 func NewForConfigOrDie(c *rest.Config) Interface {
42         ret, err := NewForConfig(c)
43         if err != nil {
44                 panic(err)
45         }
46         return ret
47 }
48
49 func NewForConfig(inConfig *rest.Config) (Interface, error) {
50         config := rest.CopyConfig(inConfig)
51         // for serializing the options
52         config.GroupVersion = &schema.GroupVersion{}
53         config.APIPath = "/if-you-see-this-search-for-the-break"
54         config.AcceptContentTypes = "application/json"
55         config.ContentType = "application/json"
56         config.NegotiatedSerializer = basicNegotiatedSerializer{} // this gets used for discovery and error handling types
57         if config.UserAgent == "" {
58                 config.UserAgent = rest.DefaultKubernetesUserAgent()
59         }
60
61         restClient, err := rest.RESTClientFor(config)
62         if err != nil {
63                 return nil, err
64         }
65
66         return &dynamicClient{client: restClient}, nil
67 }
68
69 type dynamicResourceClient struct {
70         client    *dynamicClient
71         namespace string
72         resource  schema.GroupVersionResource
73 }
74
75 func (c *dynamicClient) Resource(resource schema.GroupVersionResource) NamespaceableResourceInterface {
76         return &dynamicResourceClient{client: c, resource: resource}
77 }
78
79 func (c *dynamicResourceClient) Namespace(ns string) ResourceInterface {
80         ret := *c
81         ret.namespace = ns
82         return &ret
83 }
84
85 func (c *dynamicResourceClient) Create(obj *unstructured.Unstructured, opts metav1.CreateOptions, subresources ...string) (*unstructured.Unstructured, error) {
86         outBytes, err := runtime.Encode(unstructured.UnstructuredJSONScheme, obj)
87         if err != nil {
88                 return nil, err
89         }
90         name := ""
91         if len(subresources) > 0 {
92                 accessor, err := meta.Accessor(obj)
93                 if err != nil {
94                         return nil, err
95                 }
96                 name = accessor.GetName()
97         }
98
99         result := c.client.client.
100                 Post().
101                 AbsPath(append(c.makeURLSegments(name), subresources...)...).
102                 Body(outBytes).
103                 SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
104                 Do()
105         if err := result.Error(); err != nil {
106                 return nil, err
107         }
108
109         retBytes, err := result.Raw()
110         if err != nil {
111                 return nil, err
112         }
113         uncastObj, err := runtime.Decode(unstructured.UnstructuredJSONScheme, retBytes)
114         if err != nil {
115                 return nil, err
116         }
117         return uncastObj.(*unstructured.Unstructured), nil
118 }
119
120 func (c *dynamicResourceClient) Update(obj *unstructured.Unstructured, opts metav1.UpdateOptions, subresources ...string) (*unstructured.Unstructured, error) {
121         accessor, err := meta.Accessor(obj)
122         if err != nil {
123                 return nil, err
124         }
125         outBytes, err := runtime.Encode(unstructured.UnstructuredJSONScheme, obj)
126         if err != nil {
127                 return nil, err
128         }
129
130         result := c.client.client.
131                 Put().
132                 AbsPath(append(c.makeURLSegments(accessor.GetName()), subresources...)...).
133                 Body(outBytes).
134                 SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
135                 Do()
136         if err := result.Error(); err != nil {
137                 return nil, err
138         }
139
140         retBytes, err := result.Raw()
141         if err != nil {
142                 return nil, err
143         }
144         uncastObj, err := runtime.Decode(unstructured.UnstructuredJSONScheme, retBytes)
145         if err != nil {
146                 return nil, err
147         }
148         return uncastObj.(*unstructured.Unstructured), nil
149 }
150
151 func (c *dynamicResourceClient) UpdateStatus(obj *unstructured.Unstructured, opts metav1.UpdateOptions) (*unstructured.Unstructured, error) {
152         accessor, err := meta.Accessor(obj)
153         if err != nil {
154                 return nil, err
155         }
156
157         outBytes, err := runtime.Encode(unstructured.UnstructuredJSONScheme, obj)
158         if err != nil {
159                 return nil, err
160         }
161
162         result := c.client.client.
163                 Put().
164                 AbsPath(append(c.makeURLSegments(accessor.GetName()), "status")...).
165                 Body(outBytes).
166                 SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
167                 Do()
168         if err := result.Error(); err != nil {
169                 return nil, err
170         }
171
172         retBytes, err := result.Raw()
173         if err != nil {
174                 return nil, err
175         }
176         uncastObj, err := runtime.Decode(unstructured.UnstructuredJSONScheme, retBytes)
177         if err != nil {
178                 return nil, err
179         }
180         return uncastObj.(*unstructured.Unstructured), nil
181 }
182
183 func (c *dynamicResourceClient) Delete(name string, opts *metav1.DeleteOptions, subresources ...string) error {
184         if opts == nil {
185                 opts = &metav1.DeleteOptions{}
186         }
187         deleteOptionsByte, err := runtime.Encode(deleteOptionsCodec.LegacyCodec(schema.GroupVersion{Version: "v1"}), opts)
188         if err != nil {
189                 return err
190         }
191
192         result := c.client.client.
193                 Delete().
194                 AbsPath(append(c.makeURLSegments(name), subresources...)...).
195                 Body(deleteOptionsByte).
196                 Do()
197         return result.Error()
198 }
199
200 func (c *dynamicResourceClient) DeleteCollection(opts *metav1.DeleteOptions, listOptions metav1.ListOptions) error {
201         if opts == nil {
202                 opts = &metav1.DeleteOptions{}
203         }
204         deleteOptionsByte, err := runtime.Encode(deleteOptionsCodec.LegacyCodec(schema.GroupVersion{Version: "v1"}), opts)
205         if err != nil {
206                 return err
207         }
208
209         result := c.client.client.
210                 Delete().
211                 AbsPath(c.makeURLSegments("")...).
212                 Body(deleteOptionsByte).
213                 SpecificallyVersionedParams(&listOptions, dynamicParameterCodec, versionV1).
214                 Do()
215         return result.Error()
216 }
217
218 func (c *dynamicResourceClient) Get(name string, opts metav1.GetOptions, subresources ...string) (*unstructured.Unstructured, error) {
219         result := c.client.client.Get().AbsPath(append(c.makeURLSegments(name), subresources...)...).SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).Do()
220         if err := result.Error(); err != nil {
221                 return nil, err
222         }
223         retBytes, err := result.Raw()
224         if err != nil {
225                 return nil, err
226         }
227         uncastObj, err := runtime.Decode(unstructured.UnstructuredJSONScheme, retBytes)
228         if err != nil {
229                 return nil, err
230         }
231         return uncastObj.(*unstructured.Unstructured), nil
232 }
233
234 func (c *dynamicResourceClient) List(opts metav1.ListOptions) (*unstructured.UnstructuredList, error) {
235         result := c.client.client.Get().AbsPath(c.makeURLSegments("")...).SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).Do()
236         if err := result.Error(); err != nil {
237                 return nil, err
238         }
239         retBytes, err := result.Raw()
240         if err != nil {
241                 return nil, err
242         }
243         uncastObj, err := runtime.Decode(unstructured.UnstructuredJSONScheme, retBytes)
244         if err != nil {
245                 return nil, err
246         }
247         if list, ok := uncastObj.(*unstructured.UnstructuredList); ok {
248                 return list, nil
249         }
250
251         list, err := uncastObj.(*unstructured.Unstructured).ToList()
252         if err != nil {
253                 return nil, err
254         }
255         return list, nil
256 }
257
258 func (c *dynamicResourceClient) Watch(opts metav1.ListOptions) (watch.Interface, error) {
259         internalGV := schema.GroupVersions{
260                 {Group: c.resource.Group, Version: runtime.APIVersionInternal},
261                 // always include the legacy group as a decoding target to handle non-error `Status` return types
262                 {Group: "", Version: runtime.APIVersionInternal},
263         }
264         s := &rest.Serializers{
265                 Encoder: watchNegotiatedSerializerInstance.EncoderForVersion(watchJsonSerializerInfo.Serializer, c.resource.GroupVersion()),
266                 Decoder: watchNegotiatedSerializerInstance.DecoderToVersion(watchJsonSerializerInfo.Serializer, internalGV),
267
268                 RenegotiatedDecoder: func(contentType string, params map[string]string) (runtime.Decoder, error) {
269                         return watchNegotiatedSerializerInstance.DecoderToVersion(watchJsonSerializerInfo.Serializer, internalGV), nil
270                 },
271                 StreamingSerializer: watchJsonSerializerInfo.StreamSerializer.Serializer,
272                 Framer:              watchJsonSerializerInfo.StreamSerializer.Framer,
273         }
274
275         wrappedDecoderFn := func(body io.ReadCloser) streaming.Decoder {
276                 framer := s.Framer.NewFrameReader(body)
277                 return streaming.NewDecoder(framer, s.StreamingSerializer)
278         }
279
280         opts.Watch = true
281         return c.client.client.Get().AbsPath(c.makeURLSegments("")...).
282                 SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
283                 WatchWithSpecificDecoders(wrappedDecoderFn, unstructured.UnstructuredJSONScheme)
284 }
285
286 func (c *dynamicResourceClient) Patch(name string, pt types.PatchType, data []byte, opts metav1.UpdateOptions, subresources ...string) (*unstructured.Unstructured, error) {
287         result := c.client.client.
288                 Patch(pt).
289                 AbsPath(append(c.makeURLSegments(name), subresources...)...).
290                 Body(data).
291                 SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
292                 Do()
293         if err := result.Error(); err != nil {
294                 return nil, err
295         }
296         retBytes, err := result.Raw()
297         if err != nil {
298                 return nil, err
299         }
300         uncastObj, err := runtime.Decode(unstructured.UnstructuredJSONScheme, retBytes)
301         if err != nil {
302                 return nil, err
303         }
304         return uncastObj.(*unstructured.Unstructured), nil
305 }
306
307 func (c *dynamicResourceClient) makeURLSegments(name string) []string {
308         url := []string{}
309         if len(c.resource.Group) == 0 {
310                 url = append(url, "api")
311         } else {
312                 url = append(url, "apis", c.resource.Group)
313         }
314         url = append(url, c.resource.Version)
315
316         if len(c.namespace) > 0 {
317                 url = append(url, "namespaces", c.namespace)
318         }
319         url = append(url, c.resource.Resource)
320
321         if len(name) > 0 {
322                 url = append(url, name)
323         }
324
325         return url
326 }