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

fakeDynamicClient cannot filter resources using FieldSelector #1393

Open
chinazj opened this issue Dec 11, 2024 · 1 comment
Open

fakeDynamicClient cannot filter resources using FieldSelector #1393

chinazj opened this issue Dec 11, 2024 · 1 comment

Comments

@chinazj
Copy link

chinazj commented Dec 11, 2024

example code

package main

import (
	"context"
	"fmt"
	v1 "k8s.io/api/apps/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/client-go/dynamic/fake"
)

var d1 = &v1.Deployment{
	ObjectMeta: metav1.ObjectMeta{
		Name:      "nginx-deployment",
		Namespace: "default",
	},
	Spec: v1.DeploymentSpec{},
}

var d2 = &v1.Deployment{
	ObjectMeta: metav1.ObjectMeta{
		Name:      "nginx2-deployment",
		Namespace: "default",
	},
	Spec: v1.DeploymentSpec{},
}

func main() {
	var f5scheme = runtime.NewScheme()
	f5scheme.AddKnownTypes(v1.SchemeGroupVersion,
		&v1.Deployment{},
		&v1.DeploymentList{},
	)
	dynamicClient := fake.NewSimpleDynamicClient(f5scheme, d1, d2)
	DeploymentList, _ := dynamicClient.Resource(schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"}).
		Namespace("default").List(context.TODO(), metav1.ListOptions{
		FieldSelector: "metadata.name=nginx-deployment",
	})
	for _, d := range DeploymentList.Items {
		fmt.Println(d.GetName())
	}
}

output

nginx-deployment
nginx2-deployment

from debug. only filter by namespace
image

@shillberry
Copy link

shillberry commented Dec 27, 2024

For anyone digging into this, alsoFakeLister discards field selectors before building the list.

label, _, _ := testing.ExtractFromListOptions(opts)

Update: we were able to replicate the real client functionality by prepending a reactor to the fake client set. See #500

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants