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 27, 2019
1 parent d29fa2b commit a9db65e
Show file tree
Hide file tree
Showing 3 changed files with 16 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
4 changes: 4 additions & 0 deletions client/taskenv/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions website/source/docs/runtime/_envvars.html.md.erb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
<td><tt>NOMAD&lowbar;DC</tt></td>
<td>Datacenter in which the allocation is running</td>
</tr>
<tr>
<td><tt>NOMAD&lowbar;NAMESPACE</tt></td>
<td>Namespace in which the allocation is running</td>
</tr>
<tr>
<td><tt>NOMAD&lowbar;REGION</tt></td>
<td>Region in which the allocation is running</td>
Expand Down

0 comments on commit a9db65e

Please sign in to comment.