Improve buildx configuration

This commit is contained in:
Manuel Alejandro de Brito Fontes 2020-06-23 18:05:07 -04:00
parent d3832915e1
commit 020d8729d4
5 changed files with 85 additions and 40 deletions

View file

@ -239,15 +239,10 @@ else
@hack/check-go-version.sh @hack/check-go-version.sh
endif endif
.PHONY: init-docker-buildx .PHONY: ensure-buildx
init-docker-buildx: ensure-buildx:
ifeq ($(DIND_TASKS),) ifeq ($(DIND_TASKS),)
ifneq ($(shell docker buildx 2>&1 >/dev/null; echo $?),) ./hack/init-buildx.sh
$(error "buildx not available. Docker 19.03 or higher is required with experimental features enabled")
endif
docker run --rm --privileged docker/binfmt:a7996909642ee92942dcd6cff44b9b95f08dad64
docker buildx create --name ingress-nginx --use || true
docker buildx inspect --bootstrap
endif endif
.PHONY: show-version .PHONY: show-version
@ -261,7 +256,7 @@ SPACE := $(EMPTY) $(EMPTY)
COMMA := , COMMA := ,
.PHONY: release # Build a multi-arch docker image .PHONY: release # Build a multi-arch docker image
release: init-docker-buildx clean release: ensure-buildx clean
echo "Building binaries..." echo "Building binaries..."
$(foreach PLATFORM,$(PLATFORMS), echo -n "$(PLATFORM)..."; ARCH=$(PLATFORM) make build;) $(foreach PLATFORM,$(PLATFORMS), echo -n "$(PLATFORM)..."; ARCH=$(PLATFORM) make build;)

View file

@ -75,16 +75,7 @@ cd ingress-nginx
export DOCKER_CLI_EXPERIMENTAL=enabled export DOCKER_CLI_EXPERIMENTAL=enabled
make init-docker-buildx make ensure-buildx
docker buildx use ingress-nginx --default --global
# disable docker in docker tasks echo "Building ingress controller image..."
export DIND_TASKS=0 make release
echo "Building NGINX image..."
ARCH=amd64 make build image push
ARCH=arm make build image push
ARCH=arm64 make build image push
echo "Creating multi-arch images..."
make push-manifest

View file

@ -67,8 +67,7 @@ cd ingress-nginx/images/nginx
export TAG=$(git rev-parse HEAD) export TAG=$(git rev-parse HEAD)
make init-docker-buildx make ensure-buildx
docker buildx use ingress-nginx --default --global
echo "Building NGINX images..." echo "Building NGINX images..."
make image make image

54
hack/init-buildx.sh Executable file
View file

@ -0,0 +1,54 @@
#!/usr/bin/env bash
# Copyright 2020 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
if [ -n "$DEBUG" ]; then
set -x
fi
set -o errexit
set -o nounset
set -o pipefail
export DOCKER_CLI_EXPERIMENTAL=enabled
if ! docker buildx 2>&1 >/dev/null; then
echo "buildx not available. Docker 19.03 or higher is required with experimental features enabled"
exit 1
fi
# We can skip setup if the current builder already has multi-arch
# AND if it isn't the docker driver, which doesn't work
current_builder="$(docker buildx inspect)"
# linux/amd64, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6
if ! grep -q "^Driver: docker$" <<<"${current_builder}" && \
grep -q "linux/amd64" <<<"${current_builder}" && \
grep -q "linux/arm" <<<"${current_builder}" && \
grep -q "linux/arm64" <<<"${current_builder}" && \
grep -q "linux/s390x" <<<"${current_builder}"; then
exit 0
fi
# Ensure qemu is in binfmt_misc
# Docker desktop already has these in versions recent enough to have buildx
# We only need to do this setup on linux hosts
if [ "$(uname)" == 'Linux' ]; then
# NOTE: this is pinned to a digest for a reason!
docker run --rm --privileged multiarch/qemu-user-static@sha256:c772ee1965aa0be9915ee1b018a0dd92ea361b4fa1bcab5bbc033517749b2af4 --reset -p yes
fi
# Ensure we use a builder that can leverage it (the default on linux will not)
docker buildx rm ingress-nginx || true
docker buildx create --use --name=ingress-nginx

View file

@ -12,31 +12,37 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
.DEFAULT_GOAL:=image .DEFAULT_GOAL:=build
# set default shell # set default shell
SHELL=/bin/bash -o pipefail SHELL=/bin/bash
# 0.0.0 shouldn't clobber any released builds # 0.0.0 shouldn't clobber any released builds
TAG ?= 0.103 TAG ?= 0.104
REGISTRY ?= quay.io/kubernetes-ingress-controller REGISTRY ?= gcr.io/k8s-staging-ingress-nginx
IMAGE = $(REGISTRY)/nginx IMAGE = $(REGISTRY)/nginx
.PHONY: image # required to enable buildx
image: export DOCKER_CLI_EXPERIMENTAL=enabled
# build with buildx
PLATFORMS?=linux/amd64,linux/arm,linux/arm64,linux/s390x
OUTPUT=
PROGRESS=plain
build: ensure-buildx
docker buildx build \ docker buildx build \
--platform=${PLATFORMS} $(OUTPUT) \
--progress=$(PROGRESS) \
--pull \ --pull \
--push \
--progress plain \
--platform amd64,arm,arm64,s390x \
--tag $(IMAGE):$(TAG) rootfs --tag $(IMAGE):$(TAG) rootfs
.PHONY: init-docker-buildx # push the cross built image
init-docker-buildx: push: OUTPUT=--push
ifneq ($(shell docker buildx 2>&1 >/dev/null; echo $?),) push: build
$(error "buildx not vailable. Docker 19.03 or higher is required")
endif # enable buildx
docker run --rm --privileged docker/binfmt:a7996909642ee92942dcd6cff44b9b95f08dad64 ensure-buildx:
docker buildx create --name ingress-nginx --use || true ./../../hack/init-buildx.sh
docker buildx inspect --bootstrap
.PHONY: build push ensure-buildx