Skip to content

Commit

Permalink
fix: speed up container image builds
Browse files Browse the repository at this point in the history
Signed-off-by: Mateus Oliveira <[email protected]>
  • Loading branch information
mateusoliveira43 committed Nov 4, 2024
1 parent 5f8342e commit c38305e
Show file tree
Hide file tree
Showing 21 changed files with 42 additions and 56 deletions.
1 change: 0 additions & 1 deletion docs/book/src/cronjob-tutorial/testdata/project/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*.so
*.dylib
bin/*
Dockerfile.cross

# Test binary, built with `go test -c`
*.test
Expand Down
3 changes: 2 additions & 1 deletion docs/book/src/cronjob-tutorial/testdata/project/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Build the manager binary
FROM golang:1.22 AS builder
FROM --platform=${BUILDPLATFORM} golang:1.22 AS builder
ARG BUILDPLATFORM
ARG TARGETOS
ARG TARGETARCH

Expand Down
10 changes: 4 additions & 6 deletions docs/book/src/cronjob-tutorial/testdata/project/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,19 @@ docker-push: ## Push docker image with the manager.
$(CONTAINER_TOOL) 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:
# architectures.
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
# 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=<myregistry/image:<tag>> 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,linux/s390x,linux/ppc64le
.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
- $(CONTAINER_TOOL) buildx create --name project-builder
$(CONTAINER_TOOL) buildx use project-builder
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} .
- $(CONTAINER_TOOL) buildx rm project-builder
rm Dockerfile.cross

.PHONY: build-installer
build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.
Expand Down
1 change: 0 additions & 1 deletion docs/book/src/getting-started/testdata/project/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*.so
*.dylib
bin/*
Dockerfile.cross

# Test binary, built with `go test -c`
*.test
Expand Down
3 changes: 2 additions & 1 deletion docs/book/src/getting-started/testdata/project/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Build the manager binary
FROM golang:1.22 AS builder
FROM --platform=${BUILDPLATFORM} golang:1.22 AS builder
ARG BUILDPLATFORM
ARG TARGETOS
ARG TARGETARCH

Expand Down
10 changes: 4 additions & 6 deletions docs/book/src/getting-started/testdata/project/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,19 @@ docker-push: ## Push docker image with the manager.
$(CONTAINER_TOOL) 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:
# architectures.
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
# 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=<myregistry/image:<tag>> 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,linux/s390x,linux/ppc64le
.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
- $(CONTAINER_TOOL) buildx create --name project-builder
$(CONTAINER_TOOL) buildx use project-builder
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} .
- $(CONTAINER_TOOL) buildx rm project-builder
rm Dockerfile.cross

.PHONY: build-installer
build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*.so
*.dylib
bin/*
Dockerfile.cross

# Test binary, built with `go test -c`
*.test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Build the manager binary
FROM golang:1.22 AS builder
FROM --platform=${BUILDPLATFORM} golang:1.22 AS builder
ARG BUILDPLATFORM
ARG TARGETOS
ARG TARGETARCH

Expand Down
10 changes: 4 additions & 6 deletions docs/book/src/multiversion-tutorial/testdata/project/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,19 @@ docker-push: ## Push docker image with the manager.
$(CONTAINER_TOOL) 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:
# architectures.
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
# 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=<myregistry/image:<tag>> 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,linux/s390x,linux/ppc64le
.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
- $(CONTAINER_TOOL) buildx create --name project-builder
$(CONTAINER_TOOL) buildx use project-builder
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} .
- $(CONTAINER_TOOL) buildx rm project-builder
rm Dockerfile.cross

.PHONY: build-installer
build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func (f *Dockerfile) SetTemplateDefaults() error {
}

const dockerfileTemplate = `# Build the manager binary
FROM golang:1.22 AS builder
FROM --platform=${BUILDPLATFORM} golang:1.22 AS builder
ARG BUILDPLATFORM
ARG TARGETOS
ARG TARGETARCH
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ const gitignoreTemplate = `# Binaries for programs and plugins
*.so
*.dylib
bin/*
Dockerfile.cross
# Test binary, built with ` + "`go test -c`" + `
*.test
Expand Down
10 changes: 4 additions & 6 deletions pkg/plugins/golang/v4/scaffolds/internal/templates/makefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,21 +187,19 @@ docker-push: ## Push docker image with the manager.
$(CONTAINER_TOOL) 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:
# architectures.
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
# 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=<myregistry/image:<tag>> 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,linux/s390x,linux/ppc64le
.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
- $(CONTAINER_TOOL) buildx create --name {{ .ProjectName }}-builder
$(CONTAINER_TOOL) buildx use {{ .ProjectName }}-builder
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} .
- $(CONTAINER_TOOL) buildx rm {{ .ProjectName }}-builder
rm Dockerfile.cross
.PHONY: build-installer
build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.
Expand Down
1 change: 0 additions & 1 deletion testdata/project-v4-multigroup/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*.so
*.dylib
bin/*
Dockerfile.cross

# Test binary, built with `go test -c`
*.test
Expand Down
3 changes: 2 additions & 1 deletion testdata/project-v4-multigroup/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Build the manager binary
FROM golang:1.22 AS builder
FROM --platform=${BUILDPLATFORM} golang:1.22 AS builder
ARG BUILDPLATFORM
ARG TARGETOS
ARG TARGETARCH

Expand Down
10 changes: 4 additions & 6 deletions testdata/project-v4-multigroup/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,19 @@ docker-push: ## Push docker image with the manager.
$(CONTAINER_TOOL) 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:
# architectures.
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
# 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=<myregistry/image:<tag>> 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,linux/s390x,linux/ppc64le
.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
- $(CONTAINER_TOOL) buildx create --name project-v4-multigroup-builder
$(CONTAINER_TOOL) buildx use project-v4-multigroup-builder
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} .
- $(CONTAINER_TOOL) buildx rm project-v4-multigroup-builder
rm Dockerfile.cross

.PHONY: build-installer
build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.
Expand Down
1 change: 0 additions & 1 deletion testdata/project-v4-with-plugins/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*.so
*.dylib
bin/*
Dockerfile.cross

# Test binary, built with `go test -c`
*.test
Expand Down
3 changes: 2 additions & 1 deletion testdata/project-v4-with-plugins/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Build the manager binary
FROM golang:1.22 AS builder
FROM --platform=${BUILDPLATFORM} golang:1.22 AS builder
ARG BUILDPLATFORM
ARG TARGETOS
ARG TARGETARCH

Expand Down
10 changes: 4 additions & 6 deletions testdata/project-v4-with-plugins/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,19 @@ docker-push: ## Push docker image with the manager.
$(CONTAINER_TOOL) 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:
# architectures.
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
# 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=<myregistry/image:<tag>> 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,linux/s390x,linux/ppc64le
.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
- $(CONTAINER_TOOL) buildx create --name project-v4-with-plugins-builder
$(CONTAINER_TOOL) buildx use project-v4-with-plugins-builder
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} .
- $(CONTAINER_TOOL) buildx rm project-v4-with-plugins-builder
rm Dockerfile.cross

.PHONY: build-installer
build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.
Expand Down
1 change: 0 additions & 1 deletion testdata/project-v4/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*.so
*.dylib
bin/*
Dockerfile.cross

# Test binary, built with `go test -c`
*.test
Expand Down
3 changes: 2 additions & 1 deletion testdata/project-v4/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Build the manager binary
FROM golang:1.22 AS builder
FROM --platform=${BUILDPLATFORM} golang:1.22 AS builder
ARG BUILDPLATFORM
ARG TARGETOS
ARG TARGETARCH

Expand Down
10 changes: 4 additions & 6 deletions testdata/project-v4/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,19 @@ docker-push: ## Push docker image with the manager.
$(CONTAINER_TOOL) 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:
# architectures.
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
# 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=<myregistry/image:<tag>> 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,linux/s390x,linux/ppc64le
.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
- $(CONTAINER_TOOL) buildx create --name project-v4-builder
$(CONTAINER_TOOL) buildx use project-v4-builder
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} .
- $(CONTAINER_TOOL) buildx rm project-v4-builder
rm Dockerfile.cross

.PHONY: build-installer
build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.
Expand Down

0 comments on commit c38305e

Please sign in to comment.