From 5aaa0798e6fe0297afd7840a9d1ca18bedde8baa Mon Sep 17 00:00:00 2001 From: Matt Loberg Date: Mon, 30 Jan 2023 18:20:04 -0600 Subject: [PATCH 1/2] fix(env): expand variables Some of our variables reference other variables, which expanded when using consul-template --- env.go | 30 ++++++++++++++++++++++-------- env_test.go | 4 ++++ go.mod | 2 ++ go.sum | 4 ++++ 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/env.go b/env.go index 5d83d89..6e2ef62 100644 --- a/env.go +++ b/env.go @@ -3,6 +3,8 @@ package main import ( "fmt" "os" + + "github.com/samber/lo" ) // EnvMap represents a map for environment variables @@ -28,13 +30,25 @@ func (e *EnvMap) Add(key, value string) { } // Environ returns the map in the format of "key=value", skipping any already set, -// non-empty environment variables +// non-empty environment variables, and expanding variables func (e *EnvMap) Environ() []string { - env := []string{} - for k, v := range e.env { - if x := os.Getenv(k); x == "" { - env = append(env, fmt.Sprintf("%s=%s", k, v)) - } - } - return env + env := lo.OmitBy(e.env, func(k string, _ string) bool { + return os.Getenv(k) != "" + }) + + env = lo.MapValues(env, func(v string, _ string) string { + return os.Expand(v, func(s string) string { + if l := os.Getenv(s); l != "" { + return l + } + if v, ok := e.env[s]; ok { + return v + } + return "" + }) + }) + + return lo.MapToSlice(env, func(k string, v string) string { + return fmt.Sprintf("%s=%s", k, v) + }) } diff --git a/env_test.go b/env_test.go index a0e9684..2ef0591 100644 --- a/env_test.go +++ b/env_test.go @@ -20,9 +20,13 @@ func TestEnvMap(t *testing.T) { "DOCKER_CONSUL_BOOTSTRAP_TEST_BAZ": "bar", }) e.Add("DOCKER_CONSUL_BOOTSTRAP_TEST_BAR", "testing") + e.Add("DOCKER_CONSUL_BOOTSTRAP_EXPAND_ONE", "foo-${DOCKER_CONSUL_BOOTSTRAP_TEST_FOO}-bar") + e.Add("DOCKER_CONSUL_BOOTSTRAP_EXPAND_TWO", "foo-${DOCKER_CONSUL_BOOTSTRAP_TEST_BAZ}-baz") assert.ElementsMatch(t, []string{ "DOCKER_CONSUL_BOOTSTRAP_TEST_TEST=testing", "DOCKER_CONSUL_BOOTSTRAP_TEST_BAZ=bar", + "DOCKER_CONSUL_BOOTSTRAP_EXPAND_ONE=foo-ignore-bar", + "DOCKER_CONSUL_BOOTSTRAP_EXPAND_TWO=foo-bar-baz", }, e.Environ()) } diff --git a/go.mod b/go.mod index 2827a8e..915286b 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/hashicorp/vault/api/auth/aws v0.3.0 github.com/hashicorp/vault/api/auth/kubernetes v0.3.0 github.com/rs/zerolog v1.29.0 + github.com/samber/lo v1.37.0 github.com/stretchr/testify v1.8.1 ) @@ -69,6 +70,7 @@ require ( github.com/stretchr/objx v0.5.0 // indirect go.uber.org/atomic v1.9.0 // indirect golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect + golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect golang.org/x/net v0.0.0-20211216030914-fe4d6282115f // indirect golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect golang.org/x/text v0.3.8 // indirect diff --git a/go.sum b/go.sum index 18696e7..5d01e2e 100644 --- a/go.sum +++ b/go.sum @@ -301,6 +301,8 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk= github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc= +github.com/samber/lo v1.37.0 h1:XjVcB8g6tgUp8rsPsJ2CvhClfImrpL04YpQHXeHPhRw= +github.com/samber/lo v1.37.0/go.mod h1:9vaz2O4o8oOnK23pd2TrXufcbdbJIa3b6cstBWKpopA= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 h1:nn5Wsu0esKSJiIVhscUtVbo7ada43DJhG55ua/hjS5I= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= @@ -330,6 +332,8 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM= +golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= From 6c9b8e4886bf7ba64adab1aba25fefc9187b5f72 Mon Sep 17 00:00:00 2001 From: Matt Loberg Date: Mon, 30 Jan 2023 18:22:14 -0600 Subject: [PATCH 2/2] feat!: add PROCESSOR_COUNT env var Some of our services use a PROCESSOR_COUNT env var to help determine how many processes to run (e.g. bundler, etc). In the old consul-template version this was a shell call. Use runtime to set this value instead BREAKING CHANGE: adds PROCESSOR_COUNT which will override any Consul or Vault value --- main.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/main.go b/main.go index e522480..2ed2b27 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,8 @@ import ( "errors" "os" "os/exec" + "runtime" + "strconv" "strings" "github.com/rs/zerolog" @@ -59,6 +61,8 @@ func main() { logger.Warn().Msg("Not loading values from Vault. VAULT_ADDR is not set") } + env.Add("PROCESSOR_COUNT", strconv.Itoa(runtime.NumCPU())) + exit := run(os.Args[1], os.Args[2:], env.Environ(), logger) os.Exit(exit) }