From d12c42d04376711d7570eebb3e1b00f8627508b8 Mon Sep 17 00:00:00 2001 From: Vladislav Kuzmin Date: Thu, 16 Jul 2020 18:58:31 +0400 Subject: [PATCH] Exclude kubeconfig from use-context update Since kubeconfig was moved to manifest we don't need to change it during use-context update. Change-Id: Ib6a0ff19eee89bc86a343e374d50938482e33941 Closes: #294 --- cmd/config/init.go | 2 +- cmd/config/set_management_configuration.go | 2 +- .../config-use-context-does-not-exist.golden | 2 +- .../config-use-context-no-args.golden | 2 +- cmd/config/use_context.go | 3 ++- docs/source/cli/airshipctl_config_use-context.md | 3 ++- pkg/config/config.go | 14 ++++++++------ pkg/config/config_helper.go | 10 +++++----- pkg/config/config_test.go | 4 ++-- 9 files changed, 23 insertions(+), 19 deletions(-) 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 807336f29..677db389e 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -174,7 +174,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 } @@ -410,7 +410,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 @@ -429,9 +429,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 @@ -857,7 +859,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 fbd251958..9f7f246f4 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