Skip to content

Commit 270c79b

Browse files
[kubeclt-plugin] fix get cluster all namespace (#3809) (#3814)
Signed-off-by: fscnick <[email protected]> Co-authored-by: fscnick <[email protected]>
1 parent ee1a435 commit 270c79b

File tree

2 files changed

+71
-44
lines changed

2 files changed

+71
-44
lines changed

kubectl-plugin/pkg/cmd/get/get_cluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (options *GetClusterOptions) Complete(args []string, cmd *cobra.Command) er
6868
}
6969
options.namespace = namespace
7070
if options.namespace == "" {
71-
options.allNamespaces = true
71+
options.namespace = "default"
7272
}
7373

7474
if len(args) >= 1 {

kubectl-plugin/pkg/cmd/get/get_cluster_test.go

Lines changed: 70 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestRayClusterGetComplete(t *testing.T) {
4242
name: "neither namespace nor args set",
4343
namespace: "",
4444
args: []string{},
45-
expectedAllNamespaces: true,
45+
expectedAllNamespaces: false,
4646
expectedCluster: "",
4747
},
4848
{
@@ -56,7 +56,7 @@ func TestRayClusterGetComplete(t *testing.T) {
5656
name: "namespace not set, args set",
5757
namespace: "",
5858
args: []string{"foo", "bar"},
59-
expectedAllNamespaces: true,
59+
expectedAllNamespaces: false,
6060
expectedCluster: "foo",
6161
},
6262
{
@@ -223,75 +223,102 @@ func TestGetRayClusters(t *testing.T) {
223223
},
224224
}
225225

226+
defaultNamespace := "default"
227+
rayClusterInDefault := &rayv1.RayCluster{
228+
ObjectMeta: v1.ObjectMeta{
229+
Name: "raycluster-kuberay-default",
230+
Namespace: defaultNamespace,
231+
},
232+
}
233+
226234
tests := []struct {
227-
namespace *string
228-
name string
229-
expectedError string
230-
expectedOutput string
231-
cluster string
232-
rayClusters []runtime.Object
235+
namespace *string
236+
name string
237+
expectedError string
238+
expectedOutput string
239+
cluster string
240+
expectedRayClusters []runtime.Object
241+
allFakeRayClusters []runtime.Object
242+
allNamespaces bool
233243
}{
234244
{
235-
name: "should not error if no cluster name is provided, searching all namespaces, and no clusters are found",
236-
namespace: nil,
237-
rayClusters: []runtime.Object{},
245+
name: "should not error if no cluster name is provided, searching all namespaces, and no clusters are found",
246+
namespace: nil,
247+
expectedRayClusters: []runtime.Object{},
248+
},
249+
{
250+
name: "should error if a cluster name is provided, searching all namespaces, and no clusters are found",
251+
cluster: "my-cluster",
252+
namespace: nil,
253+
expectedRayClusters: []runtime.Object{},
254+
expectedError: "Ray cluster my-cluster not found",
255+
},
256+
{
257+
name: "should not error if no cluster name is provided, searching one namespace, and no clusters are found",
258+
namespace: &namespace,
259+
expectedRayClusters: []runtime.Object{},
238260
},
239261
{
240-
name: "should error if a cluster name is provided, searching all namespaces, and no clusters are found",
241-
cluster: "my-cluster",
242-
namespace: nil,
243-
rayClusters: []runtime.Object{},
244-
expectedError: "Ray cluster my-cluster not found",
262+
name: "should error if a cluster name is provided, searching one namespace, and no clusters are found",
263+
cluster: "my-cluster",
264+
namespace: &namespace,
265+
expectedRayClusters: []runtime.Object{},
266+
expectedError: fmt.Sprintf("Ray cluster my-cluster not found in namespace %s", namespace),
245267
},
246268
{
247-
name: "should not error if no cluster name is provided, searching one namespace, and no clusters are found",
248-
namespace: &namespace,
249-
rayClusters: []runtime.Object{},
269+
name: "should not error if no cluster name is provided, searching all namespaces, and clusters are found",
270+
namespace: nil,
271+
allFakeRayClusters: []runtime.Object{rayCluster},
272+
expectedRayClusters: []runtime.Object{rayCluster},
250273
},
251274
{
252-
name: "should error if a cluster name is provided, searching one namespace, and no clusters are found",
253-
cluster: "my-cluster",
254-
namespace: &namespace,
255-
rayClusters: []runtime.Object{},
256-
expectedError: fmt.Sprintf("Ray cluster my-cluster not found in namespace %s", namespace),
275+
name: "should not error if a cluster name is provided, searching all namespaces, and clusters are found",
276+
cluster: "my-cluster",
277+
namespace: nil,
278+
allFakeRayClusters: []runtime.Object{rayCluster},
279+
expectedRayClusters: []runtime.Object{rayCluster},
257280
},
258281
{
259-
name: "should not error if no cluster name is provided, searching all namespaces, and clusters are found",
260-
namespace: nil,
261-
rayClusters: []runtime.Object{rayCluster},
282+
name: "should not error if no cluster name is provided, searching one namespace, and clusters are found",
283+
namespace: &namespace,
284+
allFakeRayClusters: []runtime.Object{rayCluster},
285+
expectedRayClusters: []runtime.Object{rayCluster},
262286
},
263287
{
264-
name: "should not error if a cluster name is provided, searching all namespaces, and clusters are found",
265-
cluster: "my-cluster",
266-
namespace: nil,
267-
rayClusters: []runtime.Object{rayCluster},
288+
name: "should not error if a cluster name is provided, searching one namespace, and clusters are found",
289+
cluster: "my-cluster",
290+
namespace: &namespace,
291+
allFakeRayClusters: []runtime.Object{rayCluster},
292+
expectedRayClusters: []runtime.Object{rayCluster},
268293
},
269294
{
270-
name: "should not error if no cluster name is provided, searching one namespace, and clusters are found",
271-
namespace: &namespace,
272-
rayClusters: []runtime.Object{rayCluster},
295+
name: "should not error if no cluster name is provided, searching default namespace, and clusters are found",
296+
namespace: &defaultNamespace,
297+
allFakeRayClusters: []runtime.Object{rayCluster, rayClusterInDefault},
298+
expectedRayClusters: []runtime.Object{rayClusterInDefault},
273299
},
274300
{
275-
name: "should not error if a cluster name is provided, searching one namespace, and clusters are found",
276-
cluster: "my-cluster",
277-
namespace: &namespace,
278-
rayClusters: []runtime.Object{rayCluster},
301+
name: "should not error if no cluster name is provided, searching all namespace, and clusters are found",
302+
allNamespaces: true,
303+
allFakeRayClusters: []runtime.Object{rayCluster, rayClusterInDefault},
304+
expectedRayClusters: []runtime.Object{rayCluster, rayClusterInDefault},
279305
},
280306
}
281307

282308
for _, tc := range tests {
283309
t.Run(tc.name, func(t *testing.T) {
284310
fakeClusterGetOptions := GetClusterOptions{
285-
cmdFactory: cmdFactory,
286-
ioStreams: &testStreams,
287-
cluster: tc.cluster,
311+
cmdFactory: cmdFactory,
312+
ioStreams: &testStreams,
313+
cluster: tc.cluster,
314+
allNamespaces: tc.allNamespaces,
288315
}
289316
if tc.namespace != nil {
290317
fakeClusterGetOptions.namespace = *tc.namespace
291318
}
292319

293320
kubeClientSet := kubefake.NewClientset()
294-
rayClient := rayClientFake.NewSimpleClientset(tc.rayClusters...)
321+
rayClient := rayClientFake.NewSimpleClientset(tc.allFakeRayClusters...)
295322
k8sClients := client.NewClientForTesting(kubeClientSet, rayClient)
296323

297324
rayClusters, err := getRayClusters(context.Background(), &fakeClusterGetOptions, k8sClients)
@@ -302,7 +329,7 @@ func TestGetRayClusters(t *testing.T) {
302329
}
303330

304331
require.NoError(t, err)
305-
assert.Equal(t, len(tc.rayClusters), len(rayClusters.Items))
332+
assert.Equal(t, len(tc.expectedRayClusters), len(rayClusters.Items))
306333
})
307334
}
308335
}

0 commit comments

Comments
 (0)