Skip to content

Commit e83442f

Browse files
authored
Merge pull request #1058 from vincepri/expose-client-scheme
⚠ Expose Client runtime.Scheme
2 parents cfdc32f + e263d2c commit e83442f

File tree

9 files changed

+127
-50
lines changed

9 files changed

+127
-50
lines changed

pkg/client/client.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func New(config *rest.Config, options Options) (Client, error) {
8585
cache: clientcache,
8686
paramCodec: noConversionParamCodec{},
8787
},
88+
scheme: options.Scheme,
8889
}
8990

9091
return c, nil
@@ -97,6 +98,7 @@ var _ Client = &client{}
9798
type client struct {
9899
typedClient typedClient
99100
unstructuredClient unstructuredClient
101+
scheme *runtime.Scheme
100102
}
101103

102104
// resetGroupVersionKind is a helper function to restore and preserve GroupVersionKind on an object.
@@ -109,6 +111,11 @@ func (c *client) resetGroupVersionKind(obj runtime.Object, gvk schema.GroupVersi
109111
}
110112
}
111113

114+
// Scheme returns the scheme this client is using.
115+
func (c *client) Scheme() *runtime.Scheme {
116+
return c.scheme
117+
}
118+
112119
// Create implements client.Client
113120
func (c *client) Create(ctx context.Context, obj runtime.Object, opts ...CreateOption) error {
114121
_, ok := obj.(*unstructured.Unstructured)

pkg/client/client_test.go

Lines changed: 67 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2625,62 +2625,96 @@ var _ = Describe("Client", func() {
26252625
})
26262626
})
26272627

