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

Cannot use client-go's fake client with Gateway API objects #116253

Open
pmalek opened this issue Mar 3, 2023 · 16 comments
Open

Cannot use client-go's fake client with Gateway API objects #116253

pmalek opened this issue Mar 3, 2023 · 16 comments
Assignees
Labels
kind/bug Categorizes issue or PR as related to a bug. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. triage/accepted Indicates an issue or PR is ready to be actively worked on.

Comments

@pmalek
Copy link
Contributor

pmalek commented Mar 3, 2023

What happened?

I'm trying to work with Gateway API fake objects by doing the following:

package x

import (
	"testing"

	"github.com/stretchr/testify/require"
	"k8s.io/client-go/kubernetes/fake"
	clientsetscheme "k8s.io/client-go/kubernetes/scheme"
	gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
	gatewayscheme "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned/scheme"
)

func TestGatewayFake(t *testing.T) {
	require.NoError(t, gatewayscheme.AddToScheme(clientsetscheme.Scheme))

	_ = fake.NewSimpleClientset(&gatewayv1beta1.Gateway{})
}

but that doesn't work, i.e. I get this:

panic: no kind is registered for the type v1beta1.Gateway in scheme "pkg/runtime/scheme.go:100" [recovered]
        panic: no kind is registered for the type v1beta1.Gateway in scheme "pkg/runtime/scheme.go:100"

What would work is

package x

import (
	"testing"

	"github.com/stretchr/testify/require"
	"k8s.io/client-go/kubernetes/fake"
	gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
	gatewayscheme "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned/scheme"
)

func TestGatewayFake(t *testing.T) {
	require.NoError(t, gatewayscheme.AddToScheme(fake.Scheme)) // <- this is not possible currently.

	_ = fake.NewSimpleClientset(&gatewayv1beta1.Gateway{})
}

with the twist that the Scheme in client-go's fake package gets exported.

I was suggested to create an issue in this repo as client-go might get overlooked and not triaged.

Related #sig-api-machinery thread: https://kubernetes.slack.com/archives/C0EG7JC6T/p1676125039126589

What did you expect to happen?

Fake object to work with client-go

How can we reproduce it (as minimally and precisely as possible)?

package x

import (
	"testing"

	"github.com/stretchr/testify/require"
	"k8s.io/client-go/kubernetes/fake"
	clientsetscheme "k8s.io/client-go/kubernetes/scheme"
	gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
	gatewayscheme "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned/scheme"
)

func TestGatewayFake(t *testing.T) {
	require.NoError(t, gatewayscheme.AddToScheme(clientsetscheme.Scheme))

	_ = fake.NewSimpleClientset(&gatewayv1beta1.Gateway{})
}

Anything else we need to know?

No response

Kubernetes version

N/A

Cloud provider

N/A

OS version

# On Linux:
$ cat /etc/os-release
# paste output here
$ uname -a
# paste output here

# On Windows:
C:\> wmic os get Caption, Version, BuildNumber, OSArchitecture
# paste output here

Install tools

Container runtime (CRI) and version (if applicable)

Related plugins (CNI, CSI, ...) and versions (if applicable)

No response

@pmalek pmalek added the kind/bug Categorizes issue or PR as related to a bug. label Mar 3, 2023
@k8s-ci-robot k8s-ci-robot added needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Mar 3, 2023
@pmalek
Copy link
Contributor Author

pmalek commented Mar 3, 2023

/sig api-machinery

@k8s-ci-robot k8s-ci-robot added sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. and removed needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Mar 3, 2023
@cici37
Copy link
Contributor

cici37 commented Mar 9, 2023

/sig network

@k8s-ci-robot k8s-ci-robot added the sig/network Categorizes an issue or PR as relevant to SIG Network. label Mar 9, 2023
@aojea
Copy link
Member

aojea commented Mar 9, 2023

/cc @shaneutt @robscott

@robscott
Copy link
Member

robscott commented Mar 9, 2023

Hey @pmalek, you also need to find a way to add the Gateway API types to the scheme first, something like this:

https://github.com/kubernetes-sigs/gateway-api/blob/aa35a96894895b52911ea0ece00b49717be55b7e/conformance/conformance_test.go#L45-L46.

