Skip to content

Commit

Permalink
Align fake client-go clients with the main interface
Browse files Browse the repository at this point in the history
"Real" clients use objectWithMeta to enforce support for meta.Object;
strictly speaking, fakes don't need this, but it's best to align them
with the real clients to ensure that fakes don't end up allowing types
that can't be used with the real clients.

Signed-off-by: Stephen Kitt <[email protected]>
  • Loading branch information
skitt committed Nov 8, 2024
1 parent be03bcf commit 736e556
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions staging/src/k8s.io/client-go/gentype/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
)

// FakeClient represents a fake client
type FakeClient[T runtime.Object] struct {
type FakeClient[T objectWithMeta] struct {
*testing.Fake
ns string
resource schema.GroupVersionResource
Expand All @@ -41,47 +41,47 @@ type FakeClient[T runtime.Object] struct {
}

// FakeClientWithList represents a fake client with support for lists.
type FakeClientWithList[T runtime.Object, L runtime.Object] struct {
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 runtime.Object, C namedObject] struct {
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 runtime.Object, L runtime.Object, C namedObject] struct {
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 runtime.Object, L runtime.Object] struct {
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 runtime.Object, C namedObject] struct {
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 runtime.Object](
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](
func NewFakeClientWithList[T objectWithMeta, 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] {
Expand All @@ -93,7 +93,7 @@ func NewFakeClientWithList[T runtime.Object, L runtime.Object](
}

// NewFakeClientWithApply constructs a namespaced client with support for apply declarative configurations.
func NewFakeClientWithApply[T runtime.Object, C namedObject](
func NewFakeClientWithApply[T objectWithMeta, 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)
Expand All @@ -104,7 +104,7 @@ func NewFakeClientWithApply[T runtime.Object, C namedObject](
}

// NewFakeClientWithListAndApply constructs a client with support for lists and applying declarative configurations.
func NewFakeClientWithListAndApply[T runtime.Object, L runtime.Object, C namedObject](
func NewFakeClientWithListAndApply[T objectWithMeta, 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] {
Expand Down

0 comments on commit 736e556

Please sign in to comment.