diff --git a/Dockerfile b/Dockerfile index cc8d04f3..69c985c3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -29,14 +29,18 @@ FROM ${builder_image} as builder WORKDIR /workspace # Run this with docker build --build-arg goproxy=$(go env GOPROXY) to override the goproxy -ARG goproxy=off -# Run this with docker build --build-arg package=./controlplane/kubeadm or --build-arg package=./bootstrap/kubeadm +ARG goproxy=https://proxy.golang.org ENV GOPROXY=$goproxy # Copy the Go Modules manifests COPY go.mod go.mod COPY go.sum go.sum +# 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 \ + go mod download + # Copy the sources COPY ./ ./ @@ -46,7 +50,9 @@ ARG ARCH ARG ldflags # Do not force rebuild of up-to-date packages (do not use -a) and use the compiler cache folder -RUN CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} \ +RUN --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/go/pkg/mod \ + CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} \ go build -trimpath -ldflags "${ldflags} -extldflags '-static'" \ -o manager ${package} diff --git a/Makefile b/Makefile index a3aa15ee..e8afe051 100644 --- a/Makefile +++ b/Makefile @@ -25,13 +25,6 @@ GO_CONTAINER_IMAGE ?= docker.io/library/golang:$(GO_VERSION) ARCH ?= $(shell go env GOARCH) -# Use GOPROXY environment variable if set -GOPROXY := $(shell go env GOPROXY) -ifeq ($(GOPROXY),) -GOPROXY := https://proxy.golang.org -endif -export GOPROXY - # Active module mode, as we use go modules to manage dependencies export GO111MODULE=on @@ -231,7 +224,7 @@ generate-bootstrap-conversions: $(CONVERSION_GEN) # Build the docker image docker-build-bootstrap: manager-bootstrap ## Build bootstrap - DOCKER_BUILDKIT=1 docker build --build-arg builder_image=$(GO_CONTAINER_IMAGE) --build-arg goproxy=$(GOPROXY) --build-arg ARCH=$(ARCH) --build-arg package=./bootstrap/main.go --build-arg ldflags="$(LDFLAGS)" . -t ${BOOTSTRAP_IMG}:${BOOTSTRAP_IMG_TAG} + DOCKER_BUILDKIT=1 docker build --build-arg builder_image=$(GO_CONTAINER_IMAGE) --build-arg ARCH=$(ARCH) --build-arg package=./bootstrap/main.go --build-arg ldflags="$(LDFLAGS)" . -t ${BOOTSTRAP_IMG}:${BOOTSTRAP_IMG_TAG} # Push the docker image docker-push-bootstrap: ## Push bootstrap @@ -303,7 +296,7 @@ generate-controlplane-conversions: $(CONVERSION_GEN) --go-header-file=./hack/boilerplate.go.txt docker-build-controlplane: manager-controlplane ## Build control-plane - DOCKER_BUILDKIT=1 docker build --build-arg builder_image=$(GO_CONTAINER_IMAGE) --build-arg goproxy=$(GOPROXY) --build-arg ARCH=$(ARCH) --build-arg package=./controlplane/main.go --build-arg ldflags="$(LDFLAGS)" . -t ${CONTROLPLANE_IMG}:$(CONTROLPLANE_IMG_TAG) + DOCKER_BUILDKIT=1 docker build --build-arg builder_image=$(GO_CONTAINER_IMAGE) --build-arg ARCH=$(ARCH) --build-arg package=./controlplane/main.go --build-arg ldflags="$(LDFLAGS)" . -t ${CONTROLPLANE_IMG}:$(CONTROLPLANE_IMG_TAG) docker-push-controlplane: ## Push control-plane docker push ${CONTROLPLANE_IMG}:$(CONTROLPLANE_IMG_TAG)