Skip to content

Commit

Permalink
add reference to build as a label on built images
Browse files Browse the repository at this point in the history
  • Loading branch information
bparees committed Apr 10, 2017
1 parent f65accd commit b34fbee
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/build/builder/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,13 @@ func (d *DockerBuilder) buildLabels(sourceInfo *git.SourceInfo) []dockerfile.Key
sourceInfo.ContextDir = d.build.Spec.Source.ContextDir
}
labels = util.GenerateLabelsFromSourceInfo(labels, &sourceInfo.SourceInfo, api.DefaultDockerLabelNamespace)
addBuildLabels(labels, d.build)

kv := make([]dockerfile.KeyValue, 0, len(labels)+len(d.build.Spec.Output.ImageLabels))
for k, v := range labels {
kv = append(kv, dockerfile.KeyValue{Key: k, Value: v})
}
// override autogenerated labels
// override autogenerated labels with user provided labels
for _, lbl := range d.build.Spec.Output.ImageLabels {
kv = append(kv, dockerfile.KeyValue{Key: lbl.Name, Value: lbl.Value})
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/build/builder/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ func TestDockerfilePath(t *testing.T) {
"\"io.openshift.build.commit.id\"=\"commitid\"",
"\"io.openshift.build.commit.ref\"=\"ref\"",
"\"io.openshift.build.commit.message\"=\"message\"",
"\"io.openshift.build.name\"=\"name\"",
"\"io.openshift.build.namespace\"=\"namespace\"",
}

for _, test := range tests {
Expand Down
1 change: 1 addition & 0 deletions pkg/build/builder/sti.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ func buildEnvVars(build *api.Build, sourceInfo *git.SourceInfo) s2iapi.Environme

func buildLabels(build *api.Build) map[string]string {
labels := make(map[string]string)
addBuildLabels(labels, build)
for _, lbl := range build.Spec.Output.ImageLabels {
labels[lbl.Name] = lbl.Value
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/build/builder/sti_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ func TestBuildEnvVars(t *testing.T) {
Value: "http://test/insecure:8080",
},
}
expectedLabelList := map[string]string{
"io.openshift.build.name": "openshift-test-1-build",
"io.openshift.build.namespace": "openshift-demo",
}

mockBuild := makeBuild()
mockBuild.Name = "openshift-test-1-build"
Expand All @@ -191,6 +195,12 @@ func TestBuildEnvVars(t *testing.T) {
if !reflect.DeepEqual(expectedEnvList, resultedEnvList) {
t.Errorf("Expected EnvironmentList to match: %#v, got %#v", expectedEnvList, resultedEnvList)
}

resultedLabelList := buildLabels(mockBuild)
if !reflect.DeepEqual(expectedLabelList, resultedLabelList) {
t.Errorf("Expected LabelList to match: %#v, got %#v", expectedLabelList, resultedLabelList)
}

}

func TestScriptProxyConfig(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions pkg/build/builder/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
docker "github.com/fsouza/go-dockerclient"

s2iapi "github.com/openshift/source-to-image/pkg/api"

buildapi "github.com/openshift/origin/pkg/build/api"
)

var (
Expand Down Expand Up @@ -192,3 +194,10 @@ func reportPushFailure(err error, authPresent bool, pushAuthConfig docker.AuthCo
}
return fmt.Errorf("Failed to push image: %v", err)
}

// addBuildLabels adds some common image labels describing the build that produced
// this time.
func addBuildLabels(labels map[string]string, build *buildapi.Build) {
labels[buildapi.DefaultDockerLabelNamespace+"build.name"] = build.Name
labels[buildapi.DefaultDockerLabelNamespace+"build.namespace"] = build.Namespace
}

0 comments on commit b34fbee

Please sign in to comment.