Skip to content

Commit

Permalink
executor: Add support for inline --platform within Dockerfile
Browse files Browse the repository at this point in the history
Allows end users to configure executor's `OS`, `ARCH`,`VARIANT` via inline
`--platform`.

Usage
```
FROM --platform=linux/arm64 alpine
RUN uname -a
```
While this allows executor to pull base images with custom
`OS`, `ARCH`, `VARIANT` it still allows end-users to tag images with
different format if they need to.

Signed-off-by: Aditya R <[email protected]>
  • Loading branch information
flouthoc committed Feb 3, 2022
1 parent 622bc3a commit 425572d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
21 changes: 20 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,24 @@ 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 != "" {
builderSystemContext.ArchitectureChoice = arch
}
if os != "" {
builderSystemContext.OSChoice = os
}
if variant != "" {
builderSystemContext.VariantChoice = variant
}
}

builderOptions := buildah.BuilderOptions{
Args: ib.Args,
FromImage: from,
Expand All @@ -604,7 +623,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
56 changes: 56 additions & 0 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,62 @@ 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
run_buildah info --format '{{.host.arch}}'
myarch="$output"
otherarch="arm64"
for try in amd64 arm64 ppc64le s390x; do
if [[ "$otherarch" != "$myarch" ]]; then
otherarch="$otherarch"
break
fi
done
# ...create a Containerfile with --platform=linux/$otherarch
cat > ${TESTSDIR}/bud/platform/Dockerfile << _EOF
FROM --platform=linux/${otherarch} alpine
RUN uname -m
_EOF

run_buildah '?' build --signature-policy ${TESTSDIR}/policy.json -t test ${TESTSDIR}/bud/platform
if [[ $status -eq 0 ]]; then
# Build succeeded: we must have qemu-user-static installed. Confirm that
# the 'uname' emits what we expect
expect_output --from="${lines[-1]}" "$otherarch"
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
run_buildah info --format '{{.host.arch}}'
myarch="$output"
targetarch="arm64"

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

cat > ${TESTSDIR}/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 ${TESTSDIR}/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
2 changes: 2 additions & 0 deletions tests/bud/platform/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM --platform=linux/amd64 alpine
RUN uname -m
2 changes: 2 additions & 0 deletions tests/bud/platform/Dockerfileamd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM --platform=linux/amd64 busybox
RUN uname -m

0 comments on commit 425572d

Please sign in to comment.