Skip to content

Commit

Permalink
Merge pull request #364 from vshn/fix357
Browse files Browse the repository at this point in the history
Fix loading of Operator environment variables
  • Loading branch information
ccremer authored Feb 16, 2021
2 parents 5708ef7 + 8419801 commit fe5b188
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"os"
"strings"

Expand Down Expand Up @@ -136,19 +137,19 @@ func loadEnvironmentVariables() {
prefix := "BACKUP_"
// Load environment variables
err := koanfInstance.Load(env.Provider(prefix, ".", func(s string) string {
s = strings.TrimLeft(s, prefix)
s = strings.TrimPrefix(s, prefix)
s = strings.Replace(strings.ToLower(s), "_", "-", -1)
return s
}), nil)
if err != nil {
setupLog.Error(err, "could not load environment variables")
_, _ = fmt.Fprintf(os.Stderr, "could not load environment variables: %v\n", err)
}

if err := koanfInstance.UnmarshalWithConf("", &cfg.Config, koanf.UnmarshalConf{Tag: "koanf", FlatPaths: true}); err != nil {
setupLog.Error(err, "could not merge defaults with settings from environment variables")
_, _ = fmt.Fprintf(os.Stderr, "could not merge defaults with settings from environment variables: %v\n", err)
}
if err := cfg.Config.ValidateSyntax(); err != nil {
setupLog.Error(err, "settings invalid")
_, _ = fmt.Fprintf(os.Stderr, "settings invalid: %v\n", err)
os.Exit(2)
}
}
50 changes: 50 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/vshn/k8up/cfg"
)

func Test_loadEnvironmentVariables(t *testing.T) {
tests := map[string]struct {
givenKey string
givenValue string
assertConfig func(t *testing.T, c *cfg.Configuration)
}{
"GivenStringVariable_WhenLoading_ThenParseStringVar": {
givenKey: "BACKUP_PROMURL",
givenValue: "https://prometheus:9090/metrics",
assertConfig: func(t *testing.T, c *cfg.Configuration) {
assert.Equal(t, "https://prometheus:9090/metrics", c.PromURL)
},
},
"GivenNumericEnvironmentVariable_WhenLoading_ThenParseIntVar": {
givenKey: "BACKUP_GLOBALKEEPJOBS",
givenValue: "2",
assertConfig: func(t *testing.T, c *cfg.Configuration) {
assert.Equal(t, 2, c.GlobalKeepJobs)
},
},
"GivenBooleanEnvironmentVariable_WhenLoading_ThenParseBoolVar": {
givenKey: "BACKUP_ENABLE_LEADER_ELECTION",
givenValue: "false",
assertConfig: func(t *testing.T, c *cfg.Configuration) {
assert.True(t, cfg.NewDefaultConfig().EnableLeaderElection) // To ensure it's not asserting the default
assert.False(t, c.EnableLeaderElection)
},
},
}
cfg.Config.OperatorNamespace = "not-part-of-this-test"
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
require.NoError(t, os.Setenv(tt.givenKey, tt.givenValue))
loadEnvironmentVariables()
tt.assertConfig(t, cfg.Config)
})
}
}

0 comments on commit fe5b188

Please sign in to comment.