From 02af50f83271e76dbabf5a06747a0ec38c234dff Mon Sep 17 00:00:00 2001 From: Jonathan Ballet Date: Mon, 13 Mar 2017 21:40:37 +0100 Subject: [PATCH] Parse template before splitting host/port Ref: https://github.com/hashicorp/nomad/pull/2399/files/a33af1ca0be07b6be0a884c3a3ea2a402a377de3#r105444568 --- command/agent/config.go | 12 ++++++------ command/agent/config_test.go | 10 ++++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/command/agent/config.go b/command/agent/config.go index 6903a7b07..9623f1d5e 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -794,6 +794,11 @@ func normalizeBind(addr, bind string) (string, error) { // // Loopback is only considered a valid advertise address in dev mode. func normalizeAdvertise(addr string, bind string, defport int) (string, error) { + addr, err := parseSingleIPTemplate(addr) + if err != nil { + return "", fmt.Errorf("Error parsing advertise address template: %v", err) + } + if addr != "" { // Default to using manually configured address host, port, err := net.SplitHostPort(addr) @@ -805,12 +810,7 @@ func normalizeAdvertise(addr string, bind string, defport int) (string, error) { port = strconv.Itoa(defport) } - ipStr, err := parseSingleIPTemplate(host) - if err != nil { - return "", fmt.Errorf("Error parsing advertise address template: %v", err) - } - - return net.JoinHostPort(ipStr, port), nil + return net.JoinHostPort(host, port), nil } // Fallback to bind address, as it has been resolved before. diff --git a/command/agent/config_test.go b/command/agent/config_test.go index eaa33bb6e..3739133f4 100644 --- a/command/agent/config_test.go +++ b/command/agent/config_test.go @@ -612,8 +612,10 @@ func TestConfig_normalizeAddrs(t *testing.T) { RPC: 4647, Serf: 4648, }, - Addresses: &Addresses{}, - AdvertiseAddrs: &AdvertiseAddrs{}, + Addresses: &Addresses{}, + AdvertiseAddrs: &AdvertiseAddrs{ + RPC: "{{ GetPrivateIP }}:8888", + }, Server: &ServerConfig{ Enabled: true, }, @@ -627,8 +629,8 @@ func TestConfig_normalizeAddrs(t *testing.T) { t.Fatalf("expected HTTP advertise address %s:4646, got %s", c.BindAddr, c.AdvertiseAddrs.HTTP) } - if c.AdvertiseAddrs.RPC != fmt.Sprintf("%s:4647", c.BindAddr) { - t.Fatalf("expected RPC advertise address %s:4647, got %s", c.BindAddr, c.AdvertiseAddrs.RPC) + if c.AdvertiseAddrs.RPC != fmt.Sprintf("%s:8888", c.BindAddr) { + t.Fatalf("expected RPC advertise address %s:8888, got %s", c.BindAddr, c.AdvertiseAddrs.RPC) } if c.AdvertiseAddrs.Serf != fmt.Sprintf("%s:4648", c.BindAddr) {