Skip to content

Commit

Permalink
only set a single label for now
Browse files Browse the repository at this point in the history
Other labels aren't strictly necessary here, and we may follow up with a
better way to customize.
  • Loading branch information
Mahmood Ali committed Oct 18, 2019
1 parent 487b0d8 commit 04a2e05
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
16 changes: 5 additions & 11 deletions drivers/docker/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ var (
)

const (
dockerLabelTaskID = "com.hashicorp.nomad.task_id"
dockerLabelTaskName = "com.hashicorp.nomad.task_name"
dockerLabelAllocID = "com.hashicorp.nomad.alloc_id"
dockerLabelJobName = "com.hashicorp.nomad.job_name"
dockerLabelAllocID = "com.hashicorp.nomad.alloc_id"
)

type Driver struct {
Expand Down Expand Up @@ -992,15 +989,12 @@ func (d *Driver) createContainerConfig(task *drivers.TaskConfig, driverConfig *T
config.Labels = driverConfig.Labels
}

config.Labels = map[string]string{
dockerLabelTaskID: task.ID,
dockerLabelTaskName: task.Name,
dockerLabelAllocID: task.AllocID,
dockerLabelJobName: task.JobName,
}
labels := make(map[string]string, len(driverConfig.Labels)+1)
for k, v := range driverConfig.Labels {
config.Labels[k] = v
labels[k] = v
}
labels[dockerLabelAllocID] = task.AllocID
config.Labels = labels
logger.Debug("applied labels on the container", "labels", config.Labels)

config.Env = task.EnvList()
Expand Down
13 changes: 7 additions & 6 deletions drivers/docker/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -905,8 +905,8 @@ func TestDockerDriver_Labels(t *testing.T) {
t.Fatalf("err: %v", err)
}

// expect to see 4 additional standard labels
require.Equal(t, len(cfg.Labels)+4, len(container.Config.Labels))
// expect to see 1 additional standard labels
require.Equal(t, len(cfg.Labels)+1, len(container.Config.Labels))
for k, v := range cfg.Labels {
require.Equal(t, v, container.Config.Labels[k])
}
Expand Down Expand Up @@ -1018,6 +1018,10 @@ func TestDockerDriver_CreateContainerConfig_Labels(t *testing.T) {

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

// com.hashicorp.nomad. labels are reserved and
// cannot be overridden
"com.hashicorp.nomad.alloc_id": "bad_value",
}

require.NoError(t, task.EncodeConcreteDriverConfig(cfg))
Expand All @@ -1032,10 +1036,7 @@ 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.job_name": "redis-demo-job",
"com.hashicorp.nomad.task_id": task.ID,
"com.hashicorp.nomad.task_name": "redis-demo",
"com.hashicorp.nomad.alloc_id": task.AllocID,
}

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

0 comments on commit 04a2e05

Please sign in to comment.