diff --git a/client/allocrunner/taskrunner/task_runner.go b/client/allocrunner/taskrunner/task_runner.go index f4ece2366f1..f074a5657aa 100644 --- a/client/allocrunner/taskrunner/task_runner.go +++ b/client/allocrunner/taskrunner/task_runner.go @@ -1144,6 +1144,7 @@ func (tr *TaskRunner) buildTaskConfig() *drivers.TaskConfig { Namespace: alloc.Namespace, NodeName: alloc.NodeName, NodeID: alloc.NodeID, + ParentJobName: alloc.Job.ParentID, Resources: &drivers.Resources{ NomadResources: taskResources, LinuxResources: &drivers.LinuxResources{ diff --git a/drivers/docker/driver.go b/drivers/docker/driver.go index fb71f5b1e2a..07f5fe36301 100644 --- a/drivers/docker/driver.go +++ b/drivers/docker/driver.go @@ -74,6 +74,7 @@ const ( dockerLabelNamespace = "com.hashicorp.nomad.namespace" dockerLabelNodeName = "com.hashicorp.nomad.node_name" dockerLabelNodeID = "com.hashicorp.nomad.node_id" + dockerLabelParentJobID = "com.hashicorp.nomad.parent_job_id" ) type pauseContainerStore struct { @@ -1324,6 +1325,9 @@ func (d *Driver) createContainerConfig(task *drivers.TaskConfig, driverConfig *T if glob.Glob(configurationExtraLabel, "job_id") { labels[dockerLabelJobID] = task.JobID } + if glob.Glob(configurationExtraLabel, "parent_job_id") { + labels[dockerLabelParentJobID] = task.ParentJobName + } if glob.Glob(configurationExtraLabel, "task_group_name") { labels[dockerLabelTaskGroupName] = task.TaskGroupName } diff --git a/drivers/docker/driver_test.go b/drivers/docker/driver_test.go index 92e0bc373fb..d221685df2f 100644 --- a/drivers/docker/driver_test.go +++ b/drivers/docker/driver_test.go @@ -801,7 +801,7 @@ func TestDockerDriver_ExtraLabels(t *testing.T) { dockerClientConfig := make(map[string]interface{}) - dockerClientConfig["extra_labels"] = []string{"task*", "job_name"} + dockerClientConfig["extra_labels"] = []string{"task*", "job_name", "parent_job_id"} client, d, handle, cleanup := dockerSetup(t, task, dockerClientConfig) defer cleanup() require.NoError(t, d.WaitUntilStarted(task.ID, 5*time.Second)) @@ -816,10 +816,11 @@ func TestDockerDriver_ExtraLabels(t *testing.T) { "com.hashicorp.nomad.task_name": task.Name, "com.hashicorp.nomad.task_group_name": task.TaskGroupName, "com.hashicorp.nomad.job_name": task.JobName, + "com.hashicorp.nomad.parent_job_id": task.ParentJobName, } - // expect to see 4 labels (allocID by default, task_name and task_group_name due to task*, and job_name) - require.Equal(t, 4, len(container.Config.Labels)) + // expect to see 5 labels (allocID by default, task_name and task_group_name due to task*, and job_name) + require.Equal(t, 5, len(container.Config.Labels)) for k, v := range expectedLabels { require.Equal(t, v, container.Config.Labels[k]) } diff --git a/plugins/drivers/driver.go b/plugins/drivers/driver.go index e01b717ea77..fccabbe1e07 100644 --- a/plugins/drivers/driver.go +++ b/plugins/drivers/driver.go @@ -274,6 +274,7 @@ type TaskConfig struct { JobName string JobID string TaskGroupName string + ParentJobName string Name string // task.Name Namespace string NodeName string