Skip to content

Commit

Permalink
expose nomad namespace as environment variable in allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
jazzyfresh committed Aug 15, 2019
1 parent aaba483 commit 7e3ad90
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
8 changes: 8 additions & 0 deletions client/taskenv/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -303,6 +306,7 @@ type Builder struct {
taskName string
allocIndex int
datacenter string
namespace string
region string
allocId string
allocName string
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions client/taskenv/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func TestEnvironment_AsList(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",
Expand Down Expand Up @@ -301,6 +302,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",
Expand Down Expand Up @@ -418,6 +420,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",
Expand Down

0 comments on commit 7e3ad90

Please sign in to comment.