standardize makefile for images

This commit is contained in:
Benjamin Elder
2020-06-17 19:30:39 -07:00
parent f9b7e5a430
commit 07e601ffb5
6 changed files with 42 additions and 134 deletions

38
images/Makefile.common.in Normal file
View File

@@ -0,0 +1,38 @@
# shared makefile for all images
# get image name from directory we're building
IMAGE_NAME=$(notdir $(CURDIR))
# docker image registry, default to upstream
REGISTRY?=kindest
# tag based on date-sha
TAG?=$(shell echo "$$(date +v%Y%m%d)-$$(git describe --always --dirty)")
# the full image tag
IMAGE?=$(REGISTRY)/$(IMAGE_NAME):$(TAG)
# required to enable buildx
export DOCKER_CLI_EXPERIMENTAL=enabled
# build with buildx
PLATFORMS?=linux/amd64,linux/arm64,linux/ppc64le
OUTPUT=
PROGRESS=auto
build: ensure-buildx
docker buildx build --platform=${PLATFORMS} $(OUTPUT) --progress=$(PROGRESS) -t ${IMAGE} --pull .
# push the cross built image
push: OUTPUT=--push
push: build
# quick can be used to do a build that will be imported into the local docker
# for sanity checking before doing a cross build push
# cross builds cannot be imported locally at the moment
# https://github.com/docker/buildx/issues/59
quick: PLATFORMS=linux/amd64
quick: OUTPUT=--load
quick: build
# enable buildx
ensure-buildx:
./../../hack/build/init-buildx.sh
.PHONY: push build quick ensure-buildx

View File

@@ -1,29 +1 @@
TAG?=$(shell echo "$$(date +v%Y%m%d)-$$(git describe --always --dirty)")
IMAGE?=kindest/base:$(TAG)
# required for buildx
export DOCKER_CLI_EXPERIMENTAL=enabled
# build with buildx
PLATFORMS?=linux/amd64,linux/arm64
OUTPUT=
PROGRESS=auto
build: ensure-buildx
docker buildx build --platform=${PLATFORMS} $(OUTPUT) --progress=$(PROGRESS) -t ${IMAGE} --pull .
# push the cross built image
push: OUTPUT=--push
push: build
# quick can be used to do a build that will be imported into the local docker
# for sanity checking before doing a cross build push
# cross builds cannot be imported locally at the moment
# https://github.com/docker/buildx/issues/59
quick: PLATFORMS=linux/amd64
quick: OUTPUT=--load
quick: build
# enable buildx
ensure-buildx:
./../../hack/build/init-buildx.sh
.PHONY: push build quick ensure-buildx
include $(CURDIR)/../Makefile.common.in

View File

@@ -13,7 +13,6 @@
# limitations under the License.
# standard haproxy image + minimal config so the container will not exit
ARG ARCH="amd64"
ARG BASE="haproxy:2.1.1-alpine"
FROM ${ARCH}/${BASE}
FROM ${BASE}
COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg

1
images/haproxy/Makefile Normal file
View File

@@ -0,0 +1 @@
include $(CURDIR)/../Makefile.common.in

View File

@@ -1,74 +0,0 @@
#!/bin/bash
# Copyright 2019 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.
# builds and pushes the haproxy image for all architectures
set -o errexit
set -o nounset
set -o pipefail
# cd to the repo root
REPO_ROOT=$(git rev-parse --show-toplevel)
cd "${REPO_ROOT}"
# cd to the image
cd ./images/haproxy
IMAGE="${IMAGE:-kindest/haproxy}"
TAG="${TAG:-2.1.1-alpine}"
BASE="haproxy:${TAG}"
# tag arch, manifest architecture, variant
ARCHES=(
"amd64,amd64,"
"arm64v8,arm64,v8"
"ppc64le,ppc64le,"
)
# build all images
images=()
image_infos=()
for arch_info in "${ARCHES[@]}"; do
# build image
tag_arch="$(sed -E 's#^([^,]+),[^,]*,[^,]*$#\1#' <<<"${arch_info}")"
image="${IMAGE}:${tag_arch}-${TAG}"
docker build "--build-arg=ARCH=${tag_arch}" "--build-arg=BASE=${BASE}" -f Dockerfile -t "${image}" .
docker push "${image}"
# join image we tagged with arch info for building the manifest later
images+=("${image}")
image_infos+=("${image},${arch_info}")
done
# This option is required for running the docker manifest command
export DOCKER_CLI_EXPERIMENTAL="enabled"
# create and push the manifest
docker manifest create "${IMAGE}:${TAG}" "${images[@]}"
for image_info in "${image_infos[@]}"; do
# split out image info
# image:arch-tag, grab arch
image="$(sed -E 's#^([^,]+),[^,]+,[^,]+,[^,]*$#\1#' <<<"${image_info}")"
#tag_arch="$(sed -E 's#^[^,]+,([^,]+),[^,]+,[^,]*$#\1#' <<<"${image_info}")"
architecture="$(sed -E 's#^[^,]+,[^,]+,([^,]+),[^,]*$#\1#' <<<"${image_info}")"
variant="$(sed -E 's#^[^,]+,[^,]+,[^,]+,([^,]*)$#\1#' <<<"${image_info}")"
args=("--arch" "${architecture}")
if [ -n "${variant}" ]; then
args+=("--variant" "${variant}")
fi
# add the image to the manifest
docker manifest annotate "${IMAGE}:${TAG}" "${image}" "${args[@]}"
done
docker manifest push --purge "${IMAGE}:${TAG}"

View File

@@ -1,29 +1 @@
TAG?=$(shell echo "$$(date +v%Y%m%d)-$$(git describe --always --dirty)")
IMAGE?=kindest/kindnetd:$(TAG)
# required for buildx
export DOCKER_CLI_EXPERIMENTAL=enabled
# build with buildx
PLATFORMS?=linux/amd64,linux/arm64,linux/ppc64le
OUTPUT=
PROGRESS=auto
build: ensure-buildx
docker buildx build --platform=${PLATFORMS} $(OUTPUT) --progress=$(PROGRESS) -t ${IMAGE} --pull .
# push the cross built image
push: OUTPUT=--push
push: build
# quick can be used to do a build that will be imported into the local docker
# for sanity checking before doing a cross build push
# cross builds cannot be imported locally at the moment
# https://github.com/docker/buildx/issues/59
quick: PLATFORMS=linux/amd64
quick: OUTPUT=--load
quick: build
# enable buildx
ensure-buildx:
./../../hack/build/init-buildx.sh
.PHONY: push build quick ensure-buildx
include $(CURDIR)/../Makefile.common.in