From e4d8e3d3ad73af7c1b95d15b333ec783fc10c600 Mon Sep 17 00:00:00 2001 From: James Rasell Date: Thu, 7 Jun 2018 09:45:15 +0200 Subject: [PATCH 1/2] Add the local clients advertise address to interpolation env vars This commit adds the Nomad local client advertise address in the form host:port to the environment variables passed to each task. --- client/driver/env/env.go | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/client/driver/env/env.go b/client/driver/env/env.go index 19f934967..c0cada251 100644 --- a/client/driver/env/env.go +++ b/client/driver/env/env.go @@ -59,6 +59,10 @@ const ( // Region is the environment variable for passing the region in which the alloc is running. Region = "NOMAD_REGION" + // NomadAdvertiseAddr is the environment variable for passing the client HTTP + // advertise address. GH-4381. + NomadAdvertiseAddr = "NOMAD_ADVERTISE_ADDR" + // AddrPrefix is the prefix for passing both dynamic and static port // allocations to tasks. // E.g $NOMAD_ADDR_http=127.0.0.1:80 @@ -207,18 +211,19 @@ type Builder struct { // secretsDir from task's perspective; eg /secrets secretsDir string - cpuLimit int - memLimit int - taskName string - allocIndex int - datacenter string - region string - allocId string - allocName string - groupName string - vaultToken string - injectVaultToken bool - jobName string + cpuLimit int + memLimit int + taskName string + allocIndex int + datacenter string + region string + nomadAdvertiseAddr string + allocId string + allocName string + groupName string + vaultToken string + injectVaultToken bool + jobName string // otherPorts for tasks in the same alloc otherPorts map[string]string @@ -306,6 +311,9 @@ func (b *Builder) Build() *TaskEnv { // Copy region over to node attrs nodeAttrs[nodeRegionKey] = b.region } + if b.nomadAdvertiseAddr != "" { + envMap[NomadAdvertiseAddr] = b.nomadAdvertiseAddr + } // Build the network related env vars buildNetworkEnv(envMap, b.networks, b.driverNetwork) @@ -445,6 +453,7 @@ func (b *Builder) setNode(n *structs.Node) *Builder { b.nodeAttrs[nodeClassKey] = n.NodeClass b.nodeAttrs[nodeDcKey] = n.Datacenter b.datacenter = n.Datacenter + b.nomadAdvertiseAddr = n.HTTPAddr // Set up the attributes. for k, v := range n.Attributes { From 40a66756cdbc1e105d2aab36cb5b8da6668d32c5 Mon Sep 17 00:00:00 2001 From: James Rasell Date: Fri, 8 Jun 2018 09:44:10 +0200 Subject: [PATCH 2/2] Add 'nomad.advertise.address' to client meta via NomadFingerPrint This change removes the addition of the advertise address to the exported task env vars and instead moves this work into the NomadFingerprint.Fingerprint which adds this value to the client attrs. This can then be used within a Nomad job like ${attr.nomad.advertise.address}. --- client/driver/env/env.go | 33 ++++++++++++-------------------- client/fingerprint/nomad.go | 1 + client/fingerprint/nomad_test.go | 13 ++++++++++--- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/client/driver/env/env.go b/client/driver/env/env.go index c0cada251..19f934967 100644 --- a/client/driver/env/env.go +++ b/client/driver/env/env.go @@ -59,10 +59,6 @@ const ( // Region is the environment variable for passing the region in which the alloc is running. Region = "NOMAD_REGION" - // NomadAdvertiseAddr is the environment variable for passing the client HTTP - // advertise address. GH-4381. - NomadAdvertiseAddr = "NOMAD_ADVERTISE_ADDR" - // AddrPrefix is the prefix for passing both dynamic and static port // allocations to tasks. // E.g $NOMAD_ADDR_http=127.0.0.1:80 @@ -211,19 +207,18 @@ type Builder struct { // secretsDir from task's perspective; eg /secrets secretsDir string - cpuLimit int - memLimit int - taskName string - allocIndex int - datacenter string - region string - nomadAdvertiseAddr string - allocId string - allocName string - groupName string - vaultToken string - injectVaultToken bool - jobName string + cpuLimit int + memLimit int + taskName string + allocIndex int + datacenter string + region string + allocId string + allocName string + groupName string + vaultToken string + injectVaultToken bool + jobName string // otherPorts for tasks in the same alloc otherPorts map[string]string @@ -311,9 +306,6 @@ func (b *Builder) Build() *TaskEnv { // Copy region over to node attrs nodeAttrs[nodeRegionKey] = b.region } - if b.nomadAdvertiseAddr != "" { - envMap[NomadAdvertiseAddr] = b.nomadAdvertiseAddr - } // Build the network related env vars buildNetworkEnv(envMap, b.networks, b.driverNetwork) @@ -453,7 +445,6 @@ func (b *Builder) setNode(n *structs.Node) *Builder { b.nodeAttrs[nodeClassKey] = n.NodeClass b.nodeAttrs[nodeDcKey] = n.Datacenter b.datacenter = n.Datacenter - b.nomadAdvertiseAddr = n.HTTPAddr // Set up the attributes. for k, v := range n.Attributes { diff --git a/client/fingerprint/nomad.go b/client/fingerprint/nomad.go index 0a00cc026..a8415f69d 100644 --- a/client/fingerprint/nomad.go +++ b/client/fingerprint/nomad.go @@ -19,6 +19,7 @@ func NewNomadFingerprint(logger *log.Logger) Fingerprint { } func (f *NomadFingerprint) Fingerprint(req *cstructs.FingerprintRequest, resp *cstructs.FingerprintResponse) error { + resp.AddAttribute("nomad.advertise.address", req.Node.HTTPAddr) resp.AddAttribute("nomad.version", req.Config.Version.VersionNumber()) resp.AddAttribute("nomad.revision", req.Config.Version.Revision) resp.Detected = true diff --git a/client/fingerprint/nomad_test.go b/client/fingerprint/nomad_test.go index 2060fd8e2..8d2c04c03 100644 --- a/client/fingerprint/nomad_test.go +++ b/client/fingerprint/nomad_test.go @@ -11,17 +11,20 @@ import ( func TestNomadFingerprint(t *testing.T) { f := NewNomadFingerprint(testLogger()) - node := &structs.Node{ - Attributes: make(map[string]string), - } + v := "foo" r := "123" + h := "8.8.8.8:4646" c := &config.Config{ Version: &version.VersionInfo{ Revision: r, Version: v, }, } + node := &structs.Node{ + Attributes: make(map[string]string), + HTTPAddr: h, + } request := &cstructs.FingerprintRequest{Config: c, Node: node} var response cstructs.FingerprintResponse @@ -45,4 +48,8 @@ func TestNomadFingerprint(t *testing.T) { if response.Attributes["nomad.revision"] != r { t.Fatalf("incorrect revision") } + + if response.Attributes["nomad.advertise.address"] != h { + t.Fatalf("incorrect advertise address") + } }