Skip to content

Commit

Permalink
Generify fake clientsets
Browse files Browse the repository at this point in the history
This adds a generic implementation of a fake clientset, and uses it to
replace the template code in generated fake clientsets for the default
methods. The templates are preserved as-is (or as close as they can
be) for use in extensions, whether for resources or subresources.

Fake clientsets with no extensions are reduced to their main getter,
their specific struct, and their constructor. All method
implementations are provided by the generic implementation. The
dedicated struct is preserved to allow extensions and expansions to be
defined where necessary.

Instead of handling the variants (with/without list, apply) with a
complex sequence of if statements, build up an index into an array
containing the various declarations.

Similarly, instead of calling different action constructors for
namespaced and non-namespaced clientsets, assume the current behaviour
of non-namespaced action creation (equivalent to creating a namespaced
action with an empty namespace) and document that assumption in the
action implementation.

Signed-off-by: Stephen Kitt <skitt@redhat.com>

Kubernetes-commit: b0ce65df9b74d4dc72050840d5ad067596d7b822
  • Loading branch information
skitt authored and k8s-publishing-bot committed Jul 30, 2024
1 parent 955401c commit c475fe0
Show file tree
Hide file tree
Showing 13 changed files with 365 additions and 67 deletions.
297 changes: 297 additions & 0 deletions gentype/fake.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,297 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package gentype

import (
"context"
json "encoding/json"
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
labels "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
types "k8s.io/apimachinery/pkg/types"
watch "k8s.io/apimachinery/pkg/watch"
testing "k8s.io/client-go/testing"
)

// FakeClient represents a fake client
type FakeClient[T objectWithMeta] struct {
*testing.Fake
ns string
resource schema.GroupVersionResource
kind schema.GroupVersionKind
newObject func() T
}

// FakeClientWithList represents a fake client with support for lists.
type FakeClientWithList[T objectWithMeta, L runtime.Object] struct {
*FakeClient[T]
alsoFakeLister[T, L]
}

// FakeClientWithApply represents a fake client with support for apply declarative configurations.
type FakeClientWithApply[T objectWithMeta, C namedObject] struct {
*FakeClient[T]
alsoFakeApplier[T, C]
}

// FakeClientWithListAndApply represents a fake client with support for lists and apply declarative configurations.
type FakeClientWithListAndApply[T objectWithMeta, L runtime.Object, C namedObject] struct {
*FakeClient[T]
alsoFakeLister[T, L]
alsoFakeApplier[T, C]
}

// Helper types for composition
type alsoFakeLister[T objectWithMeta, L runtime.Object] struct {
client *FakeClient[T]
newList func() L
copyListMeta func(L, L)
getItems func(L) []T
setItems func(L, []T)
}

type alsoFakeApplier[T objectWithMeta, C namedObject] struct {
client *FakeClient[T]
}

// NewFakeClient constructs a fake client, namespaced or not, with no support for lists or apply.
// Non-namespaced clients are constructed by passing an empty namespace ("").
func NewFakeClient[T objectWithMeta](
fake *testing.Fake, namespace string, resource schema.GroupVersionResource, kind schema.GroupVersionKind, emptyObjectCreator func() T,
) *FakeClient[T] {
return &FakeClient[T]{fake, namespace, resource, kind, emptyObjectCreator}
}

// NewFakeClientWithList constructs a namespaced client with support for lists.
func NewFakeClientWithList[T runtime.Object, L runtime.Object](
fake *testing.Fake, namespace string, resource schema.GroupVersionResource, kind schema.GroupVersionKind, emptyObjectCreator func() T,
emptyListCreator func() L, listMetaCopier func(L, L), itemGetter func(L) []T, itemSetter func(L, []T),
) *FakeClientWithList[T, L] {
fakeClient := NewFakeClient[T](fake, namespace, resource, kind, emptyObjectCreator)
return &FakeClientWithList[T, L]{
fakeClient,
alsoFakeLister[T, L]{fakeClient, emptyListCreator, listMetaCopier, itemGetter, itemSetter},
}
}

// NewFakeClientWithApply constructs a namespaced client with support for apply declarative configurations.
func NewFakeClientWithApply[T runtime.Object, C namedObject](
fake *testing.Fake, namespace string, resource schema.GroupVersionResource, kind schema.GroupVersionKind, emptyObjectCreator func() T,
) *FakeClientWithApply[T, C] {
fakeClient := NewFakeClient[T](fake, namespace, resource, kind, emptyObjectCreator)
return &FakeClientWithApply[T, C]{
fakeClient,
alsoFakeApplier[T, C]{fakeClient},
}
}

