Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚠️ Expose RESTMapper on Client interface #1109

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 5 additions & 0 deletions pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ func (c *fakeClient) Scheme() *runtime.Scheme {
return c.scheme
}

func (c *fakeClient) RESTMapper() meta.RESTMapper {
// TODO: Implement a fake 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
2 changes: 2 additions & 0 deletions pkg/client/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ 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