Skip to content

Commit

Permalink
add an ability to run a function in a named context
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
pureneelesh authored and lpabon committed Mar 18, 2021
1 parent 3d70822 commit 9f80900
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
33 changes: 31 additions & 2 deletions pkg/config/cm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
33 changes: 14 additions & 19 deletions pkg/config/pxcconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
}

0 comments on commit 9f80900

Please sign in to comment.