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 <madhupr007@gmail.com>
This commit is contained in:
Madhu Rajanna 2020-04-21 12:09:03 +05:30 committed by mergify[bot]
parent 43fbc00d49
commit 32e2a713e6
3 changed files with 51 additions and 18 deletions

View File

@ -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)

View File

@ -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
}

View File

@ -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"]