From 6be8d47d922d02e3949e0e71ef95be624e901e18 Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Fri, 3 Feb 2023 12:46:25 +0300 Subject: [PATCH] [TrueCloudLab#12] Support multiple configs Signed-off-by: Denis Kirillov --- README.md | 17 +++++++++ app.go | 5 ++- config/config.yaml | 4 +- config/dir/pprof.yaml | 3 ++ config/dir/prometheus.yaml | 3 ++ settings.go | 76 +++++++++++++++++++++++++++++++++----- 6 files changed, 94 insertions(+), 14 deletions(-) create mode 100644 config/dir/pprof.yaml create mode 100644 config/dir/prometheus.yaml diff --git a/README.md b/README.md index d27d4e8..7d2e315 100644 --- a/README.md +++ b/README.md @@ -182,6 +182,23 @@ $ frostfs-http-gw --config your-config.yaml See [config](./config/config.yaml) and [defaults](./docs/gate-configuration.md) for example. +#### Multiple configs + +You can use several config files when running application. It allows you to split configuration into parts. +For example, you can use separate yaml file for pprof and prometheus section in config (see [config examples](./config)). +You can either provide several files with repeating `--config` flag or provide path to the dir that contains all configs using `--config-dir` flag. +Also, you can combine these flags: + +```shell +$ frostfs-http-gw --config ./config/config.yaml --config /your/partial/config.yaml --config-dir ./config/dir +``` + +**Note:** next file in `--config` flag overwrites values from the previous one. +Files from `--config-dir` directory overwrite values from `--config` files. +So the command above run `frostfs-http-gw` to listen on `0.0.0.0:8080` address (value from `./config/config.yaml`), +applies parameters from `/your/partial/config.yaml`, +enable pprof (value from `./config/dir/pprof.yaml`) and prometheus (value from `./config/dir/prometheus.yaml`). + ## HTTP API provided This gateway intentionally provides limited feature set and doesn't try to diff --git a/app.go b/app.go index b43b144..e17aca9 100644 --- a/app.go +++ b/app.go @@ -380,14 +380,15 @@ LOOP: func (a *app) configReload() { a.log.Info("SIGHUP config reload started") - if !a.cfg.IsSet(cmdConfig) { + if !a.cfg.IsSet(cmdConfig) && !a.cfg.IsSet(cmdConfigDir) { a.log.Warn("failed to reload config because it's missed") return } - if err := readConfig(a.cfg); err != nil { + if err := readInConfig(a.cfg); err != nil { a.log.Warn("failed to reload config", zap.Error(err)) return } + if lvl, err := getLogLevel(a.cfg); err != nil { a.log.Warn("log level won't be updated", zap.Error(err)) } else { diff --git a/config/config.yaml b/config/config.yaml index 71bab50..0a117df 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -4,10 +4,10 @@ wallet: passphrase: pwd # Passphrase to decrypt wallet. If you're using a wallet without a password, place '' here. pprof: - enabled: true # Enable pprof. + enabled: false # Enable pprof. address: localhost:8083 prometheus: - enabled: true # Enable metrics. + enabled: false # Enable metrics. address: localhost:8084 logger: diff --git a/config/dir/pprof.yaml b/config/dir/pprof.yaml new file mode 100644 index 0000000..44f93ca --- /dev/null +++ b/config/dir/pprof.yaml @@ -0,0 +1,3 @@ +pprof: + enabled: true + address: localhost:8083 diff --git a/config/dir/prometheus.yaml b/config/dir/prometheus.yaml new file mode 100644 index 0000000..f29db69 --- /dev/null +++ b/config/dir/prometheus.yaml @@ -0,0 +1,3 @@ +prometheus: + enabled: true + address: localhost:8084 diff --git a/settings.go b/settings.go index c6ee33f..cd71e4a 100644 --- a/settings.go +++ b/settings.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "path" "runtime" "sort" "strconv" @@ -84,6 +85,7 @@ const ( cmdWallet = "wallet" cmdAddress = "address" cmdConfig = "config" + cmdConfigDir = "config-dir" cmdListenAddress = "listen_address" ) @@ -114,7 +116,8 @@ func settings() *viper.Viper { flags.StringP(cmdWallet, "w", "", `path to the wallet`) flags.String(cmdAddress, "", `address of wallet account`) - flags.String(cmdConfig, "", "config path") + flags.StringArray(cmdConfig, nil, "config paths") + flags.String(cmdConfigDir, "", "config dir path") flags.Duration(cfgConTimeout, defaultConnectTimeout, "gRPC connect timeout") flags.Duration(cfgStreamTimeout, defaultStreamTimeout, "gRPC individual message timeout") flags.Duration(cfgReqTimeout, defaultRequestTimeout, "gRPC request timeout") @@ -233,10 +236,8 @@ func settings() *viper.Viper { os.Exit(0) } - if v.IsSet(cmdConfig) { - if err := readConfig(v); err != nil { - panic(err) - } + if err := readInConfig(v); err != nil { + panic(err) } if peers != nil && len(*peers) > 0 { @@ -250,17 +251,72 @@ func settings() *viper.Viper { return v } -func readConfig(v *viper.Viper) error { - cfgFileName := v.GetString(cmdConfig) - cfgFile, err := os.Open(cfgFileName) +func readInConfig(v *viper.Viper) error { + if v.IsSet(cmdConfig) { + if err := readConfig(v); err != nil { + return err + } + } + + if v.IsSet(cmdConfigDir) { + if err := readConfigDir(v); err != nil { + return err + } + } + + return nil +} + +func readConfigDir(v *viper.Viper) error { + cfgSubConfigDir := v.GetString(cmdConfigDir) + entries, err := os.ReadDir(cfgSubConfigDir) if err != nil { return err } - if err = v.ReadConfig(cfgFile); err != nil { + + for _, entry := range entries { + if entry.IsDir() { + continue + } + ext := path.Ext(entry.Name()) + if ext != ".yaml" && ext != ".yml" { + continue + } + + if err = mergeConfig(v, path.Join(cfgSubConfigDir, entry.Name())); err != nil { + return err + } + } + + return nil +} + +func readConfig(v *viper.Viper) error { + for _, fileName := range v.GetStringSlice(cmdConfig) { + if err := mergeConfig(v, fileName); err != nil { + return err + } + } + return nil +} + +func mergeConfig(v *viper.Viper, fileName string) error { + cfgFile, err := os.Open(fileName) + if err != nil { return err } - return cfgFile.Close() + defer func() { + if errClose := cfgFile.Close(); errClose != nil { + panic(errClose) + } + }() + + if err = v.MergeConfig(cfgFile); err != nil { + return err + } + + return nil } // newLogger constructs a zap.Logger instance for current application.