@@ -17,7 +17,6 @@ limitations under the License.
17
17
package client
18
18
19
19
import (
20
- "reflect"
21
20
"strings"
22
21
"sync"
23
22
@@ -45,19 +44,14 @@ type clientCache struct {
45
44
codecs serializer.CodecFactory
46
45
47
46
// resourceByType caches type metadata
48
- resourceByType map [reflect. Type ]* resourceMeta
47
+ resourceByType map [schema. GroupVersionKind ]* resourceMeta
49
48
mu sync.RWMutex
50
49
}
51
50
52
51
// newResource maps obj to a Kubernetes Resource and constructs a client for that Resource.
53
52
// 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 ) {
53
+ func (c * clientCache ) newResource (gvk schema.GroupVersionKind , isList bool ) (* resourceMeta , error ) {
54
+ if strings .HasSuffix (gvk .Kind , "List" ) && isList {
61
55
// if this was a list, treat it as a request for the item's resource
62
56
gvk .Kind = gvk .Kind [:len (gvk .Kind )- 4 ]
63
57
}
@@ -76,12 +70,15 @@ func (c *clientCache) newResource(obj runtime.Object) (*resourceMeta, error) {
76
70
// getResource returns the resource meta information for the given type of object.
77
71
// If the object is a list, the resource represents the item's type instead.
78
72
func (c * clientCache ) getResource (obj runtime.Object ) (* resourceMeta , error ) {
79
- typ := reflect .TypeOf (obj )
73
+ gvk , err := apiutil .GVKForObject (obj , c .scheme )
74
+ if err != nil {
75
+ return nil , err
76
+ }
80
77
81
78
// It's better to do creation work twice than to not let multiple
82
79
// people make requests at once
83
80
c .mu .RLock ()
84
- r , known := c .resourceByType [typ ]
81
+ r , known := c .resourceByType [gvk ]
85
82
c .mu .RUnlock ()
86
83
87
84
if known {
@@ -91,11 +88,11 @@ func (c *clientCache) getResource(obj runtime.Object) (*resourceMeta, error) {
91
88
// Initialize a new Client
92
89
c .mu .Lock ()
93
90
defer c .mu .Unlock ()
94
- r , err : = c .newResource (obj )
91
+ r , err = c .newResource (gvk , meta . IsListType ( obj ) )
95
92
if err != nil {
96
93
return nil , err
97
94
}
98
- c .resourceByType [typ ] = r
95
+ c .resourceByType [gvk ] = r
99
96
return r , err
100
97
}
101
98
0 commit comments