From 8e7ef8cc13afc7a311c317d2998cc84debf25383 Mon Sep 17 00:00:00 2001 From: Sina Date: Mon, 25 Dec 2023 10:01:30 +0330 Subject: [PATCH] revised how custom config is used --- cmd/main.go | 19 +++++++++++++------ config/default/manager_auth_proxy_patch.yaml | 1 + internal/config/config.go | 6 ++++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 92f4695..62c038c 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -58,7 +58,7 @@ func main() { var customConfigPath string flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") - flag.StringVar(&customConfigPath, "custom-config-path", "./custom/config.yaml", "the path to custom config.") + flag.StringVar(&customConfigPath, "custom-config-path", "", "the path to custom config.") flag.BoolVar(&enableLeaderElection, "leader-elect", false, "Enable leader election for controller manager. "+ "Enabling this will ensure there is only one active controller manager.") @@ -69,12 +69,19 @@ func main() { flag.Parse() ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) - customConfig, err := config.InitConfig(customConfigPath) - if err != nil { - setupLog.Info("custom config not loaded") - } else { - authenticatorv1alpha1.ValidationTimeout = time.Second * time.Duration(customConfig.WebhookConf.ValidationTimeoutSecond) + + var customConfig *config.CustomConfig + if customConfigPath != "" { + setupLog.Info(customConfigPath) + tmpConf, err := config.InitConfig(customConfigPath) + if err != nil { + setupLog.Error(err, "failed to load custom config") + } else { + customConfig = tmpConf + authenticatorv1alpha1.ValidationTimeout = time.Second * time.Duration(customConfig.WebhookConf.ValidationTimeoutSecond) + } } + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ Scheme: scheme, MetricsBindAddress: metricsAddr, diff --git a/config/default/manager_auth_proxy_patch.yaml b/config/default/manager_auth_proxy_patch.yaml index b751266..9030be8 100644 --- a/config/default/manager_auth_proxy_patch.yaml +++ b/config/default/manager_auth_proxy_patch.yaml @@ -53,3 +53,4 @@ spec: - "--health-probe-bind-address=:8081" - "--metrics-bind-address=127.0.0.1:8080" - "--leader-elect" + - $(ARGS) diff --git a/internal/config/config.go b/internal/config/config.go index 6b94803..4f6151c 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,6 +1,8 @@ package config -import "github.com/spf13/viper" +import ( + "github.com/spf13/viper" +) type CustomConfig struct { WebserverConf WebserverConfig `mapstructure:"webserver"` @@ -17,7 +19,7 @@ type WebhookConfig struct { } func InitConfig(configPath string) (*CustomConfig, error) { - viper.AddConfigPath(configPath) + viper.SetConfigFile(configPath) viper.SetConfigType("yaml") err := viper.ReadInConfig() if err != nil {