diff --git a/client/taskenv/env.go b/client/taskenv/env.go index b1dde64f88d..cdea2f5e4f8 100644 --- a/client/taskenv/env.go +++ b/client/taskenv/env.go @@ -48,9 +48,15 @@ const ( // GroupName is the environment variable for passing the task group name. GroupName = "NOMAD_GROUP_NAME" + // JobID is the environment variable for passing the job ID. + JobID = "NOMAD_JOB_ID" + // JobName is the environment variable for passing the job name. JobName = "NOMAD_JOB_NAME" + // JobParentID is the environment variable for passing the ID of the parnt of the job + JobParentID = "NOMAD_JOB_PARENT_ID" + // AllocIndex is the environment variable for passing the allocation index. AllocIndex = "NOMAD_ALLOC_INDEX" @@ -323,7 +329,9 @@ type Builder struct { vaultToken string vaultNamespace string injectVaultToken bool + jobID string jobName string + jobParentID string // otherPorts for tasks in the same alloc otherPorts map[string]string @@ -417,9 +425,15 @@ func (b *Builder) Build() *TaskEnv { if b.taskName != "" { envMap[TaskName] = b.taskName } + if b.jobID != "" { + envMap[JobID] = b.jobID + } if b.jobName != "" { envMap[JobName] = b.jobName } + if b.jobParentID != "" { + envMap[JobParentID] = b.jobParentID + } if b.datacenter != "" { envMap[Datacenter] = b.datacenter } @@ -580,7 +594,9 @@ func (b *Builder) setAlloc(alloc *structs.Allocation) *Builder { b.allocName = alloc.Name b.groupName = alloc.TaskGroup b.allocIndex = int(alloc.Index()) + b.jobID = alloc.Job.ID b.jobName = alloc.Job.Name + b.jobParentID = alloc.Job.ParentID b.namespace = alloc.Namespace // Set meta diff --git a/client/taskenv/env_test.go b/client/taskenv/env_test.go index 08c96eb5452..34ac85dcc24 100644 --- a/client/taskenv/env_test.go +++ b/client/taskenv/env_test.go @@ -11,6 +11,7 @@ import ( hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2/gohcl" "github.com/hashicorp/hcl/v2/hclsyntax" + "github.com/hashicorp/nomad/helper/uuid" "github.com/hashicorp/nomad/nomad/mock" "github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/plugins/drivers" @@ -141,6 +142,7 @@ func TestEnvironment_AsList(t *testing.T) { "metaKey": "metaVal", } a := mock.Alloc() + a.Job.ParentID = fmt.Sprintf("mock-parent-service-%s", uuid.Generate()) a.AllocatedResources.Tasks["web"].Networks[0] = &structs.NetworkResource{ Device: "eth0", IP: "127.0.0.1", @@ -204,7 +206,9 @@ func TestEnvironment_AsList(t *testing.T) { "NOMAD_META_elb_check_type=http", "NOMAD_META_foo=bar", "NOMAD_META_owner=armon", + fmt.Sprintf("NOMAD_JOB_ID=%s", a.Job.ID), "NOMAD_JOB_NAME=my-job", + fmt.Sprintf("NOMAD_JOB_PARENT_ID=%s", a.Job.ParentID), fmt.Sprintf("NOMAD_ALLOC_ID=%s", a.ID), "NOMAD_ALLOC_INDEX=0", } @@ -320,6 +324,7 @@ func TestEnvironment_AsList_Old(t *testing.T) { "NOMAD_META_elb_check_type=http", "NOMAD_META_foo=bar", "NOMAD_META_owner=armon", + fmt.Sprintf("NOMAD_JOB_ID=%s", a.Job.ID), "NOMAD_JOB_NAME=my-job", fmt.Sprintf("NOMAD_ALLOC_ID=%s", a.ID), "NOMAD_ALLOC_INDEX=0", @@ -339,6 +344,7 @@ func TestEnvironment_AllValues(t *testing.T) { "invalid...metakey": "b", } a := mock.ConnectAlloc() + a.Job.ParentID = fmt.Sprintf("mock-parent-service-%s", uuid.Generate()) a.AllocatedResources.Tasks["web"].Networks[0] = &structs.NetworkResource{ Device: "eth0", IP: "127.0.0.1", @@ -463,7 +469,9 @@ func TestEnvironment_AllValues(t *testing.T) { "NOMAD_META_elb_check_type": "http", "NOMAD_META_foo": "bar", "NOMAD_META_owner": "armon", + "NOMAD_JOB_ID": a.Job.ID, "NOMAD_JOB_NAME": "my-job", + "NOMAD_JOB_PARENT_ID": a.Job.ParentID, "NOMAD_ALLOC_ID": a.ID, "NOMAD_ALLOC_INDEX": "0", "NOMAD_PORT_connect_proxy_testconnect": "9999", diff --git a/website/pages/docs/runtime/environment.mdx b/website/pages/docs/runtime/environment.mdx index a8ff11d615e..a328d67f9f9 100644 --- a/website/pages/docs/runtime/environment.mdx +++ b/website/pages/docs/runtime/environment.mdx @@ -22,11 +22,13 @@ environment variable names such as `NOMAD_ADDR__