diff options
author | Dan Duvall <dduvall@wikimedia.org> | 2017-10-16 11:58:00 -0700 |
---|---|---|
committer | Dan Duvall <dduvall@wikimedia.org> | 2017-10-19 09:39:49 -0700 |
commit | 818a0fcc0ffcda1c80a33c4f1ff6e1cc4d52abe3 (patch) | |
tree | 9ee6e4067414506e6bd422888ea0d08dc7eefef7 /build | |
parent | d53a06a138106c1652fff0b8576ae907a29a727c (diff) | |
download | blubber-818a0fcc0ffcda1c80a33c4f1ff6e1cc4d52abe3.tar.gz |
Include meta data as labels in Dockerfile output
Summary:
Certain meta data including the Blubber version and variant used at
invocation may be useful in downstream tracking.
Implemented abstract and Docker-specific instructions for labels and
modified the Docker compiler to include these instructions at the end of
the output for the final stage.
Depends on D816
Fixes T178022
Test Plan:
Run unit tests. Run `make && bin/blubber blubber.example.yaml production` and
verify that both `blubber.version` and `blubber.variant` labels are present
and accurate.
Reviewers: thcipriani, hashar, #release-engineering-team
Reviewed By: thcipriani, #release-engineering-team
Tags: #release-engineering-team
Maniphest Tasks: T178022
Differential Revision: https://phabricator.wikimedia.org/D818
Diffstat (limited to 'build')
-rw-r--r-- | build/instructions.go | 39 | ||||
-rw-r--r-- | build/instructions_test.go | 14 |
2 files changed, 41 insertions, 12 deletions
diff --git a/build/instructions.go b/build/instructions.go index 38ea322..41c959d 100644 --- a/build/instructions.go +++ b/build/instructions.go @@ -64,20 +64,18 @@ type Env struct { } func (env Env) Compile() []string { - defs := make([]string, 0, len(env.Definitions)) - names := make([]string, 0, len(env.Definitions)) - - for name := range env.Definitions { - names = append(names, name) - } - - sort.Strings(names) + return compileSortedKeyValues(env.Definitions) +} - for _, name := range names { - defs = append(defs, name+"="+quote(env.Definitions[name])) - } +// Label represents a number of meta-data key/value pairs +type Label struct { + Definitions map[string]string +} - return defs +// Compile returns the label key/value pairs as a number of `key="value"` +// strings where the values are properly quoted +func (label Label) Compile() []string { + return compileSortedKeyValues(label.Definitions) } type Volume struct { @@ -88,6 +86,23 @@ func (vol Volume) Compile() []string { return []string{quote(vol.Path)} } +func compileSortedKeyValues(keyValues map[string]string) []string { + defs := make([]string, 0, len(keyValues)) + names := make([]string, 0, len(keyValues)) + + for name := range keyValues { + names = append(names, name) + } + + sort.Strings(names) + + for _, name := range names { + defs = append(defs, name+"="+quote(keyValues[name])) + } + + return defs +} + func quote(arg string) string { return strconv.Quote(arg) } diff --git a/build/instructions_test.go b/build/instructions_test.go index acbdb7c..4098711 100644 --- a/build/instructions_test.go +++ b/build/instructions_test.go @@ -56,6 +56,20 @@ func TestEnv(t *testing.T) { }, i.Compile()) } +func TestLabel(t *testing.T) { + i := build.Label{map[string]string{ + "fooname": "foovalue", + "barname": "barvalue", + "quxname": "quxvalue", + }} + + assert.Equal(t, []string{ + `barname="barvalue"`, + `fooname="foovalue"`, + `quxname="quxvalue"`, + }, i.Compile()) +} + func TestVolume(t *testing.T) { i := build.Volume{"/foo/dir"} |