// NewFakeClientWithListAndApply constructs a client with support for lists and applying declarative configurations.
func NewFakeClientWithListAndApply[T runtime.Object, L runtime.Object, C namedObject](
fake *testing.Fake, namespace string, resource schema.GroupVersionResource, kind schema.GroupVersionKind, emptyObjectCreator func() T,
emptyListCreator func() L, listMetaCopier func(L, L), itemGetter func(L) []T, itemSetter func(L, []T),
) *FakeClientWithListAndApply[T, L, C] {
fakeClient := NewFakeClient[T](fake, namespace, resource, kind, emptyObjectCreator)
return &FakeClientWithListAndApply[T, L, C]{
fakeClient,
alsoFakeLister[T, L]{fakeClient, emptyListCreator, listMetaCopier, itemGetter, itemSetter},
alsoFakeApplier[T, C]{fakeClient},
}
}

// Get takes name of a resource, and returns the corresponding object, and an error if there is any.
func (c *FakeClient[T]) Get(ctx context.Context, name string, options metav1.GetOptions) (T, error) {
emptyResult := c.newObject()

obj, err := c.Fake.
Invokes(testing.NewGetActionWithOptions(c.resource, c.ns, name, options), emptyResult)
if obj == nil {
return emptyResult, err
}
return obj.(T), err
}

func ToPointerSlice[T any](src []T) []*T {
if src == nil {
return nil
}
result := make([]*T, len(src))
for i := range src {
result[i] = &src[i]
}
return result
}

func FromPointerSlice[T any](src []*T) []T {
if src == nil {
return nil
}
result := make([]T, len(src))
for i := range src {
result[i] = *src[i]
}
return result
}

// List takes label and field selectors, and returns the list of ClusterRoles that match those selectors.
func (l *alsoFakeLister[T, L]) List(ctx context.Context, opts metav1.ListOptions) (result L, err error) {
emptyResult := l.newList()
obj, err := l.client.Fake.
Invokes(testing.NewListActionWithOptions(l.client.resource, l.client.kind, l.client.ns, opts), emptyResult)
if obj == nil {
return emptyResult, err
}

label, _, _ := testing.ExtractFromListOptions(opts)
if label == nil {
label = labels.Everything()
}
list := l.newList()
l.copyListMeta(list, obj.(L))
var items []T
for _, item := range l.getItems(obj.(L)) {
if label.Matches(labels.Set(item.GetLabels())) {
items = append(items, item)
}
}
l.setItems(list, items)
return list, err
}

// Watch returns a watch.Interface that watches the requested resources.
func (c *FakeClient[T]) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) {
return c.Fake.
InvokesWatch(testing.NewWatchActionWithOptions(c.resource, c.ns, opts))
}

// Create takes the representation of a resource and creates it. Returns the server's representation of the resource, and an error, if there is any.
func (c *FakeClient[T]) Create(ctx context.Context, resource T, opts metav1.CreateOptions) (result T, err error) {
emptyResult := c.newObject()
obj, err := c.Fake.
Invokes(testing.NewCreateActionWithOptions(c.resource, c.ns, resource, opts), emptyResult)
if obj == nil {
return emptyResult, err
}
return obj.(T), err
}

// Update takes the representation of a resource and updates it. Returns the server's representation of the resource, and an error, if there is any.
func (c *FakeClient[T]) Update(ctx context.Context, resource T, opts metav1.UpdateOptions) (result T, err error) {
emptyResult := c.newObject()
obj, err := c.Fake.
Invokes(testing.NewUpdateActionWithOptions(c.resource, c.ns, resource, opts), emptyResult)
if obj == nil {
return emptyResult, err
}
return obj.(T), err
}

// UpdateStatus updates the resource's status and returns the updated resource.
func (c *FakeClient[T]) UpdateStatus(ctx context.Context, resource T, opts metav1.UpdateOptions) (result T, err error) {
emptyResult := c.newObject()
obj, err := c.Fake.
Invokes(testing.NewUpdateSubresourceActionWithOptions(c.resource, "status", c.ns, resource, opts), emptyResult)

if obj == nil {
return emptyResult, err
}
return obj.(T), err
}

