Skip to content

Commit

Permalink
executor: apply label to only final stage
Browse files Browse the repository at this point in the history
In #4673 we made a change were
we were applying labels to end of each stage, which is different than
what we were doing before i.e applying label at the end of the each
step.

However buildkit does not adds label to any stage or steps it only adds
label at the end of final stage so lets do that.

Closes: #4804

Signed-off-by: Aditya R <arajan@redhat.com>
  • Loading branch information
flouthoc committed May 26, 2023
1 parent ebf4140 commit ab2280d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
50 changes: 25 additions & 25 deletions imagebuildah/executor.go
Expand Up @@ -470,33 +470,33 @@ func (b *Executor) buildStage(ctx context.Context, cleanupStages map[int]*StageE
output := ""
if stageIndex == len(stages)-1 {
output = b.output
}
// Check if any labels were passed in via the API, and add a final line
// to the Dockerfile that would provide the same result.
// Reason: Docker adds label modification as a last step which can be
// processed like regular steps, and if no modification is done to
// layers, its easier to re-use cached layers.
if len(b.labels) > 0 {
var labelLine string
labels := append([]string{}, b.labels...)
for _, labelSpec := range labels {
label := strings.SplitN(labelSpec, "=", 2)
key := label[0]
value := ""
if len(label) > 1 {
value = label[1]
}
// check only for an empty key since docker allows empty values
if key != "" {
labelLine += fmt.Sprintf(" %q=%q", key, value)
// Check if any labels were passed in via the API, and add a final line
// to the Dockerfile that would provide the same result.
// Reason: Docker adds label modification as a last step which can be
// processed like regular steps, and if no modification is done to
// layers, its easier to re-use cached layers.
if len(b.labels) > 0 {
var labelLine string
labels := append([]string{}, b.labels...)
for _, labelSpec := range labels {
label := strings.SplitN(labelSpec, "=", 2)
key := label[0]
value := ""
if len(label) > 1 {
value = label[1]
}
// check only for an empty key since docker allows empty values
if key != "" {
labelLine += fmt.Sprintf(" %q=%q", key, value)
}
}
}
if len(labelLine) > 0 {
additionalNode, err := imagebuilder.ParseDockerfile(strings.NewReader("LABEL" + labelLine + "\n"))
if err != nil {
return "", nil, fmt.Errorf("while adding additional LABEL step: %w", err)
if len(labelLine) > 0 {
additionalNode, err := imagebuilder.ParseDockerfile(strings.NewReader("LABEL" + labelLine + "\n"))
if err != nil {
return "", nil, fmt.Errorf("while adding additional LABEL step: %w", err)
}
stage.Node.Children = append(stage.Node.Children, additionalNode.Children...)
}
stage.Node.Children = append(stage.Node.Children, additionalNode.Children...)
}
}

Expand Down
11 changes: 11 additions & 0 deletions tests/bud.bats
Expand Up @@ -1965,6 +1965,17 @@ _EOF
done
run_buildah inspect --format '{{ index .Docker.Config.Labels "somefancylabel"}}' imagetwo
expect_output ""

# build another multi-stage image with different label, it should use stages from cache from previous build
run_buildah build --layers $WITH_POLICY_JSON --label anotherfancylabel=true -t imagethree -f Dockerfile.name $BUDFILES/multi-stage-builds
# must use all steps from cache but should not contain label
for i in 2 6;do
expect_output --substring --from="${lines[$i]}" "Using cache"
done
run_buildah inspect --format '{{ index .Docker.Config.Labels "somefancylabel"}}' imagetwo
expect_output ""
run_buildah inspect --format '{{ index .Docker.Config.Labels "anotherfancylabel"}}' imagethree
expect_output "true"
}

@test "bud-multi-stage-builds" {
Expand Down

0 comments on commit ab2280d

Please sign in to comment.