diff --git a/client/taskenv/env.go b/client/taskenv/env.go index 5b42a17a7533..4b40ff68e43d 100644 --- a/client/taskenv/env.go +++ b/client/taskenv/env.go @@ -57,6 +57,9 @@ const ( // Datacenter is the environment variable for passing the datacenter in which the alloc is running. Datacenter = "NOMAD_DC" + // Namespace is the environment variable for passing the namespace in which the alloc is running. + Namespace = "NOMAD_NAMESPACE" + // Region is the environment variable for passing the region in which the alloc is running. Region = "NOMAD_REGION" @@ -303,6 +306,7 @@ type Builder struct { taskName string allocIndex int datacenter string + namespace string region string allocId string allocName string @@ -407,6 +411,9 @@ func (b *Builder) Build() *TaskEnv { if b.datacenter != "" { envMap[Datacenter] = b.datacenter } + if b.namespace != "" { + envMap[Namespace] = b.namespace + } if b.region != "" { envMap[Region] = b.region @@ -559,6 +566,7 @@ func (b *Builder) setAlloc(alloc *structs.Allocation) *Builder { b.groupName = alloc.TaskGroup b.allocIndex = int(alloc.Index()) b.jobName = alloc.Job.Name + b.namespace = alloc.Namespace // Set meta combined := alloc.Job.CombinedTaskMeta(alloc.TaskGroup, b.taskName) diff --git a/client/taskenv/env_test.go b/client/taskenv/env_test.go index 531d8f168400..c089a3f91582 100644 --- a/client/taskenv/env_test.go +++ b/client/taskenv/env_test.go @@ -161,6 +161,7 @@ func TestEnvironment_AsList(t *testing.T) { }, }, } + a.Namespace = "not-default" task := a.Job.TaskGroups[0].Tasks[0] task.Env = map[string]string{ "taskEnvKey": "taskEnvVal", @@ -190,6 +191,7 @@ func TestEnvironment_AsList(t *testing.T) { "NOMAD_PORT_ssh_ssh=22", "NOMAD_CPU_LIMIT=500", "NOMAD_DC=dc1", + "NOMAD_NAMESPACE=not-default", "NOMAD_REGION=global", "NOMAD_MEMORY_LIMIT=256", "NOMAD_META_ELB_CHECK_INTERVAL=30s", @@ -301,6 +303,7 @@ func TestEnvironment_AsList_Old(t *testing.T) { "NOMAD_PORT_ssh_ssh=22", "NOMAD_CPU_LIMIT=500", "NOMAD_DC=dc1", + "NOMAD_NAMESPACE=default", "NOMAD_REGION=global", "NOMAD_MEMORY_LIMIT=256", "NOMAD_META_ELB_CHECK_INTERVAL=30s", @@ -418,6 +421,7 @@ func TestEnvironment_AllValues(t *testing.T) { "NOMAD_PORT_ssh_ssh": "22", "NOMAD_CPU_LIMIT": "500", "NOMAD_DC": "dc1", + "NOMAD_NAMESPACE": "default", "NOMAD_REGION": "global", "NOMAD_MEMORY_LIMIT": "256", "NOMAD_META_ELB_CHECK_INTERVAL": "30s", diff --git a/website/source/docs/runtime/_envvars.html.md.erb b/website/source/docs/runtime/_envvars.html.md.erb index 05968bad6d63..4cc2ec591d02 100644 --- a/website/source/docs/runtime/_envvars.html.md.erb +++ b/website/source/docs/runtime/_envvars.html.md.erb @@ -63,6 +63,10 @@ NOMAD_DC Datacenter in which the allocation is running + + NOMAD_NAMESPACE + Namespace in which the allocation is running + NOMAD_REGION Region in which the allocation is running