From 9f80900eb915e455f25a7c5d5991b360008cbbfd Mon Sep 17 00:00:00 2001 From: Neelesh Thakur Date: Fri, 12 Mar 2021 11:29:19 -0700 Subject: [PATCH] add an ability to run a function in a named context Added a method to ConfigManager to run a specified function in a named context. The named context can be a kubectl context or a pxc context. Also, - fixed a bug in NewConfigManagerForContext() that was preventing a context switch when run directly under pxc. - added back the validations for pxc config.yaml that were commented out. Lack of validation was causing a seg fault if the config file was missing a current context etc. Signed-off-by: Neelesh Thakur --- pkg/config/cm.go | 33 +++++++++++++++++++++++++++++++-- pkg/config/pxcconfig.go | 33 ++++++++++++++------------------- 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/pkg/config/cm.go b/pkg/config/cm.go index 963da7dc..12517969 100644 --- a/pkg/config/cm.go +++ b/pkg/config/cm.go @@ -71,11 +71,15 @@ func NewConfigManagerForContext(context string) (*ConfigManager, error) { configManager.configrw = newPxcConfigReaderWriter() } - err := configManager.Load() - if err != nil { + if err := configManager.Load(); err != nil { return nil, err } + if _, ok := configManager.Config.Contexts[context]; !ok { + return nil, fmt.Errorf("could not find the context '%s'", context) + } + + configManager.Config.CurrentContext = context return configManager, nil } @@ -136,6 +140,31 @@ func (cm *ConfigManager) ForEachContext( } } +func (cm *ConfigManager) RunInNamedContext(contextName string, handler func() error) error { + // get current cluster + original := CM() + originalK := KM() + + // defer resetting cluster + defer func() { + SetCM(original) + SetKM(originalK) + }() + + if _, ok := cm.Config.Contexts[contextName]; !ok { + return fmt.Errorf("could not find the context '%s'", contextName) + } + + newCm, err := NewConfigManagerForContext(contextName) + if err != nil { + return fmt.Errorf("failed to create manager for the context '%s'", contextName) + } + SetCM(newCm) + SetKM(NewKubernetesConfigManagerForContext(contextName)) + + return handler() +} + // GetFlags returns all the pxc persistent flags func (cm *ConfigManager) GetFlags() *ConfigFlags { return cm.Flags diff --git a/pkg/config/pxcconfig.go b/pkg/config/pxcconfig.go index 320b6a9b..0399c794 100644 --- a/pkg/config/pxcconfig.go +++ b/pkg/config/pxcconfig.go @@ -227,12 +227,9 @@ func (p *pxcConfigReaderWriter) newDefaultConfig() *Config { } func (p *pxcConfigReaderWriter) validate(c *Config) error { - // REMOVED THESE LINES TO allow View for debugging. May have to check if these affect the rest of the system - /* - if len(c.CurrentContext) == 0 { - return fmt.Errorf("Current context missing from config file %s", CM().GetConfigFile()) - } - */ + if len(c.CurrentContext) == 0 { + return fmt.Errorf("Current context missing from config file %s", CM().GetConfigFile()) + } if c.AuthInfos == nil { c.AuthInfos = make(map[string]*AuthInfo) } @@ -243,21 +240,19 @@ func (p *pxcConfigReaderWriter) validate(c *Config) error { c.Contexts = make(map[string]*Context) } - /* - if _, ok := c.Contexts[c.CurrentContext]; !ok { - return fmt.Errorf("Context %s missing from config file %s", c.CurrentContext, CM().GetConfigFile()) - } + if _, ok := c.Contexts[c.CurrentContext]; !ok { + return fmt.Errorf("Context %s missing from config file %s", c.CurrentContext, CM().GetConfigFile()) + } - currentCreds := c.Contexts[c.CurrentContext].AuthInfo - if _, ok := c.AuthInfos[currentCreds]; !ok { - return fmt.Errorf("Credentials %s missing from config file %s", currentCreds, CM().GetConfigFile()) - } + currentCreds := c.Contexts[c.CurrentContext].AuthInfo + if _, ok := c.AuthInfos[currentCreds]; !ok { + return fmt.Errorf("Credentials %s missing from config file %s", currentCreds, CM().GetConfigFile()) + } - currentCluster := c.Contexts[c.CurrentContext].Cluster - if _, ok := c.Clusters[currentCluster]; !ok { - return fmt.Errorf("Cluster %s missing from config file %s", currentCluster, CM().GetConfigFile()) - } + currentCluster := c.Contexts[c.CurrentContext].Cluster + if _, ok := c.Clusters[currentCluster]; !ok { + return fmt.Errorf("Cluster %s missing from config file %s", currentCluster, CM().GetConfigFile()) + } - */ return nil }