2628-
var _ = Describe("DelegatingReader", func() {
2628+
var _ = Describe("DelegatingClient", func() {
26292629
Describe("Get", func() {
26302630
It("should call cache reader when structured object", func() {
26312631
cachedReader := &fakeReader{}
2632-
clientReader := &fakeReader{}
2633-
dReader := client.DelegatingReader{
2634-
CacheReader: cachedReader,
2635-
ClientReader: clientReader,
2636-
}
2632+
cl, err := client.New(cfg, client.Options{})
2633+
Expect(err).NotTo(HaveOccurred())
2634+
dReader := client.NewDelegatingClient(client.NewDelegatingClientInput{
2635+
CacheReader: cachedReader,
2636+
Client: cl,
2637+
})
26372638
var actual appsv1.Deployment
26382639
key := client.ObjectKey{Namespace: "ns", Name: "name"}
26392640
Expect(dReader.Get(context.TODO(), key, &actual)).To(Succeed())
26402641
Expect(1).To(Equal(cachedReader.Called))
2641-
Expect(0).To(Equal(clientReader.Called))
26422642
})
2643-
It("should call client reader when structured object", func() {
2643+
2644+
It("should call client reader when unstructured object", func() {
26442645
cachedReader := &fakeReader{}
2645-
clientReader := &fakeReader{}
2646-
dReader := client.DelegatingReader{
2647-
CacheReader: cachedReader,
2648-
ClientReader: clientReader,
2646+
cl, err := client.New(cfg, client.Options{})
2647+
Expect(err).NotTo(HaveOccurred())
2648+
dReader := client.NewDelegatingClient(client.NewDelegatingClientInput{
2649+
CacheReader: cachedReader,
2650+
Client: cl,
2651+
})
2652+
dep := &appsv1.Deployment{
2653+
ObjectMeta: metav1.ObjectMeta{
2654+
Name: "deployment1",
2655+
Labels: map[string]string{"app": "frontend"},
2656+
},
2657+
Spec: appsv1.DeploymentSpec{
2658+
Selector: &metav1.LabelSelector{
2659+
MatchLabels: map[string]string{"app": "frontend"},
2660+
},
2661+
Template: corev1.PodTemplateSpec{
2662+
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": "frontend"}},
2663+
Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "x", Image: "x"}}},
2664+
},
2665+
},
26492666
}
2650-
var actual unstructured.Unstructured
2651-
key := client.ObjectKey{Namespace: "ns", Name: "name"}
2652-
Expect(dReader.Get(context.TODO(), key, &actual)).To(Succeed())
2667+
dep, err = clientset.AppsV1().Deployments("default").Create(context.Background(), dep, metav1.CreateOptions{})
2668+
Expect(err).NotTo(HaveOccurred())
2669+
2670+
actual := &unstructured.Unstructured{}
2671+
actual.SetGroupVersionKind(schema.GroupVersionKind{
2672+
Group: "apps",
2673+
Kind: "Deployment",
2674+
Version: "v1",
2675+
})
2676+
actual.SetName(dep.Name)
2677+
key := client.ObjectKey{Namespace: dep.Namespace, Name: dep.Name}
2678+
Expect(dReader.Get(context.TODO(), key, actual)).To(Succeed())
26532679
Expect(0).To(Equal(cachedReader.Called))
2654-
Expect(1).To(Equal(clientReader.Called))
2680+
Expect(clientset.AppsV1().Deployments("default").Delete(
2681+
context.Background(),
2682+
dep.Name,
2683+
metav1.DeleteOptions{},
2684+
)).To(Succeed())
26552685
})
26562686
})
26572687
Describe("List", func() {
26582688
It("should call cache reader when structured object", func() {
26592689
cachedReader := &fakeReader{}
2660-
clientReader := &fakeReader{}
2661-
dReader := client.DelegatingReader{
2662-
CacheReader: cachedReader,
2663-
ClientReader: clientReader,
2664-
}
2690+
cl, err := client.New(cfg, client.Options{})
2691+
Expect(err).NotTo(HaveOccurred())
2692+
dReader := client.NewDelegatingClient(client.NewDelegatingClientInput{
2693+
CacheReader: cachedReader,
2694+
Client: cl,
2695+
})
26652696
var actual appsv1.DeploymentList
26662697
Expect(dReader.List(context.Background(), &actual)).To(Succeed())
26672698
Expect(1).To(Equal(cachedReader.Called))
2668-
Expect(0).To(Equal(clientReader.Called))
2669-
26702699
})
2671-
It("should call client reader when structured object", func() {
2700+
2701+
It("should call client reader when unstructured object", func() {
26722702
cachedReader := &fakeReader{}
2673-
clientReader := &fakeReader{}
2674-
dReader := client.DelegatingReader{
2675-
CacheReader: cachedReader,
2676-
ClientReader: clientReader,
2677-
}
2703+
cl, err := client.New(cfg, client.Options{})
2704+
Expect(err).NotTo(HaveOccurred())
2705+
dReader := client.NewDelegatingClient(client.NewDelegatingClientInput{
2706+
CacheReader: cachedReader,
2707+
Client: cl,
2708+
})
26782709

2679-
var actual unstructured.UnstructuredList
2680-
Expect(dReader.List(context.Background(), &actual)).To(Succeed())
2710+
actual := &unstructured.UnstructuredList{}
2711+
actual.SetGroupVersionKind(schema.GroupVersionKind{
2712+
Group: "apps",
2713+
Kind: "DeploymentList",
2714+
Version: "v1",
2715+
})
2716+
Expect(dReader.List(context.Background(), actual)).To(Succeed())
26812717
Expect(0).To(Equal(cachedReader.Called))
2682-
Expect(1).To(Equal(clientReader.Called))
2683-
26842718
})
26852719
})
26862720
})

pkg/client/dryrun.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ type dryRunClient struct {
3535
client Client
3636
}
3737

38+
// Scheme returns the scheme this client is using.
39+
func (c *dryRunClient) Scheme() *runtime.Scheme {
40+
return c.client.Scheme()
41+
}
42+
3843
// Create implements client.Client
3944
func (c *dryRunClient) Create(ctx context.Context, obj runtime.Object, opts ...CreateOption) error {
4045
return c.client.Create(ctx, obj, append(opts, DryRunAll)...)

pkg/client/example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func ExampleClient_create() {
8787
},
8888
Spec: corev1.PodSpec{
8989
Containers: []corev1.Container{
90-
corev1.Container{
90+
{
9191
Image: "nginx",
9292
Name: "nginx",
9393
},

pkg/client/fake/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ func (c *fakeClient) List(ctx context.Context, obj runtime.Object, opts ...clien
224224
return nil
225225
}
226226

227+
func (c *fakeClient) Scheme() *runtime.Scheme {
228+
return c.scheme
229+
}
230+
227231
func (c *fakeClient) Create(ctx context.Context, obj runtime.Object, opts ...client.CreateOption) error {
228232
createOptions := &client.CreateOptions{}
229233
createOptions.ApplyOptions(opts)

pkg/client/interfaces.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ type Client interface {
105105
Reader
106106
Writer
107107
StatusClient
108+
109+
// Scheme returns the scheme this client is using.
110+
Scheme() *runtime.Scheme
108111
}
109112

110113
// IndexerFunc knows how to take an object and turn it into a series

pkg/client/split.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,55 @@ import (
2323
"k8s.io/apimachinery/pkg/runtime"
2424
)
2525

26-
// DelegatingClient forms a Client by composing separate reader, writer and
26+
// NewDelegatingClientInput encapsulates the input parameters to create a new delegating client.
27+
type NewDelegatingClientInput struct {
28+
Scheme *runtime.Scheme
29+
CacheReader Reader
30+
Client Client
31+
}
32+
33+
// NewDelegatingClient creates a new delegating client.
34+
//
35+
// A delegating client forms a Client by composing separate reader, writer and
2736
// statusclient interfaces. This way, you can have an Client that reads from a
2837
// cache and writes to the API server.
29-
type DelegatingClient struct {
38+
func NewDelegatingClient(in NewDelegatingClientInput) Client {
39+
return &delegatingClient{
40+
scheme: in.Scheme,
41+
Reader: &delegatingReader{
42+
CacheReader: in.CacheReader,
43+
ClientReader: in.Client,
44+
},
45+
Writer: in.Client,
46+
StatusClient: in.Client,
47+
}
48+
}
49+
50+
type delegatingClient struct {
3051
Reader
3152
Writer
3253
StatusClient
54+
55+
scheme *runtime.Scheme
56+
}
57+
58+
// Scheme returns the scheme this client is using.
59+
func (d *delegatingClient) Scheme() *runtime.Scheme {
60+
return d.scheme
3361
}
3462

35-
// DelegatingReader forms a Reader that will cause Get and List requests for
63+
// delegatingReader forms a Reader that will cause Get and List requests for
3664
// unstructured types to use the ClientReader while requests for any other type
3765
// of object with use the CacheReader. This avoids accidentally caching the
3866
// entire cluster in the common case of loading arbitrary unstructured objects
3967
// (e.g. from OwnerReferences).
40-
type DelegatingReader struct {
68+
type delegatingReader struct {
4169
CacheReader Reader
4270
ClientReader Reader
4371
}
4472

4573
// Get retrieves an obj for a given object key from the Kubernetes Cluster.
46-
func (d *DelegatingReader) Get(ctx context.Context, key ObjectKey, obj runtime.Object) error {
74+
func (d *delegatingReader) Get(ctx context.Context, key ObjectKey, obj runtime.Object) error {
4775
_, isUnstructured := obj.(*unstructured.Unstructured)
4876
if isUnstructured {
4977
return d.ClientReader.Get(ctx, key, obj)
@@ -52,7 +80,7 @@ func (d *DelegatingReader) Get(ctx context.Context, key ObjectKey, obj runtime.O
5280
}
5381

5482
// List retrieves list of objects for a given namespace and list options.
55-
func (d *DelegatingReader) List(ctx context.Context, list runtime.Object, opts ...ListOption) error {
83+
func (d *delegatingReader) List(ctx context.Context, list runtime.Object, opts ...ListOption) error {
5684
_, isUnstructured := list.(*unstructured.UnstructuredList)
5785
if isUnstructured {
5886
return d.ClientReader.List(ctx, list, opts...)

pkg/manager/manager.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -378,14 +378,10 @@ func DefaultNewClient(cache cache.Cache, config *rest.Config, options client.Opt
378378
return nil, err
379379
}
380380

381-
return &client.DelegatingClient{
382-
Reader: &client.DelegatingReader{
383-
CacheReader: cache,
384-
ClientReader: c,
385-
},
386-
Writer: c,
387-
StatusClient: c,
388-
}, nil
381+
return client.NewDelegatingClient(client.NewDelegatingClientInput{
382+
CacheReader: cache,
383+
Client: c,
384+
}), nil
389385
}
390386

391387
// defaultHealthProbeListener creates the default health probes listener bound to the given address

pkg/runtime/inject/inject_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ var _ = Describe("runtime inject", func() {
8686
})
8787

8888
It("should set client", func() {
89-
client := client.DelegatingClient{}
89+
client := client.NewDelegatingClient(client.NewDelegatingClientInput{})
9090

9191
By("Validating injecting client")
9292
res, err := ClientInto(client, instance)
@@ -151,7 +151,7 @@ var _ = Describe("runtime inject", func() {
151151
})
152152

153153
It("should set api reader", func() {
154-
apiReader := &client.DelegatingReader{}
154+
apiReader := client.NewDelegatingClient(client.NewDelegatingClientInput{})
155155

156156
By("Validating injecting client")
157157
res, err := APIReaderInto(apiReader, instance)

0 commit comments

Comments
 (0)