Skip to content

Commit

Permalink
Support cross-building
Browse files Browse the repository at this point in the history
  • Loading branch information
cofyc committed May 6, 2019
1 parent 09228c6 commit a52bda9
Show file tree
Hide file tree
Showing 8 changed files with 486 additions and 26 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# kubetest will extract kubernetes here, ignore them to speed up docker build
kubernetes/
kubernetes.tar.gz
_output/
74 changes: 54 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,41 @@
# See the License for the specific language governing permissions and
# limitations under the License.

ifeq ($(REGISTRY),)
REGISTRY = quay.io/external_storage
endif
REGISTRY ?= quay.io/external_storage
VERSION ?= latest
GOVERSION ?= 1.11.1
# Architectures supported: amd64, arm, arm64, ppc64le and s390x
ARCH ?= amd64

ifeq ($(VERSION),)
VERSION = latest
endif
ALL_ARCH = amd64 arm arm64 ppc64le s390x

ifeq ($(GOVERSION),)
GOVERSION = 1.11.1
endif
IMAGE = $(REGISTRY)/local-volume-provisioner-$(ARCH):$(VERSION)
MUTABLE_IMAGE = $(REGISTRY)/local-volume-provisioner-$(ARCH):latest

IMAGE = $(REGISTRY)/local-volume-provisioner:$(VERSION)
MUTABLE_IMAGE = $(REGISTRY)/local-volume-provisioner:latest
TEMP_DIR := $(shell mktemp -d)
QEMUVERSION = v2.9.1

ifeq ($(ARCH),arm)
QEMUARCH = arm
endif
ifeq ($(ARCH),arm64)
QEMUARCH = aarch64
endif
ifeq ($(ARCH),ppc64le)
QEMUARCH = ppc64le
endif
ifeq ($(ARCH),s390x)
QEMUARCH = s390x
endif

SUDO = $(if $(filter 0,$(shell id -u)),,sudo)

all: provisioner
all: cross
.PHONY: all

cross: $(addprefix provisioner-,$(ALL_ARCH))
.PHONY: cross

verify:
./hack/verify-all.sh
.PHONY: verify
Expand All @@ -42,21 +59,38 @@ release:
./hack/release.sh
.PHONY: release

provisioner-%:
$(MAKE) ARCH=$* provisioner

provisioner:
docker build -t $(MUTABLE_IMAGE) --build-arg GOVERSION=${GOVERSION} -f deployment/docker/Dockerfile .
mkdir -p _output
# because COPY does not expand build arguments, we need substitute it
cat ./deployment/docker/Dockerfile \
| sed "s|QEMUARCH|$(QEMUARCH)|g" \
> $(TEMP_DIR)/Dockerfile
ifeq ($(ARCH),amd64)
# When building "normally" for amd64, remove the whole line, it has no part in the amd64 image
sed "/CROSS_BUILD_/d" $(TEMP_DIR)/Dockerfile > $(TEMP_DIR)/Dockerfile.tmp
else
# When cross-building, only the placeholder "CROSS_BUILD_" should be removed
# Register /usr/bin/qemu-ARCH-static as the handler for non-x86 binaries in the kernel
$(SUDO) ./third_party/multiarch/qemu-user-static/register/register.sh --reset
curl -sSL https://github.com/multiarch/qemu-user-static/releases/download/$(QEMUVERSION)/x86_64_qemu-$(QEMUARCH)-static.tar.gz | tar -xz -C _output
# Ensure we don't get surprised by umask settings
chmod 0755 _output/qemu-$(QEMUARCH)-static
sed "s/CROSS_BUILD_//g" $(TEMP_DIR)/Dockerfile > $(TEMP_DIR)/Dockerfile.tmp
endif
mv $(TEMP_DIR)/Dockerfile.tmp $(TEMP_DIR)/Dockerfile
docker build -t $(MUTABLE_IMAGE) --build-arg GOVERSION=$(GOVERSION) --build-arg ARCH=$(ARCH) -f $(TEMP_DIR)/Dockerfile .
docker tag $(MUTABLE_IMAGE) $(IMAGE)
rm -rf $(TEMP_DIR)
.PHONY: provisioner

push: provisioner
docker push $(IMAGE)
docker push $(MUTABLE_IMAGE)
.PHONY: push

test: provisioner
go test ./cmd/... ./pkg/...
docker run --privileged -v $(PWD)/deployment/docker/test.sh:/test.sh --entrypoint bash quay.io/external_storage/local-volume-provisioner:latest /test.sh
docker run --privileged -v $(PWD)/deployment/docker/test.sh:/test.sh --entrypoint bash $(IMAGE) /test.sh
.PHONY: test

clean:
rm -f deployment/docker/local-volume-provisioner
rm -rf _output
.PHONY: clean
13 changes: 10 additions & 3 deletions deployment/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,22 @@
# See the License for the specific language governing permissions and
# limitations under the License.

ARG OS=linux
ARG ARCH=amd64
ARG GOVERSION=1.11.1
FROM golang:$GOVERSION-alpine as builder
WORKDIR /go/src/sigs.k8s.io/sig-storage-local-static-provisioner
ADD . .
RUN CGO_ENABLED=0 go build -a -ldflags '-extldflags "-static"' -o local-volume-provisioner ./cmd/local-volume-provisioner
RUN CGO_ENABLED=0 GOOS=${OS} GOARCH=${ARCH} go build -a -ldflags '-extldflags "-static"' -o _output/${OS}/${ARCH}/local-volume-provisioner ./cmd/local-volume-provisioner

# For security, we use kubernetes community maintained debian base image.
# https://github.com/kubernetes/kubernetes/blob/master/build/debian-base/
FROM k8s.gcr.io/debian-base-amd64:0.4.0
FROM k8s.gcr.io/debian-base-${ARCH}:0.4.0

# If we're building for another architecture than amd64, the CROSS_BUILD_ placeholder is removed so
# e.g. CROSS_BUILD_COPY turns into COPY
# If we're building normally, for amd64, CROSS_BUILD lines are removed
CROSS_BUILD_COPY _output/qemu-QEMUARCH-static /usr/bin/

# Keep packages up to date and install packages for our needs.
RUN apt-get update \
Expand All @@ -30,6 +37,6 @@ RUN apt-get update \
e2fsprogs \
bash

COPY --from=builder /go/src/sigs.k8s.io/sig-storage-local-static-provisioner/local-volume-provisioner /local-provisioner
COPY --from=builder /go/src/sigs.k8s.io/sig-storage-local-static-provisioner/_output/${OS}/${ARCH}/local-volume-provisioner /local-provisioner
ADD deployment/docker/scripts /scripts
ENTRYPOINT ["/local-provisioner"]
4 changes: 2 additions & 2 deletions hack/run-e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ fi

# build image if not specified
if [ -z "$PROVISIONER_E2E_IMAGE" ]; then
make
PROVISIONER_E2E_IMAGE="quay.io/external_storage/local-volume-provisioner:latest"
make provisioner
PROVISIONER_E2E_IMAGE="quay.io/external_storage/local-volume-provisioner-amd64:latest"
else
docker pull $PROVISIONER_E2E_IMAGE
fi
Expand Down
21 changes: 21 additions & 0 deletions third_party/multiarch/qemu-user-static/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015-2016 Manfred Touron

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions third_party/multiarch/qemu-user-static/README.kubernetes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Files copied from https://github.com/multiarch/qemu-user-static at commit 22b0013668d2aed4a2cfd21650e85c664b1f21c6.
Loading

0 comments on commit a52bda9

Please sign in to comment.