From cf1b298f72b47c72235e4b291b07a014e9378c30 Mon Sep 17 00:00:00 2001 From: Kate Taggart Date: Wed, 8 Mar 2017 17:57:38 -0800 Subject: [PATCH 01/12] starting the jelly bean trail for two new env vars --- client/driver/env/env.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/client/driver/env/env.go b/client/driver/env/env.go index 845453b0094..d2a6372563b 100644 --- a/client/driver/env/env.go +++ b/client/driver/env/env.go @@ -48,6 +48,12 @@ const ( // AllocIndex is the environment variable for passing the allocation index. AllocIndex = "NOMAD_ALLOC_INDEX" + // Datacenter is the environment variable for passing the datacenter in which the alloc is running. + Datacenter = "NOMAD_DC" + + // Region is the environment variable for passing the region in which the alloc is running. + Region = "NOMAD_REGION" + // AddrPrefix is the prefix for passing both dynamic and static port // allocations to tasks. // E.g $NOMAD_ADDR_http=127.0.0.1:80 @@ -94,6 +100,8 @@ type TaskEnvironment struct { MemLimit int TaskName string AllocIndex int + Datacenter string + Region string AllocId string AllocName string Node *structs.Node From aeec7b35910ee978508ed2230b9dfad1348271e9 Mon Sep 17 00:00:00 2001 From: Kate Taggart Date: Wed, 29 Mar 2017 15:21:21 -0700 Subject: [PATCH 02/12] add dc and region to task env building function. flipped order of a couple functions to be consistent with the ordering of the rest of that section. --- client/driver/env/env.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/client/driver/env/env.go b/client/driver/env/env.go index d2a6372563b..08f8968e9bb 100644 --- a/client/driver/env/env.go +++ b/client/driver/env/env.go @@ -203,6 +203,12 @@ func (t *TaskEnvironment) Build() *TaskEnvironment { if t.JobName != "" { t.TaskEnv[JobName] = t.JobName } + if t.Datacenter != "" { + t.TaskEnv[Datacenter] = t.Datacenter + } + if t.Region != "" { + t.TaskEnv[Region] = t.Region + } // Build the addr of the other tasks if t.Alloc != nil { @@ -496,13 +502,13 @@ func (t *TaskEnvironment) SetTaskName(name string) *TaskEnvironment { return t } -func (t *TaskEnvironment) SetJobName(name string) *TaskEnvironment { - t.JobName = name +func (t *TaskEnvironment) ClearTaskName() *TaskEnvironment { + t.TaskName = "" return t } -func (t *TaskEnvironment) ClearTaskName() *TaskEnvironment { - t.TaskName = "" +func (t *TaskEnvironment) SetJobName(name string) *TaskEnvironment { + t.JobName = name return t } From a5c4663efbeb496de246f7a01872de1d5b93333a Mon Sep 17 00:00:00 2001 From: Kate Taggart Date: Wed, 29 Mar 2017 16:20:10 -0700 Subject: [PATCH 03/12] add helper functions to set and clear dc, region names. --- client/driver/env/env.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/client/driver/env/env.go b/client/driver/env/env.go index 08f8968e9bb..90b7d2cfe65 100644 --- a/client/driver/env/env.go +++ b/client/driver/env/env.go @@ -517,6 +517,26 @@ func (t *TaskEnvironment) ClearJobName() *TaskEnvironment { return t } +func (t *TaskEnvironment) SetDatacenterName(name string) *TaskEnvironment { + t.JobName = name + return t +} + +func (t *TaskEnvironment) ClearDatacenterName() *TaskEnvironment { + t.JobName = "" + return t +} + +func (t *TaskEnvironment) SetRegionName(name string) *TaskEnvironment { + t.JobName = name + return t +} + +func (t *TaskEnvironment) ClearRegionName() *TaskEnvironment { + t.JobName = "" + return t +} + func (t *TaskEnvironment) SetVaultToken(token string, inject bool) *TaskEnvironment { t.VaultToken = token t.InjectVaultToken = inject From d10bfb46c19e846939479ca672fd94044f0dc068 Mon Sep 17 00:00:00 2001 From: Kate Taggart Date: Wed, 29 Mar 2017 16:27:08 -0700 Subject: [PATCH 04/12] just found out dc and region are going to be a bit different, as Region didn't previously exist in the Node struct. --- client/driver/driver.go | 2 ++ nomad/structs/structs.go | 3 +++ 2 files changed, 5 insertions(+) diff --git a/client/driver/driver.go b/client/driver/driver.go index ee28888cfad..d48fb007a67 100644 --- a/client/driver/driver.go +++ b/client/driver/driver.go @@ -295,6 +295,8 @@ func GetTaskEnv(taskDir *allocdir.TaskDir, node *structs.Node, SetJobName(alloc.Job.Name). SetEnvvars(task.Env). SetTaskName(task.Name) + SetDatacenterName(node.Datacenter) + SetRegionName(node.Region) // Vary paths by filesystem isolation used drv, err := NewDriver(task.Driver, NewEmptyDriverContext()) diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index 924c39009e0..357c953327d 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -735,6 +735,9 @@ type Node struct { // Datacenter for this node Datacenter string + // Region for this node + Region string + // Node name Name string From 530cf0317766f9b75bbe909b0b69e7091a4d2ced Mon Sep 17 00:00:00 2001 From: Kate Taggart Date: Wed, 29 Mar 2017 16:31:23 -0700 Subject: [PATCH 05/12] allow region to also be interpolated, like datacenter. --- client/driver/env/env.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/client/driver/env/env.go b/client/driver/env/env.go index 90b7d2cfe65..a8ccca2497d 100644 --- a/client/driver/env/env.go +++ b/client/driver/env/env.go @@ -78,10 +78,11 @@ const ( // The node values that can be interpreted. const ( - nodeIdKey = "node.unique.id" - nodeDcKey = "node.datacenter" - nodeNameKey = "node.unique.name" - nodeClassKey = "node.class" + nodeIdKey = "node.unique.id" + nodeDcKey = "node.datacenter" + nodeRegionKey = "node.region" + nodeNameKey = "node.unique.name" + nodeClassKey = "node.class" // Prefixes used for lookups. nodeAttributePrefix = "attr." @@ -241,6 +242,7 @@ func (t *TaskEnvironment) Build() *TaskEnvironment { // Set up the node values. t.NodeValues[nodeIdKey] = t.Node.ID t.NodeValues[nodeDcKey] = t.Node.Datacenter + t.NodeValues[nodeRegionKey] = t.Node.Region t.NodeValues[nodeNameKey] = t.Node.Name t.NodeValues[nodeClassKey] = t.Node.NodeClass From 4a4417d556fa413ff7c7e53cfc98d0a075452b2e Mon Sep 17 00:00:00 2001 From: Kate Taggart Date: Thu, 30 Mar 2017 14:15:06 -0700 Subject: [PATCH 06/12] I think I did it. --- client/driver/driver.go | 4 ++-- client/driver/env/env.go | 8 ++++---- command/agent/agent.go | 1 + 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/client/driver/driver.go b/client/driver/driver.go index d48fb007a67..8c03284a7c6 100644 --- a/client/driver/driver.go +++ b/client/driver/driver.go @@ -293,10 +293,10 @@ func GetTaskEnv(taskDir *allocdir.TaskDir, node *structs.Node, env := env.NewTaskEnvironment(node). SetTaskMeta(alloc.Job.CombinedTaskMeta(alloc.TaskGroup, task.Name)). SetJobName(alloc.Job.Name). + SetDatacenterName(node.Datacenter). + SetRegionName(node.Region). SetEnvvars(task.Env). SetTaskName(task.Name) - SetDatacenterName(node.Datacenter) - SetRegionName(node.Region) // Vary paths by filesystem isolation used drv, err := NewDriver(task.Driver, NewEmptyDriverContext()) diff --git a/client/driver/env/env.go b/client/driver/env/env.go index a8ccca2497d..f0c12fc591f 100644 --- a/client/driver/env/env.go +++ b/client/driver/env/env.go @@ -520,22 +520,22 @@ func (t *TaskEnvironment) ClearJobName() *TaskEnvironment { } func (t *TaskEnvironment) SetDatacenterName(name string) *TaskEnvironment { - t.JobName = name + t.Datacenter = name return t } func (t *TaskEnvironment) ClearDatacenterName() *TaskEnvironment { - t.JobName = "" + t.Datacenter = "" return t } func (t *TaskEnvironment) SetRegionName(name string) *TaskEnvironment { - t.JobName = name + t.Region = name return t } func (t *TaskEnvironment) ClearRegionName() *TaskEnvironment { - t.JobName = "" + t.Region = "" return t } diff --git a/command/agent/agent.go b/command/agent/agent.go index 8080591e694..6c962067cba 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -280,6 +280,7 @@ func (a *Agent) clientConfig() (*clientconfig.Config, error) { // Setup the node conf.Node = new(structs.Node) conf.Node.Datacenter = a.config.Datacenter + conf.Node.Region = a.config.Region conf.Node.Name = a.config.NodeName conf.Node.Meta = a.config.Client.Meta conf.Node.NodeClass = a.config.Client.NodeClass From 9eac667642097c987265f418067ebac1fc16053e Mon Sep 17 00:00:00 2001 From: Kate Taggart Date: Thu, 30 Mar 2017 15:43:39 -0700 Subject: [PATCH 07/12] mention the two newly available env vars in the docs. --- website/source/docs/runtime/environment.html.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/website/source/docs/runtime/environment.html.md b/website/source/docs/runtime/environment.html.md index 1b62d7af98f..b11d075d6df 100644 --- a/website/source/docs/runtime/environment.html.md +++ b/website/source/docs/runtime/environment.html.md @@ -60,6 +60,14 @@ environment variables. `NOMAD_JOB_NAME` The job's name + + `NOMAD_DC` + The datacenter in which the job is running. + + + `NOMAD_REGION` + The region in which the job is running. + `NOMAD_IP_