POC: allow to add labels for docker containers from job metadata
This commit is contained in:
parent
88d64f5d9b
commit
669635778a
|
@ -14,6 +14,7 @@ import (
|
||||||
"github.com/hashicorp/nomad/client/allocdir"
|
"github.com/hashicorp/nomad/client/allocdir"
|
||||||
"github.com/hashicorp/nomad/client/config"
|
"github.com/hashicorp/nomad/client/config"
|
||||||
"github.com/hashicorp/nomad/client/driver/args"
|
"github.com/hashicorp/nomad/client/driver/args"
|
||||||
|
"github.com/hashicorp/nomad/client/driver/environment"
|
||||||
"github.com/hashicorp/nomad/client/fingerprint"
|
"github.com/hashicorp/nomad/client/fingerprint"
|
||||||
"github.com/hashicorp/nomad/nomad/structs"
|
"github.com/hashicorp/nomad/nomad/structs"
|
||||||
)
|
)
|
||||||
|
@ -43,6 +44,10 @@ func NewDockerDriver(ctx *DriverContext) Driver {
|
||||||
return &DockerDriver{DriverContext: *ctx}
|
return &DockerDriver{DriverContext: *ctx}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
labelPrefix = environment.MetaPrefix + "LABEL_"
|
||||||
|
)
|
||||||
|
|
||||||
// dockerClient creates *docker.Client. In test / dev mode we can use ENV vars
|
// dockerClient creates *docker.Client. In test / dev mode we can use ENV vars
|
||||||
// to connect to the docker daemon. In production mode we will read
|
// to connect to the docker daemon. In production mode we will read
|
||||||
// docker.endpoint from the config file.
|
// docker.endpoint from the config file.
|
||||||
|
@ -266,9 +271,12 @@ func (d *DockerDriver) createContainer(ctx *ExecContext, task *structs.Task) (do
|
||||||
env.SetAllocDir(filepath.Join("/", allocdir.SharedAllocName))
|
env.SetAllocDir(filepath.Join("/", allocdir.SharedAllocName))
|
||||||
env.SetTaskLocalDir(filepath.Join("/", allocdir.TaskLocal))
|
env.SetTaskLocalDir(filepath.Join("/", allocdir.TaskLocal))
|
||||||
|
|
||||||
|
envVars, labels := splitEnvVarsLabels(env.List())
|
||||||
|
|
||||||
config := &docker.Config{
|
config := &docker.Config{
|
||||||
Env: env.List(),
|
Env: envVars,
|
||||||
Image: task.Config["image"],
|
Labels: labels,
|
||||||
|
Image: task.Config["image"],
|
||||||
}
|
}
|
||||||
|
|
||||||
rawArgs, hasArgs := task.Config["args"]
|
rawArgs, hasArgs := task.Config["args"]
|
||||||
|
@ -557,3 +565,20 @@ func (h *dockerHandle) run() {
|
||||||
}
|
}
|
||||||
close(h.waitCh)
|
close(h.waitCh)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// splitEnvVarsLabels returns environment vars and labels to set on the container
|
||||||
|
func splitEnvVarsLabels(envVars []string) ([]string, map[string]string) {
|
||||||
|
var envs []string
|
||||||
|
labels := make(map[string]string)
|
||||||
|
|
||||||
|
for _, value := range envVars {
|
||||||
|
if strings.HasPrefix(value, labelPrefix) {
|
||||||
|
parts := strings.SplitN(value, "=", 2)
|
||||||
|
labels[strings.TrimPrefix(parts[0], labelPrefix)] = parts[1]
|
||||||
|
} else {
|
||||||
|
envs = append(envs, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return envs, labels
|
||||||
|
}
|
||||||
|
|
|
@ -432,3 +432,39 @@ func TestDockerHostNet(t *testing.T) {
|
||||||
}
|
}
|
||||||
defer handle.Kill()
|
defer handle.Kill()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestsSlitEnvVarsLabels(t *testing.T) {
|
||||||
|
|
||||||
|
environment := []string{
|
||||||
|
"NOMAD_IP=1.1.1.1",
|
||||||
|
"NOMAD_CPU_LIMIT=500",
|
||||||
|
"NOMAD_META_TEST1=true_one",
|
||||||
|
"NOMAD_META_LABEL_LAB1=one",
|
||||||
|
"NOMAD_META_TEST2=test_two",
|
||||||
|
"NOMAD_META_LABEL_LAB2=two",
|
||||||
|
"NOMAD_META_TEST3=true_three",
|
||||||
|
"NOMAD_META_LABEL_LAB3=three",
|
||||||
|
"NOMAD_MEMORY_LIMIT=1024",
|
||||||
|
}
|
||||||
|
|
||||||
|
envVars, labels := splitEnvVarsLabels(environment)
|
||||||
|
if got, want := len(envVars), 6; got != want {
|
||||||
|
t.Errorf("Error on len. Got: %v. Expect %v items.", got, want)
|
||||||
|
}
|
||||||
|
if got, want := len(labels), 3; got != want {
|
||||||
|
t.Errorf("Error on len. Got: %v. Expect %v items.", got, want)
|
||||||
|
}
|
||||||
|
if got, want := envVars[3], environment[4]; got != want {
|
||||||
|
t.Errorf("Error. Got: '%v'. Expect '%v'.", got, want)
|
||||||
|
}
|
||||||
|
if got, want := envVars[4], environment[6]; got != want {
|
||||||
|
t.Errorf("Error. Got: '%v'. Expect '%v'.", got, want)
|
||||||
|
}
|
||||||
|
if got, want := labels["LAB2"], "two"; got != want {
|
||||||
|
t.Errorf("Error. Got: '%v'. Expect '%v'.", got, want)
|
||||||
|
}
|
||||||
|
if got, want := labels["LAB3"], "three"; got != want {
|
||||||
|
t.Errorf("Error. Got: '%v'. Expect '%v'.", got, want)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue