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
Show file tree
Hide file tree
Changes from 10 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
4 changes: 4 additions & 0 deletions client/allocrunner/taskrunner/task_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,11 @@ func (tr *TaskRunner) buildTaskConfig() *drivers.TaskConfig {
ID: fmt.Sprintf("%s/%s/%s", alloc.ID, task.Name, invocationid),
Name: task.Name,
JobName: alloc.Job.Name,
JobID: alloc.Job.ID,
TaskGroupName: alloc.TaskGroup,
Namespace: alloc.Namespace,
NodeName: alloc.NodeName,
NodeID: alloc.NodeID,
Resources: &drivers.Resources{
NomadResources: taskResources,
LinuxResources: &drivers.LinuxResources{
Expand Down
4 changes: 4 additions & 0 deletions drivers/docker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ var (
"ca": hclspec.NewAttr("ca", "string", false),
})),

// extra docker labels, globs supported
"extra_labels": hclspec.NewAttr("extra_labels", "list(string)", false),

// garbage collection options
// default needed for both if the gc {...} block is not set and
// if the default fields are missing
Expand Down Expand Up @@ -612,6 +615,7 @@ type DriverConfig struct {
DisableLogCollection bool `codec:"disable_log_collection"`
PullActivityTimeout string `codec:"pull_activity_timeout"`
pullActivityTimeoutDuration time.Duration `codec:"-"`
ExtraLabels []string `codec:"extra_labels"`

AllowRuntimesList []string `codec:"allow_runtimes"`
allowRuntimes map[string]struct{} `codec:"-"`
Expand Down
37 changes: 36 additions & 1 deletion drivers/docker/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/hashicorp/nomad/plugins/base"
"github.com/hashicorp/nomad/plugins/drivers"
pstructs "github.com/hashicorp/nomad/plugins/shared/structs"
"github.com/ryanuber/go-glob"
)

var (
Expand Down Expand Up @@ -70,7 +71,14 @@ var (
)

const (
dockerLabelAllocID = "com.hashicorp.nomad.alloc_id"
dockerLabelAllocID = "com.hashicorp.nomad.alloc_id"
dockerLabelJobName = "com.hashicorp.nomad.job_name"
dockerLabelJobID = "com.hashicorp.nomad.job_id"
dockerLabelTaskGroupName = "com.hashicorp.nomad.task_group_name"
dockerLabelTaskName = "com.hashicorp.nomad.task_name"
dockerLabelNamespace = "com.hashicorp.nomad.namespace"
dockerLabelNodeName = "com.hashicorp.nomad.node_name"
dockerLabelNodeID = "com.hashicorp.nomad.node_id"
)

type Driver struct {
Expand Down Expand Up @@ -1114,7 +1122,34 @@ func (d *Driver) createContainerConfig(task *drivers.TaskConfig, driverConfig *T
for k, v := range driverConfig.Labels {
labels[k] = v
}
// main mandatory label
labels[dockerLabelAllocID] = task.AllocID

//optional labels, as configured in plugin configuration
for _, configurationExtraLabel := range d.config.ExtraLabels {
if glob.Glob(configurationExtraLabel, strings.Split(dockerLabelJobName, ".")[3]) {
labels[dockerLabelJobName] = task.JobName
}
if glob.Glob(configurationExtraLabel, strings.Split(dockerLabelJobID, ".")[3]) {
labels[dockerLabelJobID] = task.JobID
}
if glob.Glob(configurationExtraLabel, strings.Split(dockerLabelTaskGroupName, ".")[3]) {
labels[dockerLabelTaskGroupName] = task.TaskGroupName
}
if glob.Glob(configurationExtraLabel, strings.Split(dockerLabelTaskName, ".")[3]) {
labels[dockerLabelTaskName] = task.Name
}
if glob.Glob(configurationExtraLabel, strings.Split(dockerLabelNamespace, ".")[3]) {
labels[dockerLabelNamespace] = task.Namespace
}
if glob.Glob(configurationExtraLabel, strings.Split(dockerLabelNodeName, ".")[3]) {
labels[dockerLabelNodeName] = task.NodeName
}
if glob.Glob(configurationExtraLabel, strings.Split(dockerLabelNodeID, ".")[3]) {
labels[dockerLabelNodeID] = task.NodeID
}
}

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

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

// expect to see 1 additional standard labels
// expect to see 1 additional standard labels (allocID)
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 @@ -1065,8 +1065,8 @@ func TestDockerDriver_CreateContainerConfig_Labels(t *testing.T) {
expectedLabels := map[string]string{
// user provided labels
"user_label": "user_value",
// default labels
"com.hashicorp.nomad.alloc_id": task.AllocID,
// default label
"com.hashicorp.nomad.alloc_id": task.AllocID,
sofixa marked this conversation as resolved.
Show resolved Hide resolved
}

require.Equal(t, expectedLabels, c.Config.Labels)
Expand Down
4 changes: 4 additions & 0 deletions plugins/drivers/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,12 @@ func (c *DNSConfig) Copy() *DNSConfig {
type TaskConfig struct {
ID string
JobName string
JobID string
TaskGroupName string
Name string
Namespace string
NodeName string
NodeID string
Env map[string]string
DeviceEnv map[string]string
Resources *Resources
Expand Down
6 changes: 6 additions & 0 deletions website/content/docs/drivers/docker.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,8 @@ plugin "docker" {
ca = "/etc/nomad/nomad.cert"
}

extra_labels = ["job_name", "job_id", "task_group_name", "task_name", "namespace", "node_name", "node_id"]

gc {
image = true
image_delay = "3m"
Expand Down Expand Up @@ -866,6 +868,10 @@ plugin "docker" {
capabilities and exclusively use host based log aggregation, you may consider
this option to disable nomad log collection overhead.

- `extra_labels` - Extra labels to add to Docker containers.
Available options are `job_name`, `job_id`, `task_group_name`, `task_name`,
`namespace`, `node_name`, `node_id`. Globs are supported ( e.g. `task*`)
sofixa marked this conversation as resolved.
Show resolved Hide resolved

- `gc` stanza:

- `image` - Defaults to `true`. Changing this to `false` will prevent Nomad
Expand Down