Skip to content

Commit

Permalink
✨ Able to build provider docker without Makefile and Docker file modi…
Browse files Browse the repository at this point in the history
…fications
  • Loading branch information
dmvolod committed Dec 19, 2024
1 parent 08f2c8c commit 018162c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
13 changes: 10 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# Build the manager binary
# Run this with docker build --build-arg builder_image=<golang:x.y.z>
ARG builder_image
ARG deployment_base_image
ARG goprivate

# Build architecture
ARG ARCH
Expand All @@ -32,21 +34,26 @@ WORKDIR /workspace
ARG goproxy=https://proxy.golang.org
# Run this with docker build --build-arg package=./controlplane/kubeadm or --build-arg package=./bootstrap/kubeadm
ENV GOPROXY=$goproxy
ENV GOPRIVATE=$goprivate

# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum

ARG additional_mounts

# Cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN --mount=type=cache,target=/go/pkg/mod \
RUN --mount=type=secret,id=netrc,required=false,target=/root/.netrc \
--mount=type=cache,target=/go/pkg/mod \
go mod download

# Copy the sources
COPY ./ ./

# Cache the go build into the Go’s compiler cache folder so we take benefits of compiler caching across docker build calls
RUN --mount=type=cache,target=/root/.cache/go-build \
RUN --mount=type=secret,id=netrc,required=false,target=/root/.netrc \
--mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg/mod \
go build .

Expand All @@ -63,7 +70,7 @@ RUN --mount=type=cache,target=/root/.cache/go-build \
-o manager ${package}

# Production image
FROM gcr.io/distroless/static:nonroot-${ARCH}
FROM ${deployment_base_image}-${ARCH}
WORKDIR /
COPY --from=builder /workspace/manager .
# Use uid of nonroot user (65532) because kubernetes expects numeric user when applying pod security policies
Expand Down
19 changes: 15 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ SHELL:=/usr/bin/env bash
# Go.
#
GO_VERSION ?= 1.22.9
GO_CONTAINER_IMAGE ?= docker.io/library/golang:$(GO_VERSION)
GO_BASE_CONTAINER ?= docker.io/library/golang
GO_CONTAINER_IMAGE ?= $(GO_BASE_CONTAINER):$(GO_VERSION)

# Use GOPROXY environment variable if set
GOPROXY := $(shell go env GOPROXY)
Expand All @@ -33,9 +34,19 @@ GOPROXY := https://proxy.golang.org
endif
export GOPROXY

# Use GOPRIVATE environment variable if set
GOPRIVATE := $(shell go env GOPRIVATE)
export GOPRIVATE

# Active module mode, as we use go modules to manage dependencies
export GO111MODULE=on

# Base docker images

DOCKER_SYNTAX_CONTAINER_IMAGE ?= docker.io/docker/dockerfile:1.4
DEPLOYMENT_BASE_CONTAINER_IMAGE ?= gcr.io/distroless/static:nonroot
BUILD_CONTAINER_ADDITIONAL_ARGS ?=

#
# Kubebuilder.
#
Expand Down Expand Up @@ -387,9 +398,9 @@ manager: ## Build the manager binary into the ./bin folder

.PHONY: docker-pull-prerequisites
docker-pull-prerequisites:
docker pull docker.io/docker/dockerfile:1.4
docker pull $(DOCKER_SYNTAX_CONTAINER_IMAGE)
docker pull $(GO_CONTAINER_IMAGE)
docker pull gcr.io/distroless/static:latest
docker pull $(DEPLOYMENT_BASE_CONTAINER_IMAGE)-$(ARCH)

.PHONY: docker-build-all
docker-build-all: $(addprefix docker-build-,$(ALL_ARCH)) ## Build docker images for all architectures
Expand All @@ -399,7 +410,7 @@ docker-build-%:

.PHONY: docker-build
docker-build: docker-pull-prerequisites ## Build the docker image for core controller manager
DOCKER_BUILDKIT=1 docker build --build-arg builder_image=$(GO_CONTAINER_IMAGE) --build-arg goproxy=$(GOPROXY) --build-arg ARCH=$(ARCH) --build-arg ldflags="$(LDFLAGS)" . -t $(CONTROLLER_IMG)-$(ARCH):$(TAG)
DOCKER_BUILDKIT=1 docker build $(BUILD_CONTAINER_ADDITIONAL_ARGS) --build-arg builder_image=$(GO_CONTAINER_IMAGE) --build-arg deployment_base_image=$(DEPLOYMENT_BASE_CONTAINER_IMAGE) --build-arg goproxy=$(GOPROXY) --build-arg goprivate=$(GOPRIVATE) --build-arg ARCH=$(ARCH) --build-arg ldflags="$(LDFLAGS)" . -t $(CONTROLLER_IMG)-$(ARCH):$(TAG)
$(MAKE) set-manifest-image MANIFEST_IMG=$(CONTROLLER_IMG)-$(ARCH) MANIFEST_TAG=$(TAG) TARGET_RESOURCE="./config/default/manager_image_patch.yaml"
$(MAKE) set-manifest-pull-policy TARGET_RESOURCE="./config/default/manager_pull_policy.yaml"

Expand Down

0 comments on commit 018162c

Please sign in to comment.