Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

return error when specified context is not found #261

Merged
merged 1 commit into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pkg/converter/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
argTokenCacheDir = "--token-cache-dir"

flagClientID = "client-id"
flagContext = "context"
flagServerID = "server-id"
flagTenantID = "tenant-id"
flagEnvironment = "environment"
Expand Down Expand Up @@ -151,7 +152,10 @@ func Convert(o Options, pathOptions *clientcmd.PathOptions) error {

targetAuthInfo := ""

if o.context != "" && config.Contexts[o.context] != nil {
if o.context != "" {
if config.Contexts[o.context] == nil {
return fmt.Errorf("no context exists with the name: %q", o.context)
}
targetAuthInfo = config.Contexts[o.context].AuthInfo
}

Expand Down
26 changes: 21 additions & 5 deletions pkg/converter/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestConvert(t *testing.T) {
expectedArgs []string
execArgItems []string
command string
contextName string
expectedError string
}{
{
name: "non azure kubeconfig",
Expand Down Expand Up @@ -1045,8 +1045,7 @@ func TestConvert(t *testing.T) {
command: execName,
},
{
name: "convert with context specified, auth info not specified by the context should not be changed",
contextName: clusterName1,
name: "convert with context specified, auth info not specified by the context should not be changed",
authProviderConfig: map[string]string{
cfgEnvironment: envName,
cfgApiserverID: serverID,
Expand All @@ -1056,14 +1055,29 @@ func TestConvert(t *testing.T) {
},
overrideFlags: map[string]string{
flagLoginMethod: token.MSILogin,
"context": clusterName1,
flagContext: clusterName1,
},
expectedArgs: []string{
getTokenCommand,
argServerID, serverID,
argLoginMethod, token.MSILogin,
},
},
{
name: "convert with non-existent context specified, Convert should return error",
authProviderConfig: map[string]string{
cfgEnvironment: envName,
cfgApiserverID: serverID,
cfgClientID: clientID,
cfgTenantID: tenantID,
cfgConfigMode: "0",
},
overrideFlags: map[string]string{
flagLoginMethod: token.MSILogin,
flagContext: "badContext",
},
expectedError: "no context exists with the name: \"badContext\"",
},
}
rootTmpDir, err := os.MkdirTemp("", "kubelogin-test")
if err != nil {
Expand Down Expand Up @@ -1111,8 +1125,10 @@ func TestConvert(t *testing.T) {
},
}
err = Convert(o, &pathOptions)
if err != nil {
if data.expectedError == "" && err != nil {
t.Fatalf("Unexpected error from Convert: %v", err)
} else if data.expectedError != "" && (err == nil || err.Error() != data.expectedError) {
t.Fatalf("Expected error: %q, but got: %q", data.expectedError, err)
}

if o.context != "" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/converter/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (o *Options) AddFlags(fs *pflag.FlagSet) {
if cf, ok := o.configFlags.(*genericclioptions.ConfigFlags); ok {
cf.AddFlags(fs)
}
fs.StringVar(&o.context, "context", "", "The name of the kubeconfig context to use")
fs.StringVar(&o.context, flagContext, "", "The name of the kubeconfig context to use")
o.TokenOptions.AddFlags(fs)
}

Expand Down