Skip to content

Commit

Permalink
feature: add go-sockaddr/template support for nomad consul address
Browse files Browse the repository at this point in the history
  • Loading branch information
Nkmol committed Feb 17, 2022
1 parent cc3a7bf commit c7880b9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
8 changes: 7 additions & 1 deletion 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"
)

Expand Down Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions nomad/structs/config/consul_test.go
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
}
2 changes: 2 additions & 0 deletions website/content/docs/configuration/consul.mdx
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit c7880b9

Please sign in to comment.