Skip to content

Commit 62be852

Browse files
committed
fix: add namespace autocompletion for kubectl config set-context command
Signed-off-by: TessaIO <[email protected]>
1 parent a310305 commit 62be852

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ func NewKubectlCommand(o KubectlOptions) *cobra.Command {
480480
registerCompletionFuncForGlobalFlags(cmds, f)
481481

482482
cmds.AddCommand(alpha)
483-
cmds.AddCommand(cmdconfig.NewCmdConfig(clientcmd.NewDefaultPathOptions(), o.IOStreams))
483+
cmds.AddCommand(cmdconfig.NewCmdConfig(f, clientcmd.NewDefaultPathOptions(), o.IOStreams))
484484
cmds.AddCommand(plugin.NewCmdPlugin(o.IOStreams))
485485
cmds.AddCommand(version.NewCmdVersion(f, o.IOStreams))
486486
cmds.AddCommand(apiresources.NewCmdAPIVersions(f, o.IOStreams))

staging/src/k8s.io/kubectl/pkg/cmd/config/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/spf13/cobra"
2525

26+
"k8s.io/cli-runtime/pkg/genericclioptions"
2627
"k8s.io/cli-runtime/pkg/genericiooptions"
2728
"k8s.io/client-go/tools/clientcmd"
2829
cmdutil "k8s.io/kubectl/pkg/cmd/util"
@@ -31,7 +32,7 @@ import (
3132
)
3233

3334
// NewCmdConfig creates a command object for the "config" action, and adds all child commands to it.
34-
func NewCmdConfig(pathOptions *clientcmd.PathOptions, streams genericiooptions.IOStreams) *cobra.Command {
35+
func NewCmdConfig(restClientGetter genericclioptions.RESTClientGetter, pathOptions *clientcmd.PathOptions, streams genericiooptions.IOStreams) *cobra.Command {
3536
if len(pathOptions.ExplicitFileFlag) == 0 {
3637
pathOptions.ExplicitFileFlag = clientcmd.RecommendedConfigPathFlag
3738
}
@@ -58,7 +59,7 @@ func NewCmdConfig(pathOptions *clientcmd.PathOptions, streams genericiooptions.I
5859
cmd.AddCommand(NewCmdConfigView(streams, pathOptions))
5960
cmd.AddCommand(NewCmdConfigSetCluster(streams.Out, pathOptions))
6061
cmd.AddCommand(NewCmdConfigSetCredentials(streams.Out, pathOptions))
61-
cmd.AddCommand(NewCmdConfigSetContext(streams.Out, pathOptions))
62+
cmd.AddCommand(NewCmdConfigSetContext(restClientGetter, streams.Out, pathOptions))
6263
cmd.AddCommand(NewCmdConfigSet(streams.Out, pathOptions))
6364
cmd.AddCommand(NewCmdConfigUnset(streams.Out, pathOptions))
6465
cmd.AddCommand(NewCmdConfigCurrentContext(streams.Out, pathOptions))

staging/src/k8s.io/kubectl/pkg/cmd/config/config_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"k8s.io/client-go/tools/clientcmd"
3131
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
3232
utiltesting "k8s.io/client-go/util/testing"
33+
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
3334
cmdutil "k8s.io/kubectl/pkg/cmd/util"
3435
)
3536

@@ -948,8 +949,11 @@ func testConfigCommand(args []string, startingConfig clientcmdapi.Config, t *tes
948949
argsToUse = append(argsToUse, "--kubeconfig="+fakeKubeFile.Name())
949950
argsToUse = append(argsToUse, args...)
950951

952+
tf := cmdtesting.NewTestFactory().WithNamespace("test")
953+
defer tf.Cleanup()
954+
951955
streams, _, buf, _ := genericiooptions.NewTestIOStreams()
952-
cmd := NewCmdConfig(clientcmd.NewDefaultPathOptions(), streams)
956+
cmd := NewCmdConfig(tf, clientcmd.NewDefaultPathOptions(), streams)
953957
// "context" is a global flag, inherited from base kubectl command in the real world
954958
cmd.PersistentFlags().String("context", "", "The name of the kubeconfig context to use")
955959
cmd.SetArgs(argsToUse)

staging/src/k8s.io/kubectl/pkg/cmd/config/set_context.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/spf13/cobra"
2525

26+
"k8s.io/cli-runtime/pkg/genericclioptions"
2627
"k8s.io/client-go/tools/clientcmd"
2728
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
2829
cliflag "k8s.io/component-base/cli/flag"
@@ -53,7 +54,7 @@ var (
5354
)
5455

5556
// NewCmdConfigSetContext returns a Command instance for 'config set-context' sub command
56-
func NewCmdConfigSetContext(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
57+
func NewCmdConfigSetContext(restClientGetter genericclioptions.RESTClientGetter, out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
5758
options := &setContextOptions{configAccess: configAccess}
5859

5960
cmd := &cobra.Command{
@@ -79,6 +80,12 @@ func NewCmdConfigSetContext(out io.Writer, configAccess clientcmd.ConfigAccess)
7980
cmd.Flags().Var(&options.cluster, clientcmd.FlagClusterName, clientcmd.FlagClusterName+" for the context entry in kubeconfig")
8081
cmd.Flags().Var(&options.authInfo, clientcmd.FlagAuthInfoName, clientcmd.FlagAuthInfoName+" for the context entry in kubeconfig")
8182
cmd.Flags().Var(&options.namespace, clientcmd.FlagNamespace, clientcmd.FlagNamespace+" for the context entry in kubeconfig")
83+
cmdutil.CheckErr(cmd.RegisterFlagCompletionFunc(
84+
"namespace",
85+
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
86+
return completion.CompGetResource(cmdutil.NewFactory(restClientGetter), "namespace", toComplete), cobra.ShellCompDirectiveNoFileComp
87+
},
88+
))
8289

8390
return cmd
8491
}

staging/src/k8s.io/kubectl/pkg/cmd/config/set_context_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ import (
2121
"os"
2222
"testing"
2323

24-
utiltesting "k8s.io/client-go/util/testing"
25-
2624
"k8s.io/client-go/tools/clientcmd"
2725
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
26+
utiltesting "k8s.io/client-go/util/testing"
27+
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
2828
)
2929

3030
type setContextTest struct {
@@ -122,7 +122,11 @@ func (test setContextTest) run(t *testing.T) {
122122
pathOptions.GlobalFile = fakeKubeFile.Name()
123123
pathOptions.EnvVar = ""
124124
buf := bytes.NewBuffer([]byte{})
125-
cmd := NewCmdConfigSetContext(buf, pathOptions)
125+
126+
tf := cmdtesting.NewTestFactory().WithNamespace("test")
127+
defer tf.Cleanup()
128+
129+
cmd := NewCmdConfigSetContext(tf, buf, pathOptions)
126130
cmd.SetArgs(test.args)
127131
cmd.Flags().Parse(test.flags)
128132
if err := cmd.Execute(); err != nil {

0 commit comments

Comments
 (0)