From 3d1cfb22c57ffe4806f62c47e2f649e72073f7a3 Mon Sep 17 00:00:00 2001 From: Naoki MATSUMOTO Date: Tue, 20 Aug 2024 09:10:52 +0000 Subject: [PATCH] support building on an arm64 environtment Signed-off-by: Naoki MATSUMOTO --- Makefile | 99 +++++++++++++++++++++++++-------------- cmd/csi_driver/Dockerfile | 16 ++++--- 2 files changed, 74 insertions(+), 41 deletions(-) diff --git a/Makefile b/Makefile index 740e765..e5d0474 100755 --- a/Makefile +++ b/Makefile @@ -28,11 +28,14 @@ DRIVER_IMAGE = ${REGISTRY}/${DRIVER_BINARY} STARTER_IMAGE = ${REGISTRY}/${STARTER_BINARY} EXAMPLE_IMAGE = ${REGISTRY}/mfcp-example -DOCKER_BUILD_ARGS ?= --load --build-arg STAGINGVERSION=${STAGINGVERSION} +DOCKER_BUILD_ARGS ?= --build-arg STAGINGVERSION=${STAGINGVERSION} ifneq ("$(shell docker buildx build --help | grep 'provenance')", "") DOCKER_BUILD_ARGS += --provenance=false endif +ARCH = $(shell dpkg --print-architecture) +BUILDX_BUILDER = mfcp-builder + LOAD_TO_KIND ?= false PUBLISH_IMAGE ?= false @@ -40,77 +43,98 @@ $(info STAGINGVERSION is ${STAGINGVERSION}) $(info DRIVER_IMAGE is ${DRIVER_IMAGE}) $(info STARTER_IMAGE is ${STARTER_IMAGE}) -.PHONY: all build-image-linux-amd64 +.PHONY: all all: build-driver build-examples driver: mkdir -p ${BINDIR} - CGO_ENABLED=0 GOOS=linux GOARCH=$(shell dpkg --print-architecture) go build -ldflags "${LDFLAGS}" -o ${BINDIR}/${DRIVER_BINARY} cmd/csi_driver/main.go + CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} go build -ldflags "${LDFLAGS}" -o ${BINDIR}/${DRIVER_BINARY} cmd/csi_driver/main.go fuse-starter: mkdir -p ${BINDIR} - CGO_ENABLED=0 GOOS=linux GOARCH=$(shell dpkg --print-architecture) go build -ldflags "${LDFLAGS}" -o ${BINDIR}/${STARTER_BINARY} cmd/fuse_starter/main.go + CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} go build -ldflags "${LDFLAGS}" -o ${BINDIR}/${STARTER_BINARY} cmd/fuse_starter/main.go fusermount3-proxy: mkdir -p ${BINDIR} - CGO_ENABLED=0 GOOS=linux GOARCH=$(shell dpkg --print-architecture) go build -ldflags "${LDFLAGS}" -o ${BINDIR}/${FUSERMOUNT3PROXY_BINARY} cmd/fusermount3-proxy/main.go + CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} go build -ldflags "${LDFLAGS}" -o ${BINDIR}/${FUSERMOUNT3PROXY_BINARY} cmd/fusermount3-proxy/main.go build-driver: $(eval IMAGE_NAME := ${DRIVER_IMAGE}:${STAGINGVERSION}) - docker buildx build ${DOCKER_BUILD_ARGS} ${DOCKER_CACHE_ARGS} \ + docker buildx build --load ${DOCKER_BUILD_ARGS} ${DOCKER_CACHE_ARGS} \ --file ./cmd/csi_driver/Dockerfile \ --tag ${IMAGE_NAME} \ - --platform linux/amd64 . - if [ "${PUBLISH_IMAGE}" = "true" ]; then \ - docker push ${IMAGE_NAME}; \ - docker tag ${IMAGE_NAME} ${DRIVER_IMAGE}:latest; \ - docker push ${DRIVER_IMAGE}:latest; \ - fi + --platform linux/${ARCH} . if [ "${LOAD_TO_KIND}" = "true" ]; then \ kind load docker-image ${IMAGE_NAME};\ fi -define build-example-template -ifneq ("$(EXAMPLES)", "") -EXAMPLES += build-example-$(1)-$(2) +.PHONY: init-buildx-driver +init-buildx-driver: + - docker buildx rm mfcp-builder + docker buildx create --name ${BUILDX_BUILDER} + +.PHONY: push-driver +push-driver: init-buildx-driver + docker buildx build ${DOCKER_BUILD_ARGS} ${DOCKER_CACHE_ARGS} \ + --file ./cmd/csi_driver/Dockerfile \ + --tag ${DRIVER_IMAGE}:${STAGINGVERSION} \ + --tag ${DRIVER_IMAGE}:latest \ + --builder ${BUILDX_BUILDER} \ + --platform linux/amd64,linux/arm64 . + +define example-template +ifneq ("$(BUILD_EXAMPLES)", "") +BUILD_EXAMPLES += build-example-$(1)-$(2) +PUSH_EXAMPLES += push-example-$(1)-$(2) else -EXAMPLES := build-example-$(1)-$(2) +BUILD_EXAMPLES := build-example-$(1)-$(2) +PUSH_EXAMPLES := push-example-$(1)-$(2) endif .PHONY: build-example-$1-$2 build-example-$(1)-$(2): $(eval IMAGE_NAME := ${EXAMPLE_IMAGE}-$1-$2:${STAGINGVERSION}) - docker buildx build ${DOCKER_BUILD_ARGS} ${DOCKER_CACHE_ARGS} \ + docker buildx build --load ${DOCKER_BUILD_ARGS} ${DOCKER_CACHE_ARGS} \ --file ./examples/$1/$2/Dockerfile \ --tag ${IMAGE_NAME} \ - --platform linux/amd64 . - if [ "${PUBLISH_IMAGE}" = "true" ]; then \ - docker push ${IMAGE_NAME}; \ - docker tag ${IMAGE_NAME} ${EXAMPLE_IMAGE}-$1-$2:latest; \ - docker push ${EXAMPLE_IMAGE}-$1-$2:latest; \ - fi + --platform linux/${ARCH} . if [ "${LOAD_TO_KIND}" = "true" ]; then \ kind load docker-image ${IMAGE_NAME};\ fi -endef -$(eval $(call build-example-template,proxy,mountpoint-s3)) -$(eval $(call build-example-template,proxy,goofys)) -$(eval $(call build-example-template,proxy,s3fs)) -$(eval $(call build-example-template,proxy,ros3fs)) -$(eval $(call build-example-template,proxy,gcsfuse)) -$(eval $(call build-example-template,proxy,sshfs)) -$(eval $(call build-example-template,starter,ros3fs)) -$(eval $(call build-example-template,starter,sshfs)) +.PHONY: push-example-$1-$2 +push-example-$(1)-$(2): + docker buildx build ${DOCKER_BUILD_ARGS} ${DOCKER_CACHE_ARGS} \ + --file ./examples/$1/$2/Dockerfile \ + --tag ${EXAMPLE_IMAGE}-$1-$2:${STAGINGVERSION} \ + --tag ${EXAMPLE_IMAGE}-$1-$2:latest \ + --builder ${BUILDX_BUILDER} \ + --platform linux/amd64,linux/arm64 . +endef -$(info $(EXAMPLES)) +ifeq ("$(ARCH)", "arm64") +$(eval $(call example-template,proxy,sshfs)) +$(eval $(call example-template,starter,sshfs)) +else +$(eval $(call example-template,proxy,mountpoint-s3)) +$(eval $(call example-template,proxy,goofys)) +$(eval $(call example-template,proxy,s3fs)) +$(eval $(call example-template,proxy,ros3fs)) +$(eval $(call example-template,proxy,gcsfuse)) +$(eval $(call example-template,proxy,sshfs)) +$(eval $(call example-template,starter,ros3fs)) +$(eval $(call example-template,starter,sshfs)) +endif .PHONY: build-examples -build-examples: $(EXAMPLES) +build-examples: $(BUILD_EXAMPLES) + +.PHONY: push-examples +push-examples: $(PUSH_EXAMPLES) define test-example-template -ifneq ("$(EXAMPLES)", "") +ifneq ("$(EXAMPLE_TESTS)", "") EXAMPLE_TESTS += test-example-$(1)-$(2) else EXAMPLE_TESTS := test-example-$(1)-$(2) @@ -121,6 +145,10 @@ test-example-$(1)-$(2): ./examples/check.sh ./$1/$2 mfcp-example-$1-$2 $3 $4 $5 $6 endef +ifeq ("$(ARCH)", "arm64") +$(eval $(call test-example-template,proxy,sshfs,starter,/root/sshfs-example/test.txt,busybox,/data/test.txt)) +$(eval $(call test-example-template,starter,sshfs,starter,/root/sshfs-example/test.txt,busybox,/data/test.txt)) +else $(eval $(call test-example-template,proxy,mountpoint-s3,starter,/test.txt,busybox,/data/test.txt)) $(eval $(call test-example-template,proxy,goofys,starter,/test.txt,busybox,/data/test.txt)) $(eval $(call test-example-template,proxy,s3fs,starter,/test.txt,busybox,/data/test.txt)) @@ -128,6 +156,7 @@ $(eval $(call test-example-template,proxy,ros3fs,starter,/test.txt,busybox,/data $(eval $(call test-example-template,proxy,sshfs,starter,/root/sshfs-example/test.txt,busybox,/data/test.txt)) $(eval $(call test-example-template,starter,ros3fs,starter,/test.txt,busybox,/data/test.txt)) $(eval $(call test-example-template,starter,sshfs,starter,/root/sshfs-example/test.txt,busybox,/data/test.txt)) +endif .PHONY: test-examples test-examples: $(EXAMPLE_TESTS) diff --git a/cmd/csi_driver/Dockerfile b/cmd/csi_driver/Dockerfile index eb80bd6..af596dd 100755 --- a/cmd/csi_driver/Dockerfile +++ b/cmd/csi_driver/Dockerfile @@ -15,7 +15,7 @@ # limitations under the License. # Build driver go binary -FROM golang:1.20.7 as driver-builder +FROM golang:1.20.7 AS driver-builder ARG STAGINGVERSION @@ -24,19 +24,23 @@ ADD . . RUN make driver BINDIR=/bin # Start from Kubernetes Debian base. -FROM gke.gcr.io/debian-base:bullseye-v1.4.3-gke.5 as debian +FROM gke.gcr.io/debian-base:bullseye-v1.4.3-gke.5 AS debian # Install necessary dependencies RUN clean-install mount bash # go/gke-releasing-policies#base-images # We use `gcr.io/distroless/base` because it includes glibc. -FROM gcr.io/distroless/base-debian11 as distroless-base +FROM gcr.io/distroless/base-debian11 AS distroless-base # The distroless amd64 image has a target triplet of x86_64 FROM distroless-base AS distroless-amd64 -ENV LIB_DIR_PREFIX x86_64 +ENV LIB_DIR_PREFIX=x86_64 -FROM distroless-$TARGETARCH as output-image +# The distroless arm64 image has a target triplet of aarch64 +FROM distroless-base AS distroless-arm64 +ENV LIB_DIR_PREFIX=aarch64 + +FROM distroless-$TARGETARCH AS output-image # Copy the mount/umount binaries COPY --from=debian /bin/mount /bin/mount @@ -50,7 +54,7 @@ COPY --from=debian /usr/lib/${LIB_DIR_PREFIX}-linux-gnu/libblkid.so.1 \ /usr/lib/${LIB_DIR_PREFIX}-linux-gnu/libpcre2-8.so.0 /usr/lib/${LIB_DIR_PREFIX}-linux-gnu/ # Build stage used for validation of the output-image -FROM output-image as validation-image +FROM output-image AS validation-image COPY --from=debian /lib/${LIB_DIR_PREFIX}-linux-gnu/libtinfo.so.6 \ /lib/${LIB_DIR_PREFIX}-linux-gnu/libpcre.so.3 /lib/${LIB_DIR_PREFIX}-linux-gnu/ COPY --from=debian /bin/bash /bin/bash