Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra Docker labels ( job name, task and task group name) #9885

Merged
merged 14 commits into from
Mar 8, 2021
Merged
9 changes: 8 additions & 1 deletion drivers/docker/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ var (
)

const (
dockerLabelAllocID = "com.hashicorp.nomad.alloc_id"
dockerLabelAllocID = "com.hashicorp.nomad.alloc_id"
dockerLabelJobName = "com.hashicorp.nomad.job_name"
dockerLabelTaskGroupName = "com.hashicorp.nomad.task_group_name"
dockerLabelTaskName = "com.hashicorp.nomad.task_name"
)

type Driver struct {
Expand Down Expand Up @@ -1115,6 +1118,10 @@ func (d *Driver) createContainerConfig(task *drivers.TaskConfig, driverConfig *T
labels[k] = v
}
labels[dockerLabelAllocID] = task.AllocID
labels[dockerLabelJobName] = task.JobName
labels[dockerLabelTaskGroupName] = task.TaskGroupName
labels[dockerLabelTaskName] = task.Name

config.Labels = labels
logger.Debug("applied labels on the container", "labels", config.Labels)

Expand Down
9 changes: 6 additions & 3 deletions drivers/docker/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,8 +799,8 @@ func TestDockerDriver_Labels(t *testing.T) {
t.Fatalf("err: %v", err)
}

// expect to see 1 additional standard labels
require.Equal(t, len(cfg.Labels)+1, len(container.Config.Labels))
// expect to see 4 additional standard labels (allocID, jobName, TaskGroupName and TaskName)
require.Equal(t, len(cfg.Labels)+4, len(container.Config.Labels))
for k, v := range cfg.Labels {
require.Equal(t, v, container.Config.Labels[k])
}
Expand Down Expand Up @@ -1066,7 +1066,10 @@ func TestDockerDriver_CreateContainerConfig_Labels(t *testing.T) {
// user provided labels
"user_label": "user_value",
// default labels
"com.hashicorp.nomad.alloc_id": task.AllocID,
"com.hashicorp.nomad.alloc_id": task.AllocID,
sofixa marked this conversation as resolved.
Show resolved Hide resolved
"com.hashicorp.nomad.job_name": task.JobName,
"com.hashicorp.nomad.task_group_name": task.TaskGroupName,
"com.hashicorp.nomad.task_name": task.Name,
}

require.Equal(t, expectedLabels, c.Config.Labels)
Expand Down