Skip to content

Commit c78c975

Browse files
committed
multiarch: use normal Dockerfiles and build command
This removes code duplication: - the same Go environment is used for multiarch and for testing - BUILD_PLATFORMS determines what is getting build instead of hard-coding that in the make target - no separate Dockerfile.multiarch We still need separate Dockerfiles for Linux and Windows because Windows has special requirements. Indention is changed to use tabs.
1 parent f7a2f42 commit c78c975

File tree

3 files changed

+49
-38
lines changed

3 files changed

+49
-38
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
FROM gcr.io/distroless/static:latest
22
LABEL maintainers="Kubernetes Authors"
33
LABEL description="CSI Node driver registrar"
4+
ARG binary=./bin/csi-node-driver-registrar
45

5-
COPY ./bin/csi-node-driver-registrar csi-node-driver-registrar
6+
COPY ${binary} csi-node-driver-registrar
67
ENTRYPOINT ["/csi-node-driver-registrar"]

Dockerfile.multiarch

Lines changed: 0 additions & 14 deletions
This file was deleted.

Makefile

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,62 @@ all: build
1919

2020
include release-tools/build.make
2121

22-
# This target builds multiarch images using Moby BuildKit builder toolkit.
23-
# Docker Buildx is included in Docker 19.03 and needs DOCKER_CLI_EXPERIMENTAL enabled to run corresponding commands.
24-
# Currently amd, s390x and Windows manifest is pushed for canary, release branch and released tags.
25-
# Images generated from Prow build are pushed to staging area on gcr
26-
push-multiarch-%:
27-
make BUILD_PLATFORMS="windows amd64 .exe"
22+
# Additional parameters are needed when pushing to a local registry,
23+
# see https://github.com/docker/buildx/issues/94.
24+
# However, that then runs into https://github.com/docker/cli/issues/2396.
25+
#
26+
# What works for local testing is:
27+
# make push-multiarch PULL_BASE_REF=master REGISTRY_NAME=<your account on dockerhub.io> BUILD_PLATFORMS="linux amd64; windows amd64 .exe; linux ppc64le -ppc64le; linux s390x -s390x"
28+
DOCKER_BUILDX_CREATE_ARGS ?=
29+
30+
# This target builds a multiarch image for one command using Moby BuildKit builder toolkit.
31+
# Docker Buildx is included in Docker 19.03.
32+
#
33+
# ./cmd/<command>/Dockerfile[.Windows] is used if found, otherwise Dockerfile[.Windows].
34+
# BUILD_PLATFORMS determines which individual images are included in the multiarch image.
35+
# PULL_BASE_REF must be set to 'master', 'release-x.y', or a tag name, and determines
36+
# the tag for the resulting multiarch image.
37+
push-multiarch-%: build-%
38+
if ! [ "$(PULL_BASE_REF)" ]; then \
39+
echo >&2 "ERROR: PULL_BASE_REF must be set to 'master', 'release-x.y', or a tag name."; \
40+
exit 1; \
41+
fi
2842
set -ex; \
2943
DOCKER_CLI_EXPERIMENTAL=enabled; \
3044
export DOCKER_CLI_EXPERIMENTAL; \
31-
docker buildx create --use --name multiarchimage-buildertest; \
45+
docker buildx create $(DOCKER_BUILDX_CREATE_ARGS) --use --name multiarchimage-buildertest; \
46+
trap "docker buildx rm multiarchimage-buildertest" EXIT; \
47+
dockerfile_linux=$$(if [ -e ./cmd/$*/Dockerfile ]; then echo ./cmd/$*/Dockerfile; else echo Dockerfile; fi); \
48+
dockerfile_windows=$$(if [ -e ./cmd/$*/Dockerfile.Windows ]; then echo ./cmd/$*/Dockerfile.Windows; else echo Dockerfile.Windows; fi); \
49+
build_platforms='$(BUILD_PLATFORMS)'; \
50+
if ! [ "$$build_platforms" ]; then build_platforms="linux amd64"; fi; \
3251
pushMultiArch () { \
33-
tag=$$1; \
34-
docker buildx build --push -t $(IMAGE_NAME):amd64-linux-$$tag --platform=linux/amd64 -f $(shell if [ -e ./cmd/$*/Dockerfile.multiarch ]; then echo ./cmd/$*/Dockerfile.multiarch; else echo Dockerfile.multiarch; fi) --label revision=$(REV) .; \
35-
docker buildx build --push -t $(IMAGE_NAME):s390x-linux-$$tag --platform=linux/s390x -f $(shell if [ -e ./cmd/$*/Dockerfile.multiarch ]; then echo ./cmd/$*/Dockerfile.multiarch; else echo Dockerfile.multiarch; fi) --label revision=$(REV) .; \
36-
docker buildx build --push -t $(IMAGE_NAME):amd64-windows-$$tag --platform=windows -f $(shell if [ -e ./cmd/$*/Dockerfile.Windows ]; then echo ./cmd/$*/Dockerfile.Windows; else echo Dockerfile.Windows; fi) --label revision=$(REV) .; \
37-
docker manifest create --amend $(IMAGE_NAME):$$tag $(IMAGE_NAME):amd64-linux-$$tag \
38-
$(IMAGE_NAME):s390x-linux-$$tag \
39-
$(IMAGE_NAME):amd64-windows-$$tag; \
40-
docker manifest push -p $(IMAGE_NAME):$$tag; \
52+
tag=$$1; \
53+
echo "$$build_platforms" | tr ';' '\n' | while read -r os arch suffix; do \
54+
docker buildx build --push \
55+
--tag $(IMAGE_NAME):$$arch-$$os-$$tag \
56+
--platform=$$os/$$arch \
57+
--file $$(eval echo \$${dockerfile_$$os}) \
58+
--build-arg binary=./bin/$*$$suffix \
59+
--label revision=$(REV) \
60+
.; \
61+
done; \
62+
images=$$(echo "$$build_platforms" | tr ';' '\n' | while read -r os arch suffix; do echo $(IMAGE_NAME):$$arch-$$os-$$tag; done); \
63+
docker manifest create --amend $(IMAGE_NAME):$$tag $$images; \
64+
docker manifest push -p $(IMAGE_NAME):$$tag; \
4165
}; \
4266
if [ $(PULL_BASE_REF) = "master" ]; then \
43-
: "creating or overwriting canary image"; \
44-
pushMultiArch canary; \
67+
: "creating or overwriting canary image"; \
68+
pushMultiArch canary; \
4569
elif echo $(PULL_BASE_REF) | grep -q -e 'release-*' ; then \
46-
: "creating or overwriting canary image for release branch"; \
47-
release_canary_tag=$$(echo $(PULL_BASE_REF) | cut -f2 -d '-')-canary; \
48-
pushMultiArch $$release_canary_tag; \
70+
: "creating or overwriting canary image for release branch"; \
71+
release_canary_tag=$$(echo $(PULL_BASE_REF) | cut -f2 -d '-')-canary; \
72+
pushMultiArch $$release_canary_tag; \
4973
elif docker pull $(IMAGE_NAME):$(PULL_BASE_REF) 2>&1 | tee /dev/stderr | grep -q "manifest for $(IMAGE_NAME):$(PULL_BASE_REF) not found"; then \
50-
: "creating release image"; \
51-
pushMultiArch $(PULL_BASE_REF); \
74+
: "creating release image"; \
75+
pushMultiArch $(PULL_BASE_REF); \
5276
else \
53-
: "release image $(IMAGE_NAME):$(PULL_BASE_REF) already exists, skipping push"; \
77+
: "release image $(IMAGE_NAME):$(PULL_BASE_REF) already exists, skipping push"; \
5478
fi; \
5579

5680
push-multiarch: $(CMDS:%=push-multiarch-%)

0 commit comments

Comments
 (0)