From c7880b9891999805f6b74e0066eda44db6d2c35d Mon Sep 17 00:00:00 2001 From: Sander Mol Date: Thu, 17 Feb 2022 20:41:44 +0100 Subject: [PATCH] feature: add go-sockaddr/template support for nomad consul address --- nomad/structs/config/consul.go | 8 ++++- nomad/structs/config/consul_test.go | 31 +++++++++++++++++++ website/content/docs/configuration/consul.mdx | 2 ++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/nomad/structs/config/consul.go b/nomad/structs/config/consul.go index f228c6321d42..f9224ae233c4 100644 --- a/nomad/structs/config/consul.go +++ b/nomad/structs/config/consul.go @@ -1,11 +1,13 @@ package config import ( + "fmt" "net/http" "strings" "time" consul "github.com/hashicorp/consul/api" + "github.com/hashicorp/go-sockaddr/template" "github.com/hashicorp/nomad/helper" ) @@ -251,7 +253,11 @@ func (c *ConsulConfig) ApiConfig() (*consul.Config, error) { // http.Transport. config := consul.DefaultConfig() if c.Addr != "" { - config.Address = c.Addr + out, err := template.Parse(c.Addr) + if err != nil { + panic(fmt.Errorf("unable to parse address template %q: %v", c.Addr, err)) + } + config.Address = out } if c.Token != "" { config.Token = c.Token diff --git a/nomad/structs/config/consul_test.go b/nomad/structs/config/consul_test.go index eb228814fd1c..79a501132968 100644 --- a/nomad/structs/config/consul_test.go +++ b/nomad/structs/config/consul_test.go @@ -5,10 +5,12 @@ import ( "fmt" "os" "os/exec" + "strings" "testing" "time" consulapi "github.com/hashicorp/consul/api" + sockaddr "github.com/hashicorp/go-sockaddr" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -167,3 +169,32 @@ func TestConsulConfig_Exec(t *testing.T) { require.NotNil(t, conf.VerifySSL) assert.True(t, *conf.VerifySSL) } + +func TestConsulConfig_SockAddr(t *testing.T) { + t.Parallel() + + privateIps, err := sockaddr.GetPrivateIPs() + if err != nil { + t.Fatalf("failed to get private IPs: %v", err) + } + privateIp := strings.Split(privateIps, " ")[0] + conf := ConsulConfig{ + Addr: "{{ GetPrivateInterfaces | limit 1 | attr \"address\" }}:8500", + } + + apiConf, err := conf.ApiConfig() + assert.Nil(t, err) + assert.Equal(t, apiConf.Address, privateIp+":8500") +} + +func TestConsulConfig_StringAddr(t *testing.T) { + t.Parallel() + + conf := ConsulConfig{ + Addr: "10.0.1.0:8500", + } + + apiConf, err := conf.ApiConfig() + assert.Nil(t, err) + assert.Equal(t, apiConf.Address, "10.0.1.0:8500") +} diff --git a/website/content/docs/configuration/consul.mdx b/website/content/docs/configuration/consul.mdx index a53cf6d89bb5..37b18509fc3c 100644 --- a/website/content/docs/configuration/consul.mdx +++ b/website/content/docs/configuration/consul.mdx @@ -44,6 +44,7 @@ configuring Nomad to talk to Consul via DNS such as consul.service.consul Consul agent, given in the format `host:port`. Supports Unix sockets with the format: `unix:///tmp/consul/consul.sock`. Will default to the `CONSUL_HTTP_ADDR` environment variable if set. + The value supports [go-sockaddr/template format][go-sockaddr/template]. - `allow_unauthenticated` `(bool: true)` - Specifies if users submitting jobs to the Nomad server should be required to provide their own Consul token, proving @@ -225,3 +226,4 @@ namespace "nomad-ns" { [consul]: https://www.consul.io/ 'Consul by HashiCorp' [bootstrap]: https://learn.hashicorp.com/tutorials/nomad/clustering 'Automatic Bootstrapping' +[go-sockaddr/template]: https://godoc.org/github.com/hashicorp/go-sockaddr/template