diff --git a/cmd/config/init.go b/cmd/config/init.go index 0a3ec9e8b..ef4a34bf9 100644 --- a/cmd/config/init.go +++ b/cmd/config/init.go @@ -44,7 +44,7 @@ func NewInitCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command Short: "Generate initial configuration files for airshipctl", Long: initLong[1:], RunE: func(cmd *cobra.Command, args []string) error { - return rootSettings.Config.PersistConfig() + return rootSettings.Config.PersistConfig(true) }, } diff --git a/cmd/config/set_management_configuration.go b/cmd/config/set_management_configuration.go index 9f33d5832..921fb5eb0 100644 --- a/cmd/config/set_management_configuration.go +++ b/cmd/config/set_management_configuration.go @@ -88,7 +88,7 @@ func NewSetManagementConfigCommand(rootSettings *environment.AirshipCTLSettings) return nil } - if err = rootSettings.Config.PersistConfig(); err != nil { + if err = rootSettings.Config.PersistConfig(true); err != nil { return err } diff --git a/cmd/config/testdata/TestConfigUseContextGoldenOutput/config-use-context-does-not-exist.golden b/cmd/config/testdata/TestConfigUseContextGoldenOutput/config-use-context-does-not-exist.golden index 912efafb7..d13b9f777 100644 --- a/cmd/config/testdata/TestConfigUseContextGoldenOutput/config-use-context-does-not-exist.golden +++ b/cmd/config/testdata/TestConfigUseContextGoldenOutput/config-use-context-does-not-exist.golden @@ -4,7 +4,7 @@ Usage: Examples: -# Switch to a context named "exampleContext" +# Switch to a context named "exampleContext" in airshipctl config file airshipctl config use-context exampleContext diff --git a/cmd/config/testdata/TestConfigUseContextGoldenOutput/config-use-context-no-args.golden b/cmd/config/testdata/TestConfigUseContextGoldenOutput/config-use-context-no-args.golden index fd8e67f0e..d98a33ff2 100644 --- a/cmd/config/testdata/TestConfigUseContextGoldenOutput/config-use-context-no-args.golden +++ b/cmd/config/testdata/TestConfigUseContextGoldenOutput/config-use-context-no-args.golden @@ -4,7 +4,7 @@ Usage: Examples: -# Switch to a context named "exampleContext" +# Switch to a context named "exampleContext" in airshipctl config file airshipctl config use-context exampleContext diff --git a/cmd/config/use_context.go b/cmd/config/use_context.go index 9d1816473..1355eb5d4 100644 --- a/cmd/config/use_context.go +++ b/cmd/config/use_context.go @@ -28,10 +28,11 @@ import ( const ( useContextLong = ` Switch to a different context defined in the airshipctl config file. +This command doesn't change a context for the kubeconfig file. ` useContextExample = ` -# Switch to a context named "exampleContext" +# Switch to a context named "exampleContext" in airshipctl config file airshipctl config use-context exampleContext ` ) diff --git a/docs/source/cli/airshipctl_config_use-context.md b/docs/source/cli/airshipctl_config_use-context.md index 81027d9d9..0f14255d1 100644 --- a/docs/source/cli/airshipctl_config_use-context.md +++ b/docs/source/cli/airshipctl_config_use-context.md @@ -5,6 +5,7 @@ Switch to a different context ### Synopsis Switch to a different context defined in the airshipctl config file. +This command doesn't change a context for the kubeconfig file. ``` @@ -15,7 +16,7 @@ airshipctl config use-context NAME [flags] ``` -# Switch to a context named "exampleContext" +# Switch to a context named "exampleContext" in airshipctl config file airshipctl config use-context exampleContext ``` diff --git a/pkg/config/config.go b/pkg/config/config.go index 7c9c221d6..6a378961d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -183,7 +183,7 @@ func (c *Config) reconcileConfig() error { // Specially useful if the config is loaded during a get operation // If it was a Set this would have happened eventually any way if persistIt { - return c.PersistConfig() + return c.PersistConfig(true) } return nil } @@ -419,7 +419,7 @@ func (c *Config) EnsureComplete() error { // the current Config and KubeConfig objects. // If either file did not previously exist, the file will be created. // Otherwise, the file will be overwritten -func (c *Config) PersistConfig() error { +func (c *Config) PersistConfig(persistKubeConfig bool) error { airshipConfigYaml, err := c.ToYaml() if err != nil { return err @@ -450,9 +450,11 @@ func (c *Config) PersistConfig() error { return err } - // Persist the kubeconfig file referenced - if err := clientcmd.WriteToFile(*c.kubeConfig, c.kubeConfigPath); err != nil { - return err + if persistKubeConfig { + // Persist the kubeconfig file referenced + if err := clientcmd.WriteToFile(*c.kubeConfig, c.kubeConfigPath); err != nil { + return err + } } return nil @@ -878,7 +880,7 @@ func (c *Config) ImportFromKubeConfig(kubeConfigPath string) error { c.importClusters(kubeConfig) c.importContexts(kubeConfig) c.importAuthInfos(kubeConfig) - return c.PersistConfig() + return c.PersistConfig(true) } func (c *Config) importClusters(importKubeConfig *clientcmdapi.Config) { diff --git a/pkg/config/config_helper.go b/pkg/config/config_helper.go index 18fe1b4e2..eb69191ad 100644 --- a/pkg/config/config_helper.go +++ b/pkg/config/config_helper.go @@ -46,7 +46,7 @@ func RunSetAuthInfo(o *AuthInfoOptions, airconfig *Config, writeToStorage bool) } // Update configuration file just in time persistence approach if writeToStorage { - if err := airconfig.PersistConfig(); err != nil { + if err := airconfig.PersistConfig(true); err != nil { // Error that it didnt persist the changes return modified, ErrConfigFailed{} } @@ -89,7 +89,7 @@ func RunSetCluster(o *ClusterOptions, airconfig *Config, writeToStorage bool) (b // Update configuration file // Just in time persistence approach if writeToStorage { - if err := airconfig.PersistConfig(); err != nil { + if err := airconfig.PersistConfig(true); err != nil { // Some warning here , that it didnt persist the changes because of this // Or should we float this up // What would it mean? No value. @@ -142,7 +142,7 @@ func RunSetContext(o *ContextOptions, airconfig *Config, writeToStorage bool) (b } // Update configuration file just in time persistence approach if writeToStorage { - if err := airconfig.PersistConfig(); err != nil { + if err := airconfig.PersistConfig(true); err != nil { // Error that it didnt persist the changes return modified, ErrConfigFailed{} } @@ -159,7 +159,7 @@ func RunUseContext(desiredContext string, airconfig *Config) error { if airconfig.CurrentContext != desiredContext { airconfig.CurrentContext = desiredContext - if err := airconfig.PersistConfig(); err != nil { + if err := airconfig.PersistConfig(false); err != nil { return err } } @@ -189,7 +189,7 @@ func RunSetManifest(o *ManifestOptions, airconfig *Config, writeToStorage bool) } // Update configuration file just in time persistence approach if writeToStorage { - if err := airconfig.PersistConfig(); err != nil { + if err := airconfig.PersistConfig(true); err != nil { // Error that it didnt persist the changes return modified, ErrConfigFailed{} } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index d85b1d69e..011664c9b 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -133,7 +133,7 @@ func TestPersistConfig(t *testing.T) { conf, cleanup := testutil.InitConfig(t) defer cleanup(t) - err := conf.PersistConfig() + err := conf.PersistConfig(true) require.NoError(t, err) // Check that the files were created @@ -305,7 +305,7 @@ func TestPurge(t *testing.T) { defer cleanup(t) // Store it - err := conf.PersistConfig() + err := conf.PersistConfig(true) assert.NoErrorf(t, err, "Unable to persist configuration expected at %v", conf.LoadedConfigPath()) // Verify that the file is there