Skip to content

Commit 10a0272

Browse files
committed
client/cache/client_cache.go should be called resources
This file and bits in it were confusing, the client package doesn't have any sort of built-in cache (as in, watch/list cache from informers). Every request goes to the API server directly instead. The "internal cache" is really just a storage for all rest client resources, this commit changes that to clarify and avoid confusion. Signed-off-by: Vince Prignano <[email protected]>
1 parent 0a38bbf commit 10a0272

File tree

5 files changed

+39
-43
lines changed

5 files changed

+39
-43
lines changed

pkg/client/client.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func newClient(config *rest.Config, options Options) (*client, error) {
112112
}
113113
}
114114

115-
clientcache := &clientCache{
115+
resources := &clientRestResources{
116116
config: config,
117117
scheme: options.Scheme,
118118
mapper: options.Mapper,
@@ -129,11 +129,11 @@ func newClient(config *rest.Config, options Options) (*client, error) {
129129

130130
c := &client{
131131
typedClient: typedClient{
132-
cache: clientcache,
132+
resources: resources,
133133
paramCodec: runtime.NewParameterCodec(options.Scheme),
134134
},
135135
unstructuredClient: unstructuredClient{
136-
cache: clientcache,
136+
resources: resources,
137137
paramCodec: noConversionParamCodec{},
138138
},
139139
metadataClient: metadataClient{
@@ -149,8 +149,8 @@ func newClient(config *rest.Config, options Options) (*client, error) {
149149

150150
var _ Client = &client{}
151151

152-
// client is a client.Client that reads and writes directly from/to an API server. It lazily initializes
153-
// new clients at the time they are used, and caches the client.
152+
// client is a client.Client that reads and writes directly from/to an API server.
153+
// It lazily initializes new clients at the time they are used.
154154
type client struct {
155155
typedClient typedClient
156156
unstructuredClient unstructuredClient

pkg/client/client_cache.go pkg/client/client_rest_resources.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ import (
3030
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
3131
)
3232

33-
// clientCache creates and caches rest clients and metadata for Kubernetes types.
34-
type clientCache struct {
33+
// clientRestResources creates and stores rest clients and metadata for Kubernetes types.
34+
type clientRestResources struct {
3535
// config is the rest.Config to talk to an apiserver
3636
config *rest.Config
3737

@@ -44,16 +44,16 @@ type clientCache struct {
4444
// codecs are used to create a REST client for a gvk
4545
codecs serializer.CodecFactory
4646

47-
// structuredResourceByType caches structured type metadata
47+
// structuredResourceByType stores structured type metadata
4848
structuredResourceByType map[schema.GroupVersionKind]*resourceMeta
49-
// unstructuredResourceByType caches unstructured type metadata
49+
// unstructuredResourceByType stores unstructured type metadata
5050
unstructuredResourceByType map[schema.GroupVersionKind]*resourceMeta
5151
mu sync.RWMutex
5252
}
5353

5454
// newResource maps obj to a Kubernetes Resource and constructs a client for that Resource.
5555
// If the object is a list, the resource represents the item's type instead.
56-
func (c *clientCache) newResource(gvk schema.GroupVersionKind, isList, isUnstructured bool) (*resourceMeta, error) {
56+
func (c *clientRestResources) newResource(gvk schema.GroupVersionKind, isList, isUnstructured bool) (*resourceMeta, error) {
5757
if strings.HasSuffix(gvk.Kind, "List") && isList {
5858
// if this was a list, treat it as a request for the item's resource
5959
gvk.Kind = gvk.Kind[:len(gvk.Kind)-4]
@@ -72,7 +72,7 @@ func (c *clientCache) newResource(gvk schema.GroupVersionKind, isList, isUnstruc
7272

7373
// getResource returns the resource meta information for the given type of object.
7474
// If the object is a list, the resource represents the item's type instead.
75-
func (c *clientCache) getResource(obj runtime.Object) (*resourceMeta, error) {
75+
func (c *clientRestResources) getResource(obj runtime.Object) (*resourceMeta, error) {
7676
gvk, err := apiutil.GVKForObject(obj, c.scheme)
7777
if err != nil {
7878
return nil, err
@@ -108,7 +108,7 @@ func (c *clientCache) getResource(obj runtime.Object) (*resourceMeta, error) {
108108
}
109109

110110
// getObjMeta returns objMeta containing both type and object metadata and state.
111-
func (c *clientCache) getObjMeta(obj runtime.Object) (*objMeta, error) {
111+
func (c *clientRestResources) getObjMeta(obj runtime.Object) (*objMeta, error) {
112112
r, err := c.getResource(obj)
113113
if err != nil {
114114
return nil, err
@@ -120,7 +120,7 @@ func (c *clientCache) getObjMeta(obj runtime.Object) (*objMeta, error) {
120120
return &objMeta{resourceMeta: r, Object: m}, err
121121
}
122122

123-
// resourceMeta caches state for a Kubernetes type.
123+
// resourceMeta stores state for a Kubernetes type.
124124
type resourceMeta struct {
125125
// client is the rest client used to talk to the apiserver
126126
rest.Interface

pkg/client/typed_client.go

+12-14
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,14 @@ import (
2525
var _ Reader = &typedClient{}
2626
var _ Writer = &typedClient{}
2727

28-
// client is a client.Client that reads and writes directly from/to an API server. It lazily initializes
29-
// new clients at the time they are used, and caches the client.
3028
type typedClient struct {
31-
cache *clientCache
29+
resources *clientRestResources
3230
paramCodec runtime.ParameterCodec
3331
}
3432

3533
// Create implements client.Client.
3634
func (c *typedClient) Create(ctx context.Context, obj Object, opts ...CreateOption) error {
37-
o, err := c.cache.getObjMeta(obj)
35+
o, err := c.resources.getObjMeta(obj)
3836
if err != nil {
3937
return err
4038
}
@@ -53,7 +51,7 @@ func (c *typedClient) Create(ctx context.Context, obj Object, opts ...CreateOpti
5351

5452
// Update implements client.Client.
5553
func (c *typedClient) Update(ctx context.Context, obj Object, opts ...UpdateOption) error {
56-
o, err := c.cache.getObjMeta(obj)
54+
o, err := c.resources.getObjMeta(obj)
5755
if err != nil {
5856
return err
5957
}
@@ -73,7 +71,7 @@ func (c *typedClient) Update(ctx context.Context, obj Object, opts ...UpdateOpti
7371

7472
// Delete implements client.Client.
7573
func (c *typedClient) Delete(ctx context.Context, obj Object, opts ...DeleteOption) error {
76-
o, err := c.cache.getObjMeta(obj)
74+
o, err := c.resources.getObjMeta(obj)
7775
if err != nil {
7876
return err
7977
}
@@ -92,7 +90,7 @@ func (c *typedClient) Delete(ctx context.Context, obj Object, opts ...DeleteOpti
9290

9391
// DeleteAllOf implements client.Client.
9492
func (c *typedClient) DeleteAllOf(ctx context.Context, obj Object, opts ...DeleteAllOfOption) error {
95-
o, err := c.cache.getObjMeta(obj)
93+
o, err := c.resources.getObjMeta(obj)
9694
if err != nil {
9795
return err
9896
}
@@ -111,7 +109,7 @@ func (c *typedClient) DeleteAllOf(ctx context.Context, obj Object, opts ...Delet
111109

112110
// Patch implements client.Client.
113111
func (c *typedClient) Patch(ctx context.Context, obj Object, patch Patch, opts ...PatchOption) error {
114-
o, err := c.cache.getObjMeta(obj)
112+
o, err := c.resources.getObjMeta(obj)
115113
if err != nil {
116114
return err
117115
}
@@ -136,7 +134,7 @@ func (c *typedClient) Patch(ctx context.Context, obj Object, patch Patch, opts .
136134

137135
// Get implements client.Client.
138136
func (c *typedClient) Get(ctx context.Context, key ObjectKey, obj Object, opts ...GetOption) error {
139-
r, err := c.cache.getResource(obj)
137+
r, err := c.resources.getResource(obj)
140138
if err != nil {
141139
return err
142140
}
@@ -151,7 +149,7 @@ func (c *typedClient) Get(ctx context.Context, key ObjectKey, obj Object, opts .
151149

152150
// List implements client.Client.
153151
func (c *typedClient) List(ctx context.Context, obj ObjectList, opts ...ListOption) error {
154-
r, err := c.cache.getResource(obj)
152+
r, err := c.resources.getResource(obj)
155153
if err != nil {
156154
return err
157155
}
@@ -168,7 +166,7 @@ func (c *typedClient) List(ctx context.Context, obj ObjectList, opts ...ListOpti
168166
}
169167

170168
func (c *typedClient) GetSubResource(ctx context.Context, obj, subResourceObj Object, subResource string, opts ...SubResourceGetOption) error {
171-
o, err := c.cache.getObjMeta(obj)
169+
o, err := c.resources.getObjMeta(obj)
172170
if err != nil {
173171
return err
174172
}
@@ -191,7 +189,7 @@ func (c *typedClient) GetSubResource(ctx context.Context, obj, subResourceObj Ob
191189
}
192190

193191
func (c *typedClient) CreateSubResource(ctx context.Context, obj Object, subResourceObj Object, subResource string, opts ...SubResourceCreateOption) error {
194-
o, err := c.cache.getObjMeta(obj)
192+
o, err := c.resources.getObjMeta(obj)
195193
if err != nil {
196194
return err
197195
}
@@ -216,7 +214,7 @@ func (c *typedClient) CreateSubResource(ctx context.Context, obj Object, subReso
216214

217215
// UpdateSubResource used by SubResourceWriter to write status.
218216
func (c *typedClient) UpdateSubResource(ctx context.Context, obj Object, subResource string, opts ...SubResourceUpdateOption) error {
219-
o, err := c.cache.getObjMeta(obj)
217+
o, err := c.resources.getObjMeta(obj)
220218
if err != nil {
221219
return err
222220
}
@@ -251,7 +249,7 @@ func (c *typedClient) UpdateSubResource(ctx context.Context, obj Object, subReso
251249

252250
// PatchSubResource used by SubResourceWriter to write subresource.
253251
func (c *typedClient) PatchSubResource(ctx context.Context, obj Object, subResource string, patch Patch, opts ...SubResourcePatchOption) error {
254-
o, err := c.cache.getObjMeta(obj)
252+
o, err := c.resources.getObjMeta(obj)
255253
if err != nil {
256254
return err
257255
}

pkg/client/unstructured_client.go

+12-14
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ import (
2828
var _ Reader = &unstructuredClient{}
2929
var _ Writer = &unstructuredClient{}
3030

31-
// client is a client.Client that reads and writes directly from/to an API server. It lazily initializes
32-
// new clients at the time they are used, and caches the client.
3331
type unstructuredClient struct {
34-
cache *clientCache
32+
resources *clientRestResources
3533
paramCodec runtime.ParameterCodec
3634
}
3735

@@ -44,7 +42,7 @@ func (uc *unstructuredClient) Create(ctx context.Context, obj Object, opts ...Cr
4442

4543
gvk := u.GroupVersionKind()
4644

47-
o, err := uc.cache.getObjMeta(obj)
45+
o, err := uc.resources.getObjMeta(obj)
4846
if err != nil {
4947
return err
5048
}
@@ -73,7 +71,7 @@ func (uc *unstructuredClient) Update(ctx context.Context, obj Object, opts ...Up
7371

7472
gvk := u.GroupVersionKind()
7573

76-
o, err := uc.cache.getObjMeta(obj)
74+
o, err := uc.resources.getObjMeta(obj)
7775
if err != nil {
7876
return err
7977
}
@@ -100,7 +98,7 @@ func (uc *unstructuredClient) Delete(ctx context.Context, obj Object, opts ...De
10098
return fmt.Errorf("unstructured client did not understand object: %T", obj)
10199
}
102100

103-
o, err := uc.cache.getObjMeta(obj)
101+
o, err := uc.resources.getObjMeta(obj)
104102
if err != nil {
105103
return err
106104
}
@@ -123,7 +121,7 @@ func (uc *unstructuredClient) DeleteAllOf(ctx context.Context, obj Object, opts
123121
return fmt.Errorf("unstructured client did not understand object: %T", obj)
124122
}
125123

126-
o, err := uc.cache.getObjMeta(obj)
124+
o, err := uc.resources.getObjMeta(obj)
127125
if err != nil {
128126
return err
129127
}
@@ -146,7 +144,7 @@ func (uc *unstructuredClient) Patch(ctx context.Context, obj Object, patch Patch
146144
return fmt.Errorf("unstructured client did not understand object: %T", obj)
147145
}
148146

149-
o, err := uc.cache.getObjMeta(obj)
147+
o, err := uc.resources.getObjMeta(obj)
150148
if err != nil {
151149
return err
152150
}
@@ -181,7 +179,7 @@ func (uc *unstructuredClient) Get(ctx context.Context, key ObjectKey, obj Object
181179
getOpts := GetOptions{}
182180
getOpts.ApplyOptions(opts)
183181

184-
r, err := uc.cache.getResource(obj)
182+
r, err := uc.resources.getResource(obj)
185183
if err != nil {
186184
return err
187185
}
@@ -209,7 +207,7 @@ func (uc *unstructuredClient) List(ctx context.Context, obj ObjectList, opts ...
209207
gvk := u.GroupVersionKind()
210208
gvk.Kind = strings.TrimSuffix(gvk.Kind, "List")
211209

212-
r, err := uc.cache.getResource(obj)
210+
r, err := uc.resources.getResource(obj)
213211
if err != nil {
214212
return err
215213
}
@@ -238,7 +236,7 @@ func (uc *unstructuredClient) GetSubResource(ctx context.Context, obj, subResour
238236
subResourceObj.SetName(obj.GetName())
239237
}
240238

241-
o, err := uc.cache.getObjMeta(obj)
239+
o, err := uc.resources.getObjMeta(obj)
242240
if err != nil {
243241
return err
244242
}
@@ -269,7 +267,7 @@ func (uc *unstructuredClient) CreateSubResource(ctx context.Context, obj, subRes
269267
subResourceObj.SetName(obj.GetName())
270268
}
271269

272-
o, err := uc.cache.getObjMeta(obj)
270+
o, err := uc.resources.getObjMeta(obj)
273271
if err != nil {
274272
return err
275273
}
@@ -293,7 +291,7 @@ func (uc *unstructuredClient) UpdateSubResource(ctx context.Context, obj Object,
293291
return fmt.Errorf("unstructured client did not understand object: %T", obj)
294292
}
295293

296-
o, err := uc.cache.getObjMeta(obj)
294+
o, err := uc.resources.getObjMeta(obj)
297295
if err != nil {
298296
return err
299297
}
@@ -331,7 +329,7 @@ func (uc *unstructuredClient) PatchSubResource(ctx context.Context, obj Object,
331329

332330
gvk := u.GroupVersionKind()
333331

334-
o, err := uc.cache.getObjMeta(obj)
332+
o, err := uc.resources.getObjMeta(obj)
335333
if err != nil {
336334
return err
337335
}

pkg/client/watch.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (w *watchingClient) unstructuredWatch(ctx context.Context, obj *unstructure
8585
gvk := obj.GroupVersionKind()
8686
gvk.Kind = strings.TrimSuffix(gvk.Kind, "List")
8787

88-
r, err := w.client.unstructuredClient.cache.getResource(obj)
88+
r, err := w.client.unstructuredClient.resources.getResource(obj)
8989
if err != nil {
9090
return nil, err
9191
}
@@ -99,7 +99,7 @@ func (w *watchingClient) unstructuredWatch(ctx context.Context, obj *unstructure
9999
}
100100

101101
func (w *watchingClient) typedWatch(ctx context.Context, obj ObjectList, opts ...ListOption) (watch.Interface, error) {
102-
r, err := w.client.typedClient.cache.getResource(obj)
102+
r, err := w.client.typedClient.resources.getResource(obj)
103103
if err != nil {
104104
return nil, err
105105
}

0 commit comments

Comments
 (0)