Skip to content

Commit 308bb88

Browse files
committed
🐛 respect context in unstructured client
1 parent 19e72eb commit 308bb88

File tree

5 files changed

+178
-109
lines changed

5 files changed

+178
-109
lines changed

pkg/client/client.go

+11-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package client
1919
import (
2020
"context"
2121
"fmt"
22-
"reflect"
2322

2423
"k8s.io/apimachinery/pkg/api/meta"
2524
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -75,20 +74,24 @@ func New(config *rest.Config, options Options) (Client, error) {
7574
return nil, err
7675
}
7776

77+
clientcache := &clientCache{
78+
config: config,
79+
scheme: options.Scheme,
80+
mapper: options.Mapper,
81+
codecs: serializer.NewCodecFactory(options.Scheme),
82+
resourceByType: make(map[schema.GroupVersionKind]*resourceMeta),
83+
}
84+
7885
c := &client{
7986
typedClient: typedClient{
80-
cache: clientCache{
81-
config: config,
82-
scheme: options.Scheme,
83-
mapper: options.Mapper,
84-
codecs: serializer.NewCodecFactory(options.Scheme),
85-
resourceByType: make(map[reflect.Type]*resourceMeta),
86-
},
87+
cache: clientcache,
8788
paramCodec: runtime.NewParameterCodec(options.Scheme),
8889
},
8990
unstructuredClient: unstructuredClient{
91+
cache: clientcache,
9092
client: dynamicClient,
9193
restMapper: options.Mapper,
94+
paramCodec: parameterCodec{},
9295
},
9396
}
9497

pkg/client/client_cache.go

+10-13
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package client
1818

1919
import (
20-
"reflect"
2120
"strings"
2221
"sync"
2322

@@ -45,19 +44,14 @@ type clientCache struct {
4544
codecs serializer.CodecFactory
4645

4746
// resourceByType caches type metadata
48-
resourceByType map[reflect.Type]*resourceMeta
47+
resourceByType map[schema.GroupVersionKind]*resourceMeta
4948
mu sync.RWMutex
5049
}
5150

5251
// newResource maps obj to a Kubernetes Resource and constructs a client for that Resource.
5352
// 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 {
6155
// if this was a list, treat it as a request for the item's resource
6256
gvk.Kind = gvk.Kind[:len(gvk.Kind)-4]
6357
}
@@ -76,12 +70,15 @@ func (c *clientCache) newResource(obj runtime.Object) (*resourceMeta, error) {
7670
// getResource returns the resource meta information for the given type of object.
7771
// If the object is a list, the resource represents the item's type instead.
7872
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+
}
8077

8178
// It's better to do creation work twice than to not let multiple
8279
// people make requests at once
8380
c.mu.RLock()
84-
r, known := c.resourceByType[typ]
81+
r, known := c.resourceByType[gvk]
8582
c.mu.RUnlock()
8683

8784
if known {
@@ -91,11 +88,11 @@ func (c *clientCache) getResource(obj runtime.Object) (*resourceMeta, error) {
9188
// Initialize a new Client
9289
c.mu.Lock()
9390
defer c.mu.Unlock()
94-
r, err := c.newResource(obj)
91+
r, err = c.newResource(gvk, meta.IsListType(obj))
9592
if err != nil {
9693
return nil, err
9794
}
98-
c.resourceByType[typ] = r
95+
c.resourceByType[gvk] = r
9996
return r, err
10097
}
10198

pkg/client/codec.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package client
2+
3+
import (
4+
"errors"
5+
"net/url"
6+
7+
"k8s.io/apimachinery/pkg/conversion/queryparams"
8+
"k8s.io/apimachinery/pkg/runtime"
9+
"k8s.io/apimachinery/pkg/runtime/schema"
10+
)
11+
12+
type parameterCodec struct{}
13+
14+
func (parameterCodec) EncodeParameters(obj runtime.Object, to schema.GroupVersion) (url.Values, error) {
15+
return queryparams.Convert(obj)
16+
}
17+
18+
func (parameterCodec) DecodeParameters(parameters url.Values, from schema.GroupVersion, into runtime.Object) error {
19+
return errors.New("DecodeParameters not implemented on dynamic parameterCodec")
20+
}

pkg/client/typed_client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
// client is a client.Client that reads and writes directly from/to an API server. It lazily initializes
2626
// new clients at the time they are used, and caches the client.
2727
type typedClient struct {
28-
cache clientCache
28+
cache *clientCache
2929
paramCodec runtime.ParameterCodec
3030
}
3131

0 commit comments

Comments
 (0)