mirror of
https://github.com/kubernetes/kubernetes.git
synced 2026-02-03 20:40:26 -05:00
kubectl config set-context: Add -n flag
This is simply a shorthand for --namespace as with other commands.
This commit is contained in:
parent
8ac5701d3a
commit
d03fab555f
3 changed files with 66 additions and 43 deletions
|
|
@ -79,7 +79,7 @@ func NewCmdConfigSetContext(restClientGetter genericclioptions.RESTClientGetter,
|
|||
cmd.Flags().BoolVar(&options.currContext, "current", options.currContext, "Modify the current context")
|
||||
cmd.Flags().Var(&options.cluster, clientcmd.FlagClusterName, clientcmd.FlagClusterName+" for the context entry in kubeconfig")
|
||||
cmd.Flags().Var(&options.authInfo, clientcmd.FlagAuthInfoName, clientcmd.FlagAuthInfoName+" for the context entry in kubeconfig")
|
||||
cmd.Flags().Var(&options.namespace, clientcmd.FlagNamespace, clientcmd.FlagNamespace+" for the context entry in kubeconfig")
|
||||
cmd.Flags().VarP(&options.namespace, clientcmd.FlagNamespace, "n", clientcmd.FlagNamespace+" for the context entry in kubeconfig")
|
||||
cmdutil.CheckErr(cmd.RegisterFlagCompletionFunc(
|
||||
"namespace",
|
||||
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||
|
|
|
|||
|
|
@ -37,48 +37,71 @@ type setContextTest struct {
|
|||
expectedConfig clientcmdapi.Config //expect kubectl config
|
||||
}
|
||||
|
||||
func TestCreateContext(t *testing.T) {
|
||||
conf := clientcmdapi.Config{}
|
||||
test := setContextTest{
|
||||
testContext: "shaker-context",
|
||||
description: "Testing for create a new context",
|
||||
config: conf,
|
||||
args: []string{"shaker-context"},
|
||||
flags: []string{
|
||||
"--cluster=cluster_nickname",
|
||||
"--user=user_nickname",
|
||||
"--namespace=namespace",
|
||||
},
|
||||
expected: `Context "shaker-context" created.` + "\n",
|
||||
expectedConfig: clientcmdapi.Config{
|
||||
Contexts: map[string]*clientcmdapi.Context{
|
||||
"shaker-context": {AuthInfo: "user_nickname", Cluster: "cluster_nickname", Namespace: "namespace"}},
|
||||
},
|
||||
}
|
||||
test.run(t)
|
||||
var namespaceFlagCases = []struct {
|
||||
description string
|
||||
namespaceFlag string
|
||||
}{
|
||||
{
|
||||
description: "long namespace flag",
|
||||
namespaceFlag: "--namespace",
|
||||
},
|
||||
{
|
||||
description: "short namespace flag",
|
||||
namespaceFlag: "-n",
|
||||
},
|
||||
}
|
||||
func TestModifyContext(t *testing.T) {
|
||||
conf := clientcmdapi.Config{
|
||||
Contexts: map[string]*clientcmdapi.Context{
|
||||
"shaker-context": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"},
|
||||
"not-this": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"}}}
|
||||
test := setContextTest{
|
||||
testContext: "shaker-context",
|
||||
description: "Testing for modify a already exist context",
|
||||
config: conf,
|
||||
args: []string{"shaker-context"},
|
||||
flags: []string{
|
||||
"--cluster=cluster_nickname",
|
||||
"--user=user_nickname",
|
||||
"--namespace=namespace",
|
||||
},
|
||||
expected: `Context "shaker-context" modified.` + "\n",
|
||||
expectedConfig: clientcmdapi.Config{
|
||||
Contexts: map[string]*clientcmdapi.Context{
|
||||
"shaker-context": {AuthInfo: "user_nickname", Cluster: "cluster_nickname", Namespace: "namespace"},
|
||||
"not-this": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"}}},
|
||||
|
||||
func TestCreateContext(t *testing.T) {
|
||||
for _, tc := range namespaceFlagCases {
|
||||
t.Run(tc.description, func(t *testing.T) {
|
||||
conf := clientcmdapi.Config{}
|
||||
test := setContextTest{
|
||||
testContext: "shaker-context",
|
||||
description: "Testing for create a new context",
|
||||
config: conf,
|
||||
args: []string{"shaker-context"},
|
||||
flags: []string{
|
||||
"--cluster=cluster_nickname",
|
||||
"--user=user_nickname",
|
||||
tc.namespaceFlag + "=namespace",
|
||||
},
|
||||
expected: `Context "shaker-context" created.` + "\n",
|
||||
expectedConfig: clientcmdapi.Config{
|
||||
Contexts: map[string]*clientcmdapi.Context{
|
||||
"shaker-context": {AuthInfo: "user_nickname", Cluster: "cluster_nickname", Namespace: "namespace"}},
|
||||
},
|
||||
}
|
||||
test.run(t)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestModifyContext(t *testing.T) {
|
||||
for _, tc := range namespaceFlagCases {
|
||||
t.Run(tc.description, func(t *testing.T) {
|
||||
conf := clientcmdapi.Config{
|
||||
Contexts: map[string]*clientcmdapi.Context{
|
||||
"shaker-context": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"},
|
||||
"not-this": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"}}}
|
||||
test := setContextTest{
|
||||
testContext: "shaker-context",
|
||||
description: "Testing for modify a already exist context",
|
||||
config: conf,
|
||||
args: []string{"shaker-context"},
|
||||
flags: []string{
|
||||
"--cluster=cluster_nickname",
|
||||
"--user=user_nickname",
|
||||
tc.namespaceFlag + "=namespace",
|
||||
},
|
||||
expected: `Context "shaker-context" modified.` + "\n",
|
||||
expectedConfig: clientcmdapi.Config{
|
||||
Contexts: map[string]*clientcmdapi.Context{
|
||||
"shaker-context": {AuthInfo: "user_nickname", Cluster: "cluster_nickname", Namespace: "namespace"},
|
||||
"not-this": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"}}},
|
||||
}
|
||||
test.run(t)
|
||||
})
|
||||
}
|
||||
test.run(t)
|
||||
}
|
||||
|
||||
func TestModifyCurrentContext(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ run_kubectl_delete_allnamespaces_tests() {
|
|||
kubectl delete configmap --dry-run=server -l deletetest=true --all-namespaces
|
||||
kubectl config set-context "${CONTEXT}" --namespace="${ns_one}"
|
||||
kube::test::get_object_assert 'configmap -l deletetest' "{{range.items}}{{${id_field:?}}}:{{end}}" 'one:'
|
||||
kubectl config set-context "${CONTEXT}" --namespace="${ns_two}"
|
||||
kubectl config set-context "${CONTEXT}" -n "${ns_two}"
|
||||
kube::test::get_object_assert 'configmap -l deletetest' "{{range.items}}{{${id_field:?}}}:{{end}}" 'two:'
|
||||
|
||||
kubectl delete configmap -l deletetest=true --all-namespaces
|
||||
|
|
@ -46,7 +46,7 @@ run_kubectl_delete_allnamespaces_tests() {
|
|||
# no configmaps should be in either of those namespaces with label deletetest
|
||||
kubectl config set-context "${CONTEXT}" --namespace="${ns_one}"
|
||||
kube::test::get_object_assert 'configmap -l deletetest' "{{range.items}}{{${id_field:?}}}:{{end}}" ''
|
||||
kubectl config set-context "${CONTEXT}" --namespace="${ns_two}"
|
||||
kubectl config set-context "${CONTEXT}" -n "${ns_two}"
|
||||
kube::test::get_object_assert 'configmap -l deletetest' "{{range.items}}{{${id_field:?}}}:{{end}}" ''
|
||||
|
||||
set +o nounset
|
||||
|
|
|
|||
Loading…
Reference in a new issue