Align fake client-go clients with the main interface

"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 <skitt@redhat.com>

Kubernetes-commit: 736e5560ba6b21247c21f8ed12007e1d6d5fec1a
This commit is contained in:
Stephen Kitt 2024-11-08 10:42:12 +01:00 committed by Kubernetes Publisher
parent 646e79b061
commit 55d23e26d5
3 changed files with 16 additions and 16 deletions

View File

@ -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
@ -41,26 +41,26 @@ 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)
@ -68,20 +68,20 @@ type alsoFakeLister[T runtime.Object, L runtime.Object] struct {
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] {
@ -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)
@ -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] {

4
go.mod
View File

@ -26,8 +26,8 @@ require (
golang.org/x/time v0.7.0
google.golang.org/protobuf v1.35.1
gopkg.in/evanphx/json-patch.v4 v4.12.0
k8s.io/api v0.0.0-20241108114318-6cc44b8953ae
k8s.io/apimachinery v0.0.0-20241108022104-96b97de8d6ba
k8s.io/api v0.0.0-20241127162655-f8e5e36c84f1
k8s.io/apimachinery v0.0.0-20240920213627-16af2ff33fbf
k8s.io/klog/v2 v2.130.1
k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738

8
go.sum
View File

@ -150,10 +150,10 @@ gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
k8s.io/api v0.0.0-20241108114318-6cc44b8953ae h1:XX7vEVBchw0xx4YJZ6OyPxOq3e5hX2PTZ5wu8dw0vco=
k8s.io/api v0.0.0-20241108114318-6cc44b8953ae/go.mod h1:jw6pQTESH9mdZL2vOK3twojvpPxipl5TpLZpPyl5ZYU=
k8s.io/apimachinery v0.0.0-20241108022104-96b97de8d6ba h1:ghB5Iygt6Ge8UyIwW7C1kJx4kP7AUTCL9Qg6GCsUUOY=
k8s.io/apimachinery v0.0.0-20241108022104-96b97de8d6ba/go.mod h1:HqhdaJUgQqky29T1V0o2yFkt/pZqLFIDyn9Zi/8rxoY=
k8s.io/api v0.0.0-20241127162655-f8e5e36c84f1 h1:MTqd8524+MzN0Kxt42qAvh/aUYC18yz1BJUmfWADaDg=
k8s.io/api v0.0.0-20241127162655-f8e5e36c84f1/go.mod h1:qs155+gTdM43TXy/cV8a8yOjDeNR8kGJc82AraJrh/c=
k8s.io/apimachinery v0.0.0-20240920213627-16af2ff33fbf h1:ZRwu8YHh3bFbQU4NRvHB6fiovWLBouxY86wIcLd7sBA=
k8s.io/apimachinery v0.0.0-20240920213627-16af2ff33fbf/go.mod h1:HqhdaJUgQqky29T1V0o2yFkt/pZqLFIDyn9Zi/8rxoY=
k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk=
k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f h1:GA7//TjRY9yWGy1poLzYYJJ4JRdzg3+O6e8I+e+8T5Y=