Skip to content

Commit

Permalink
Merge pull request #441 from travis-ci/meat-docker-labels-rework
Browse files Browse the repository at this point in the history
Rework Docker container labeling to allow for arbitrary input via config
  • Loading branch information
meatballhat authored Mar 2, 2018
2 parents 64df403 + 202583c commit 770f216
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
## [Unreleased]

### Added
- backend/docker: include arbitrary container labels from config

### Changed
- backend/gce: add a `no-ip` tag when allocating instance without public IP
Expand Down
42 changes: 19 additions & 23 deletions backend/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path/filepath"
Expand Down Expand Up @@ -56,6 +55,7 @@ var (
"TMPFS_MAP": fmt.Sprintf("space-delimited key:value map of tmpfs mounts (default %q)", defaultTmpfsMap),
"MEMORY": "memory to allocate to each container (0 disables allocation, default \"4G\")",
"SHM": "/dev/shm to allocate to each container (0 disables allocation, default \"64MiB\")",
"CONTAINER_LABELS": "\",\"-delimited key:value pairs of labels to apply to each container (default \"\")",
"CPUS": "cpu count to allocate to each container (0 disables allocation, default 2)",
"CPU_SET_SIZE": "size of available cpu set (default detected locally via runtime.NumCPU)",
"NATIVE": "upload and run build script via docker API instead of over ssh (default false)",
Expand Down Expand Up @@ -97,6 +97,7 @@ type dockerProvider struct {
inspectInterval time.Duration
tmpFs map[string]string
imageSelector image.Selector
containerLabels map[string]string

cpuSetsMutex sync.Mutex
cpuSets []bool
Expand Down Expand Up @@ -236,19 +237,25 @@ func newDockerProvider(cfg *config.ProviderConfig) (Provider, error) {
return nil, errors.Wrap(err, "couldn't build docker image selector")
}

containerLabels := map[string]string{}
if cfg.IsSet("CONTAINER_LABELS") {
containerLabels = str2map(cfg.Get("CONTAINER_LABELS"))
}

return &dockerProvider{
client: client,
sshDialer: sshDialer,
sshDialTimeout: sshDialTimeout,

runPrivileged: privileged,
runCmd: cmd,
runBinds: binds,
runMemory: memory,
runShm: shm,
runCPUs: uint(cpus),
runNative: runNative,
imageSelector: imageSelector,
runPrivileged: privileged,
runCmd: cmd,
runBinds: binds,
runMemory: memory,
runShm: shm,
runCPUs: uint(cpus),
runNative: runNative,
imageSelector: imageSelector,
containerLabels: containerLabels,

execCmd: execCmd,
inspectInterval: inspectInterval,
Expand Down Expand Up @@ -377,6 +384,9 @@ func (p *dockerProvider) Start(ctx gocontext.Context, startAttributes *StartAttr
labels := map[string]string{
"travis.dist": startAttributes.Dist,
}
for key, value := range p.containerLabels {
labels[key] = value
}

r, ok := context.RepositoryFromContext(ctx)
if ok {
Expand All @@ -388,20 +398,6 @@ func (p *dockerProvider) Start(ctx gocontext.Context, startAttributes *StartAttr
labels["travis.job_id"] = strconv.FormatUint(jid, 10)
}

ipv4, err := ioutil.ReadFile("/var/tmp/travis-run.d/instance-ipv4")
if err != nil {
logger.WithField("err", err).Error("couldn't read instance IP from /var/tmp/travis-run.d/instance-ipv4")
} else {
labels["travis.ipv4"] = string(ipv4)
}

iid, err := ioutil.ReadFile("/var/tmp/travis-run.d/instance-id")
if err != nil {
logger.WithField("err", err).Error("couldn't read instance ID from /var/tmp/travis-run.d/instance-id")
} else {
labels["travis.instance_id"] = string(iid)
}

dockerConfig := &dockercontainer.Config{
Cmd: p.runCmd,
Image: imageID,
Expand Down

0 comments on commit 770f216

Please sign in to comment.