apply global flag "context" for kubectl config view

This commit is contained in:
Di Xu 2018-06-01 18:01:12 +08:00
parent 5da925ad4f
commit acabdcb96a
3 changed files with 72 additions and 21 deletions

View File

@ -865,12 +865,12 @@ func testConfigCommand(args []string, startingConfig clientcmdapi.Config, t *tes
streams, _, buf, _ := genericclioptions.NewTestIOStreams()
cmd := NewCmdConfig(cmdutil.NewFactory(genericclioptions.NewTestConfigFlags()), clientcmd.NewDefaultPathOptions(), streams)
// "context" is a global flag, inherited from base kubectl command in the real world
cmd.PersistentFlags().String("context", "", "The name of the kubeconfig context to use")
cmd.SetArgs(argsToUse)
cmd.Execute()
// outBytes, _ := ioutil.ReadFile(fakeKubeFile.Name())
config := clientcmd.GetConfigFromFileOrDie(fakeKubeFile.Name())
return buf.String(), *config
}

View File

@ -43,6 +43,7 @@ type ViewOptions struct {
Minify bool
RawByteData bool
Context string
OutputFormat string
genericclioptions.IOStreams
@ -110,6 +111,7 @@ func (o *ViewOptions) Complete(cmd *cobra.Command) error {
return err
}
o.PrintObject = printer.PrintObj
o.Context = cmdutil.GetFlagString(cmd, "context")
return nil
}
@ -129,6 +131,9 @@ func (o ViewOptions) Run() error {
}
if o.Minify {
if len(o.Context) > 0 {
config.CurrentContext = o.Context
}
if err := clientcmdapi.MinifyConfig(config); err != nil {
return err
}

View File

@ -44,18 +44,18 @@ func TestViewCluster(t *testing.T) {
},
Contexts: map[string]*clientcmdapi.Context{
"minikube": {AuthInfo: "minikube", Cluster: "minikube"},
"my-cluser": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
"my-cluster": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
},
CurrentContext: "minikube",
AuthInfos: map[string]*clientcmdapi.AuthInfo{
"minikube": {Token: "minikube-token"},
"my-cluser": {Token: "minikube-token"},
"mu-cluster": {Token: "minikube-token"},
},
}
test := viewClusterTest{
description: "Testing for kubectl config view",
config: conf,
flags: []string{},
expected: `apiVersion: v1
clusters:
- cluster:
@ -72,7 +72,7 @@ contexts:
- context:
cluster: my-cluster
user: mu-cluster
name: my-cluser
name: my-cluster
current-context: minikube
kind: Config
preferences: {}
@ -80,10 +80,13 @@ users:
- name: minikube
user:
token: minikube-token
- name: my-cluser
- name: mu-cluster
user:
token: minikube-token` + "\n"}
token: minikube-token` + "\n",
}
test.run(t)
}
func TestViewClusterMinify(t *testing.T) {
@ -96,15 +99,22 @@ func TestViewClusterMinify(t *testing.T) {
},
Contexts: map[string]*clientcmdapi.Context{
"minikube": {AuthInfo: "minikube", Cluster: "minikube"},
"my-cluser": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
"my-cluster": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
},
CurrentContext: "minikube",
AuthInfos: map[string]*clientcmdapi.AuthInfo{
"minikube": {Token: "minikube-token"},
"my-cluser": {Token: "minikube-token"},
"mu-cluster": {Token: "minikube-token"},
},
}
test := viewClusterTest{
testCases := []struct {
description string
config clientcmdapi.Config
flags []string
expected string
}{
{
description: "Testing for kubectl config view --minify=true",
config: conf,
flags: []string{"--minify=true"},
@ -124,8 +134,41 @@ preferences: {}
users:
- name: minikube
user:
token: minikube-token` + "\n"}
test.run(t)
token: minikube-token` + "\n",
},
{
description: "Testing for kubectl config view --minify=true --context=my-cluster",
config: conf,
flags: []string{"--minify=true", "--context=my-cluster"},
expected: `apiVersion: v1
clusters:
- cluster:
server: https://192.168.0.1:3434
name: my-cluster
contexts:
- context:
cluster: my-cluster
user: mu-cluster
name: my-cluster
current-context: my-cluster
kind: Config
preferences: {}
users:
- name: mu-cluster
user:
token: minikube-token` + "\n",
},
}
for _, test := range testCases {
cmdTest := viewClusterTest{
description: test.description,
config: test.config,
flags: test.flags,
expected: test.expected,
}
cmdTest.run(t)
}
}
func (test viewClusterTest) run(t *testing.T) {
@ -143,7 +186,10 @@ func (test viewClusterTest) run(t *testing.T) {
pathOptions.EnvVar = ""
streams, _, buf, _ := genericclioptions.NewTestIOStreams()
cmd := NewCmdConfigView(cmdutil.NewFactory(genericclioptions.NewTestConfigFlags()), streams, pathOptions)
// "context" is a global flag, inherited from base kubectl command in the real world
cmd.Flags().String("context", "", "The name of the kubeconfig context to use")
cmd.Flags().Parse(test.flags)
if err := cmd.Execute(); err != nil {
t.Fatalf("unexpected error executing command: %v,kubectl config view flags: %v", err, test.flags)
}