Skip to content

Commit 5b6d083

Browse files
authored
Merge pull request #1109 from vincepri/expose-client-restmapperx
⚠️ Expose RESTMapper on Client interface
2 parents a72e9e1 + 6a05778 commit 5b6d083

File tree

6 files changed

+32
-4
lines changed

6 files changed

+32
-4
lines changed

pkg/client/client.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func New(config *rest.Config, options Options) (Client, error) {
8686
paramCodec: noConversionParamCodec{},
8787
},
8888
scheme: options.Scheme,
89+
mapper: options.Mapper,
8990
}
9091

9192
return c, nil
@@ -99,6 +100,7 @@ type client struct {
99100
typedClient typedClient
100101
unstructuredClient unstructuredClient
101102
scheme *runtime.Scheme
103+
mapper meta.RESTMapper
102104
}
103105

104106
// resetGroupVersionKind is a helper function to restore and preserve GroupVersionKind on an object.
@@ -116,6 +118,11 @@ func (c *client) Scheme() *runtime.Scheme {
116118
return c.scheme
117119
}
118120

121+
// RESTMapper returns the scheme this client is using.
122+
func (c *client) RESTMapper() meta.RESTMapper {
123+
return c.mapper
124+
}
125+
119126
// Create implements client.Client
120127
func (c *client) Create(ctx context.Context, obj runtime.Object, opts ...CreateOption) error {
121128
_, ok := obj.(*unstructured.Unstructured)

pkg/client/dryrun.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package client
1919
import (
2020
"context"
2121

22+
"k8s.io/apimachinery/pkg/api/meta"
2223
"k8s.io/apimachinery/pkg/runtime"
2324
)
2425

@@ -40,6 +41,11 @@ func (c *dryRunClient) Scheme() *runtime.Scheme {
4041
return c.client.Scheme()
4142
}
4243

44+
// RESTMapper returns the rest mapper this client is using.
45+
func (c *dryRunClient) RESTMapper() meta.RESTMapper {
46+
return c.client.RESTMapper()
47+
}
48+
4349
// Create implements client.Client
4450
func (c *dryRunClient) Create(ctx context.Context, obj runtime.Object, opts ...CreateOption) error {
4551
return c.client.Create(ctx, obj, append(opts, DryRunAll)...)

pkg/client/fake/client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ func (c *fakeClient) Scheme() *runtime.Scheme {
228228
return c.scheme
229229
}
230230

231+
func (c *fakeClient) RESTMapper() meta.RESTMapper {
232+
// TODO: Implement a fake RESTMapper.
233+
return nil
234+
}
235+
231236
func (c *fakeClient) Create(ctx context.Context, obj runtime.Object, opts ...client.CreateOption) error {
232237
createOptions := &client.CreateOptions{}
233238
createOptions.ApplyOptions(opts)

pkg/client/interfaces.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ type Client interface {
108108

109109
// Scheme returns the scheme this client is using.
110110
Scheme() *runtime.Scheme
111+
// RESTMapper returns the rest this client is using.
112+
RESTMapper() meta.RESTMapper
111113
}
112114

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

pkg/client/split.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ package client
1919
import (
2020
"context"
2121

22+
"k8s.io/apimachinery/pkg/api/meta"
2223
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2324
"k8s.io/apimachinery/pkg/runtime"
2425
)
2526

2627
// NewDelegatingClientInput encapsulates the input parameters to create a new delegating client.
2728
type NewDelegatingClientInput struct {
28-
Scheme *runtime.Scheme
2929
CacheReader Reader
3030
Client Client
3131
}
@@ -37,7 +37,8 @@ type NewDelegatingClientInput struct {
3737
// cache and writes to the API server.
3838
func NewDelegatingClient(in NewDelegatingClientInput) Client {
3939
return &delegatingClient{
40-
scheme: in.Scheme,
40+
scheme: in.Client.Scheme(),
41+
mapper: in.Client.RESTMapper(),
4142
Reader: &delegatingReader{
4243
CacheReader: in.CacheReader,
4344
ClientReader: in.Client,
@@ -53,13 +54,19 @@ type delegatingClient struct {
5354
StatusClient
5455

5556
scheme *runtime.Scheme
57+
mapper meta.RESTMapper
5658
}
5759

5860
// Scheme returns the scheme this client is using.
5961
func (d *delegatingClient) Scheme() *runtime.Scheme {
6062
return d.scheme
6163
}
6264

65+
// RESTMapper returns the rest mapper this client is using.
66+
func (d *delegatingClient) RESTMapper() meta.RESTMapper {
67+
return d.mapper
68+
}
69+
6370
// delegatingReader forms a Reader that will cause Get and List requests for
6471
// unstructured types to use the ClientReader while requests for any other type
6572
// of object with use the CacheReader. This avoids accidentally caching the

pkg/runtime/inject/inject_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"sigs.k8s.io/controller-runtime/pkg/cache"
2828
"sigs.k8s.io/controller-runtime/pkg/cache/informertest"
2929
"sigs.k8s.io/controller-runtime/pkg/client"
30+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3031
)
3132

3233
var instance *testSource
@@ -86,7 +87,7 @@ var _ = Describe("runtime inject", func() {
8687
})
8788

8889
It("should set client", func() {
89-
client := client.NewDelegatingClient(client.NewDelegatingClientInput{})
90+
client := client.NewDelegatingClient(client.NewDelegatingClientInput{Client: fake.NewFakeClient()})
9091

9192
By("Validating injecting client")
9293
res, err := ClientInto(client, instance)
@@ -151,7 +152,7 @@ var _ = Describe("runtime inject", func() {
151152
})
152153

153154
It("should set api reader", func() {
154-
apiReader := client.NewDelegatingClient(client.NewDelegatingClientInput{})
155+
apiReader := client.NewDelegatingClient(client.NewDelegatingClientInput{Client: fake.NewFakeClient()})
155156

156157
By("Validating injecting client")
157158
res, err := APIReaderInto(apiReader, instance)

0 commit comments

Comments
 (0)