Feel free to follow up on Gateway API Slack if you're still running into issues, lots of people there that would have dealt with this.

/close

@k8s-ci-robot
Copy link
Contributor

@robscott: Closing this issue.

In response to this:

Hey @pmalek, you also need to find a way to add the Gateway API types to the scheme first, something like this:

https://github.com/kubernetes-sigs/gateway-api/blob/aa35a96894895b52911ea0ece00b49717be55b7e/conformance/conformance_test.go#L45-L46.

Feel free to follow up on Gateway API Slack if you're still running into issues, lots of people there that would have dealt with this.

/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@robscott
Copy link
Member

robscott commented Mar 9, 2023

Actually, looks like I misread the issue, would still recommend chatting in #sig-network-gateway-api on Slack, but reopening because I don't think my suggested solution was helpful.

/reopen

@k8s-ci-robot k8s-ci-robot reopened this Mar 9, 2023
@k8s-ci-robot
Copy link
Contributor

@robscott: Reopened this issue.

In response to this:

Actually, looks like I misread the issue, would still recommend chatting in #sig-network-gateway-api on Slack, but reopening because I don't think my suggested solution was helpful.

/reopen

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@pmalek pmalek changed the title Cannot use client-go's ga Cannot use client-go's fake client with Gateway API objects Mar 10, 2023
@MikeZappa87
Copy link

/assign @shaneutt

@lavalamp
Copy link
Member

For clarity, the request here is to export fake's schema so that other objects can be added to it, or for the ability to pass a schema in for fakes to use.

@pmalek
Copy link
Contributor Author

pmalek commented Mar 16, 2023

For clarity, the request here is to export fake's schema so that other objects can be added to it, or for the ability to pass a schema in for fakes to use.

Yes. Either that or explain/document a way to do it differently.

@cici37
Copy link
Contributor

cici37 commented Mar 16, 2023

/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Mar 16, 2023
@shaneutt shaneutt removed their assignment Mar 16, 2023
@lavalamp
Copy link
Member

Yeah, I don't think there's going to be any other options.

/assign @apelisse

@shaneutt
Copy link
Member

As we discussed in slack this is not networking specific, thanks @lavalamp! 👍

/remove-sig network

@k8s-ci-robot k8s-ci-robot removed the sig/network Categorizes an issue or PR as relevant to SIG Network. label Mar 16, 2023
@arbreezy
Copy link

is there any workaround or a different approach maybe ?

@pmalek
Copy link
Contributor Author

pmalek commented Dec 9, 2023

is there any workaround or a different approach maybe ?

Unfortunately not for the k8s.io/client-go/kubernetes/fake but for gateway-api's fake client (sigs.k8s.io/gateway-api/pkg/client/clientset/versioned/fake) it does seem to (kinda) work:

package main

import (
	"testing"

	gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
	gatewaysfake "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned/fake"
)

func TestGatewayFakeClient(t *testing.T) {
	k8sclient := gatewaysfake.NewSimpleClientset(&gatewayv1.Gateway{})
	_ = k8sclient
}

So you can get a working client but unfortunately due to kubernetes/client-go#1082 you won't get it work work:

package main

import (
	"context"
	"testing"

	"github.com/stretchr/testify/require"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
	gatewaysfake "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned/fake"
)

func TestGatewayFakeClientGet(t *testing.T) {
	ctx := context.Background()
	gw := &gatewayv1.Gateway{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "gw",
			Namespace: "default",
		},
	}
	k8sclient := gatewaysfake.NewSimpleClientset(gw)
	gatewaysClient := k8sclient.GatewayV1().Gateways("default")
	gw, err := gatewaysClient.Get(ctx, "gw", metav1.GetOptions{})
	require.NoError(t, err)
}
        	Error:      	Received unexpected error:
        	            	gateways.gateway.networking.k8s.io "gw" not found
        	Test:       	TestGatewayFakeClientGet

This is due to https://github.com/kubernetes/client-go/blob/84a6fe7e4032ae1b8bc03b5208e771c5f7103549/testing/fixture.go#L292 holding gatewaies not gateways:

image

@miguelvr
Copy link

I just bumped into this. Is it currently impossible to use a fake client with Gateway resources?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

No branches or pull requests