From 34725763b3f65324b92e1844b2ba0ed0662e3803 Mon Sep 17 00:00:00 2001 From: Razvan Dobre Date: Tue, 4 Jun 2024 15:26:09 +0300 Subject: [PATCH 1/2] Cross-compile koperator for arm and intel. --- Dockerfile | 2 +- Makefile | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index d4b8b4d54..43cbe6143 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Build the manager binary -FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:1.21 as builder +FROM golang:1.21 as builder ARG BUILDPLATFORM ARG TARGETPLATFORM diff --git a/Makefile b/Makefile index d202e3195..8559de552 100644 --- a/Makefile +++ b/Makefile @@ -156,6 +156,23 @@ docker-build: ## Build the operator docker image. docker-push: ## Push the operator docker image. docker push ${IMG} +# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple +# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to: +# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/ +# - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/ +# - be able to push the image to your registry (i.e. if you do not set a valid value via IMG=> then the export will fail) +# To adequately provide solutions that are compatible with multiple platforms, you should consider using this option. +PLATFORMS ?= linux/arm64,linux/amd64 +.PHONY: docker-buildx +docker-buildx: ## Build and push docker image for the manager for cross-platform support + # copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile + sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross + - docker buildx create --name project-v3-builder + docker buildx use project-v3-builder + - docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross . + - docker buildx rm project-v3-builder + rm Dockerfile.cross + bin/controller-gen: bin/controller-gen-$(CONTROLLER_GEN_VERSION) ## Symlink controller-gen- into versionless controller-gen. @ln -sf controller-gen-$(CONTROLLER_GEN_VERSION) bin/controller-gen From bb3a51192adc948139641e5fcd2440e13f8c72dd Mon Sep 17 00:00:00 2001 From: Razvan Dobre Date: Tue, 25 Jun 2024 14:45:19 +0300 Subject: [PATCH 2/2] implement review --- Dockerfile | 2 +- Makefile | 11 ++++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index 43cbe6143..d4b8b4d54 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Build the manager binary -FROM golang:1.21 as builder +FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:1.21 as builder ARG BUILDPLATFORM ARG TARGETPLATFORM diff --git a/Makefile b/Makefile index 8559de552..faf45722b 100644 --- a/Makefile +++ b/Makefile @@ -165,13 +165,10 @@ docker-push: ## Push the operator docker image. PLATFORMS ?= linux/arm64,linux/amd64 .PHONY: docker-buildx docker-buildx: ## Build and push docker image for the manager for cross-platform support - # copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile - sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross - - docker buildx create --name project-v3-builder - docker buildx use project-v3-builder - - docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross . - - docker buildx rm project-v3-builder - rm Dockerfile.cross + - docker buildx create --name koperator-builder + docker buildx use koperator-builder + docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile . + - docker buildx rm koperator-builder bin/controller-gen: bin/controller-gen-$(CONTROLLER_GEN_VERSION) ## Symlink controller-gen- into versionless controller-gen. @ln -sf controller-gen-$(CONTROLLER_GEN_VERSION) bin/controller-gen