Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add reference to build as a label on built images #13703

Merged
merged 1 commit into from
Apr 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grammar nit: that was produced

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually it's correct as is. what's incorrect is "this time" on the next line which should read "this image"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

// this image.
func addBuildLabels(labels map[string]string, build *buildapi.Build) {
labels[buildapi.DefaultDockerLabelNamespace+"build.name"] = build.Name
labels[buildapi.DefaultDockerLabelNamespace+"build.namespace"] = build.Namespace
}