From 42068f88235755b5c93418cd12c47fb32a5e6b53 Mon Sep 17 00:00:00 2001 From: James Rasell Date: Wed, 20 Apr 2022 10:30:48 +0200 Subject: [PATCH] client: add NOMAD_SHORT_ALLOC_ID allocation env var. (#12603) --- .changelog/12603.txt | 3 +++ client/taskenv/env.go | 5 +++++ client/taskenv/env_test.go | 2 ++ website/content/partials/envvars.mdx | 10 ++++++++-- 4 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 .changelog/12603.txt diff --git a/.changelog/12603.txt b/.changelog/12603.txt new file mode 100644 index 000000000..5da2df476 --- /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/client/taskenv/env.go b/client/taskenv/env.go index eaed60567..d40b52ab9 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 5d9bf75e0..b149efdbd 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", diff --git a/website/content/partials/envvars.mdx b/website/content/partials/envvars.mdx index 9edca49a9..ef290c131 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)