// Delete deletes the resource matching the given name. Returns an error if one occurs.
func (c *FakeClient[T]) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error {
_, err := c.Fake.
Invokes(testing.NewDeleteActionWithOptions(c.resource, c.ns, name, opts), c.newObject())
return err
}

// DeleteCollection deletes a collection of objects.
func (l *alsoFakeLister[T, L]) DeleteCollection(ctx context.Context, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error {
_, err := l.client.Fake.
Invokes(testing.NewDeleteCollectionActionWithOptions(l.client.resource, l.client.ns, opts, listOpts), l.newList())
return err
}

// Patch applies the patch and returns the patched resource.
func (c *FakeClient[T]) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (result T, err error) {
emptyResult := c.newObject()
obj, err := c.Fake.
Invokes(testing.NewPatchSubresourceActionWithOptions(c.resource, c.ns, name, pt, data, opts, subresources...), emptyResult)
if obj == nil {
return emptyResult, err
}
return obj.(T), err
}

// Apply takes the given apply declarative configuration, applies it and returns the applied resource.
func (a *alsoFakeApplier[T, C]) Apply(ctx context.Context, configuration C, opts metav1.ApplyOptions) (result T, err error) {
if configuration == *new(C) {
return *new(T), fmt.Errorf("configuration provided to Apply must not be nil")
}
data, err := json.Marshal(configuration)
if err != nil {
return *new(T), err
}
name := configuration.GetName()
if name == nil {
return *new(T), fmt.Errorf("configuration.Name must be provided to Apply")
}
emptyResult := a.client.newObject()
obj, err := a.client.Fake.
Invokes(testing.NewPatchSubresourceActionWithOptions(a.client.resource, a.client.ns, *name, types.ApplyPatchType, data, opts.ToPatchOptions()), emptyResult)
if obj == nil {
return emptyResult, err
}
return obj.(T), err
}

// ApplyStatus applies the given apply declarative configuration to the resource's status and returns the updated resource.
func (a *alsoFakeApplier[T, C]) ApplyStatus(ctx context.Context, configuration C, opts metav1.ApplyOptions) (result T, err error) {
if configuration == *new(C) {
return *new(T), fmt.Errorf("configuration provided to Apply must not be nil")
}
data, err := json.Marshal(configuration)
if err != nil {
return *new(T), err
}
name := configuration.GetName()
if name == nil {
return *new(T), fmt.Errorf("configuration.Name must be provided to Apply")
}
emptyResult := a.client.newObject()
obj, err := a.client.Fake.
Invokes(testing.NewPatchSubresourceActionWithOptions(a.client.resource, a.client.ns, *name, types.ApplyPatchType, data, opts.ToPatchOptions(), "status"), emptyResult)

if obj == nil {
return emptyResult, err
}
return obj.(T), err
}

func (c *FakeClient[T]) Namespace() string {
return c.ns
}

func (c *FakeClient[T]) Kind() schema.GroupVersionKind {
return c.kind
}

func (c *FakeClient[T]) Resource() schema.GroupVersionResource {
return c.resource
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
core "k8s.io/client-go/testing"
)

func (c *FakeCertificateSigningRequests) UpdateApproval(ctx context.Context, certificateSigningRequest *certificates.CertificateSigningRequest, opts metav1.UpdateOptions) (result *certificates.CertificateSigningRequest, err error) {
func (c *fakeCertificateSigningRequests) UpdateApproval(ctx context.Context, certificateSigningRequest *certificates.CertificateSigningRequest, opts metav1.UpdateOptions) (result *certificates.CertificateSigningRequest, err error) {
obj, err := c.Fake.
Invokes(core.NewRootUpdateSubresourceAction(certificatesigningrequestsResource, "approval", certificateSigningRequest), &certificates.CertificateSigningRequest{})
Invokes(core.NewRootUpdateSubresourceAction(c.Resource(), "approval", certificateSigningRequest), &certificates.CertificateSigningRequest{})
if obj == nil {
return nil, err
}
Expand Down
36 changes: 18 additions & 18 deletions kubernetes/typed/core/v1/fake/fake_event_expansion.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import (
core "k8s.io/client-go/testing"
)

func (c *FakeEvents) CreateWithEventNamespace(event *v1.Event) (*v1.Event, error) {
func (c *fakeEvents) CreateWithEventNamespace(event *v1.Event) (*v1.Event, error) {
var action core.CreateActionImpl
if c.ns != "" {
action = core.NewCreateAction(eventsResource, c.ns, event)
if c.Namespace() != "" {
action = core.NewCreateAction(c.Resource(), c.Namespace(), event)
} else {
action = core.NewCreateAction(eventsResource, event.GetNamespace(), event)
action = core.NewCreateAction(c.Resource(), event.GetNamespace(), event)
}
obj, err := c.Fake.Invokes(action, event)
if obj == nil {
Expand All @@ -41,12 +41,12 @@ func (c *FakeEvents) CreateWithEventNamespace(event *v1.Event) (*v1.Event, error
}

// Update replaces an existing event. Returns the copy of the event the server returns, or an error.
func (c *FakeEvents) UpdateWithEventNamespace(event *v1.Event) (*v1.Event, error) {
func (c *fakeEvents) UpdateWithEventNamespace(event *v1.Event) (*v1.Event, error) {
var action core.UpdateActionImpl
if c.ns != "" {
action = core.NewUpdateAction(eventsResource, c.ns, event)
if c.Namespace() != "" {
action = core.NewUpdateAction(c.Resource(), c.Namespace(), event)
} else {
action = core.NewUpdateAction(eventsResource, event.GetNamespace(), event)
action = core.NewUpdateAction(c.Resource(), event.GetNamespace(), event)
}
obj, err := c.Fake.Invokes(action, event)
if obj == nil {
Expand All @@ -58,14 +58,14 @@ func (c *FakeEvents) UpdateWithEventNamespace(event *v1.Event) (*v1.Event, error

// PatchWithEventNamespace patches an existing event. Returns the copy of the event the server returns, or an error.
// TODO: Should take a PatchType as an argument probably.
func (c *FakeEvents) PatchWithEventNamespace(event *v1.Event, data []byte) (*v1.Event, error) {
func (c *fakeEvents) PatchWithEventNamespace(event *v1.Event, data []byte) (*v1.Event, error) {
// TODO: Should be configurable to support additional patch strategies.
pt := types.StrategicMergePatchType
var action core.PatchActionImpl
if c.ns != "" {
action = core.NewPatchAction(eventsResource, c.ns, event.Name, pt, data)
if c.Namespace() != "" {
action = core.NewPatchAction(c.Resource(), c.Namespace(), event.Name, pt, data)
} else {
action = core.NewPatchAction(eventsResource, event.GetNamespace(), event.Name, pt, data)
action = core.NewPatchAction(c.Resource(), event.GetNamespace(), event.Name, pt, data)
}
obj, err := c.Fake.Invokes(action, event)
if obj == nil {
Expand All @@ -76,12 +76,12 @@ func (c *FakeEvents) PatchWithEventNamespace(event *v1.Event, data []byte) (*v1.
}

// Search returns a list of events matching the specified object.
func (c *FakeEvents) Search(scheme *runtime.Scheme, objOrRef runtime.Object) (*v1.EventList, error) {
func (c *fakeEvents) Search(scheme *runtime.Scheme, objOrRef runtime.Object) (*v1.EventList, error) {
var action core.ListActionImpl
if c.ns != "" {
action = core.NewListAction(eventsResource, eventsKind, c.ns, metav1.ListOptions{})
if c.Namespace() != "" {
action = core.NewListAction(c.Resource(), c.Kind(), c.Namespace(), metav1.ListOptions{})
} else {
action = core.NewListAction(eventsResource, eventsKind, v1.NamespaceDefault, metav1.ListOptions{})
action = core.NewListAction(c.Resource(), c.Kind(), v1.NamespaceDefault, metav1.ListOptions{})
}
obj, err := c.Fake.Invokes(action, &v1.EventList{})
if obj == nil {
Expand All @@ -91,10 +91,10 @@ func (c *FakeEvents) Search(scheme *runtime.Scheme, objOrRef runtime.Object) (*v
return obj.(*v1.EventList), err
}

func (c *FakeEvents) GetFieldSelector(involvedObjectName, involvedObjectNamespace, involvedObjectKind, involvedObjectUID *string) fields.Selector {
func (c *fakeEvents) GetFieldSelector(involvedObjectName, involvedObjectNamespace, involvedObjectKind, involvedObjectUID *string) fields.Selector {
action := core.GenericActionImpl{}
action.Verb = "get-field-selector"
action.Resource = eventsResource
action.Resource = c.Resource()

c.Fake.Invokes(action, nil)
return fields.Everything()
Expand Down
Loading

0 comments on commit c475fe0

Please sign in to comment.