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

Setting --arch should set the TARGETARCH build arg #5478

Merged
merged 1 commit into from
May 15, 2024
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: 4 additions & 0 deletions imagebuildah/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -221,6 +222,9 @@ func BuildDockerfiles(ctx context.Context, store storage.Store, options define.B
systemContext := options.SystemContext
for _, platform := range options.Platforms {
platformContext := *systemContext
if platform.OS == "" {
platform.OS = runtime.GOOS
}
platformSpec := internalUtil.NormalizePlatform(v1.Platform{
OS: platform.OS,
Architecture: platform.Arch,
Expand Down
52 changes: 37 additions & 15 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -243,26 +243,48 @@ _EOF
assert "${checkvars[*]}" != "" \
"INTERNAL ERROR! No 'ARG xxx' lines in $containerfile!"

ARCH=$(go env GOARCH)
# With explicit and full --platform, buildah should not warn.
run_buildah build $WITH_POLICY_JSON --platform linux/amd64/v2 \
-t source -f $containerfile
assert "$output" !~ "missing .* build argument" \
"With explicit --platform, buildah should not warn"
assert "$output" =~ "image platform \(linux/amd64\) does not match the expected platform" \
"With explicit --platform, buildah should warn about pulling difference in platform"
assert "$output" =~ "TARGETOS=linux" " --platform TARGETOS set correctly"
assert "$output" =~ "TARGETARCH=amd64" " --platform TARGETARCH set correctly"
assert "$output" =~ "TARGETVARIANT=" " --platform TARGETVARIANT set correctly"
Copy link
Member

Choose a reason for hiding this comment

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

Is TARGETVARIANT not being set to "v2", per "--platform"?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this reveals the fix just not being correct — the core issue is that the buildDockerfilesOnce code setting TARGET* expects SystemContext.*Choice to never be "", and that’s not how the values are set.

Compare more in #5543 (review)

assert "$output" =~ "TARGETPLATFORM=linux/amd64/v2" " --platform TARGETPLATFORM set correctly"

# Likewise with individual args
run_buildah build $WITH_POLICY_JSON --os linux --arch amd64 --variant v2 \
-t source -f $containerfile
assert "$output" !~ "missing .* build argument" \
"With explicit --os + --arch + --variant, buildah should not warn"

# FIXME FIXME FIXME: #4319: with --os only, buildah should not warn about OS
if false; then
run_buildah build $WITH_POLICY_JSON --os linux \
-t source -f $containerfile
assert "$output" !~ "missing.*TARGETOS" \
"With explicit --os (but no arch/variant), buildah should not warn about TARGETOS"
# FIXME: add --arch test too, and maybe make this cleaner
fi
assert "$output" =~ "image platform \(linux/amd64\) does not match the expected platform" \
"With explicit --variant, buildah should warn about pulling difference in platform"
assert "$output" =~ "TARGETOS=linux" "--os --arch --variant TARGETOS set correctly"
assert "$output" =~ "TARGETARCH=amd64" "--os --arch --variant TARGETARCH set correctly"
assert "$output" =~ "TARGETVARIANT=" "--os --arch --variant TARGETVARIANT set correctly"
assert "$output" =~ "TARGETPLATFORM=linux/amd64" "--os --arch --variant TARGETPLATFORM set correctly"

run_buildah build $WITH_POLICY_JSON --os linux -t source -f $containerfile
assert "$output" !~ "WARNING" \
"With explicit --os (but no arch/variant), buildah should not warn about TARGETOS"
assert "$output" =~ "TARGETOS=linux" "--os TARGETOS set correctly"
assert "$output" =~ "TARGETARCH=${ARCH}" "--os TARGETARCH set correctly"
assert "$output" =~ "TARGETVARIANT=" "--os TARGETVARIANT set correctly"
assert "$output" =~ "TARGETPLATFORM=linux/${ARCH}" "--os TARGETPLATFORM set correctly"

run_buildah build $WITH_POLICY_JSON --arch amd64 -t source -f $containerfile
assert "$output" !~ "WARNING" \
"With explicit --os (but no arch/variant), buildah should not warn about TARGETOS"
Copy link
Member

Choose a reason for hiding this comment

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

This error description doesn't seem to match the error message it's checking against.

assert "$output" =~ "TARGETOS=linux" "--arch TARGETOS set correctly"
assert "$output" =~ "TARGETARCH=amd64" "--arch TARGETARCH set correctly"
assert "$output" =~ "TARGETVARIANT=" "--arch TARGETVARIANT set correctly"
assert "$output" =~ "TARGETPLATFORM=linux/amd64" "--arch TARGETPLATFORM set correctly"

for option in "--arch=arm64" "--os=windows" "--variant=v2"; do
run_buildah 125 build $WITH_POLICY_JSON --platform linux/amd64 ${option} \
-t source -f $containerfile
assert "$output" =~ "Error: building system context: invalid --platform may not be used with --os, --arch, or --variant" "can't use --platform and one of --os, --arch or --variant together"
done
}

@test "build-conflicting-isolation-chroot-and-network" {
Expand Down Expand Up @@ -5729,9 +5751,9 @@ _EOF
@test "bud with --pull-always" {
_prefetch docker.io/library/alpine
run_buildah build --pull-always $WITH_POLICY_JSON -t testpull $BUDFILES/containerfile
expect_output --from="${lines[1]}" "Trying to pull docker.io/library/alpine:latest..."
expect_output --substring "Trying to pull docker.io/library/alpine:latest..."
run_buildah build --pull=always $WITH_POLICY_JSON -t testpull $BUDFILES/containerfile
expect_output --from="${lines[1]}" "Trying to pull docker.io/library/alpine:latest..."
expect_output --substring "Trying to pull docker.io/library/alpine:latest..."
}

@test "bud with --memory and --memory-swap" {
Expand Down
4 changes: 4 additions & 0 deletions tests/bud/platform-sets-args/Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ ARG TARGETOS
ARG TARGETPLATFORM
ARG TARGETVARIANT
ENV nothing="multiarch-safe statement that will result in a built image"
run echo TARGETARCH=${TARGETARCH}
run echo TARGETOS=${TARGETOS}
run echo TARGETPLATFORM=${TARGETPLATFORM}
run echo TARGETVARIANT=${TARGETVARANT}
2 changes: 1 addition & 1 deletion tests/conformance/conformance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3566,11 +3566,11 @@ func TestCommit(t *testing.T) {
derivedBuilder, err := buildah.NewBuilder(ctx, store, buildah.BuilderOptions{
FromImage: buildahID,
})
require.NoErrorf(t, err, "creating the derived container with buildah")
defer func(builder *buildah.Builder) {
err := builder.Delete()
assert.NoErrorf(t, err, "removing the derived container")
}(derivedBuilder)
require.NoErrorf(t, err, "creating the derived container with buildah")
var overrideConfig *manifest.Schema2Config
if test.derivedConfig != nil {
overrideConfig = config.Schema2ConfigFromGoDockerclientConfig(test.derivedConfig)
Expand Down
Loading