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

drivers/docker: labels containers with task info #5153

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions drivers/docker/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -859,11 +859,18 @@ func (d *Driver) createContainerConfig(task *drivers.TaskConfig, driverConfig *T
config.Cmd = driverConfig.Args
}

if len(driverConfig.Labels) > 0 {
config.Labels = driverConfig.Labels
logger.Debug("applied labels on the container", "labels", config.Labels)
config.Labels = map[string]string{
"com.hashicorp.nomad.task_id": task.ID,
"com.hashicorp.nomad.task_name": task.Name,
"com.hashicorp.nomad.alloc_id": task.AllocID,
"com.hashicorp.nomad.job_name": task.JobName,
}

for k, v := range driverConfig.Labels {
config.Labels[k] = v
}
logger.Debug("applied labels on the container", "labels", config.Labels)

config.Env = task.EnvList()

containerName := strings.Replace(task.ID, "/", "_", -1)
Expand Down
38 changes: 34 additions & 4 deletions drivers/docker/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1067,11 +1067,8 @@ func TestDockerDriver_Labels(t *testing.T) {
require.NoError(t, d.WaitUntilStarted(task.ID, 5*time.Second))

container, err := client.InspectContainer(handle.containerID)
if err != nil {
t.Fatalf("err: %v", err)
}
require.NoError(t, err)

require.Equal(t, 2, len(container.Config.Labels))
for k, v := range cfg.Labels {
require.Equal(t, v, container.Config.Labels[k])
}
Expand Down Expand Up @@ -1168,6 +1165,39 @@ func TestDockerDriver_CreateContainerConfig(t *testing.T) {
require.Equal(t, "org/repo:0.1", c.Config.Image)
require.EqualValues(t, opt, c.HostConfig.StorageOpt)
}

func TestDockerDriver_CreateContainerConfig_Labels(t *testing.T) {
t.Parallel()

task, cfg, _ := dockerTask(t)
task.AllocID = uuid.Generate()
task.JobName = "redis-demo-job"

cfg.Labels = map[string]string{
"user_label": "user_value",
}

require.NoError(t, task.EncodeConcreteDriverConfig(cfg))

dh := dockerDriverHarness(t, nil)
driver := dh.Impl().(*Driver)

c, err := driver.createContainerConfig(task, cfg, "org/repo:0.1")
require.NoError(t, err)

expectedLabels := map[string]string{
// user provided labels
"user_label": "user_value",
// default labels
"com.hashicorp.nomad.alloc_id": task.AllocID,
"com.hashicorp.nomad.job_name": "redis-demo-job",
"com.hashicorp.nomad.task_id": task.ID,
"com.hashicorp.nomad.task_name": "redis-demo",
}

require.Equal(t, expectedLabels, c.Config.Labels)
}

func TestDockerDriver_Capabilities(t *testing.T) {
if !tu.IsTravis() {
t.Parallel()
Expand Down