Allow to define exec credential plugin config options from kubectl

This commit adds support of setting config options to the exec plugin
from cli.

Next options are added:
  * --exec-command new command for the exec credential plugin
  * --exec-api-version API version of the exec credential plugin.
  * --exec-arg new arguments for the exec credential plugin command
  * --exec-env add, update or remove environment values for the exec credential plugin
This commit is contained in:
Mike Fedosin
2019-01-23 23:48:26 +01:00
parent 7a8e11c16d
commit a556e4fcd1
2 changed files with 360 additions and 2 deletions

View File

@@ -156,6 +156,48 @@ func TestCreateAuthInfoOptions(t *testing.T) {
},
wantCompleteErr: true,
},
{
name: "test10",
flags: []string{
"--exec-command=example-client-go-exec-plugin",
"me",
},
wantOptions: &createAuthInfoOptions{
name: "me",
execCommand: stringFlagFor("example-client-go-exec-plugin"),
},
},
{
name: "test11",
flags: []string{
"--exec-command=example-client-go-exec-plugin",
"--exec-arg=arg1",
"--exec-arg=arg2",
"me",
},
wantOptions: &createAuthInfoOptions{
name: "me",
execCommand: stringFlagFor("example-client-go-exec-plugin"),
execArgs: []string{"arg1", "arg2"},
},
},
{
name: "test12",
flags: []string{
"--exec-command=example-client-go-exec-plugin",
"--exec-env=key1=val1",
"--exec-env=key2=val2",
"--exec-env=env-remove1-",
"--exec-env=env-remove2-",
"me",
},
wantOptions: &createAuthInfoOptions{
name: "me",
execCommand: stringFlagFor("example-client-go-exec-plugin"),
execEnv: map[string]string{"key1": "val1", "key2": "val2"},
execEnvToRemove: []string{"env-remove1", "env-remove2"},
},
},
}
for _, tt := range tests {
@@ -205,6 +247,185 @@ func TestCreateAuthInfoOptions(t *testing.T) {
}
}
func TestModifyExistingAuthInfo(t *testing.T) {
tests := []struct {
name string
flags []string
wantParseErr bool
wantCompleteErr bool
wantValidateErr bool
existingAuthInfo clientcmdapi.AuthInfo
wantAuthInfo clientcmdapi.AuthInfo
}{
{
name: "1. create new exec config",
flags: []string{
"--exec-command=example-client-go-exec-plugin",
"--exec-api-version=client.authentication.k8s.io/v1",
"me",
},
existingAuthInfo: clientcmdapi.AuthInfo{},
wantAuthInfo: clientcmdapi.AuthInfo{
Exec: &clientcmdapi.ExecConfig{
Command: "example-client-go-exec-plugin",
APIVersion: "client.authentication.k8s.io/v1",
},
},
},
{
name: "2. redefine exec args",
flags: []string{
"--exec-arg=new-arg1",
"--exec-arg=new-arg2",
"me",
},
existingAuthInfo: clientcmdapi.AuthInfo{
Exec: &clientcmdapi.ExecConfig{
Command: "example-client-go-exec-plugin",
APIVersion: "client.authentication.k8s.io/v1beta1",
Args: []string{"existing-arg1", "existing-arg2"},
},
},
wantAuthInfo: clientcmdapi.AuthInfo{
Exec: &clientcmdapi.ExecConfig{
Command: "example-client-go-exec-plugin",
APIVersion: "client.authentication.k8s.io/v1beta1",
Args: []string{"new-arg1", "new-arg2"},
},
},
},
{
name: "3. reset exec args",
flags: []string{
"--exec-command=example-client-go-exec-plugin",
"me",
},
existingAuthInfo: clientcmdapi.AuthInfo{
Exec: &clientcmdapi.ExecConfig{
Command: "example-client-go-exec-plugin",
APIVersion: "client.authentication.k8s.io/v1beta1",
Args: []string{"existing-arg1", "existing-arg2"},
},
},
wantAuthInfo: clientcmdapi.AuthInfo{
Exec: &clientcmdapi.ExecConfig{
Command: "example-client-go-exec-plugin",
APIVersion: "client.authentication.k8s.io/v1beta1",
},
},
},
{
name: "4. modify exec env variables",
flags: []string{
"--exec-command=example-client-go-exec-plugin",
"--exec-env=name1=value1000",
"--exec-env=name3=value3",
"--exec-env=name2-",
"--exec-env=non-existing-",
"me",
},
existingAuthInfo: clientcmdapi.AuthInfo{
Exec: &clientcmdapi.ExecConfig{
Command: "existing-command",
APIVersion: "client.authentication.k8s.io/v1beta1",
Env: []clientcmdapi.ExecEnvVar{
{Name: "name1", Value: "value1"},
{Name: "name2", Value: "value2"},
},
},
},
wantAuthInfo: clientcmdapi.AuthInfo{
Exec: &clientcmdapi.ExecConfig{
Command: "example-client-go-exec-plugin",
APIVersion: "client.authentication.k8s.io/v1beta1",
Env: []clientcmdapi.ExecEnvVar{
{Name: "name1", Value: "value1000"},
{Name: "name3", Value: "value3"},
},
},
},
},
{
name: "5. modify auth provider arguments",
flags: []string{
"--auth-provider=new-auth-provider",
"--auth-provider-arg=key1=val1000",
"--auth-provider-arg=key3=val3",
"--auth-provider-arg=key2-",
"--auth-provider-arg=non-existing-",
"me",
},
existingAuthInfo: clientcmdapi.AuthInfo{
AuthProvider: &clientcmdapi.AuthProviderConfig{
Name: "auth-provider",
Config: map[string]string{
"key1": "val1",
"key2": "val2",
},
},
},
wantAuthInfo: clientcmdapi.AuthInfo{
AuthProvider: &clientcmdapi.AuthProviderConfig{
Name: "new-auth-provider",
Config: map[string]string{
"key1": "val1000",
"key3": "val3",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buff := new(bytes.Buffer)
opts := new(createAuthInfoOptions)
cmd := newCmdConfigSetAuthInfo(buff, opts)
if err := cmd.ParseFlags(tt.flags); err != nil {
if !tt.wantParseErr {
t.Errorf("case %s: parsing error for flags %q: %v: %s", tt.name, tt.flags, err, buff)
}
return
}
if tt.wantParseErr {
t.Errorf("case %s: expected parsing error for flags %q: %s", tt.name, tt.flags, buff)
return
}
if err := opts.complete(cmd, buff); err != nil {
if !tt.wantCompleteErr {
t.Errorf("case %s: complete() error for flags %q: %s", tt.name, tt.flags, buff)
}
return
}
if tt.wantCompleteErr {
t.Errorf("case %s: complete() expected errors for flags %q: %s", tt.name, tt.flags, buff)
return
}
if err := opts.validate(); err != nil {
if !tt.wantValidateErr {
t.Errorf("case %s: flags %q: validate failed: %v", tt.name, tt.flags, err)
}
return
}
if tt.wantValidateErr {
t.Errorf("case %s: flags %q: expected validate to fail", tt.name, tt.flags)
return
}
modifiedAuthInfo := opts.modifyAuthInfo(tt.existingAuthInfo)
if !reflect.DeepEqual(modifiedAuthInfo, tt.wantAuthInfo) {
t.Errorf("case %s: flags %q: mis-matched auth info,\nwanted=%#v\ngot= %#v", tt.name, tt.flags, tt.wantAuthInfo, modifiedAuthInfo)
}
})
}
}
type createAuthInfoTest struct {
description string
config clientcmdapi.Config