Skip to content

Commit

Permalink
Clearly separate loading config from CLI and file
Browse files Browse the repository at this point in the history
  • Loading branch information
swiatekm committed Aug 18, 2023
1 parent 4dec84f commit c1e89ca
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 31 deletions.
59 changes: 33 additions & 26 deletions cmd/otel-allocator/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,33 @@ func (c Config) GetTargetsFilterStrategy() string {
return ""
}

func Load(file string) (Config, error) {
cfg := createDefaultConfig()
if err := unmarshal(&cfg, file); err != nil {
return Config{}, err
func LoadFromFile(file string, target *Config) error {
return unmarshal(target, file)
}

func LoadFromCLI(target *Config) 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)
if err != nil {
pathError := &fs.PathError{}
if ok := errors.As(err, &pathError); !ok {
return err
}
clusterConfig, err = rest.InClusterConfig()
if err != nil {
return err
}
}
return cfg, nil
target.ListenAddr = *listenAddrFlag
target.PrometheusCR.Enabled = *prometheusCREnabledFlag
target.ClusterConfig = clusterConfig

return nil
}

func unmarshal(cfg *Config, configFile string) error {
Expand All @@ -91,43 +112,29 @@ func unmarshal(cfg *Config, configFile string) error {
return nil
}

func createDefaultConfig() Config {
func CreateDefaultConfig() Config {
return Config{
PrometheusCR: PrometheusCRConfig{
ScrapeInterval: DefaultCRScrapeInterval,
},
}
}

func FromCLI() (*Config, string, error) {
func Load() (*Config, string, error) {
pflag.Parse()
config := CreateDefaultConfig()

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

// set the rest of the config attributes based on command-line flag values
config.RootLogger = zap.New(zap.UseFlagOptions(&zapCmdLineOpts))
klog.SetLogger(config.RootLogger)
ctrl.SetLogger(config.RootLogger)

config.KubeConfigFilePath = *kubeConfigPathFlag
clusterConfig, err := clientcmd.BuildConfigFromFlags("", *kubeConfigPathFlag)
err = LoadFromCLI(&config)
if err != nil {
pathError := &fs.PathError{}
if ok := errors.As(err, &pathError); !ok {
return nil, "", err
}
clusterConfig, err = rest.InClusterConfig()
if err != nil {
return nil, "", err
}
return nil, "", err
}
config.ListenAddr = *listenAddrFlag
config.PrometheusCR.Enabled = *prometheusCREnabledFlag
config.ClusterConfig = clusterConfig

return &config, *configFilePathFlag, nil
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/otel-allocator/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestLoad(t *testing.T) {
args: args{
file: "./testdata/no_config.yaml",
},
want: createDefaultConfig(),
want: CreateDefaultConfig(),
wantErr: assert.NoError,
},
{
Expand Down Expand Up @@ -163,7 +163,8 @@ func TestLoad(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Load(tt.args.file)
got := CreateDefaultConfig()
err := LoadFromFile(tt.args.file, &got)
if !tt.wantErr(t, err, fmt.Sprintf("Load(%v)", tt.args.file)) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/otel-allocator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func main() {
interrupts = make(chan os.Signal, 1)
errChan = make(chan error)
)
cfg, configFilePath, err := config.FromCLI()
cfg, configFilePath, err := config.Load()
if err != nil {
setupLog.Error(err, "Failed to parse parameters")
os.Exit(1)
Expand Down
3 changes: 2 additions & 1 deletion cmd/otel-allocator/target/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ func TestDiscovery(t *testing.T) {
}()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg, err := config.Load(tt.args.file)
cfg := config.CreateDefaultConfig()
err := config.LoadFromFile(tt.args.file, &cfg)
assert.NoError(t, err)
assert.True(t, len(cfg.PromConfig.ScrapeConfigs) > 0)
err = manager.ApplyConfig(allocatorWatcher.EventSourcePrometheusCR, cfg.PromConfig)
Expand Down
3 changes: 2 additions & 1 deletion cmd/otel-allocator/watcher/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ func NewFileWatcher(logger logr.Logger, configFilePath string) (*FileWatcher, er
}

func (f *FileWatcher) LoadConfig(_ context.Context) (*promconfig.Config, error) {
cfg, err := config.Load(f.configFilePath)
cfg := config.CreateDefaultConfig()
err := config.LoadFromFile(f.configFilePath, &cfg)
if err != nil {
f.logger.Error(err, "Unable to load configuration")
return nil, err
Expand Down

0 comments on commit c1e89ca

Please sign in to comment.