From badfec94844ee13be3bd0e7cec2a6c5ec607ed86 Mon Sep 17 00:00:00 2001 From: Kevin Petremann Date: Fri, 14 Jun 2024 18:25:03 +0200 Subject: [PATCH] feat(config): config file is configurable --- .github/workflows/doc.yml | 2 +- .github/workflows/go.yml | 2 +- cmd/salt-exporter/config.go | 22 ++++++++++++-------- cmd/salt-exporter/config_test.go | 26 ++++++++++++++++++++---- cmd/salt-exporter/main.go | 4 +--- docs/docs/salt-exporter/configuration.md | 12 +++++++++-- e2e_test/docker-compose.yaml | 2 -- 7 files changed, 49 insertions(+), 21 deletions(-) diff --git a/.github/workflows/doc.yml b/.github/workflows/doc.yml index 6f4a953..d9ac5b2 100644 --- a/.github/workflows/doc.yml +++ b/.github/workflows/doc.yml @@ -12,7 +12,7 @@ permissions: contents: write jobs: - build: + build-doc: runs-on: ubuntu-latest diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index f691864..6ee4c3c 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -10,7 +10,7 @@ env: COMPOSE_FILE: e2e_test/docker-compose.yaml jobs: - build: + tests: name: "Lint and test" runs-on: ubuntu-latest steps: diff --git a/cmd/salt-exporter/config.go b/cmd/salt-exporter/config.go index 8e86c2f..9a8413e 100644 --- a/cmd/salt-exporter/config.go +++ b/cmd/salt-exporter/config.go @@ -13,6 +13,7 @@ import ( "github.com/spf13/viper" ) +const defaultConfigFilename = "config.yml" const defaultLogLevel = "info" const defaultPort = 2112 const defaultHealthMinion = true @@ -48,9 +49,10 @@ type Config struct { Metrics metrics.Config } -func parseFlags() bool { +func parseFlags() (string, bool) { // flags versionCmd := flag.Bool("version", false, "print version") + configFile := flag.String("config-file", defaultConfigFilename, "config filepath") flag.String("log-level", defaultLogLevel, "log level (debug, info, warn, error, fatal, panic, disabled)") flag.String("host", "", "listen address") flag.Int("port", defaultPort, "listen port") @@ -80,7 +82,7 @@ func parseFlags() bool { os.Exit(0) } - return *healthMinions + return *configFile, *healthMinions } func setDefaults(healthMinions bool) { @@ -117,10 +119,14 @@ func getConfig(configFileName string, healthMinions bool) (Config, error) { } // bind configuration file - ext := filepath.Ext(configFileName) - viper.SetConfigName(strings.TrimSuffix(configFileName, ext)) - viper.SetConfigType(strings.TrimPrefix(ext, ".")) - viper.AddConfigPath(".") + if filepath.IsAbs(configFileName) { + viper.SetConfigFile(configFileName) + } else { + ext := filepath.Ext(configFileName) + viper.SetConfigName(strings.TrimSuffix(configFileName, ext)) + viper.SetConfigType(strings.TrimPrefix(ext, ".")) + viper.AddConfigPath(".") + } viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "__")) viper.SetEnvPrefix("SALT") @@ -155,10 +161,10 @@ func checkRequirements(cfg Config) error { return nil } -func ReadConfig(configFileName string) (Config, error) { +func ReadConfig() (Config, error) { var err error - healthMinions := parseFlags() + configFileName, healthMinions := parseFlags() cfg, err := getConfig(configFileName, healthMinions) if err != nil { diff --git a/cmd/salt-exporter/config_test.go b/cmd/salt-exporter/config_test.go index 34f9159..67e58a7 100644 --- a/cmd/salt-exporter/config_test.go +++ b/cmd/salt-exporter/config_test.go @@ -209,7 +209,7 @@ func TestReadConfigFlagOnly(t *testing.T) { flag.CommandLine = flag.NewFlagSet(name, flag.ContinueOnError) viper.Reset() - cfg, err := ReadConfig("nofiles.yml") + cfg, err := ReadConfig() if diff := cmp.Diff(cfg, test.want); diff != "" { t.Errorf("Mismatch for '%s' test:\n%s", test.name, diff) @@ -223,8 +223,25 @@ func TestReadConfigFlagOnly(t *testing.T) { } func TestConfigFileOnly(t *testing.T) { - defer viper.Reset() - cfg, err := ReadConfig("config_test.yml") + name := os.Args[0] + backupArgs := os.Args + backupCommandLine := flag.CommandLine + defer func() { + flag.CommandLine = backupCommandLine + os.Args = backupArgs + viper.Reset() + }() + + flags := []string{ + "-config-file=config_test.yml", + } + + os.Args = append([]string{name}, flags...) + flag.CommandLine = flag.NewFlagSet(name, flag.ContinueOnError) + viper.Reset() + + cfg, err := ReadConfig() + want := Config{ LogLevel: "info", ListenAddress: "127.0.0.1", @@ -322,6 +339,7 @@ func TestConfigFileWithFlags(t *testing.T) { }() flags := []string{ + "-config-file=config_test.yml", "-host=127.0.0.1", "-port=8080", "-health-minions=false", @@ -335,7 +353,7 @@ func TestConfigFileWithFlags(t *testing.T) { flag.CommandLine = flag.NewFlagSet(name, flag.ContinueOnError) viper.Reset() - cfg, err := ReadConfig("config_test.yml") + cfg, err := ReadConfig() want := Config{ LogLevel: "info", ListenAddress: "127.0.0.1", diff --git a/cmd/salt-exporter/main.go b/cmd/salt-exporter/main.go index a91509a..349166f 100644 --- a/cmd/salt-exporter/main.go +++ b/cmd/salt-exporter/main.go @@ -18,8 +18,6 @@ import ( "github.com/rs/zerolog/log" ) -const configFileName = "config.yml" - var ( version = "unknown" commit = "unknown" @@ -107,7 +105,7 @@ func main() { defer quit() logging.Configure() - config, err := ReadConfig(configFileName) + config, err := ReadConfig() if err != nil { log.Fatal().Err(err).Msg("failed to load settings during initialization") //nolint:gocritic // force exit } diff --git a/docs/docs/salt-exporter/configuration.md b/docs/docs/salt-exporter/configuration.md index dfad931..4a8dcb2 100644 --- a/docs/docs/salt-exporter/configuration.md +++ b/docs/docs/salt-exporter/configuration.md @@ -18,6 +18,8 @@ The salt-exporter can be configured with flags, environments variables and confi The exporter is looking for `config.yml`. +Note: You can specify a specific config filepath using `--config-file`, i.e. `--config-file="/srv/salt-exporter/config.yml"` + See below a full example of a configuration file: ``` { .yaml .copy } @@ -153,18 +155,22 @@ SALT_METRICS__GLOBAL__FILTERS__IGNORE_TEST=true ``` ./salt-exporter -help + -config-file string + config filepath (default "config.yml") -health-functions-filter string [DEPRECATED] apply filter on functions to monitor, separated by a comma (default "highstate") - -health-states-filter string - [DEPRECATED] apply filter on states to monitor, separated by a comma (default "highstate") -health-minions [DEPRECATED] enable minion metrics (default true) + -health-states-filter string + [DEPRECATED] apply filter on states to monitor, separated by a comma (default "highstate") -host string listen address -ignore-mock ignore mock=True events -ignore-test ignore test=True events + -ipc-file string + file location of the salt-master event bus (default "/var/run/salt/master/master_event_pub.ipc") -log-level string log level (debug, info, warn, error, fatal, panic, disabled) (default "info") -port int @@ -175,5 +181,7 @@ SALT_METRICS__GLOBAL__FILTERS__IGNORE_TEST=true TLS certificated -tls-key string TLS private key + -version + print version ``` diff --git a/e2e_test/docker-compose.yaml b/e2e_test/docker-compose.yaml index c9e8ff7..f55ce4d 100644 --- a/e2e_test/docker-compose.yaml +++ b/e2e_test/docker-compose.yaml @@ -1,5 +1,3 @@ -version: '3' - services: salt_master: image: saltstack/salt:3006.4