diff --git a/pkg/converter/convert.go b/pkg/converter/convert.go index 10232f84..806193ad 100644 --- a/pkg/converter/convert.go +++ b/pkg/converter/convert.go @@ -34,6 +34,7 @@ const ( argTokenCacheDir = "--token-cache-dir" flagClientID = "client-id" + flagContext = "context" flagServerID = "server-id" flagTenantID = "tenant-id" flagEnvironment = "environment" @@ -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 } diff --git a/pkg/converter/convert_test.go b/pkg/converter/convert_test.go index 3ea3816e..71de64f6 100644 --- a/pkg/converter/convert_test.go +++ b/pkg/converter/convert_test.go @@ -39,7 +39,7 @@ func TestConvert(t *testing.T) { expectedArgs []string execArgItems []string command string - contextName string + expectedError string }{ { name: "non azure kubeconfig", @@ -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, @@ -1056,7 +1055,7 @@ func TestConvert(t *testing.T) { }, overrideFlags: map[string]string{ flagLoginMethod: token.MSILogin, - "context": clusterName1, + flagContext: clusterName1, }, expectedArgs: []string{ getTokenCommand, @@ -1064,6 +1063,21 @@ func TestConvert(t *testing.T) { 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 { @@ -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 != "" { diff --git a/pkg/converter/options.go b/pkg/converter/options.go index a2af5d5e..e6752fe3 100644 --- a/pkg/converter/options.go +++ b/pkg/converter/options.go @@ -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) }