Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / sigs.k8s.io / controller-runtime / pkg / client / apiutil / apimachinery.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 apiutil
18
19 import (
20         "fmt"
21
22         "k8s.io/apimachinery/pkg/api/meta"
23         "k8s.io/apimachinery/pkg/runtime"
24         "k8s.io/apimachinery/pkg/runtime/schema"
25         "k8s.io/apimachinery/pkg/runtime/serializer"
26         "k8s.io/client-go/discovery"
27         "k8s.io/client-go/rest"
28         "k8s.io/client-go/restmapper"
29 )
30
31 // NewDiscoveryRESTMapper constructs a new RESTMapper based on discovery
32 // information fetched by a new client with the given config.
33 func NewDiscoveryRESTMapper(c *rest.Config) (meta.RESTMapper, error) {
34         // Get a mapper
35         dc := discovery.NewDiscoveryClientForConfigOrDie(c)
36         gr, err := restmapper.GetAPIGroupResources(dc)
37         if err != nil {
38                 return nil, err
39         }
40         return restmapper.NewDiscoveryRESTMapper(gr), nil
41 }
42
43 // GVKForObject finds the GroupVersionKind associated with the given object, if there is only a single such GVK.
44 func GVKForObject(obj runtime.Object, scheme *runtime.Scheme) (schema.GroupVersionKind, error) {
45         gvks, isUnversioned, err := scheme.ObjectKinds(obj)
46         if err != nil {
47                 return schema.GroupVersionKind{}, err
48         }
49         if isUnversioned {
50                 return schema.GroupVersionKind{}, fmt.Errorf("cannot create a new informer for the unversioned type %T", obj)
51         }
52
53         if len(gvks) < 1 {
54                 return schema.GroupVersionKind{}, fmt.Errorf("no group-version-kinds associated with type %T", obj)
55         }
56         if len(gvks) > 1 {
57                 // this should only trigger for things like metav1.XYZ --
58                 // normal versioned types should be fine
59                 return schema.GroupVersionKind{}, fmt.Errorf(
60                         "multiple group-version-kinds associated with type %T, refusing to guess at one", obj)
61         }
62         return gvks[0], nil
63 }
64
65 // RESTClientForGVK constructs a new rest.Interface capable of accessing the resource associated
66 // with the given GroupVersionKind.
67 func RESTClientForGVK(gvk schema.GroupVersionKind, baseConfig *rest.Config, codecs serializer.CodecFactory) (rest.Interface, error) {
68         cfg := createRestConfig(gvk, baseConfig)
69         cfg.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: codecs}
70         return rest.RESTClientFor(cfg)
71 }
72
73 //createRestConfig copies the base config and updates needed fields for a new rest config
74 func createRestConfig(gvk schema.GroupVersionKind, baseConfig *rest.Config) *rest.Config {
75         gv := gvk.GroupVersion()
76
77         cfg := rest.CopyConfig(baseConfig)
78         cfg.GroupVersion = &gv
79         if gvk.Group == "" {
80                 cfg.APIPath = "/api"
81         } else {
82                 cfg.APIPath = "/apis"
83         }
84         if cfg.UserAgent == "" {
85                 cfg.UserAgent = rest.DefaultKubernetesUserAgent()
86         }
87         return cfg
88 }