Merge pull request #107978 from brianpursley/config-cleanup

kubectl config subcommand cleanup: set-cluster, set-context, set-credentials
This commit is contained in:
Kubernetes Prow Robot 2022-03-24 02:58:54 -07:00 committed by GitHub
commit 8bd198260e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 85 additions and 81 deletions

View File

@ -57,7 +57,7 @@ func NewCmdConfig(pathOptions *clientcmd.PathOptions, streams genericclioptions.
// TODO(juanvallejo): update all subcommands to work with genericclioptions.IOStreams // TODO(juanvallejo): update all subcommands to work with genericclioptions.IOStreams
cmd.AddCommand(NewCmdConfigView(streams, pathOptions)) cmd.AddCommand(NewCmdConfigView(streams, pathOptions))
cmd.AddCommand(NewCmdConfigSetCluster(streams.Out, pathOptions)) cmd.AddCommand(NewCmdConfigSetCluster(streams.Out, pathOptions))
cmd.AddCommand(NewCmdConfigSetAuthInfo(streams.Out, pathOptions)) cmd.AddCommand(NewCmdConfigSetCredentials(streams.Out, pathOptions))
cmd.AddCommand(NewCmdConfigSetContext(streams.Out, pathOptions)) cmd.AddCommand(NewCmdConfigSetContext(streams.Out, pathOptions))
cmd.AddCommand(NewCmdConfigSet(streams.Out, pathOptions)) cmd.AddCommand(NewCmdConfigSet(streams.Out, pathOptions))
cmd.AddCommand(NewCmdConfigUnset(streams.Out, pathOptions)) cmd.AddCommand(NewCmdConfigUnset(streams.Out, pathOptions))

View File

@ -33,7 +33,7 @@ import (
"k8s.io/kubectl/pkg/util/templates" "k8s.io/kubectl/pkg/util/templates"
) )
type createClusterOptions struct { type setClusterOptions struct {
configAccess clientcmd.ConfigAccess configAccess clientcmd.ConfigAccess
name string name string
server cliflag.StringFlag server cliflag.StringFlag
@ -45,19 +45,19 @@ type createClusterOptions struct {
} }
var ( var (
createClusterLong = templates.LongDesc(i18n.T(` setClusterLong = templates.LongDesc(i18n.T(`
Set a cluster entry in kubeconfig. Set a cluster entry in kubeconfig.
Specifying a name that already exists will merge new fields on top of existing values for those fields.`)) Specifying a name that already exists will merge new fields on top of existing values for those fields.`))
createClusterExample = templates.Examples(` setClusterExample = templates.Examples(`
# Set only the server field on the e2e cluster entry without touching other values # Set only the server field on the e2e cluster entry without touching other values
kubectl config set-cluster e2e --server=https://1.2.3.4 kubectl config set-cluster e2e --server=https://1.2.3.4
# Embed certificate authority data for the e2e cluster entry # Embed certificate authority data for the e2e cluster entry
kubectl config set-cluster e2e --embed-certs --certificate-authority=~/.kube/e2e/kubernetes.ca.crt kubectl config set-cluster e2e --embed-certs --certificate-authority=~/.kube/e2e/kubernetes.ca.crt
# Disable cert checking for the dev cluster entry # Disable cert checking for the e2e cluster entry
kubectl config set-cluster e2e --insecure-skip-tls-verify=true kubectl config set-cluster e2e --insecure-skip-tls-verify=true
# Set custom TLS server name to use for validation for the e2e cluster entry # Set custom TLS server name to use for validation for the e2e cluster entry
@ -69,14 +69,14 @@ var (
// NewCmdConfigSetCluster returns a Command instance for 'config set-cluster' sub command // NewCmdConfigSetCluster returns a Command instance for 'config set-cluster' sub command
func NewCmdConfigSetCluster(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command { func NewCmdConfigSetCluster(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
options := &createClusterOptions{configAccess: configAccess} options := &setClusterOptions{configAccess: configAccess}
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: fmt.Sprintf("set-cluster NAME [--%v=server] [--%v=path/to/certificate/authority] [--%v=true] [--%v=example.com]", clientcmd.FlagAPIServer, clientcmd.FlagCAFile, clientcmd.FlagInsecure, clientcmd.FlagTLSServerName), Use: fmt.Sprintf("set-cluster NAME [--%v=server] [--%v=path/to/certificate/authority] [--%v=true] [--%v=example.com]", clientcmd.FlagAPIServer, clientcmd.FlagCAFile, clientcmd.FlagInsecure, clientcmd.FlagTLSServerName),
DisableFlagsInUseLine: true, DisableFlagsInUseLine: true,
Short: i18n.T("Set a cluster entry in kubeconfig"), Short: i18n.T("Set a cluster entry in kubeconfig"),
Long: createClusterLong, Long: setClusterLong,
Example: createClusterExample, Example: setClusterExample,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(options.complete(cmd)) cmdutil.CheckErr(options.complete(cmd))
cmdutil.CheckErr(options.run()) cmdutil.CheckErr(options.run())
@ -99,7 +99,7 @@ func NewCmdConfigSetCluster(out io.Writer, configAccess clientcmd.ConfigAccess)
return cmd return cmd
} }
func (o createClusterOptions) run() error { func (o setClusterOptions) run() error {
err := o.validate() err := o.validate()
if err != nil { if err != nil {
return err return err
@ -124,8 +124,7 @@ func (o createClusterOptions) run() error {
return nil return nil
} }
// cluster builds a Cluster object from the options func (o *setClusterOptions) modifyCluster(existingCluster clientcmdapi.Cluster) clientcmdapi.Cluster {
func (o *createClusterOptions) modifyCluster(existingCluster clientcmdapi.Cluster) clientcmdapi.Cluster {
modifiedCluster := existingCluster modifiedCluster := existingCluster
if o.server.Provided() { if o.server.Provided() {
@ -169,7 +168,7 @@ func (o *createClusterOptions) modifyCluster(existingCluster clientcmdapi.Cluste
return modifiedCluster return modifiedCluster
} }
func (o *createClusterOptions) complete(cmd *cobra.Command) error { func (o *setClusterOptions) complete(cmd *cobra.Command) error {
args := cmd.Flags().Args() args := cmd.Flags().Args()
if len(args) != 1 { if len(args) != 1 {
return helpErrorf(cmd, "Unexpected args: %v", args) return helpErrorf(cmd, "Unexpected args: %v", args)
@ -179,7 +178,7 @@ func (o *createClusterOptions) complete(cmd *cobra.Command) error {
return nil return nil
} }
func (o createClusterOptions) validate() error { func (o setClusterOptions) validate() error {
if len(o.name) == 0 { if len(o.name) == 0 {
return errors.New("you must specify a non-empty cluster name") return errors.New("you must specify a non-empty cluster name")
} }

View File

@ -26,7 +26,7 @@ import (
clientcmdapi "k8s.io/client-go/tools/clientcmd/api" clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
) )
type createClusterTest struct { type setClusterTest struct {
description string description string
config clientcmdapi.Config config clientcmdapi.Config
args []string args []string
@ -37,7 +37,7 @@ type createClusterTest struct {
func TestCreateCluster(t *testing.T) { func TestCreateCluster(t *testing.T) {
conf := clientcmdapi.Config{} conf := clientcmdapi.Config{}
test := createClusterTest{ test := setClusterTest{
description: "Testing 'kubectl config set-cluster' with a new cluster", description: "Testing 'kubectl config set-cluster' with a new cluster",
config: conf, config: conf,
args: []string{"my-cluster"}, args: []string{"my-cluster"},
@ -57,7 +57,7 @@ func TestCreateCluster(t *testing.T) {
func TestCreateClusterWithProxy(t *testing.T) { func TestCreateClusterWithProxy(t *testing.T) {
conf := clientcmdapi.Config{} conf := clientcmdapi.Config{}
test := createClusterTest{ test := setClusterTest{
description: "Testing 'kubectl config set-cluster' with a new cluster", description: "Testing 'kubectl config set-cluster' with a new cluster",
config: conf, config: conf,
args: []string{"my-cluster"}, args: []string{"my-cluster"},
@ -86,7 +86,7 @@ func TestModifyCluster(t *testing.T) {
"my-cluster": {Server: "https://192.168.0.1", TLSServerName: "to-be-cleared"}, "my-cluster": {Server: "https://192.168.0.1", TLSServerName: "to-be-cleared"},
}, },
} }
test := createClusterTest{ test := setClusterTest{
description: "Testing 'kubectl config set-cluster' with an existing cluster", description: "Testing 'kubectl config set-cluster' with an existing cluster",
config: conf, config: conf,
args: []string{"my-cluster"}, args: []string{"my-cluster"},
@ -110,7 +110,7 @@ func TestModifyClusterWithProxy(t *testing.T) {
"my-cluster": {Server: "https://192.168.0.1", TLSServerName: "to-be-cleared"}, "my-cluster": {Server: "https://192.168.0.1", TLSServerName: "to-be-cleared"},
}, },
} }
test := createClusterTest{ test := setClusterTest{
description: "Testing 'kubectl config set-cluster' with an existing cluster", description: "Testing 'kubectl config set-cluster' with an existing cluster",
config: conf, config: conf,
args: []string{"my-cluster"}, args: []string{"my-cluster"},
@ -140,7 +140,7 @@ func TestModifyClusterWithProxyOverride(t *testing.T) {
}, },
}, },
} }
test := createClusterTest{ test := setClusterTest{
description: "Testing 'kubectl config set-cluster' with an existing cluster", description: "Testing 'kubectl config set-cluster' with an existing cluster",
config: conf, config: conf,
args: []string{"my-cluster"}, args: []string{"my-cluster"},
@ -164,7 +164,7 @@ func TestModifyClusterServerAndTLS(t *testing.T) {
"my-cluster": {Server: "https://192.168.0.1"}, "my-cluster": {Server: "https://192.168.0.1"},
}, },
} }
test := createClusterTest{ test := setClusterTest{
description: "Testing 'kubectl config set-cluster' with an existing cluster", description: "Testing 'kubectl config set-cluster' with an existing cluster",
config: conf, config: conf,
args: []string{"my-cluster"}, args: []string{"my-cluster"},
@ -182,7 +182,7 @@ func TestModifyClusterServerAndTLS(t *testing.T) {
test.run(t) test.run(t)
} }
func (test createClusterTest) run(t *testing.T) { func (test setClusterTest) run(t *testing.T) {
fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "") fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "")
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)

View File

@ -32,7 +32,7 @@ import (
"k8s.io/kubectl/pkg/util/templates" "k8s.io/kubectl/pkg/util/templates"
) )
type createContextOptions struct { type setContextOptions struct {
configAccess clientcmd.ConfigAccess configAccess clientcmd.ConfigAccess
name string name string
currContext bool currContext bool
@ -42,26 +42,26 @@ type createContextOptions struct {
} }
var ( var (
createContextLong = templates.LongDesc(i18n.T(` setContextLong = templates.LongDesc(i18n.T(`
Set a context entry in kubeconfig. Set a context entry in kubeconfig.
Specifying a name that already exists will merge new fields on top of existing values for those fields.`)) Specifying a name that already exists will merge new fields on top of existing values for those fields.`))
createContextExample = templates.Examples(` setContextExample = templates.Examples(`
# Set the user field on the gce context entry without touching other values # Set the user field on the gce context entry without touching other values
kubectl config set-context gce --user=cluster-admin`) kubectl config set-context gce --user=cluster-admin`)
) )
// NewCmdConfigSetContext returns a Command instance for 'config set-context' sub command // NewCmdConfigSetContext returns a Command instance for 'config set-context' sub command
func NewCmdConfigSetContext(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command { func NewCmdConfigSetContext(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
options := &createContextOptions{configAccess: configAccess} options := &setContextOptions{configAccess: configAccess}
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: fmt.Sprintf("set-context [NAME | --current] [--%v=cluster_nickname] [--%v=user_nickname] [--%v=namespace]", clientcmd.FlagClusterName, clientcmd.FlagAuthInfoName, clientcmd.FlagNamespace), Use: fmt.Sprintf("set-context [NAME | --current] [--%v=cluster_nickname] [--%v=user_nickname] [--%v=namespace]", clientcmd.FlagClusterName, clientcmd.FlagAuthInfoName, clientcmd.FlagNamespace),
DisableFlagsInUseLine: true, DisableFlagsInUseLine: true,
Short: i18n.T("Set a context entry in kubeconfig"), Short: i18n.T("Set a context entry in kubeconfig"),
Long: createContextLong, Long: setContextLong,
Example: createContextExample, Example: setContextExample,
ValidArgsFunction: util.ContextCompletionFunc, ValidArgsFunction: util.ContextCompletionFunc,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(options.complete(cmd)) cmdutil.CheckErr(options.complete(cmd))
@ -83,7 +83,7 @@ func NewCmdConfigSetContext(out io.Writer, configAccess clientcmd.ConfigAccess)
return cmd return cmd
} }
func (o createContextOptions) run() (string, bool, error) { func (o setContextOptions) run() (string, bool, error) {
err := o.validate() err := o.validate()
if err != nil { if err != nil {
return "", false, err return "", false, err
@ -116,7 +116,7 @@ func (o createContextOptions) run() (string, bool, error) {
return name, exists, nil return name, exists, nil
} }
func (o *createContextOptions) modifyContext(existingContext clientcmdapi.Context) clientcmdapi.Context { func (o *setContextOptions) modifyContext(existingContext clientcmdapi.Context) clientcmdapi.Context {
modifiedContext := existingContext modifiedContext := existingContext
if o.cluster.Provided() { if o.cluster.Provided() {
@ -132,7 +132,7 @@ func (o *createContextOptions) modifyContext(existingContext clientcmdapi.Contex
return modifiedContext return modifiedContext
} }
func (o *createContextOptions) complete(cmd *cobra.Command) error { func (o *setContextOptions) complete(cmd *cobra.Command) error {
args := cmd.Flags().Args() args := cmd.Flags().Args()
if len(args) > 1 { if len(args) > 1 {
return helpErrorf(cmd, "Unexpected args: %v", args) return helpErrorf(cmd, "Unexpected args: %v", args)
@ -143,7 +143,7 @@ func (o *createContextOptions) complete(cmd *cobra.Command) error {
return nil return nil
} }
func (o createContextOptions) validate() error { func (o setContextOptions) validate() error {
if len(o.name) == 0 && !o.currContext { if len(o.name) == 0 && !o.currContext {
return errors.New("you must specify a non-empty context name or --current") return errors.New("you must specify a non-empty context name or --current")
} }

View File

@ -26,7 +26,7 @@ import (
clientcmdapi "k8s.io/client-go/tools/clientcmd/api" clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
) )
type createContextTest struct { type setContextTest struct {
description string description string
testContext string // name of the context being modified testContext string // name of the context being modified
config clientcmdapi.Config //initiate kubectl config config clientcmdapi.Config //initiate kubectl config
@ -38,7 +38,7 @@ type createContextTest struct {
func TestCreateContext(t *testing.T) { func TestCreateContext(t *testing.T) {
conf := clientcmdapi.Config{} conf := clientcmdapi.Config{}
test := createContextTest{ test := setContextTest{
testContext: "shaker-context", testContext: "shaker-context",
description: "Testing for create a new context", description: "Testing for create a new context",
config: conf, config: conf,
@ -61,7 +61,7 @@ func TestModifyContext(t *testing.T) {
Contexts: map[string]*clientcmdapi.Context{ Contexts: map[string]*clientcmdapi.Context{
"shaker-context": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"}, "shaker-context": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"},
"not-this": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"}}} "not-this": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"}}}
test := createContextTest{ test := setContextTest{
testContext: "shaker-context", testContext: "shaker-context",
description: "Testing for modify a already exist context", description: "Testing for modify a already exist context",
config: conf, config: conf,
@ -86,7 +86,7 @@ func TestModifyCurrentContext(t *testing.T) {
Contexts: map[string]*clientcmdapi.Context{ Contexts: map[string]*clientcmdapi.Context{
"shaker-context": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"}, "shaker-context": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"},
"not-this": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"}}} "not-this": {AuthInfo: "blue-user", Cluster: "big-cluster", Namespace: "saw-ns"}}}
test := createContextTest{ test := setContextTest{
testContext: "shaker-context", testContext: "shaker-context",
description: "Testing for modify a current context", description: "Testing for modify a current context",
config: conf, config: conf,
@ -106,7 +106,7 @@ func TestModifyCurrentContext(t *testing.T) {
test.run(t) test.run(t)
} }
func (test createContextTest) run(t *testing.T) { func (test setContextTest) run(t *testing.T) {
fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "") fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "")
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)

View File

@ -35,7 +35,7 @@ import (
"k8s.io/kubectl/pkg/util/templates" "k8s.io/kubectl/pkg/util/templates"
) )
type createAuthInfoOptions struct { type setCredentialsOptions struct {
configAccess clientcmd.ConfigAccess configAccess clientcmd.ConfigAccess
name string name string
clientCertificate cliflag.StringFlag clientCertificate cliflag.StringFlag
@ -67,7 +67,7 @@ const (
) )
var ( var (
createAuthInfoLong = fmt.Sprintf(templates.LongDesc(i18n.T(` setCredentialsLong = fmt.Sprintf(templates.LongDesc(i18n.T(`
Set a user entry in kubeconfig. Set a user entry in kubeconfig.
Specifying a name that already exists will merge new fields on top of existing values. Specifying a name that already exists will merge new fields on top of existing values.
@ -83,7 +83,7 @@ var (
Bearer token and basic auth are mutually exclusive.`)), clientcmd.FlagCertFile, clientcmd.FlagKeyFile, clientcmd.FlagBearerToken, clientcmd.FlagUsername, clientcmd.FlagPassword) Bearer token and basic auth are mutually exclusive.`)), clientcmd.FlagCertFile, clientcmd.FlagKeyFile, clientcmd.FlagBearerToken, clientcmd.FlagUsername, clientcmd.FlagPassword)
createAuthInfoExample = templates.Examples(` setCredentialsExample = templates.Examples(`
# Set only the "client-key" field on the "cluster-admin" # Set only the "client-key" field on the "cluster-admin"
# entry, without touching other values # entry, without touching other values
kubectl config set-credentials cluster-admin --client-key=~/.kube/admin.key kubectl config set-credentials cluster-admin --client-key=~/.kube/admin.key
@ -116,13 +116,19 @@ var (
kubectl config set-credentials cluster-admin --exec-env=var-to-remove-`) kubectl config set-credentials cluster-admin --exec-env=var-to-remove-`)
) )
// NewCmdConfigSetAuthInfo returns an Command option instance for 'config set-credentials' sub command // NewCmdConfigSetCredentials returns a Command instance for 'config set-credentials' sub command
func NewCmdConfigSetAuthInfo(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command { func NewCmdConfigSetCredentials(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
options := &createAuthInfoOptions{configAccess: configAccess} options := &setCredentialsOptions{configAccess: configAccess}
return newCmdConfigSetAuthInfo(out, options) return newCmdConfigSetCredentials(out, options)
} }
func newCmdConfigSetAuthInfo(out io.Writer, options *createAuthInfoOptions) *cobra.Command { // NewCmdConfigSetAuthInfo returns a Command instance for 'config set-credentials' sub command
// DEPRECATED: Use NewCmdConfigSetCredentials instead
func NewCmdConfigSetAuthInfo(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
return NewCmdConfigSetCredentials(out, configAccess)
}
func newCmdConfigSetCredentials(out io.Writer, options *setCredentialsOptions) *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: fmt.Sprintf( Use: fmt.Sprintf(
"set-credentials NAME [--%v=path/to/certfile] "+ "set-credentials NAME [--%v=path/to/certfile] "+
@ -150,10 +156,10 @@ func newCmdConfigSetAuthInfo(out io.Writer, options *createAuthInfoOptions) *cob
), ),
DisableFlagsInUseLine: true, DisableFlagsInUseLine: true,
Short: i18n.T("Set a user entry in kubeconfig"), Short: i18n.T("Set a user entry in kubeconfig"),
Long: createAuthInfoLong, Long: setCredentialsLong,
Example: createAuthInfoExample, Example: setCredentialsExample,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
err := options.complete(cmd, out) err := options.complete(cmd)
if err != nil { if err != nil {
cmd.Help() cmd.Help()
cmdutil.CheckErr(err) cmdutil.CheckErr(err)
@ -182,7 +188,7 @@ func newCmdConfigSetAuthInfo(out io.Writer, options *createAuthInfoOptions) *cob
return cmd return cmd
} }
func (o createAuthInfoOptions) run() error { func (o setCredentialsOptions) run() error {
err := o.validate() err := o.validate()
if err != nil { if err != nil {
return err return err
@ -207,8 +213,7 @@ func (o createAuthInfoOptions) run() error {
return nil return nil
} }
// authInfo builds an AuthInfo object from the options func (o *setCredentialsOptions) modifyAuthInfo(existingAuthInfo clientcmdapi.AuthInfo) clientcmdapi.AuthInfo {
func (o *createAuthInfoOptions) modifyAuthInfo(existingAuthInfo clientcmdapi.AuthInfo) clientcmdapi.AuthInfo {
modifiedAuthInfo := existingAuthInfo modifiedAuthInfo := existingAuthInfo
var setToken, setBasic bool var setToken, setBasic bool
@ -356,21 +361,21 @@ func (o *createAuthInfoOptions) modifyAuthInfo(existingAuthInfo clientcmdapi.Aut
return modifiedAuthInfo return modifiedAuthInfo
} }
func (o *createAuthInfoOptions) complete(cmd *cobra.Command, out io.Writer) error { func (o *setCredentialsOptions) complete(cmd *cobra.Command) error {
args := cmd.Flags().Args() args := cmd.Flags().Args()
if len(args) != 1 { if len(args) != 1 {
return fmt.Errorf("Unexpected args: %v", args) return fmt.Errorf("unexpected args: %v", args)
} }
authProviderArgs, err := cmd.Flags().GetStringSlice(flagAuthProviderArg) authProviderArgs, err := cmd.Flags().GetStringSlice(flagAuthProviderArg)
if err != nil { if err != nil {
return fmt.Errorf("Error: %s", err) return err
} }
if len(authProviderArgs) > 0 { if len(authProviderArgs) > 0 {
newPairs, removePairs, err := cmdutil.ParsePairs(authProviderArgs, flagAuthProviderArg, true) newPairs, removePairs, err := cmdutil.ParsePairs(authProviderArgs, flagAuthProviderArg, true)
if err != nil { if err != nil {
return fmt.Errorf("Error: %s", err) return err
} }
o.authProviderArgs = newPairs o.authProviderArgs = newPairs
o.authProviderArgsToRemove = removePairs o.authProviderArgsToRemove = removePairs
@ -378,7 +383,7 @@ func (o *createAuthInfoOptions) complete(cmd *cobra.Command, out io.Writer) erro
execArgs, err := cmd.Flags().GetStringSlice(flagExecArg) execArgs, err := cmd.Flags().GetStringSlice(flagExecArg)
if err != nil { if err != nil {
return fmt.Errorf("Error: %s", err) return err
} }
if len(execArgs) > 0 { if len(execArgs) > 0 {
o.execArgs = execArgs o.execArgs = execArgs
@ -386,12 +391,12 @@ func (o *createAuthInfoOptions) complete(cmd *cobra.Command, out io.Writer) erro
execEnv, err := cmd.Flags().GetStringArray(flagExecEnv) execEnv, err := cmd.Flags().GetStringArray(flagExecEnv)
if err != nil { if err != nil {
return fmt.Errorf("Error: %s", err) return err
} }
if len(execEnv) > 0 { if len(execEnv) > 0 {
newPairs, removePairs, err := cmdutil.ParsePairs(execEnv, flagExecEnv, true) newPairs, removePairs, err := cmdutil.ParsePairs(execEnv, flagExecEnv, true)
if err != nil { if err != nil {
return fmt.Errorf("Error: %s", err) return err
} }
o.execEnv = newPairs o.execEnv = newPairs
o.execEnvToRemove = removePairs o.execEnvToRemove = removePairs
@ -401,7 +406,7 @@ func (o *createAuthInfoOptions) complete(cmd *cobra.Command, out io.Writer) erro
return nil return nil
} }
func (o createAuthInfoOptions) validate() error { func (o setCredentialsOptions) validate() error {
if len(o.name) == 0 { if len(o.name) == 0 {
return errors.New("you must specify a non-empty user name") return errors.New("you must specify a non-empty user name")
} }

View File

@ -34,7 +34,7 @@ func stringFlagFor(s string) cliflag.StringFlag {
return f return f
} }
func TestCreateAuthInfoOptions(t *testing.T) { func TestSetCredentialsOptions(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
flags []string flags []string
@ -42,14 +42,14 @@ func TestCreateAuthInfoOptions(t *testing.T) {
wantCompleteErr bool wantCompleteErr bool
wantValidateErr bool wantValidateErr bool
wantOptions *createAuthInfoOptions wantOptions *setCredentialsOptions
}{ }{
{ {
name: "test1", name: "test1",
flags: []string{ flags: []string{
"me", "me",
}, },
wantOptions: &createAuthInfoOptions{ wantOptions: &setCredentialsOptions{
name: "me", name: "me",
}, },
}, },
@ -59,7 +59,7 @@ func TestCreateAuthInfoOptions(t *testing.T) {
"me", "me",
"--token=foo", "--token=foo",
}, },
wantOptions: &createAuthInfoOptions{ wantOptions: &setCredentialsOptions{
name: "me", name: "me",
token: stringFlagFor("foo"), token: stringFlagFor("foo"),
}, },
@ -71,7 +71,7 @@ func TestCreateAuthInfoOptions(t *testing.T) {
"--username=jane", "--username=jane",
"--password=bar", "--password=bar",
}, },
wantOptions: &createAuthInfoOptions{ wantOptions: &setCredentialsOptions{
name: "me", name: "me",
username: stringFlagFor("jane"), username: stringFlagFor("jane"),
password: stringFlagFor("bar"), password: stringFlagFor("bar"),
@ -96,7 +96,7 @@ func TestCreateAuthInfoOptions(t *testing.T) {
"--auth-provider-arg=client-secret=bar", "--auth-provider-arg=client-secret=bar",
"me", "me",
}, },
wantOptions: &createAuthInfoOptions{ wantOptions: &setCredentialsOptions{
name: "me", name: "me",
authProvider: stringFlagFor("oidc"), authProvider: stringFlagFor("oidc"),
authProviderArgs: map[string]string{ authProviderArgs: map[string]string{
@ -114,7 +114,7 @@ func TestCreateAuthInfoOptions(t *testing.T) {
"--auth-provider-arg=client-secret-", "--auth-provider-arg=client-secret-",
"me", "me",
}, },
wantOptions: &createAuthInfoOptions{ wantOptions: &setCredentialsOptions{
name: "me", name: "me",
authProvider: stringFlagFor("oidc"), authProvider: stringFlagFor("oidc"),
authProviderArgs: map[string]string{}, authProviderArgs: map[string]string{},
@ -131,7 +131,7 @@ func TestCreateAuthInfoOptions(t *testing.T) {
"--auth-provider-arg=client-secret-", "--auth-provider-arg=client-secret-",
"me", "me",
}, },
wantOptions: &createAuthInfoOptions{ wantOptions: &setCredentialsOptions{
name: "me", name: "me",
authProviderArgs: map[string]string{}, authProviderArgs: map[string]string{},
authProviderArgsToRemove: []string{ authProviderArgsToRemove: []string{
@ -162,7 +162,7 @@ func TestCreateAuthInfoOptions(t *testing.T) {
"--exec-command=example-client-go-exec-plugin", "--exec-command=example-client-go-exec-plugin",
"me", "me",
}, },
wantOptions: &createAuthInfoOptions{ wantOptions: &setCredentialsOptions{
name: "me", name: "me",
execCommand: stringFlagFor("example-client-go-exec-plugin"), execCommand: stringFlagFor("example-client-go-exec-plugin"),
}, },
@ -175,7 +175,7 @@ func TestCreateAuthInfoOptions(t *testing.T) {
"--exec-arg=arg2", "--exec-arg=arg2",
"me", "me",
}, },
wantOptions: &createAuthInfoOptions{ wantOptions: &setCredentialsOptions{
name: "me", name: "me",
execCommand: stringFlagFor("example-client-go-exec-plugin"), execCommand: stringFlagFor("example-client-go-exec-plugin"),
execArgs: []string{"arg1", "arg2"}, execArgs: []string{"arg1", "arg2"},
@ -191,7 +191,7 @@ func TestCreateAuthInfoOptions(t *testing.T) {
"--exec-env=env-remove2-", "--exec-env=env-remove2-",
"me", "me",
}, },
wantOptions: &createAuthInfoOptions{ wantOptions: &setCredentialsOptions{
name: "me", name: "me",
execCommand: stringFlagFor("example-client-go-exec-plugin"), execCommand: stringFlagFor("example-client-go-exec-plugin"),
execEnv: map[string]string{"key1": "val1", "key2": "val2"}, execEnv: map[string]string{"key1": "val1", "key2": "val2"},
@ -204,8 +204,8 @@ func TestCreateAuthInfoOptions(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
buff := new(bytes.Buffer) buff := new(bytes.Buffer)
opts := new(createAuthInfoOptions) opts := new(setCredentialsOptions)
cmd := newCmdConfigSetAuthInfo(buff, opts) cmd := newCmdConfigSetCredentials(buff, opts)
if err := cmd.ParseFlags(tt.flags); err != nil { if err := cmd.ParseFlags(tt.flags); err != nil {
if !tt.wantParseErr { if !tt.wantParseErr {
t.Errorf("case %s: parsing error for flags %q: %v: %s", tt.name, tt.flags, err, buff) t.Errorf("case %s: parsing error for flags %q: %v: %s", tt.name, tt.flags, err, buff)
@ -217,7 +217,7 @@ func TestCreateAuthInfoOptions(t *testing.T) {
return return
} }
if err := opts.complete(cmd, buff); err != nil { if err := opts.complete(cmd); err != nil {
if !tt.wantCompleteErr { if !tt.wantCompleteErr {
t.Errorf("case %s: complete() error for flags %q: %s", tt.name, tt.flags, buff) t.Errorf("case %s: complete() error for flags %q: %s", tt.name, tt.flags, buff)
} }
@ -381,8 +381,8 @@ func TestModifyExistingAuthInfo(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
buff := new(bytes.Buffer) buff := new(bytes.Buffer)
opts := new(createAuthInfoOptions) opts := new(setCredentialsOptions)
cmd := newCmdConfigSetAuthInfo(buff, opts) cmd := newCmdConfigSetCredentials(buff, opts)
if err := cmd.ParseFlags(tt.flags); err != nil { if err := cmd.ParseFlags(tt.flags); err != nil {
if !tt.wantParseErr { if !tt.wantParseErr {
t.Errorf("case %s: parsing error for flags %q: %v: %s", tt.name, tt.flags, err, buff) t.Errorf("case %s: parsing error for flags %q: %v: %s", tt.name, tt.flags, err, buff)
@ -394,7 +394,7 @@ func TestModifyExistingAuthInfo(t *testing.T) {
return return
} }
if err := opts.complete(cmd, buff); err != nil { if err := opts.complete(cmd); err != nil {
if !tt.wantCompleteErr { if !tt.wantCompleteErr {
t.Errorf("case %s: complete() error for flags %q: %s", tt.name, tt.flags, buff) t.Errorf("case %s: complete() error for flags %q: %s", tt.name, tt.flags, buff)
} }
@ -426,7 +426,7 @@ func TestModifyExistingAuthInfo(t *testing.T) {
} }
} }
type createAuthInfoTest struct { type setCredentialsTest struct {
description string description string
config clientcmdapi.Config config clientcmdapi.Config
args []string args []string
@ -435,10 +435,10 @@ type createAuthInfoTest struct {
expectedConfig clientcmdapi.Config expectedConfig clientcmdapi.Config
} }
func TestCreateAuthInfo(t *testing.T) { func TestSetCredentials(t *testing.T) {
conf := clientcmdapi.Config{} conf := clientcmdapi.Config{}
test := createAuthInfoTest{ test := setCredentialsTest{
description: "Testing for create aythinfo", description: "Testing set credentials",
config: conf, config: conf,
args: []string{"cluster-admin"}, args: []string{"cluster-admin"},
flags: []string{ flags: []string{
@ -453,7 +453,7 @@ func TestCreateAuthInfo(t *testing.T) {
} }
test.run(t) test.run(t)
} }
func (test createAuthInfoTest) run(t *testing.T) { func (test setCredentialsTest) run(t *testing.T) {
fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "") fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "")
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
@ -468,7 +468,7 @@ func (test createAuthInfoTest) run(t *testing.T) {
pathOptions.GlobalFile = fakeKubeFile.Name() pathOptions.GlobalFile = fakeKubeFile.Name()
pathOptions.EnvVar = "" pathOptions.EnvVar = ""
buf := bytes.NewBuffer([]byte{}) buf := bytes.NewBuffer([]byte{})
cmd := NewCmdConfigSetAuthInfo(buf, pathOptions) cmd := NewCmdConfigSetCredentials(buf, pathOptions)
cmd.SetArgs(test.args) cmd.SetArgs(test.args)
cmd.Flags().Parse(test.flags) cmd.Flags().Parse(test.flags)
if err := cmd.Execute(); err != nil { if err := cmd.Execute(); err != nil {