-
Notifications
You must be signed in to change notification settings - Fork 712
[kubectl-plugin][Test] Use client-go reactors for FieldSelector filtering in fake client tests #4361
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
Merged
rueian
merged 7 commits into
ray-project:master
from
ikchifo:kubectl-plugin/fieldselector-reactor-tests
Jan 12, 2026
Merged
[kubectl-plugin][Test] Use client-go reactors for FieldSelector filtering in fake client tests #4361
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
954d2ab
feat(kubectl-plugin): add FieldSelector reactor helper for fake clien…
ikchifo eada581
test(kubectl-plugin): apply FieldSelector reactor to completion tests
ikchifo 464f138
refactor(kubectl-plugin): remove manual name filtering in completion
ikchifo 0ff5159
chore(kubectl-plugin): migrate from NewSimpleClientset to NewClientset
ikchifo a979e11
refactor(kubectl-plugin): add NewRayClientset wrapper for simpler tes…
ikchifo 2e4d4d4
Update kubectl-plugin/pkg/util/client/testing/reactor.go
ikchifo e5fdf04
fix(kubectl-plugin): update references to renamed reactor function
ikchifo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package testing | ||
|
|
||
| import ( | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
|
|
||
| rayClientFake "github.com/ray-project/kuberay/ray-operator/pkg/client/clientset/versioned/fake" | ||
| ) | ||
|
|
||
| // NewRayClientset creates a fake Ray clientset with FieldSelector support. | ||
| // This is a convenience wrapper that creates a clientset and adds the | ||
| // FieldSelector reactor, so tests don't need to set it up manually. | ||
| func NewRayClientset(objects ...runtime.Object) *rayClientFake.Clientset { | ||
| client := rayClientFake.NewClientset(objects...) | ||
| AddRayClusterListFieldSelectorReactor(client) | ||
| return client | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package testing | ||
|
|
||
| import ( | ||
| "k8s.io/apimachinery/pkg/fields" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| kubetesting "k8s.io/client-go/testing" | ||
|
|
||
| rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1" | ||
| rayClientFake "github.com/ray-project/kuberay/ray-operator/pkg/client/clientset/versioned/fake" | ||
| ) | ||
|
|
||
| // AddRayClusterFieldSelectorReactor adds a reactor to the fake Ray client that | ||
| // simulates server-side FieldSelector filtering for RayCluster List operations. | ||
| // This allows tests to verify FieldSelector behavior without manual name checks. | ||
| func AddRayClusterListFieldSelectorReactor(client *rayClientFake.Clientset) { | ||
| client.PrependReactor("list", "rayclusters", func(action kubetesting.Action) (bool, runtime.Object, error) { | ||
| listAction, ok := action.(kubetesting.ListAction) | ||
| if !ok { | ||
| return false, nil, nil | ||
| } | ||
|
|
||
| fieldSelector := listAction.GetListRestrictions().Fields | ||
| if fieldSelector == nil || fieldSelector.Empty() { | ||
| return false, nil, nil | ||
| } | ||
|
|
||
| // Get all clusters first using the tracker | ||
| namespace := listAction.GetNamespace() | ||
| allClusters, err := client.Tracker().List( | ||
| rayv1.SchemeGroupVersion.WithResource("rayclusters"), | ||
| rayv1.SchemeGroupVersion.WithKind("RayCluster"), | ||
| namespace, | ||
| ) | ||
| if err != nil { | ||
| return true, nil, err | ||
| } | ||
|
|
||
| clusterList, ok := allClusters.(*rayv1.RayClusterList) | ||
| if !ok { | ||
| return false, nil, nil | ||
| } | ||
|
|
||
| // Filter based on field selector | ||
| filtered := &rayv1.RayClusterList{} | ||
| for _, cluster := range clusterList.Items { | ||
| if fieldSelector.Matches(clusterFieldSet(&cluster)) { | ||
| filtered.Items = append(filtered.Items, cluster) | ||
| } | ||
| } | ||
|
|
||
| return true, filtered, nil | ||
| }) | ||
| } | ||
|
|
||
| // clusterFieldSet returns a fields.Set for a RayCluster that can be matched against a FieldSelector. | ||
| func clusterFieldSet(cluster *rayv1.RayCluster) fields.Set { | ||
| return fields.Set{ | ||
| "metadata.name": cluster.Name, | ||
| "metadata.namespace": cluster.Namespace, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package testing | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
|
||
| rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1" | ||
| rayClientFake "github.com/ray-project/kuberay/ray-operator/pkg/client/clientset/versioned/fake" | ||
| ) | ||
|
|
||
| func TestAddRayClusterListFieldSelectorReactor(t *testing.T) { | ||
| cluster1 := &rayv1.RayCluster{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "cluster-1", | ||
| Namespace: "default", | ||
| }, | ||
| } | ||
| cluster2 := &rayv1.RayCluster{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "cluster-2", | ||
| Namespace: "default", | ||
| }, | ||
| } | ||
| cluster3 := &rayv1.RayCluster{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "cluster-3", | ||
| Namespace: "other", | ||
| }, | ||
| } | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| namespace string | ||
| fieldSelector string | ||
| expectedNames []string | ||
| expectedCount int | ||
| }{ | ||
| { | ||
| name: "no field selector returns all clusters", | ||
| namespace: "default", | ||
| fieldSelector: "", | ||
| expectedCount: 2, | ||
| expectedNames: []string{"cluster-1", "cluster-2"}, | ||
| }, | ||
| { | ||
| name: "field selector filters by name", | ||
| namespace: "default", | ||
| fieldSelector: "metadata.name=cluster-1", | ||
| expectedCount: 1, | ||
| expectedNames: []string{"cluster-1"}, | ||
| }, | ||
| { | ||
| name: "field selector with non-existent name returns empty", | ||
| namespace: "default", | ||
| fieldSelector: "metadata.name=non-existent", | ||
| expectedCount: 0, | ||
| expectedNames: []string{}, | ||
| }, | ||
| { | ||
| name: "namespace filtering returns only clusters in specified namespace", | ||
| namespace: "other", | ||
| fieldSelector: "", | ||
| expectedCount: 1, | ||
| expectedNames: []string{"cluster-3"}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| rayClient := rayClientFake.NewClientset(cluster1, cluster2, cluster3) | ||
| AddRayClusterListFieldSelectorReactor(rayClient) | ||
|
|
||
| listOpts := metav1.ListOptions{} | ||
| if tc.fieldSelector != "" { | ||
| listOpts.FieldSelector = tc.fieldSelector | ||
| } | ||
|
|
||
| result, err := rayClient.RayV1().RayClusters(tc.namespace).List(context.Background(), listOpts) | ||
| require.NoError(t, err) | ||
| assert.Len(t, result.Items, tc.expectedCount) | ||
|
|
||
| names := make([]string, len(result.Items)) | ||
| for i, cluster := range result.Items { | ||
| names[i] = cluster.Name | ||
| } | ||
| assert.ElementsMatch(t, tc.expectedNames, names) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why delete this line??
cc @cheyu @lorriexingfang @Jiekai for review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah since we use field selector now