allow to set labels on docker containers
This commit is contained in:
parent
aeb726a5e0
commit
f7f83ee81c
|
@ -35,14 +35,15 @@ type DockerAuthConfig struct {
|
|||
|
||||
type DockerDriverConfig struct {
|
||||
DockerAuthConfig
|
||||
ImageName string `mapstructure:"image"` // Container's Image Name
|
||||
Command string `mapstructure:"command"` // The Command/Entrypoint to run when the container starts up
|
||||
Args string `mapstructure:"args"` // The arguments to the Command/Entrypoint
|
||||
NetworkMode string `mapstructure:"network_mode"` // The network mode of the container - host, net and none
|
||||
PortMap []map[string]int `mapstructure:"port_map"` // A map of host port labels and the ports exposed on the container
|
||||
Privileged bool `mapstructure:"privileged"` // Flag to run the container in priviledged mode
|
||||
DNS string `mapstructure:"dns_server"` // DNS Server for containers
|
||||
SearchDomains string `mapstructure:"search_domains"` // DNS Search domains for containers
|
||||
ImageName string `mapstructure:"image"` // Container's Image Name
|
||||
Command string `mapstructure:"command"` // The Command/Entrypoint to run when the container starts up
|
||||
Args string `mapstructure:"args"` // The arguments to the Command/Entrypoint
|
||||
NetworkMode string `mapstructure:"network_mode"` // The network mode of the container - host, net and none
|
||||
PortMap []map[string]int `mapstructure:"port_map"` // A map of host port labels and the ports exposed on the container
|
||||
Privileged bool `mapstructure:"privileged"` // Flag to run the container in priviledged mode
|
||||
DNS string `mapstructure:"dns_server"` // DNS Server for containers
|
||||
SearchDomains string `mapstructure:"search_domains"` // DNS Search domains for containers
|
||||
Labels []map[string]string `mapstructure:"labels"` // Labels to set when the container starts up
|
||||
}
|
||||
|
||||
func (c *DockerDriverConfig) Validate() error {
|
||||
|
@ -53,6 +54,10 @@ func (c *DockerDriverConfig) Validate() error {
|
|||
if len(c.PortMap) > 1 {
|
||||
return fmt.Errorf("Only one port_map block is allowed in the docker driver config")
|
||||
}
|
||||
|
||||
if len(c.Labels) > 1 {
|
||||
return fmt.Errorf("Only one labels block is allowed in the docker driver config")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -321,6 +326,11 @@ func (d *DockerDriver) createContainer(ctx *ExecContext, task *structs.Task, dri
|
|||
d.logger.Println("[DEBUG] driver.docker: ignoring args because command not specified")
|
||||
}
|
||||
|
||||
if len(driverConfig.Labels) == 1 {
|
||||
config.Labels = driverConfig.Labels[0]
|
||||
d.logger.Println("[DEBUG] driver.docker: applied labels on the container")
|
||||
}
|
||||
|
||||
config.Env = env.List()
|
||||
return docker.CreateContainerOptions{
|
||||
Config: config,
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package driver
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -433,3 +435,58 @@ func TestDockerHostNet(t *testing.T) {
|
|||
}
|
||||
defer handle.Kill()
|
||||
}
|
||||
|
||||
func TestDockerLabels(t *testing.T) {
|
||||
if !dockerIsConnected(t) {
|
||||
t.SkipNow()
|
||||
}
|
||||
|
||||
task := taskTemplate()
|
||||
task.Config["labels"] = []map[string]string{
|
||||
map[string]string{
|
||||
"label1": "value1",
|
||||
"label2": "value2",
|
||||
},
|
||||
}
|
||||
|
||||
driverCtx := testDockerDriverContext(task.Name)
|
||||
ctx := testDriverExecContext(task, driverCtx)
|
||||
defer ctx.AllocDir.Destroy()
|
||||
d := NewDockerDriver(driverCtx)
|
||||
|
||||
handle, err := d.Start(ctx, task)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if handle == nil {
|
||||
t.Fatalf("missing handle")
|
||||
}
|
||||
|
||||
client, err := docker.NewClientFromEnv()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
// don't know if is queriable in a clean way
|
||||
parts := strings.SplitN(handle.ID(), ":", 2)
|
||||
var pid dockerPID
|
||||
err = json.Unmarshal([]byte(parts[1]), &pid)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
container, err := client.InspectContainer(pid.ContainerID)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
|
||||
if want, got := 2, len(container.Config.Labels); want != got {
|
||||
t.Errorf("Wrong labels count for docker job. Expect: %d, got: %d", want, got)
|
||||
}
|
||||
|
||||
if want, got := "value1", container.Config.Labels["label1"]; want != got {
|
||||
t.Errorf("Wrong label value docker job. Expect: %s, got: %s", want, got)
|
||||
}
|
||||
|
||||
defer handle.Kill()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue