From e6b79faab6de3d4d3b4e59bb7b2a37335ec2d7a0 Mon Sep 17 00:00:00 2001 From: Ivan Pedrazas Date: Thu, 12 Jan 2017 16:42:28 +0000 Subject: [PATCH] add envvar fallback to non prefix --- plugin.go | 6 +++--- plugin_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/plugin.go b/plugin.go index d120d22..11b04a5 100644 --- a/plugin.go +++ b/plugin.go @@ -168,10 +168,10 @@ func replaceEnvvars(envvars [][]string, prefix string, s string) string { for _, envvar := range envvars { envvarName := envvar[0] envvarKey := envvar[2] - if prefix != "" { - envvarKey = prefix + "_" + envvarKey + envval := os.Getenv(prefix + "_" + envvarKey) + if envval == "" { + envval = os.Getenv(envvarKey) } - envval := os.Getenv(envvarKey) if strings.Contains(s, envvarName) { s = strings.Replace(s, envvarName, envval, -1) } diff --git a/plugin_test.go b/plugin_test.go index dc03ea1..b9a3718 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -167,3 +167,42 @@ func TestDetHelmInit(t *testing.T) { t.Error("Tiller not installed in proper namespace") } } + +func TestResolveSecretsFallback(t *testing.T) { + tag := "v0.1.1" + api := "http://apiserver" + os.Setenv("MY_TAG", tag) + os.Setenv("MY_API_SERVER", api) + os.Setenv("MY_TOKEN", "12345") + os.Setenv("NOTTOKEN", "99999") + + plugin := &Plugin{ + Config: Config{ + HelmCommand: nil, + Namespace: "default", + SkipTLSVerify: true, + Debug: true, + DryRun: true, + Chart: "./chart/test", + Release: "test-release", + Prefix: "MY", + Values: "image.tag=$TAG,api=${API_SERVER},nottoken=${NOTTOKEN},nameOverride=my-over-app,second.tag=${TAG}", + }, + } + + resolveSecrets(plugin) + // test that the subsitution works + if !strings.Contains(plugin.Config.Values, tag) { + t.Errorf("env var ${TAG} not resolved %s", tag) + } + if strings.Contains(plugin.Config.Values, "${TAG}") { + t.Errorf("env var ${TAG} not resolved %s", tag) + } + + if plugin.Config.APIServer != api { + t.Errorf("env var ${API_SERVER} not resolved %s", api) + } + if !strings.Contains(plugin.Config.Values, "99999") { + t.Errorf("envar ${NOTTOKEN} has not been resolved to 99999, not using prefix") + } +}