From 818a0fcc0ffcda1c80a33c4f1ff6e1cc4d52abe3 Mon Sep 17 00:00:00 2001 From: Dan Duvall Date: Mon, 16 Oct 2017 11:58:00 -0700 Subject: 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 --- build/instructions.go | 39 +++++++++++++++++++++++++++------------ build/instructions_test.go | 14 ++++++++++++++ 2 files changed, 41 insertions(+), 12 deletions(-) (limited to 'build') 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"} -- cgit v1.2.1