Skip to content

Commit cd4ee6b

Browse files
authored
Merge pull request kubernetes#89840 from eddiezane/ez/kubectl-396
Add get-users and delete-user to kubectl config
2 parents 062fe2e + 95b189f commit cd4ee6b

File tree

8 files changed

+515
-3
lines changed

8 files changed

+515
-3
lines changed

build/visible_to/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ package_group(
245245
"//staging/src/k8s.io/kubectl/pkg/cmd/attach",
246246
"//staging/src/k8s.io/kubectl/pkg/cmd/certificates",
247247
"//staging/src/k8s.io/kubectl/pkg/cmd/clusterinfo",
248+
"//staging/src/k8s.io/kubectl/pkg/cmd/config",
248249
"//staging/src/k8s.io/kubectl/pkg/cmd/cp",
249250
"//staging/src/k8s.io/kubectl/pkg/cmd/create",
250251
"//staging/src/k8s.io/kubectl/pkg/cmd/delete",

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ go_library(
1414
"current_context.go",
1515
"delete_cluster.go",
1616
"delete_context.go",
17+
"delete_user.go",
1718
"get_clusters.go",
1819
"get_contexts.go",
20+
"get_users.go",
1921
"navigation_step_parser.go",
2022
"rename_context.go",
2123
"set.go",
@@ -25,9 +27,7 @@ go_library(
2527
],
2628
importmap = "k8s.io/kubernetes/vendor/k8s.io/kubectl/pkg/cmd/config",
2729
importpath = "k8s.io/kubectl/pkg/cmd/config",
28-
visibility = [
29-
"//build/visible_to:pkg_kubectl_cmd_config_CONSUMERS",
30-
],
30+
visibility = ["//visibility:public"],
3131
deps = [
3232
"//staging/src/k8s.io/apimachinery/pkg/util/errors:go_default_library",
3333
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
@@ -56,8 +56,10 @@ go_test(
5656
"current_context_test.go",
5757
"delete_cluster_test.go",
5858
"delete_context_test.go",
59+
"delete_user_test.go",
5960
"get_clusters_test.go",
6061
"get_contexts_test.go",
62+
"get_users_test.go",
6163
"navigation_step_parser_test.go",
6264
"rename_context_test.go",
6365
"set_test.go",
@@ -73,6 +75,7 @@ go_test(
7375
"//staging/src/k8s.io/client-go/tools/clientcmd:go_default_library",
7476
"//staging/src/k8s.io/client-go/tools/clientcmd/api:go_default_library",
7577
"//staging/src/k8s.io/component-base/cli/flag:go_default_library",
78+
"//staging/src/k8s.io/kubectl/pkg/cmd/testing:go_default_library",
7679
"//staging/src/k8s.io/kubectl/pkg/cmd/util:go_default_library",
7780
],
7881
)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ func NewCmdConfig(f cmdutil.Factory, pathOptions *clientcmd.PathOptions, streams
6565
cmd.AddCommand(NewCmdConfigUseContext(streams.Out, pathOptions))
6666
cmd.AddCommand(NewCmdConfigGetContexts(streams, pathOptions))
6767
cmd.AddCommand(NewCmdConfigGetClusters(streams.Out, pathOptions))
68+
cmd.AddCommand(NewCmdConfigGetUsers(streams, pathOptions))
6869
cmd.AddCommand(NewCmdConfigDeleteCluster(streams.Out, pathOptions))
6970
cmd.AddCommand(NewCmdConfigDeleteContext(streams.Out, streams.ErrOut, pathOptions))
71+
cmd.AddCommand(NewCmdConfigDeleteUser(streams, pathOptions))
7072
cmd.AddCommand(NewCmdConfigRenameContext(streams.Out, pathOptions))
7173

7274
return cmd
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package config
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/spf13/cobra"
23+
24+
"k8s.io/cli-runtime/pkg/genericclioptions"
25+
"k8s.io/client-go/tools/clientcmd"
26+
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
27+
cmdutil "k8s.io/kubectl/pkg/cmd/util"
28+
"k8s.io/kubectl/pkg/util/i18n"
29+
"k8s.io/kubectl/pkg/util/templates"
30+
)
31+
32+
var (
33+
deleteUserExample = templates.Examples(`
34+
# Delete the minikube user
35+
kubectl config delete-user minikube`)
36+
)
37+
38+
// DeleteUserOptions holds the data needed to run the command
39+
type DeleteUserOptions struct {
40+
user string
41+
42+
configAccess clientcmd.ConfigAccess
43+
config *clientcmdapi.Config
44+
configFile string
45+
46+
genericclioptions.IOStreams
47+
}
48+
49+
// NewDeleteUserOptions creates the options for the command
50+
func NewDeleteUserOptions(ioStreams genericclioptions.IOStreams, configAccess clientcmd.ConfigAccess) *DeleteUserOptions {
51+
return &DeleteUserOptions{
52+
configAccess: configAccess,
53+
IOStreams: ioStreams,
54+
}
55+
}
56+
57+
// NewCmdConfigDeleteUser returns a Command instance for 'config delete-user' sub command
58+
func NewCmdConfigDeleteUser(streams genericclioptions.IOStreams, configAccess clientcmd.ConfigAccess) *cobra.Command {
59+
o := NewDeleteUserOptions(streams, configAccess)
60+
61+
cmd := &cobra.Command{
62+
Use: "delete-user NAME",
63+
DisableFlagsInUseLine: true,
64+
Short: i18n.T("Delete the specified user from the kubeconfig"),
65+
Long: "Delete the specified user from the kubeconfig",
66+
Example: deleteUserExample,
67+
Run: func(cmd *cobra.Command, args []string) {
68+
cmdutil.CheckErr(o.Complete(cmd, args))
69+
cmdutil.CheckErr(o.Validate())
70+
cmdutil.CheckErr(o.Run())
71+
},
72+
}
73+
74+
return cmd
75+
}
76+
77+
// Complete sets up the command to run
78+
func (o *DeleteUserOptions) Complete(cmd *cobra.Command, args []string) error {
79+
config, err := o.configAccess.GetStartingConfig()
80+
if err != nil {
81+
return err
82+
}
83+
o.config = config
84+
85+
if len(args) != 1 {
86+
return cmdutil.UsageErrorf(cmd, "user to delete is required")
87+
}
88+
o.user = args[0]
89+
90+
configFile := o.configAccess.GetDefaultFilename()
91+
if o.configAccess.IsExplicitFile() {
92+
configFile = o.configAccess.GetExplicitFile()
93+
}
94+
o.configFile = configFile
95+
96+
return nil
97+
}
98+
99+
// Validate ensures the command has enough info to run
100+
func (o *DeleteUserOptions) Validate() error {
101+
_, ok := o.config.AuthInfos[o.user]
102+
if !ok {
103+
return fmt.Errorf("cannot delete user %s, not in %s", o.user, o.configFile)
104+
}
105+
106+
return nil
107+
}
108+
109+
// Run performs the command
110+
func (o *DeleteUserOptions) Run() error {
111+
delete(o.config.AuthInfos, o.user)
112+
113+
if err := clientcmd.ModifyConfig(o.configAccess, *o.config, true); err != nil {
114+
return err
115+
}
116+
117+
fmt.Fprintf(o.Out, "deleted user %s from %s\n", o.user, o.configFile)
118+
119+
return nil
120+
}
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package config
18+
19+
import (
20+
"reflect"
21+
"strings"
22+
"testing"
23+
24+
"k8s.io/cli-runtime/pkg/genericclioptions"
25+
"k8s.io/client-go/tools/clientcmd"
26+
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
27+
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
28+
)
29+
30+
func TestDeleteUserComplete(t *testing.T) {
31+
var tests = []struct {
32+
name string
33+
args []string
34+
err string
35+
}{
36+
{
37+
name: "no args",
38+
args: []string{},
39+
err: "user to delete is required",
40+
},
41+
{
42+
name: "user provided",
43+
args: []string{"minikube"},
44+
err: "",
45+
},
46+
}
47+
48+
for i := range tests {
49+
test := tests[i]
50+
t.Run(test.name, func(t *testing.T) {
51+
tf := cmdtesting.NewTestFactory()
52+
defer tf.Cleanup()
53+
54+
ioStreams, _, out, _ := genericclioptions.NewTestIOStreams()
55+
pathOptions, err := tf.PathOptionsWithConfig(clientcmdapi.Config{})
56+
if err != nil {
57+
t.Fatalf("unexpected error executing command: %v", err)
58+
}
59+
60+
cmd := NewCmdConfigDeleteUser(ioStreams, pathOptions)
61+
cmd.SetOut(out)
62+
options := NewDeleteUserOptions(ioStreams, pathOptions)
63+
64+
if err := options.Complete(cmd, test.args); err != nil {
65+
if test.err == "" {
66+
t.Fatalf("unexpected error executing command: %v", err)
67+
}
68+
69+
if !strings.Contains(err.Error(), test.err) {
70+
t.Fatalf("expected error to contain %v, got %v", test.err, err.Error())
71+
}
72+
73+
return
74+
}
75+
76+
if options.configFile != pathOptions.GlobalFile {
77+
t.Fatalf("expected configFile to be %v, got %v", pathOptions.GlobalFile, options.configFile)
78+
}
79+
})
80+
}
81+
}
82+
83+
func TestDeleteUserValidate(t *testing.T) {
84+
var tests = []struct {
85+
name string
86+
user string
87+
config clientcmdapi.Config
88+
err string
89+
}{
90+
{
91+
name: "user not in config",
92+
user: "kube",
93+
config: clientcmdapi.Config{
94+
AuthInfos: map[string]*clientcmdapi.AuthInfo{
95+
"minikube": {Username: "minikube"},
96+
},
97+
},
98+
err: "cannot delete user kube",
99+
},
100+
{
101+
name: "user in config",
102+
user: "kube",
103+
config: clientcmdapi.Config{
104+
AuthInfos: map[string]*clientcmdapi.AuthInfo{
105+
"minikube": {Username: "minikube"},
106+
"kube": {Username: "kube"},
107+
},
108+
},
109+
err: "",
110+
},
111+
}
112+
113+
for i := range tests {
114+
test := tests[i]
115+
t.Run(test.name, func(t *testing.T) {
116+
tf := cmdtesting.NewTestFactory()
117+
defer tf.Cleanup()
118+
119+
ioStreams, _, _, _ := genericclioptions.NewTestIOStreams()
120+
pathOptions, err := tf.PathOptionsWithConfig(test.config)
121+
if err != nil {
122+
t.Fatalf("unexpected error executing command: %v", err)
123+
}
124+
125+
options := NewDeleteUserOptions(ioStreams, pathOptions)
126+
options.config = &test.config
127+
options.user = test.user
128+
129+
if err := options.Validate(); err != nil {
130+
if !strings.Contains(err.Error(), test.err) {
131+
t.Fatalf("expected: %s but got %s", test.err, err.Error())
132+
}
133+
134+
return
135+
}
136+
})
137+
}
138+
}
139+
140+
func TestDeleteUserRun(t *testing.T) {
141+
var tests = []struct {
142+
name string
143+
user string
144+
config clientcmdapi.Config
145+
expectedUsers []string
146+
out string
147+
}{
148+
{
149+
name: "delete user",
150+
user: "kube",
151+
config: clientcmdapi.Config{
152+
AuthInfos: map[string]*clientcmdapi.AuthInfo{
153+
"minikube": {Username: "minikube"},
154+
"kube": {Username: "kube"},
155+
},
156+
},
157+
expectedUsers: []string{"minikube"},
158+
out: "deleted user kube from",
159+
},
160+
}
161+
162+
for i := range tests {
163+
test := tests[i]
164+
t.Run(test.name, func(t *testing.T) {
165+
tf := cmdtesting.NewTestFactory()
166+
defer tf.Cleanup()
167+
168+
ioStreams, _, out, _ := genericclioptions.NewTestIOStreams()
169+
pathOptions, err := tf.PathOptionsWithConfig(test.config)
170+
if err != nil {
171+
t.Fatalf("unexpected error executing command: %v", err)
172+
}
173+
174+
options := NewDeleteUserOptions(ioStreams, pathOptions)
175+
options.config = &test.config
176+
options.configFile = pathOptions.GlobalFile
177+
options.user = test.user
178+
179+
if err := options.Run(); err != nil {
180+
t.Fatalf("unexpected error executing command: %v", err)
181+
}
182+
183+
if got := out.String(); !strings.Contains(got, test.out) {
184+
t.Fatalf("expected: %s but got %s", test.out, got)
185+
}
186+
187+
config, err := clientcmd.LoadFromFile(options.configFile)
188+
if err != nil {
189+
t.Fatalf("unexpected error executing command: %v", err)
190+
}
191+
192+
users := make([]string, 0, len(config.AuthInfos))
193+
for user := range config.AuthInfos {
194+
users = append(users, user)
195+
}
196+
197+
if !reflect.DeepEqual(test.expectedUsers, users) {
198+
t.Fatalf("expected %v, got %v", test.expectedUsers, users)
199+
}
200+
})
201+
}
202+
}

0 commit comments

Comments
 (0)