Skip to content

Commit 94c0e47

Browse files
marckhouzamvpnachev
authored andcommitted
Fix completion of resource names
The output format is now used by the `Complete()` function, so it must be set before invoking said function. The commit also adds a unit tests for this scenario. Signed-off-by: Marc Khouzam <[email protected]>
1 parent d133742 commit 94c0e47

File tree

2 files changed

+97
-2
lines changed

2 files changed

+97
-2
lines changed

staging/src/k8s.io/kubectl/pkg/util/completion/completion.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,14 @@ func compGetResourceList(restClientGetter genericclioptions.RESTClientGetter, cm
319319
streams := genericiooptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: io.Discard}
320320
o := apiresources.NewAPIResourceOptions(streams)
321321

322-
o.Complete(restClientGetter, cmd, nil)
323-
324322
// Get the list of resources
325323
o.PrintFlags.OutputFormat = ptr.To("name")
326324
o.Cached = true
327325
o.Verbs = []string{"get"}
328326
// TODO:Should set --request-timeout=5s
329327

328+
o.Complete(restClientGetter, cmd, nil)
329+
330330
// Ignore errors as the output may still be valid
331331
o.RunAPIResources()
332332

staging/src/k8s.io/kubectl/pkg/util/completion/completion_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,101 @@ func TestResourceAndPortCompletionFunc(t *testing.T) {
492492
}
493493
}
494494

495+
func TestResourceTypeAndNameCompletionFuncResourceList(t *testing.T) {
496+
// Set up a fake discovery client with some API resources
497+
dc := cmdtesting.NewFakeCachedDiscoveryClient()
498+
dc.PreferredResources = []*metav1.APIResourceList{
499+
{
500+
GroupVersion: "v1",
501+
APIResources: []metav1.APIResource{
502+
{
503+
Name: "pods",
504+
Namespaced: true,
505+
Kind: "Pod",
506+
Verbs: []string{"get", "list"},
507+
},
508+
{
509+
Name: "services",
510+
Namespaced: true,
511+
Kind: "Service",
512+
Verbs: []string{"get", "list"},
513+
},
514+
{
515+
Name: "secrets",
516+
Namespaced: true,
517+
Kind: "Secret",
518+
Verbs: []string{"get", "list"},
519+
},
520+
},
521+
},
522+
{
523+
GroupVersion: "apps/v1",
524+
APIResources: []metav1.APIResource{
525+
{
526+
Name: "deployments",
527+
Namespaced: true,
528+
Kind: "Deployment",
529+
Verbs: []string{"get", "list"},
530+
},
531+
},
532+
},
533+
}
534+
535+
testCases := []struct {
536+
name string
537+
args []string
538+
toComplete string
539+
expectedComps []string
540+
expectedDirective cobra.ShellCompDirective
541+
}{
542+
{
543+
name: "complete resources starting with 's'",
544+
args: []string{},
545+
toComplete: "s",
546+
expectedComps: []string{"secrets", "services"},
547+
expectedDirective: cobra.ShellCompDirectiveNoFileComp,
548+
},
549+
{
550+
name: "complete resources starting with 'p'",
551+
args: []string{},
552+
toComplete: "p",
553+
expectedComps: []string{"pods"},
554+
expectedDirective: cobra.ShellCompDirectiveNoFileComp,
555+
},
556+
{
557+
name: "complete resources starting with 'd'",
558+
args: []string{},
559+
toComplete: "d",
560+
expectedComps: []string{"deployments.apps"},
561+
expectedDirective: cobra.ShellCompDirectiveNoFileComp,
562+
},
563+
{
564+
name: "complete all resources with empty string",
565+
args: []string{},
566+
toComplete: "",
567+
expectedComps: []string{"deployments.apps", "pods", "secrets", "services"},
568+
expectedDirective: cobra.ShellCompDirectiveNoFileComp,
569+
},
570+
{
571+
name: "no matches",
572+
args: []string{},
573+
toComplete: "xyz",
574+
expectedComps: []string{},
575+
expectedDirective: cobra.ShellCompDirectiveNoFileComp,
576+
},
577+
}
578+
579+
for _, tc := range testCases {
580+
t.Run(tc.name, func(t *testing.T) {
581+
tf, cmd := prepareCompletionTest()
582+
tf.WithDiscoveryClient(dc)
583+
compFunc := ResourceTypeAndNameCompletionFunc(tf)
584+
comps, directive := compFunc(cmd, tc.args, tc.toComplete)
585+
checkCompletion(t, comps, tc.expectedComps, directive, tc.expectedDirective)
586+
})
587+
}
588+
}
589+
495590
func setMockFactory(config api.Config) {
496591
clientConfig := clientcmd.NewDefaultClientConfig(config, nil)
497592
testFactory := cmdtesting.NewTestFactory().WithClientConfig(clientConfig)

0 commit comments

Comments
 (0)