Skip to content

Commit

Permalink
Add flags validation check to error if --kubeconfig flag is specified…
Browse files Browse the repository at this point in the history
… while creating tanzu context type

Signed-off-by: Prem Kumar Kalle <prem.kalle@broadcom.com>
  • Loading branch information
prkalle committed Jun 27, 2024
1 parent 33db380 commit be47dab
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/command/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ func createCtx(cmd *cobra.Command, args []string) (err error) {
}
ctxName = args[0]
}

if err := validateContextCreateFlagValues(); err != nil {
return err
}
if !configtypes.IsValidContextType(contextTypeStr) {
return errors.New(invalidContextType)
}
Expand Down Expand Up @@ -286,6 +288,16 @@ func createCtx(cmd *cobra.Command, args []string) (err error) {
return nil
}

func validateContextCreateFlagValues() error {
if contextTypeStr == string(configtypes.ContextTypeTanzu) && kubeConfig != "" {
return fmt.Errorf("the '–-kubeconfig' flag is not applicable when creating a context of type 'tanzu'")
}
if contextTypeStr == string(configtypes.ContextTypeTanzu) && kubeContext != "" {
return fmt.Errorf("the '–-kubecontext' flag is not applicable when creating a context of type 'tanzu'")
}
return nil
}

// syncContextPlugins syncs the plugins for the given context type
func syncContextPlugins(cmd *cobra.Command, contextType configtypes.ContextType, ctxName string) error {
disablePluginSync, _ := strconv.ParseBool(os.Getenv(constants.SkipAutoInstallOfContextRecommendedPlugins))
Expand Down
8 changes: 8 additions & 0 deletions pkg/command/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1903,3 +1903,11 @@ func TestMapTanzuEndpointToTMCEndpoint(t *testing.T) {
}
}
}

func TestCreateContextWithTanzuTypeAndKubeconfigFlags(t *testing.T) {
contextTypeStr = contextTypeTanzu
kubeConfig = "fake-kubeconfig"
err := createCtx(&cobra.Command{}, []string{})
assert.NotNil(t, err)
assert.EqualError(t, err, `the '–-kubeconfig' flag is not applicable when creating a context of type 'tanzu'`)
}

0 comments on commit be47dab

Please sign in to comment.