From f35e6f92195a7b7e2ce3a06401160499f3d342fa Mon Sep 17 00:00:00 2001 From: James Rasell Date: Tue, 19 Apr 2022 10:26:45 +0100 Subject: [PATCH 1/2] client: add NOMAD_SHORT_ALLOC_ID allocation env var. --- client/taskenv/env.go | 5 +++++ client/taskenv/env_test.go | 2 ++ 2 files changed, 7 insertions(+) diff --git a/client/taskenv/env.go b/client/taskenv/env.go index eaed60567a29..d40b52ab95c4 100644 --- a/client/taskenv/env.go +++ b/client/taskenv/env.go @@ -47,6 +47,10 @@ const ( // AllocID is the environment variable for passing the allocation ID. AllocID = "NOMAD_ALLOC_ID" + // ShortAllocID is the environment variable for passing the short version + // of the allocation ID. + ShortAllocID = "NOMAD_SHORT_ALLOC_ID" + // AllocName is the environment variable for passing the allocation name. AllocName = "NOMAD_ALLOC_NAME" @@ -505,6 +509,7 @@ func (b *Builder) buildEnv(allocDir, localDir, secretsDir string, // Add the task metadata if b.allocId != "" { envMap[AllocID] = b.allocId + envMap[ShortAllocID] = b.allocId[:8] } if b.allocName != "" { envMap[AllocName] = b.allocName diff --git a/client/taskenv/env_test.go b/client/taskenv/env_test.go index 5d9bf75e09e9..b149efdbd007 100644 --- a/client/taskenv/env_test.go +++ b/client/taskenv/env_test.go @@ -238,6 +238,7 @@ func TestEnvironment_AsList(t *testing.T) { "NOMAD_JOB_NAME=my-job", fmt.Sprintf("NOMAD_JOB_PARENT_ID=%s", a.Job.ParentID), fmt.Sprintf("NOMAD_ALLOC_ID=%s", a.ID), + fmt.Sprintf("NOMAD_SHORT_ALLOC_ID=%s", a.ID[:8]), "NOMAD_ALLOC_INDEX=0", } sort.Strings(act) @@ -404,6 +405,7 @@ func TestEnvironment_AllValues(t *testing.T) { "NOMAD_JOB_NAME": "my-job", "NOMAD_JOB_PARENT_ID": a.Job.ParentID, "NOMAD_ALLOC_ID": a.ID, + "NOMAD_SHORT_ALLOC_ID": a.ID[:8], "NOMAD_ALLOC_INDEX": "0", "NOMAD_PORT_connect_proxy_testconnect": "9999", "NOMAD_HOST_PORT_connect_proxy_testconnect": "9999", From 300078e65f5a9e90edc290b353917d2e0e088a8c Mon Sep 17 00:00:00 2001 From: James Rasell Date: Tue, 19 Apr 2022 11:17:08 +0100 Subject: [PATCH 2/2] docs: add changelog and website update for #12603 --- .changelog/12603.txt | 3 +++ website/content/partials/envvars.mdx | 10 ++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 .changelog/12603.txt diff --git a/.changelog/12603.txt b/.changelog/12603.txt new file mode 100644 index 000000000000..5da2df476f91 --- /dev/null +++ b/.changelog/12603.txt @@ -0,0 +1,3 @@ +```release-note:improvement +client: Added `NOMAD_SHORT_ALLOC_ID` allocation env var +``` diff --git a/website/content/partials/envvars.mdx b/website/content/partials/envvars.mdx index 9edca49a950a..ef290c131beb 100644 --- a/website/content/partials/envvars.mdx +++ b/website/content/partials/envvars.mdx @@ -69,6 +69,12 @@ Allocation ID of the task + + + NOMAD_SHORT_ALLOC_ID + + The first 8 characters of the allocation ID of the task + NOMAD_ALLOC_NAME @@ -166,7 +172,7 @@ - Network-related Variables + Network-related Variables @@ -300,7 +306,7 @@ - Consul-related Variables (only set for connect native tasks) + Consul-related Variables (only set for connect native tasks)