mirror of https://github.com/k3s-io/k3s
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 ```pull/8/head
commit
b81a192a84
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -43,19 +43,19 @@ func TestViewCluster(t *testing.T) {
|
|||
"my-cluster": {Server: "https://192.168.0.1:3434"},
|
||||
},
|
||||
Contexts: map[string]*clientcmdapi.Context{
|
||||
"minikube": {AuthInfo: "minikube", Cluster: "minikube"},
|
||||
"my-cluser": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
|
||||
"minikube": {AuthInfo: "minikube", Cluster: "minikube"},
|
||||
"my-cluster": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
|
||||
},
|
||||
CurrentContext: "minikube",
|
||||
AuthInfos: map[string]*clientcmdapi.AuthInfo{
|
||||
"minikube": {Token: "minikube-token"},
|
||||
"my-cluser": {Token: "minikube-token"},
|
||||
"minikube": {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) {
|
||||
|
@ -95,20 +98,27 @@ func TestViewClusterMinify(t *testing.T) {
|
|||
"my-cluster": {Server: "https://192.168.0.1:3434"},
|
||||
},
|
||||
Contexts: map[string]*clientcmdapi.Context{
|
||||
"minikube": {AuthInfo: "minikube", Cluster: "minikube"},
|
||||
"my-cluser": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
|
||||
"minikube": {AuthInfo: "minikube", Cluster: "minikube"},
|
||||
"my-cluster": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
|
||||
},
|
||||
CurrentContext: "minikube",
|
||||
AuthInfos: map[string]*clientcmdapi.AuthInfo{
|
||||
"minikube": {Token: "minikube-token"},
|
||||
"my-cluser": {Token: "minikube-token"},
|
||||
"minikube": {Token: "minikube-token"},
|
||||
"mu-cluster": {Token: "minikube-token"},
|
||||
},
|
||||
}
|
||||
test := viewClusterTest{
|
||||
description: "Testing for kubectl config view --minify=true",
|
||||
config: conf,
|
||||
flags: []string{"--minify=true"},
|
||||
expected: `apiVersion: v1
|
||||
|
||||
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"},
|
||||
expected: `apiVersion: v1
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://192.168.99.100:8443
|
||||
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue