Skip to content

Commit

Permalink
Expose RESTMapper in client.Client
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <vincepri@vmware.com>
  • Loading branch information
vincepri committed Aug 4, 2020
1 parent e263d2c commit f1885ed
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 4 deletions.
7 changes: 7 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func New(config *rest.Config, options Options) (Client, error) {
paramCodec: noConversionParamCodec{},
},
scheme: options.Scheme,
mapper: options.Mapper,
}

return c, nil
Expand All @@ -99,6 +100,7 @@ type client struct {
typedClient typedClient
unstructuredClient unstructuredClient
scheme *runtime.Scheme
mapper meta.RESTMapper
}

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

// RESTMapper returns the scheme this client is using.
func (c *client) RESTMapper() meta.RESTMapper {
return c.mapper
}

// Create implements client.Client
func (c *client) Create(ctx context.Context, obj runtime.Object, opts ...CreateOption) error {
_, ok := obj.(*unstructured.Unstructured)
Expand Down
6 changes: 6 additions & 0 deletions pkg/client/dryrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package client
import (
"context"

"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
)

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

// RESTMapper returns the rest mapper this client is using.
func (c *dryRunClient) RESTMapper() meta.RESTMapper {
return c.client.RESTMapper()
}

// Create implements client.Client
func (c *dryRunClient) Create(ctx context.Context, obj runtime.Object, opts ...CreateOption) error {
return c.client.Create(ctx, obj, append(opts, DryRunAll)...)
Expand Down
4 changes: 4 additions & 0 deletions pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ func (c *fakeClient) Scheme() *runtime.Scheme {
return c.scheme
}

func (c *fakeClient) RESTMapper() meta.RESTMapper {
return nil
}

func (c *fakeClient) Create(ctx context.Context, obj runtime.Object, opts ...client.CreateOption) error {
createOptions := &client.CreateOptions{}
createOptions.ApplyOptions(opts)
Expand Down
3 changes: 3 additions & 0 deletions pkg/client/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ type Client interface {

// Scheme returns the scheme this client is using.
Scheme() *runtime.Scheme

// RESTMapper returns the rest this client is using.
RESTMapper() meta.RESTMapper
}

// IndexerFunc knows how to take an object and turn it into a series
Expand Down
11 changes: 9 additions & 2 deletions pkg/client/split.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ package client
import (
"context"

"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)

// NewDelegatingClientInput encapsulates the input parameters to create a new delegating client.
type NewDelegatingClientInput struct {
Scheme *runtime.Scheme
CacheReader Reader
Client Client
}
Expand All @@ -37,7 +37,8 @@ type NewDelegatingClientInput struct {
// cache and writes to the API server.
func NewDelegatingClient(in NewDelegatingClientInput) Client {
return &delegatingClient{
scheme: in.Scheme,
scheme: in.Client.Scheme(),
mapper: in.Client.RESTMapper(),
Reader: &delegatingReader{
CacheReader: in.CacheReader,
ClientReader: in.Client,
Expand All @@ -53,13 +54,19 @@ type delegatingClient struct {
StatusClient

scheme *runtime.Scheme
mapper meta.RESTMapper
}

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

// RESTMapper returns the rest mapper this client is using.
func (d *delegatingClient) RESTMapper() meta.RESTMapper {
return d.mapper
}

// delegatingReader forms a Reader that will cause Get and List requests for
// unstructured types to use the ClientReader while requests for any other type
// of object with use the CacheReader. This avoids accidentally caching the
Expand Down
5 changes: 3 additions & 2 deletions pkg/runtime/inject/inject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/cache/informertest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

var instance *testSource
Expand Down Expand Up @@ -86,7 +87,7 @@ var _ = Describe("runtime inject", func() {
})

It("should set client", func() {
client := client.NewDelegatingClient(client.NewDelegatingClientInput{})
client := client.NewDelegatingClient(client.NewDelegatingClientInput{Client: fake.NewFakeClient()})

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

It("should set api reader", func() {
apiReader := client.NewDelegatingClient(client.NewDelegatingClientInput{})
apiReader := client.NewDelegatingClient(client.NewDelegatingClientInput{Client: fake.NewFakeClient()})

By("Validating injecting client")
res, err := APIReaderInto(apiReader, instance)
Expand Down

0 comments on commit f1885ed

Please sign in to comment.