Skip to content

Commit

Permalink
Include parent job ID as a Docker container label hashicorp#17751
Browse files Browse the repository at this point in the history
  • Loading branch information
devashishTaneja committed Jul 7, 2023
1 parent 100c460 commit a004d33
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions client/allocrunner/taskrunner/task_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
4 changes: 4 additions & 0 deletions drivers/docker/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions drivers/docker/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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])
}
Expand Down
1 change: 1 addition & 0 deletions plugins/drivers/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ type TaskConfig struct {
JobName string
JobID string
TaskGroupName string
ParentJobName string
Name string // task.Name
Namespace string
NodeName string
Expand Down

0 comments on commit a004d33

Please sign in to comment.