Add `docker-buildx` target to `Makefile`

To allow building a multi-platform container image using `buildx`.

Various configuration flags allow for fine(r)-grain control over the
build process:

- `BASE_IMG`: FQDN of the base image that should be used, without a
  tag.
- `BASE_TAG: tag of the base image that should be used. Allows checksum
  sum to be included.
- `BUILDX_PLATFORMS`: platforms to target for the final container
  image.
- `BUILDX_ARGS`: additional `docker buildx build` arguments, e.g.
  `--push` to push the result to a (local) image registry.

Signed-off-by: Hidde Beydals <hello@hidde.co>
This commit is contained in:
Hidde Beydals 2021-09-28 00:12:39 +02:00 committed by pa250194
parent 869c7960e3
commit c9e3f97470
4 changed files with 27 additions and 12 deletions

View File

@ -1,4 +1,4 @@
FROM ghcr.io/hiddeco/golang-with-libgit2:dev as builder
FROM ghcr.io/hiddeco/golang-with-libgit2:dev as build
# Use the GitHub Actions uid:gid combination for proper fs permissions
RUN groupadd -g 116 test && \

View File

@ -44,11 +44,11 @@ jobs:
exit 1
fi
- name: Build container image
run: make docker-build IMG=test/source-controller:latest
run: make docker-build IMG=test/source-controller TAG=latest
- name: Load test image
run: kind load docker-image test/source-controller:latest
- name: Deploy controller
run: make dev-deploy IMG=test/source-controller:latest
run: make dev-deploy IMG=test/source-controller TAG=latest
- name: Run smoke tests
run: |
kubectl -n source-system apply -f ./config/samples

View File

@ -37,7 +37,7 @@ RUN groupadd controller && \
useradd --gid controller --shell /bin/sh --create-home controller
# Copy libgit2
COPY --from=build /libgit2/lib/* /usr/local/lib/
COPY --from=build /libgit2/lib/ /usr/local/lib/
RUN ldconfig
# Upgrade packages and install runtime dependencies

View File

@ -1,10 +1,17 @@
# Image URL to use all building/pushing image targets
IMG ?= fluxcd/source-controller:latest
IMG ?= fluxcd/source-controller
TAG ?= latest
# Base image used to build the Go binary
BASE_IMG ?= ghcr.io/hiddeco/golang-with-libgit2
BASE_TAG ?= dev
# Allows for defining additional Docker buildx arguments,
# e.g. '--push'.
BUILDX_ARGS ?=
# Architectures to build images for
BUILDX_PLATFORMS ?= linux/amd64,linux/arm64,linux/arm/v7
# Produce CRDs that work back to Kubernetes 1.16
CRD_OPTIONS ?= crd:crdVersions=v1
@ -60,12 +67,12 @@ uninstall: manifests ## Uninstall CRDs from a cluster
kustomize build config/crd | kubectl delete -f -
deploy: manifests ## Deploy controller in the configured Kubernetes cluster in ~/.kube/config
cd config/manager && kustomize edit set image fluxcd/source-controller=${IMG}
cd config/manager && kustomize edit set image fluxcd/source-controller=$(IMG):$(TAG)
kustomize build config/default | kubectl apply -f -
dev-deploy: ## Deploy controller dev image in the configured Kubernetes cluster in ~/.kube/config
mkdir -p config/dev && cp config/default/* config/dev
cd config/dev && kustomize edit set image fluxcd/source-controller=${IMG}
cd config/dev && kustomize edit set image fluxcd/source-controller=$(IMG):$(TAG)
kustomize build config/dev | kubectl apply -f -
rm -rf config/dev
@ -84,7 +91,7 @@ fmt: ## Run go fmt against code
go fmt ./...
cd api; go fmt ./...
vet: ## Run go vet against code
vet: $(LIBGIT2) ## Run go vet against code
PKG_CONFIG_PATH=$(LIBGIT2_LIB_PATH)/pkgconfig \
go vet ./...
cd api; go vet ./...
@ -92,14 +99,22 @@ vet: ## Run go vet against code
generate: controller-gen ## Generate API code
cd api; $(CONTROLLER_GEN) object:headerFile="../hack/boilerplate.go.txt" paths="./..."
docker-build: ## Build the docker image
docker-build: ## Build the Docker image
docker build \
--build-arg BASE_IMG=$(BASE_IMG) \
--build-arg BASE_TAG=$(BASE_TAG) \
-t ${IMG} .
-t $(IMG):$(TAG) .
docker-buildx: ## Build the cross-platform Docker image
docker buildx build \
--build-arg BASE_IMG=$(BASE_IMG) \
--build-arg BASE_TAG=$(BASE_TAG) \
--platform=$(BUILDX_PLATFORMS) \
-t $(IMG):$(TAG) \
$(BUILDX_ARGS) .
docker-push: ## Push docker image
docker push ${IMG}
docker-push: ## Push Docker image
docker push $(IMG):$(TAG)
controller-gen: ## Find or download controller-gen
ifeq (, $(shell which controller-gen))