Skip to content

Commit

Permalink
Add Cluster Override
Browse files Browse the repository at this point in the history
When working with multiple clusters it would be nice to temporarily
switch clusters by passing it as an option on the command line (without
changing the selected one) like `kubectl` handles "context" and `gcloud`
handles "project"
  • Loading branch information
sergiosalvatore committed Oct 18, 2019
1 parent c958543 commit 8bb6ef9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
4 changes: 4 additions & 0 deletions cmd/kaf/kaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,15 @@ var (
protoFiles []string
protoExclude []string
verbose bool
clusterOverride string
)

func init() {
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.kaf/config)")
rootCmd.PersistentFlags().StringSliceVarP(&brokersFlag, "brokers", "b", nil, "Comma separated list of broker ip:port pairs")
rootCmd.PersistentFlags().StringVar(&schemaRegistryURL, "schema-registry", "", "URL to a Confluent schema registry. Used for attempting to decode Avro-encoded messages")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Whether to turn on sarama logging")
rootCmd.PersistentFlags().StringVarP(&clusterOverride, "cluster", "c", "", "set a temporary current cluster")
cobra.OnInitialize(onInit)
}

Expand All @@ -144,6 +146,8 @@ func onInit() {
errorExit("Invalid config: %v", err)
}

config.ClusterOverride = clusterOverride

cluster := config.ActiveCluster()
if cluster != nil {
// Use active cluster from config
Expand Down
18 changes: 14 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ type Cluster struct {
}

type Config struct {
CurrentCluster string `yaml:"current-cluster"`
Clusters []*Cluster `yaml:"clusters"`
CurrentCluster string `yaml:"current-cluster"`
ClusterOverride string
Clusters []*Cluster `yaml:"clusters"`
}

func (c *Config) SetCurrentCluster(name string) error {
Expand All @@ -60,12 +61,21 @@ func (c *Config) SetCurrentCluster(name string) error {
}

func (c *Config) ActiveCluster() *Cluster {
if c == nil || c.CurrentCluster == "" {
if c == nil {
return nil
}

toSearch := c.ClusterOverride
if c.ClusterOverride == "" {
toSearch = c.CurrentCluster
}

if toSearch == "" {
return nil
}

for _, cluster := range c.Clusters {
if cluster.Name == c.CurrentCluster {
if cluster.Name == toSearch {
return cluster
}
}
Expand Down

0 comments on commit 8bb6ef9

Please sign in to comment.