Skip to content
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
19 changes: 18 additions & 1 deletion imagebuildah/stage_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/containers/buildah/define"
buildahdocker "github.com/containers/buildah/docker"
"github.com/containers/buildah/internal"
"github.com/containers/buildah/pkg/parse"
"github.com/containers/buildah/pkg/rusage"
"github.com/containers/buildah/util"
cp "github.com/containers/image/v5/copy"
Expand Down Expand Up @@ -595,6 +596,22 @@ func (s *StageExecutor) prepare(ctx context.Context, from string, initializeIBCo
}
}

builderSystemContext := s.executor.systemContext
// get platform string from stage
if stage.Builder.Platform != "" {
os, arch, variant, err := parse.Platform(stage.Builder.Platform)
if err != nil {
return nil, errors.Wrapf(err, "unable to parse platform %q", stage.Builder.Platform)
}
if arch != "" || variant != "" {
builderSystemContext.ArchitectureChoice = arch
builderSystemContext.VariantChoice = variant
}
if os != "" {
builderSystemContext.OSChoice = os
}
}

builderOptions := buildah.BuilderOptions{
Args: ib.Args,
FromImage: from,
Expand All @@ -604,7 +621,7 @@ func (s *StageExecutor) prepare(ctx context.Context, from string, initializeIBCo
BlobDirectory: s.executor.blobDirectory,
SignaturePolicyPath: s.executor.signaturePolicyPath,
ReportWriter: s.executor.reportWriter,
SystemContext: s.executor.systemContext,
SystemContext: builderSystemContext,
Isolation: s.executor.isolation,
NamespaceOptions: s.executor.namespaceOptions,
ConfigureNetwork: s.executor.configureNetwork,
Expand Down
54 changes: 54 additions & 0 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,60 @@ symlink(subdir)"
expect_output --substring $(realpath "${TESTSDIR}/bud/dockerignore3/.dockerignore")
}

# Following test must fail since we are trying to run linux/arm64 on linux/amd64
# Issue: https://github.com/containers/buildah/issues/3712
@test "build-with-inline-platform" {
# Host arch
mkdir -p ${TESTDIR}/bud/platform
run_buildah info --format '{{.host.arch}}'
myarch="$output"
otherarch="arm64"

# just make sure that other arch is not equivalent to host arch
if [[ "$otherarch" == "$myarch" ]]; then
otherarch="amd64"
fi
# ...create a Containerfile with --platform=linux/$otherarch
cat > ${TESTDIR}/bud/platform/Dockerfile << _EOF
FROM --platform=linux/${otherarch} alpine
RUN uname -m
_EOF

run_buildah '?' build --signature-policy ${TESTSDIR}/policy.json -t test ${TESTDIR}/bud/platform
if [[ $status -eq 0 ]]; then
run_buildah inspect --format '{{ .OCIv1.Architecture }}' test
expect_output --substring "$otherarch"
else
# Build failed: we DO NOT have qemu-user-static installed.
expect_output --substring "format error"
fi
}

# Following test must pass since we want to tag image as host arch
# Test for use-case described here: https://github.com/containers/buildah/issues/3261
@test "build-with-inline-platform-amd-but-tag-as-arm" {
# Host arch
mkdir -p ${TESTDIR}/bud/platform
run_buildah info --format '{{.host.arch}}'
myarch="$output"
Copy link
Member

Choose a reason for hiding this comment

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

Might be simpler to grep the output of buildah version's BuildPlatform, which provides the entire value you're looking to use.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@nalind I need to toggle only arch at certain places that's why i preferred this. Hope this is not a blocker.

targetarch="arm64"

if [[ "$targetArch" == "$myarch" ]]; then
targetarch="amd64"
fi

cat > ${TESTDIR}/bud/platform/Dockerfile << _EOF
FROM --platform=linux/${myarch} alpine
RUN uname -m
_EOF

# Tries building image where baseImage has --platform=linux/HostArch
run_buildah build --platform linux/${targetarch} --signature-policy ${TESTSDIR}/policy.json -t test ${TESTDIR}/bud/platform
run_buildah inspect --format '{{ .OCIv1.Architecture }}' test
# base image is pulled as HostArch but tagged as non host arch
expect_output --substring $targetarch
}

@test "bud-flags-order-verification" {
run_buildah 125 build /tmp/tmpdockerfile/ -t blabla
check_options_flag_err "-t"
Expand Down