diff --git a/pkg/cmd/cli/completion_functions.go b/pkg/cmd/cli/completion_functions.go index 3a7231484..c2ef20d04 100644 --- a/pkg/cmd/cli/completion_functions.go +++ b/pkg/cmd/cli/completion_functions.go @@ -40,9 +40,17 @@ func completeNames(f client.Factory, list kbclient.ObjectList) completionFunc { if err != nil { return nil, cobra.ShellCompDirectiveNoFileComp } - ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + parentCtx := context.Background() + if cmd != nil && cmd.Context() != nil { + parentCtx = cmd.Context() + } + ctx, cancel := context.WithTimeout(parentCtx, 3*time.Second) defer cancel() - freshList := list.DeepCopyObject().(kbclient.ObjectList) + freshObject := list.DeepCopyObject() + freshList, ok := freshObject.(kbclient.ObjectList) + if !ok { + return nil, cobra.ShellCompDirectiveNoFileComp + } if err := kbClient.List(ctx, freshList, &kbclient.ListOptions{Namespace: f.Namespace()}); err != nil { return nil, cobra.ShellCompDirectiveNoFileComp } diff --git a/pkg/cmd/cli/completion_functions_test.go b/pkg/cmd/cli/completion_functions_test.go index b765ed54a..3bc33402d 100644 --- a/pkg/cmd/cli/completion_functions_test.go +++ b/pkg/cmd/cli/completion_functions_test.go @@ -153,7 +153,7 @@ func TestCompleteNames(t *testing.T) { got, directive := completionFn(&cobra.Command{}, tc.args, tc.toComplete) assert.Equal(t, cobra.ShellCompDirectiveNoFileComp, directive) - assert.Equal(t, tc.want, got) + assert.ElementsMatch(t, tc.want, got) }) } }