Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / sigs.k8s.io / controller-runtime / pkg / client / client_cache.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         "reflect"
21         "strings"
22         "sync"
23
24         "k8s.io/apimachinery/pkg/api/meta"
25         "k8s.io/apimachinery/pkg/apis/meta/v1"
26         "k8s.io/apimachinery/pkg/runtime"
27         "k8s.io/apimachinery/pkg/runtime/schema"
28         "k8s.io/apimachinery/pkg/runtime/serializer"
29         "k8s.io/client-go/rest"
30         "sigs.k8s.io/controller-runtime/pkg/client/apiutil"
31 )
32
33 // clientCache creates and caches rest clients and metadata for Kubernetes types
34 type clientCache struct {
35         // config is the rest.Config to talk to an apiserver
36         config *rest.Config
37
38         // scheme maps go structs to GroupVersionKinds
39         scheme *runtime.Scheme
40
41         // mapper maps GroupVersionKinds to Resources
42         mapper meta.RESTMapper
43
44         // codecs are used to create a REST client for a gvk
45         codecs serializer.CodecFactory
46
47         // resourceByType caches type metadata
48         resourceByType map[reflect.Type]*resourceMeta
49         mu             sync.RWMutex
50 }
51
52 // newResource maps obj to a Kubernetes Resource and constructs a client for that Resource.
53 // If the object is a list, the resource represents the item's type instead.
54 func (c *clientCache) newResource(obj runtime.Object) (*resourceMeta, error) {
55         gvk, err := apiutil.GVKForObject(obj, c.scheme)
56         if err != nil {
57                 return nil, err
58         }
59
60         if strings.HasSuffix(gvk.Kind, "List") && meta.IsListType(obj) {
61                 // if this was a list, treat it as a request for the item's resource
62                 gvk.Kind = gvk.Kind[:len(gvk.Kind)-4]
63         }
64
65         client, err := apiutil.RESTClientForGVK(gvk, c.config, c.codecs)
66         if err != nil {
67                 return nil, err
68         }
69         mapping, err := c.mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
70         if err != nil {
71                 return nil, err
72         }
73         return &resourceMeta{Interface: client, mapping: mapping, gvk: gvk}, nil
74 }
75
76 // getResource returns the resource meta information for the given type of object.
77 // If the object is a list, the resource represents the item's type instead.
78 func (c *clientCache) getResource(obj runtime.Object) (*resourceMeta, error) {
79         typ := reflect.TypeOf(obj)
80
81         // It's better to do creation work twice than to not let multiple
82         // people make requests at once
83         c.mu.RLock()
84         r, known := c.resourceByType[typ]
85         c.mu.RUnlock()
86
87         if known {
88                 return r, nil
89         }
90
91         // Initialize a new Client
92         c.mu.Lock()
93         defer c.mu.Unlock()
94         r, err := c.newResource(obj)
95         if err != nil {
96                 return nil, err
97         }
98         c.resourceByType[typ] = r
99         return r, err
100 }
101
102 // getObjMeta returns objMeta containing both type and object metadata and state
103 func (c *clientCache) getObjMeta(obj runtime.Object) (*objMeta, error) {
104         r, err := c.getResource(obj)
105         if err != nil {
106                 return nil, err
107         }
108         m, err := meta.Accessor(obj)
109         if err != nil {
110                 return nil, err
111         }
112         return &objMeta{resourceMeta: r, Object: m}, err
113 }
114
115 // resourceMeta caches state for a Kubernetes type.
116 type resourceMeta struct {
117         // client is the rest client used to talk to the apiserver
118         rest.Interface
119         // gvk is the GroupVersionKind of the resourceMeta
120         gvk schema.GroupVersionKind
121         // mapping is the rest mapping
122         mapping *meta.RESTMapping
123 }
124
125 // isNamespaced returns true if the type is namespaced
126 func (r *resourceMeta) isNamespaced() bool {
127         if r.mapping.Scope.Name() == meta.RESTScopeNameRoot {
128                 return false
129         }
130         return true
131 }
132
133 // resource returns the resource name of the type
134 func (r *resourceMeta) resource() string {
135         return r.mapping.Resource.Resource
136 }
137
138 // objMeta stores type and object information about a Kubernetes type
139 type objMeta struct {
140         // resourceMeta contains type information for the object
141         *resourceMeta
142
143         // Object contains meta data for the object instance
144         v1.Object
145 }