Files
81c526d7d0 [kubectl-plugin][Test] Use client-go reactors for FieldSelector filtering in fake client tests (#4361)
* feat(kubectl-plugin): add FieldSelector reactor helper for fake client tests

Add AddRayClusterFieldSelectorReactor helper that simulates server-side
FieldSelector filtering in fake client tests. This addresses issue #4337
by allowing tests to verify filtering behavior without manual name checks.

Refs: #4337

* test(kubectl-plugin): apply FieldSelector reactor to completion tests

Use the new AddRayClusterFieldSelectorReactor in workergroup completion
tests to properly simulate server-side filtering behavior.

Refs: #4337

* refactor(kubectl-plugin): remove manual name filtering in completion

Remove the workaround that manually filtered clusters by name since the
fake client now properly supports FieldSelector filtering via reactors.

Refs: #4337

* chore(kubectl-plugin): migrate from NewSimpleClientset to NewClientset

NewSimpleClientset is deprecated in favor of NewClientset for better
server-side apply testing support.

Note: scale_cluster_test.go is not migrated because it uses Update
operations that require schema definitions missing from the generated
applyconfiguration internal schema.

Refs: #4337

* refactor(kubectl-plugin): add NewRayClientset wrapper for simpler test setup

Add a convenience wrapper that creates a fake Ray clientset with
FieldSelector reactor pre-configured. This simplifies test setup
and ensures consistent behavior across tests.

Also applies reactor to get_cluster_test.go and fixes test data
to match actual cluster names now that FieldSelector properly filters.

Refs: #4337

* Update kubectl-plugin/pkg/util/client/testing/reactor.go

Co-authored-by: JustinYeh <[email protected]>
Signed-off-by: Ikenna <[email protected]>

* fix(kubectl-plugin): update references to renamed reactor function

Update clientset.go and reactor_test.go to use the renamed function
AddRayClusterListFieldSelectorReactor.

---------

Signed-off-by: Ikenna <[email protected]>
Co-authored-by: JustinYeh <[email protected]>
2026-01-12 22:18:46 +08:00

62 lines
1.9 KiB
Go

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,
}
}