Skip to content

Commit

Permalink
Use an explicit FlagSet for TA flags
Browse files Browse the repository at this point in the history
  • Loading branch information
swiatekm committed Aug 16, 2023
1 parent dd2bea5 commit 8bab90f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 22 deletions.
40 changes: 32 additions & 8 deletions cmd/otel-allocator/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,18 @@ func LoadFromFile(file string, target *Config) error {
return unmarshal(target, file)
}

func LoadFromCLI(target *Config) error {
func LoadFromCLI(target *Config, flagSet *pflag.FlagSet) error {
var err error
// set the rest of the config attributes based on command-line flag values
target.RootLogger = zap.New(zap.UseFlagOptions(&zapCmdLineOpts))
klog.SetLogger(target.RootLogger)
ctrl.SetLogger(target.RootLogger)

target.KubeConfigFilePath = *kubeConfigPathFlag
clusterConfig, err := clientcmd.BuildConfigFromFlags("", *kubeConfigPathFlag)
target.KubeConfigFilePath, err = flagSet.GetString(kubeConfigPathFlagName)
if err != nil {
return err
}
clusterConfig, err := clientcmd.BuildConfigFromFlags("", target.KubeConfigFilePath)
if err != nil {
pathError := &fs.PathError{}
if ok := errors.As(err, &pathError); !ok {
Expand All @@ -93,10 +97,18 @@ func LoadFromCLI(target *Config) error {
return err
}
}
target.ListenAddr = *listenAddrFlag
target.PrometheusCR.Enabled = *prometheusCREnabledFlag
target.ClusterConfig = clusterConfig

target.ListenAddr, err = flagSet.GetString(target.ListenAddr)
if err != nil {
return err
}

target.PrometheusCR.Enabled, err = flagSet.GetBool(prometheusCREnabledFlagName)
if err != nil {
return err
}

return nil
}

Expand All @@ -121,21 +133,33 @@ func CreateDefaultConfig() Config {
}

func Load() (*Config, string, error) {
var err error

pflag.Parse()
flagSet := getFlagSet(pflag.ExitOnError)
err = flagSet.Parse(os.Args)
if err != nil {
return nil, "", err
}

config := CreateDefaultConfig()

// load the config from the config file
err := LoadFromFile(*configFilePathFlag, &config)
configFilePath, err := getConfigFilePath(flagSet)
if err != nil {
return nil, "", err
}
err = LoadFromFile(configFilePath, &config)
if err != nil {
return nil, "", err
}

err = LoadFromCLI(&config)
err = LoadFromCLI(&config, flagSet)
if err != nil {
return nil, "", err
}

return &config, *configFilePathFlag, nil
return &config, configFilePath, nil
}

// ValidateConfig validates the cli and file configs together.
Expand Down
35 changes: 21 additions & 14 deletions cmd/otel-allocator/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,30 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)

// Flag definitions.
var (
configFilePathFlag *string
listenAddrFlag *string
prometheusCREnabledFlag *bool
kubeConfigPathFlag *string
zapCmdLineOpts zap.Options
// Flag names.
const (
configFilePathFlagName = "config-file"
listenAddrFlagName = "listen-addr"
prometheusCREnabledFlagName = "enable-prometheus-cr-watcher"
kubeConfigPathFlagName = "kubeconfig-path"
)

func initFlags() {
configFilePathFlag = pflag.String("config-file", DefaultConfigFilePath, "The path to the config file.")
listenAddrFlag = pflag.String("listen-addr", ":8080", "The address where this service serves.")
prometheusCREnabledFlag = pflag.Bool("enable-prometheus-cr-watcher", false, "Enable Prometheus CRs as target sources")
kubeConfigPathFlag = pflag.String("kubeconfig-path", filepath.Join(homedir.HomeDir(), ".kube", "config"), "absolute path to the KubeconfigPath file")
zapCmdLineOpts.BindFlags(flag.CommandLine)
// We can't bind this flag to our FlagSet, so we need to handle it separately.
var zapCmdLineOpts zap.Options

func getFlagSet(errorHandling pflag.ErrorHandling) *pflag.FlagSet {
flagSet := pflag.NewFlagSet("", errorHandling)
flagSet.String(configFilePathFlagName, DefaultConfigFilePath, "The path to the config file.")
flagSet.String(listenAddrFlagName, ":8080", "The address where this service serves.")
flagSet.Bool(prometheusCREnabledFlagName, false, "Enable Prometheus CRs as target sources")
flagSet.String(kubeConfigPathFlagName, filepath.Join(homedir.HomeDir(), ".kube", "config"), "absolute path to the KubeconfigPath file")
return flagSet
}

func getConfigFilePath(flagSet *pflag.FlagSet) (string, error) {
return flagSet.GetString(configFilePathFlagName)
}

func init() {
initFlags()
zapCmdLineOpts.BindFlags(flag.CommandLine)
}

0 comments on commit 8bab90f

Please sign in to comment.