Merge pull request #64608 from dixudx/config_view_context

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

apply global flag "context" for kubectl config view

**What this PR does / why we need it**:
`--context` is a global flag, which should be applied to `kubectl config view` as well when minifying.

Currently this command is only available for `current-context`. With this PR, it will be easier for users to view other non current contexts when minifying.

**Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*:
Fixes #64583

**Special notes for your reviewer**:
/cc soltysh juanvallejo 
 
**Release note**:

```release-note
apply global flag "context" for kubectl config view --minify
```
This commit is contained in:
Kubernetes Submit Queue 2018-06-05 06:05:49 -07:00 committed by GitHub
commit b81a192a84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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() streams, _, buf, _ := genericclioptions.NewTestIOStreams()
cmd := NewCmdConfig(cmdutil.NewFactory(genericclioptions.NewTestConfigFlags()), clientcmd.NewDefaultPathOptions(), streams) 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.SetArgs(argsToUse)
cmd.Execute() cmd.Execute()
// outBytes, _ := ioutil.ReadFile(fakeKubeFile.Name())
config := clientcmd.GetConfigFromFileOrDie(fakeKubeFile.Name()) config := clientcmd.GetConfigFromFileOrDie(fakeKubeFile.Name())
return buf.String(), *config return buf.String(), *config
} }

View File

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

View File

@ -44,18 +44,18 @@ func TestViewCluster(t *testing.T) {
}, },
Contexts: map[string]*clientcmdapi.Context{ Contexts: map[string]*clientcmdapi.Context{
"minikube": {AuthInfo: "minikube", Cluster: "minikube"}, "minikube": {AuthInfo: "minikube", Cluster: "minikube"},
"my-cluser": {AuthInfo: "mu-cluster", Cluster: "my-cluster"}, "my-cluster": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
}, },
CurrentContext: "minikube", CurrentContext: "minikube",
AuthInfos: map[string]*clientcmdapi.AuthInfo{ AuthInfos: map[string]*clientcmdapi.AuthInfo{
"minikube": {Token: "minikube-token"}, "minikube": {Token: "minikube-token"},
"my-cluser": {Token: "minikube-token"}, "mu-cluster": {Token: "minikube-token"},
}, },
} }
test := viewClusterTest{ test := viewClusterTest{
description: "Testing for kubectl config view", description: "Testing for kubectl config view",
config: conf, config: conf,
flags: []string{},
expected: `apiVersion: v1 expected: `apiVersion: v1
clusters: clusters:
- cluster: - cluster:
@ -72,7 +72,7 @@ contexts:
- context: - context:
cluster: my-cluster cluster: my-cluster
user: mu-cluster user: mu-cluster
name: my-cluser name: my-cluster
current-context: minikube current-context: minikube
kind: Config kind: Config
preferences: {} preferences: {}
@ -80,10 +80,13 @@ users:
- name: minikube - name: minikube
user: user:
token: minikube-token token: minikube-token
- name: my-cluser - name: mu-cluster
user: user:
token: minikube-token` + "\n"} token: minikube-token` + "\n",
}
test.run(t) test.run(t)
} }
func TestViewClusterMinify(t *testing.T) { func TestViewClusterMinify(t *testing.T) {
@ -96,15 +99,22 @@ func TestViewClusterMinify(t *testing.T) {
}, },
Contexts: map[string]*clientcmdapi.Context{ Contexts: map[string]*clientcmdapi.Context{
"minikube": {AuthInfo: "minikube", Cluster: "minikube"}, "minikube": {AuthInfo: "minikube", Cluster: "minikube"},
"my-cluser": {AuthInfo: "mu-cluster", Cluster: "my-cluster"}, "my-cluster": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
}, },
CurrentContext: "minikube", CurrentContext: "minikube",
AuthInfos: map[string]*clientcmdapi.AuthInfo{ AuthInfos: map[string]*clientcmdapi.AuthInfo{
"minikube": {Token: "minikube-token"}, "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", description: "Testing for kubectl config view --minify=true",
config: conf, config: conf,
flags: []string{"--minify=true"}, flags: []string{"--minify=true"},
@ -124,8 +134,41 @@ preferences: {}
users: users:
- name: minikube - name: minikube
user: user:
token: minikube-token` + "\n"} token: minikube-token` + "\n",
test.run(t) },
{
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) { func (test viewClusterTest) run(t *testing.T) {
@ -143,7 +186,10 @@ func (test viewClusterTest) run(t *testing.T) {
pathOptions.EnvVar = "" pathOptions.EnvVar = ""
streams, _, buf, _ := genericclioptions.NewTestIOStreams() streams, _, buf, _ := genericclioptions.NewTestIOStreams()
cmd := NewCmdConfigView(cmdutil.NewFactory(genericclioptions.NewTestConfigFlags()), streams, pathOptions) 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) cmd.Flags().Parse(test.flags)
if err := cmd.Execute(); err != nil { if err := cmd.Execute(); err != nil {
t.Fatalf("unexpected error executing command: %v,kubectl config view flags: %v", err, test.flags) t.Fatalf("unexpected error executing command: %v,kubectl config view flags: %v", err, test.flags)
} }