Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose nomad namespace as environment variable in allocations #6192

Merged
merged 1 commit into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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