Skip to content

Commit

Permalink
Fix VolumeClaimTemplate in Collector CRD
Browse files Browse the repository at this point in the history
  • Loading branch information
swiatekm committed Aug 18, 2023
1 parent c1e89ca commit ed62b86
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 25 deletions.
41 changes: 32 additions & 9 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,32 @@ func CreateDefaultConfig() Config {
}

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

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
37 changes: 22 additions & 15 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 (
targetAllocatorName = "target-allocator"
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(targetAllocatorName, 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")
zapFlagSet := flag.NewFlagSet("", flag.ErrorHandling(errorHandling))
zapCmdLineOpts.BindFlags(zapFlagSet)
flagSet.AddGoFlagSet(zapFlagSet)
return flagSet
}

func init() {
initFlags()
func getConfigFilePath(flagSet *pflag.FlagSet) (string, error) {
return flagSet.GetString(configFilePathFlagName)
}
4 changes: 3 additions & 1 deletion cmd/otel-allocator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package main

import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
Expand Down Expand Up @@ -66,9 +67,10 @@ func main() {
)
cfg, configFilePath, err := config.Load()
if err != nil {
setupLog.Error(err, "Failed to parse parameters")
fmt.Printf("Failed to load config: %v", err)
os.Exit(1)
}
ctrl.SetLogger(cfg.RootLogger)

if validationErr := config.ValidateConfig(cfg); validationErr != nil {
setupLog.Error(validationErr, "Invalid configuration")
Expand Down

0 comments on commit ed62b86

Please sign in to comment.