From 32e2a713e64f3190dd5bcf5449e1cbe2de8012c9 Mon Sep 17 00:00:00 2001 From: Madhu Rajanna Date: Tue, 21 Apr 2020 12:09:03 +0530 Subject: [PATCH] Fix multi architecture dockerfile Add support for multi architecture build for cephcsi. with multistage docker build we we build cephcsi binary for both arm64 and amd64 architecture. Signed-off-by: Madhu Rajanna --- Makefile | 11 ++++--- deploy.sh | 6 ++-- deploy/cephcsi/image/Dockerfile | 52 +++++++++++++++++++++++++++------ 3 files changed, 51 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 1ce24dd02..cf393fb5f 100644 --- a/Makefile +++ b/Makefile @@ -21,8 +21,9 @@ CSI_IMAGE_VERSION=$(if $(ENV_CSI_IMAGE_VERSION),$(ENV_CSI_IMAGE_VERSION),canary) CSI_IMAGE=$(CSI_IMAGE_NAME):$(CSI_IMAGE_VERSION) $(info cephcsi image settings: $(CSI_IMAGE_NAME) version $(CSI_IMAGE_VERSION)) - +ifeq ($(origin GIT_COMMIT), undefined) GIT_COMMIT=$(shell git rev-list -1 HEAD) +endif GO_PROJECT=github.com/ceph/ceph-csi @@ -70,7 +71,7 @@ func-test: .PHONY: cephcsi cephcsi: if [ ! -d ./vendor ]; then (go mod tidy && go mod vendor); fi - GOOS=linux GOARCH=$(GOARCH) go build -mod vendor -a -ldflags '$(LDFLAGS)' -o _output/cephcsi ./cmd/ + GOOS=linux go build -mod vendor -a -ldflags '$(LDFLAGS)' -o _output/cephcsi ./cmd/ .PHONY: containerized-build containerized-test containerized-build: .devel-container-id @@ -91,10 +92,8 @@ containerized-test: .test-container-id $(CONTAINER_CMD) build -t $(CSI_IMAGE_NAME):test -f ./scripts/Dockerfile.test . $(CONTAINER_CMD) inspect -f '{{.Id}}' $(CSI_IMAGE_NAME):test > .test-container-id -image-cephcsi: cephcsi - cp _output/cephcsi deploy/cephcsi/image/cephcsi - chmod +x deploy/cephcsi/image/cephcsi - $(CONTAINER_CMD) build -t $(CSI_IMAGE) deploy/cephcsi/image +image-cephcsi: + $(CONTAINER_CMD) build -t $(CSI_IMAGE) -f deploy/cephcsi/image/Dockerfile . --build-arg GOLANG_VERSION=1.13.8 --build-arg CSI_IMAGE_NAME=$(CSI_IMAGE_NAME) --build-arg CSI_IMAGE_VERSION=$(CSI_IMAGE_VERSION) --build-arg GIT_COMMIT=$(shell git rev-list -1 HEAD) --build-arg ARCH=$(ARCH) push-image-cephcsi: image-cephcsi $(CONTAINER_CMD) tag $(CSI_IMAGE) $(CSI_IMAGE)-$(GOARCH) diff --git a/deploy.sh b/deploy.sh index 1aa646bf2..b88c1f8cf 100755 --- a/deploy.sh +++ b/deploy.sh @@ -47,7 +47,7 @@ build_push_images() { # get baseimg (ceph/ceph:tag) dockerfile="deploy/cephcsi/image/Dockerfile" - baseimg=$(awk '/^FROM/ {print $NF}' "${dockerfile}") + baseimg=$(awk 'NR==1{print $2}' "${dockerfile}") # get image digest per architecture # { @@ -59,7 +59,7 @@ build_push_images() { # "digest": "sha256:YYY" # } manifests=$("${CONTAINER_CMD:-docker}" manifest inspect "${baseimg}" | jq '.manifests[] | {arch: .platform.architecture, digest: .digest}') - + docker run --rm --privileged multiarch/qemu-user-static --reset -p yes # build and push per arch images for ARCH in amd64 arm64; do ifs=$IFS @@ -67,7 +67,7 @@ build_push_images() { digest=$(awk -v ARCH=${ARCH} '{if (archfound) {print $NF; exit 0}}; {archfound=($0 ~ "arch.*"ARCH)}' <<<"${manifests}") IFS=$ifs sed -i "s|\(^FROM.*\)${baseimg}.*$|\1${baseimg}@${digest}|" "${dockerfile}" - GOARCH=${ARCH} make push-image-cephcsi + ARCH=${ARCH} make push-image-cephcsi done } diff --git a/deploy/cephcsi/image/Dockerfile b/deploy/cephcsi/image/Dockerfile index 2ad80d82f..0d1fd8745 100644 --- a/deploy/cephcsi/image/Dockerfile +++ b/deploy/cephcsi/image/Dockerfile @@ -1,15 +1,49 @@ +ARG SRC_DIR="/go/src/github.com/ceph/ceph-csi/" +ARG GO_ARCH FROM ceph/ceph:v15 -LABEL maintainers="Ceph-CSI Authors" -LABEL description="Ceph-CSI Plugin" -# To support cross building, do NOT RUN native binaries here. -# If we have to run native binaries, qemu-user-static must be installed on -# build host and mounted to container. +LABEL stage="build" -# Removing ceph-iscsi repo to workaround the repo issue while upgrading -#RUN rm -f /etc/yum.repos.d/ceph-iscsi.repo && yum -y update && yum clean all -ENV CSIBIN=/usr/local/bin/cephcsi +ARG GOLANG_VERSION=1.13.9 +ARG CSI_IMAGE_NAME=quay.io/cephcsi/cephcsi +ARG CSI_IMAGE_VERSION=canary +ARG GO_ARCH +ARG SRC_DIR +ARG GIT_COMMIT +ARG GOROOT=/usr/local/go +RUN dnf install libcephfs-devel librados-devel librbd-devel /usr/bin/cc make -y -COPY cephcsi $CSIBIN +RUN mkdir -p ${GOROOT} && \ + curl https://storage.googleapis.com/golang/go${GOLANG_VERSION}.linux-${GO_ARCH}.tar.gz | tar xzf - -C ${GOROOT} --strip-components=1 + + +ENV GOROOT=$GOROOT \ + GOPATH=/go \ + CGO_ENABLED=1 \ + GIT_COMMIT="${GIT_COMMIT}" \ + ENV_CSI_IMAGE_VERSION="${CSI_IMAGE_VERSION}" \ + ENV_CSI_IMAGE_NAME="${CSI_IMAGE_NAME}" \ + PATH="${GOROOT}/bin:${GOPATH}/bin:${PATH}" + + +WORKDIR ${SRC_DIR} + +# Copy source directories +COPY . ${SRC_DIR} + +# Build executable +RUN make cephcsi + +#-- Final container +FROM ceph/ceph:v15 + +ARG SRC_DIR + +LABEL maintainers="Ceph-CSI Authors" \ + version=$CSI_IMAGE_VERSION \ + architecture=$GO_ARCH \ + description="Ceph-CSI Plugin" + +COPY --from=0 ${SRC_DIR}/_output/cephcsi /usr/local/bin/cephcsi ENTRYPOINT ["/usr/local/bin/cephcsi"]