Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): config file is configurable #64

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ permissions:
contents: write

jobs:
build:
build-doc:

runs-on: ubuntu-latest

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ env:
COMPOSE_FILE: e2e_test/docker-compose.yaml

jobs:
build:
tests:
name: "Lint and test"
runs-on: ubuntu-latest
steps:
Expand Down
22 changes: 14 additions & 8 deletions cmd/salt-exporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/spf13/viper"
)

const defaultConfigFilename = "config.yml"
const defaultLogLevel = "info"
const defaultPort = 2112
const defaultHealthMinion = true
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -80,7 +82,7 @@ func parseFlags() bool {
os.Exit(0)
}

return *healthMinions
return *configFile, *healthMinions
}

func setDefaults(healthMinions bool) {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 22 additions & 4 deletions cmd/salt-exporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
4 changes: 1 addition & 3 deletions cmd/salt-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"github.com/rs/zerolog/log"
)

const configFileName = "config.yml"

var (
version = "unknown"
commit = "unknown"
Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 10 additions & 2 deletions docs/docs/salt-exporter/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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
Expand All @@ -175,5 +181,7 @@ SALT_METRICS__GLOBAL__FILTERS__IGNORE_TEST=true
TLS certificated
-tls-key string
TLS private key
-version
print version
```

2 changes: 0 additions & 2 deletions e2e_test/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3'

services:
salt_master:
image: saltstack/salt:3006.4
Expand Down
Loading