mirror of
https://github.com/grafana/grafana.git
synced 2025-08-03 01:42:12 +08:00
Add ability to override config variables with env variables (#32554)
* Add ability to override config variables with env variables * Inline checkForOverrides * Update pkg/plugins/backendplugin/manager/plugin_settings.go Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com> Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:

committed by
GitHub

parent
6a3faad0b0
commit
d42bfedd9e
@ -2,6 +2,7 @@ package manager
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
@ -10,9 +11,14 @@ import (
|
||||
type pluginSettings map[string]string
|
||||
|
||||
func (ps pluginSettings) ToEnv(prefix string, hostEnv []string) []string {
|
||||
env := []string{}
|
||||
var env []string
|
||||
for k, v := range ps {
|
||||
env = append(env, fmt.Sprintf("%s_%s=%s", prefix, strings.ToUpper(k), v))
|
||||
key := fmt.Sprintf("%s_%s", prefix, strings.ToUpper(k))
|
||||
if value := os.Getenv(key); value != "" {
|
||||
v = value
|
||||
}
|
||||
|
||||
env = append(env, fmt.Sprintf("%s=%s", key, v))
|
||||
}
|
||||
|
||||
env = append(env, hostEnv...)
|
||||
|
@ -1,6 +1,7 @@
|
||||
package manager
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
@ -41,5 +42,55 @@ func TestPluginSettings(t *testing.T) {
|
||||
require.Len(t, env, 3)
|
||||
require.EqualValues(t, []string{"GF_PLUGIN_KEY1=value1", "GF_PLUGIN_KEY2=value2", "GF_VERSION=6.7.0"}, env)
|
||||
})
|
||||
|
||||
t.Run("Should override config variable with environment variable ", func(t *testing.T) {
|
||||
_ = os.Setenv("GF_PLUGIN_KEY1", "sth")
|
||||
t.Cleanup(func() {
|
||||
_ = os.Unsetenv("GF_PLUGIN_KEY1")
|
||||
})
|
||||
|
||||
ps := getPluginSettings("plugin", cfg)
|
||||
env := ps.ToEnv("GF_PLUGIN", []string{"GF_VERSION=6.7.0"})
|
||||
sort.Strings(env)
|
||||
require.Len(t, env, 3)
|
||||
require.EqualValues(t, []string{"GF_PLUGIN_KEY1=sth", "GF_PLUGIN_KEY2=value2", "GF_VERSION=6.7.0"}, env)
|
||||
})
|
||||
|
||||
t.Run("Config variable doesn't match env variable ", func(t *testing.T) {
|
||||
_ = os.Setenv("GF_PLUGIN_KEY3", "value3")
|
||||
t.Cleanup(func() {
|
||||
_ = os.Unsetenv("GF_PLUGIN_KEY3")
|
||||
})
|
||||
|
||||
ps := getPluginSettings("plugin", cfg)
|
||||
env := ps.ToEnv("GF_PLUGIN", []string{"GF_VERSION=6.7.0"})
|
||||
sort.Strings(env)
|
||||
require.Len(t, env, 3)
|
||||
require.EqualValues(t, []string{"GF_PLUGIN_KEY1=value1", "GF_PLUGIN_KEY2=value2", "GF_VERSION=6.7.0"}, env)
|
||||
})
|
||||
|
||||
t.Run("Should override missing config variable with environment variable ", func(t *testing.T) {
|
||||
cfg := &setting.Cfg{
|
||||
PluginSettings: setting.PluginSettings{
|
||||
"plugin": map[string]string{
|
||||
"key1": "value1",
|
||||
"key2": "",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
ps := getPluginSettings("plugin", cfg)
|
||||
require.Len(t, ps, 2)
|
||||
|
||||
_ = os.Setenv("GF_PLUGIN_KEY2", "sth")
|
||||
t.Cleanup(func() {
|
||||
_ = os.Unsetenv("GF_PLUGIN_KEY1")
|
||||
})
|
||||
|
||||
env := ps.ToEnv("GF_PLUGIN", []string{"GF_VERSION=6.7.0"})
|
||||
sort.Strings(env)
|
||||
require.Len(t, env, 3)
|
||||
require.EqualValues(t, []string{"GF_PLUGIN_KEY1=value1", "GF_PLUGIN_KEY2=sth", "GF_VERSION=6.7.0"}, env)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user