Skip to content

Commit

Permalink
Merge pull request #1686 from k8s-infra-cherrypick-robot/cherry-pick-…
Browse files Browse the repository at this point in the history
…1683-to-release-0.10

🌱 fake-client: extend builder with an option to set a RESTMapper
  • Loading branch information
k8s-ci-robot authored Oct 7, 2021
2 parents 78ce10e + 5767de5 commit 307b8e1
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type versionedTracker struct {
type fakeClient struct {
tracker versionedTracker
scheme *runtime.Scheme
restMapper meta.RESTMapper
schemeWriteLock sync.Mutex
}

Expand Down Expand Up @@ -87,6 +88,7 @@ func NewClientBuilder() *ClientBuilder {
// ClientBuilder builds a fake client.
type ClientBuilder struct {
scheme *runtime.Scheme
restMapper meta.RESTMapper
initObject []client.Object
initLists []client.ObjectList
initRuntimeObjects []runtime.Object
Expand All @@ -99,6 +101,15 @@ func (f *ClientBuilder) WithScheme(scheme *runtime.Scheme) *ClientBuilder {
return f
}

// WithRESTMapper sets this builder's restMapper.
// The restMapper is directly set as mapper in the Client. This can be used for example
// with a meta.DefaultRESTMapper to provide a static rest mapping.
// If not set, defaults to an empty meta.DefaultRESTMapper.
func (f *ClientBuilder) WithRESTMapper(restMapper meta.RESTMapper) *ClientBuilder {
f.restMapper = restMapper
return f
}

// WithObjects can be optionally used to initialize this fake client with client.Object(s).
func (f *ClientBuilder) WithObjects(initObjs ...client.Object) *ClientBuilder {
f.initObject = append(f.initObject, initObjs...)
Expand All @@ -122,6 +133,9 @@ func (f *ClientBuilder) Build() client.WithWatch {
if f.scheme == nil {
f.scheme = scheme.Scheme
}
if f.restMapper == nil {
f.restMapper = meta.NewDefaultRESTMapper([]schema.GroupVersion{})
}

tracker := versionedTracker{ObjectTracker: testing.NewObjectTracker(f.scheme, scheme.Codecs.UniversalDecoder()), scheme: f.scheme}
for _, obj := range f.initObject {
Expand All @@ -140,8 +154,9 @@ func (f *ClientBuilder) Build() client.WithWatch {
}
}
return &fakeClient{
tracker: tracker,
scheme: f.scheme,
tracker: tracker,
scheme: f.scheme,
restMapper: f.restMapper,
}
}

Expand Down Expand Up @@ -417,8 +432,7 @@ func (c *fakeClient) Scheme() *runtime.Scheme {
}

func (c *fakeClient) RESTMapper() meta.RESTMapper {
// TODO: Implement a fake RESTMapper.
return nil
return c.restMapper
}

func (c *fakeClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
Expand Down

0 comments on commit 307b8e1

Please sign in to comment.