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.
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"
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
38 // scheme maps go structs to GroupVersionKinds
39 scheme *runtime.Scheme
41 // mapper maps GroupVersionKinds to Resources
42 mapper meta.RESTMapper
44 // codecs are used to create a REST client for a gvk
45 codecs serializer.CodecFactory
47 // resourceByType caches type metadata
48 resourceByType map[reflect.Type]*resourceMeta
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)
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]
65 client, err := apiutil.RESTClientForGVK(gvk, c.config, c.codecs)
69 mapping, err := c.mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
73 return &resourceMeta{Interface: client, mapping: mapping, gvk: gvk}, nil
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)
81 // It's better to do creation work twice than to not let multiple
82 // people make requests at once
84 r, known := c.resourceByType[typ]
91 // Initialize a new Client
94 r, err := c.newResource(obj)
98 c.resourceByType[typ] = r
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)
108 m, err := meta.Accessor(obj)
112 return &objMeta{resourceMeta: r, Object: m}, err
115 // resourceMeta caches state for a Kubernetes type.
116 type resourceMeta struct {
117 // client is the rest client used to talk to the apiserver
119 // gvk is the GroupVersionKind of the resourceMeta
120 gvk schema.GroupVersionKind
121 // mapping is the rest mapping
122 mapping *meta.RESTMapping
125 // isNamespaced returns true if the type is namespaced
126 func (r *resourceMeta) isNamespaced() bool {
127 if r.mapping.Scope.Name() == meta.RESTScopeNameRoot {
133 // resource returns the resource name of the type
134 func (r *resourceMeta) resource() string {
135 return r.mapping.Resource.Resource
138 // objMeta stores type and object information about a Kubernetes type
139 type objMeta struct {
140 // resourceMeta contains type information for the object
143 // Object contains meta data for the object instance