Skip to content

Commit 80653d1

Browse files
authored
Merge pull request #3210 from fabriziopandini/use-docker-buildkit
🌱 Use docker build kit to reuse go cache
2 parents 46bf40c + 20eb90a commit 80653d1

File tree

7 files changed

+21
-8
lines changed

7 files changed

+21
-8
lines changed

Dockerfile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# syntax = docker/dockerfile:1-experimental
2+
13
# Copyright 2018 The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -33,14 +35,16 @@ RUN go mod download
3335
COPY ./ ./
3436

3537
# Cache the go build
36-
RUN go build .
38+
RUN --mount=type=cache,target=/root/.cache/go-build \
39+
go build .
3740

3841
# Build
3942
ARG package=.
4043
ARG ARCH
4144

4245
# Do not force rebuild of up-to-date packages (do not use -a)
43-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} \
46+
RUN --mount=type=cache,target=/root/.cache/go-build \
47+
CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} \
4448
go build -ldflags '-extldflags "-static"' \
4549
-o manager ${package}
4650

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,19 +326,19 @@ docker-build: ## Build the docker images for controller managers
326326

327327
.PHONY: docker-build-core
328328
docker-build-core: ## Build the docker image for core controller manager
329-
docker build --pull --build-arg goproxy=$(GOPROXY) --build-arg ARCH=$(ARCH) . -t $(CONTROLLER_IMG)-$(ARCH):$(TAG)
329+
DOCKER_BUILDKIT=1 docker build --pull --build-arg goproxy=$(GOPROXY) --build-arg ARCH=$(ARCH) . -t $(CONTROLLER_IMG)-$(ARCH):$(TAG)
330330
$(MAKE) set-manifest-image MANIFEST_IMG=$(CONTROLLER_IMG)-$(ARCH) MANIFEST_TAG=$(TAG) TARGET_RESOURCE="./config/manager/manager_image_patch.yaml"
331331
$(MAKE) set-manifest-pull-policy TARGET_RESOURCE="./config/manager/manager_pull_policy.yaml"
332332

333333
.PHONY: docker-build-kubeadm-bootstrap
334334
docker-build-kubeadm-bootstrap: ## Build the docker image for kubeadm bootstrap controller manager
335-
docker build --pull --build-arg goproxy=$(GOPROXY) --build-arg ARCH=$(ARCH) --build-arg package=./bootstrap/kubeadm . -t $(KUBEADM_BOOTSTRAP_CONTROLLER_IMG)-$(ARCH):$(TAG)
335+
DOCKER_BUILDKIT=1 docker build --pull --build-arg goproxy=$(GOPROXY) --build-arg ARCH=$(ARCH) --build-arg package=./bootstrap/kubeadm . -t $(KUBEADM_BOOTSTRAP_CONTROLLER_IMG)-$(ARCH):$(TAG)
336336
$(MAKE) set-manifest-image MANIFEST_IMG=$(KUBEADM_BOOTSTRAP_CONTROLLER_IMG)-$(ARCH) MANIFEST_TAG=$(TAG) TARGET_RESOURCE="./bootstrap/kubeadm/config/manager/manager_image_patch.yaml"
337337
$(MAKE) set-manifest-pull-policy TARGET_RESOURCE="./bootstrap/kubeadm/config/manager/manager_pull_policy.yaml"
338338

339339
.PHONY: docker-build-kubeadm-control-plane
340340
docker-build-kubeadm-control-plane: ## Build the docker image for kubeadm control plane controller manager
341-
docker build --pull --build-arg goproxy=$(GOPROXY) --build-arg ARCH=$(ARCH) --build-arg package=./controlplane/kubeadm . -t $(KUBEADM_CONTROL_PLANE_CONTROLLER_IMG)-$(ARCH):$(TAG)
341+
DOCKER_BUILDKIT=1 docker build --pull --build-arg goproxy=$(GOPROXY) --build-arg ARCH=$(ARCH) --build-arg package=./controlplane/kubeadm . -t $(KUBEADM_CONTROL_PLANE_CONTROLLER_IMG)-$(ARCH):$(TAG)
342342
$(MAKE) set-manifest-image MANIFEST_IMG=$(KUBEADM_CONTROL_PLANE_CONTROLLER_IMG)-$(ARCH) MANIFEST_TAG=$(TAG) TARGET_RESOURCE="./controlplane/kubeadm/config/manager/manager_image_patch.yaml"
343343
$(MAKE) set-manifest-pull-policy TARGET_RESOURCE="./controlplane/kubeadm/config/manager/manager_pull_policy.yaml"
344344

@@ -494,7 +494,7 @@ EXAMPLE_PROVIDER_IMG ?= $(REGISTRY)/example-provider-controller
494494

495495
.PHONY: docker-build-example-provider
496496
docker-build-example-provider: ## Build the docker image for example provider
497-
docker build --pull --build-arg goproxy=$(GOPROXY) --build-arg ARCH=$(ARCH) . -f ./cmd/example-provider/Dockerfile -t $(EXAMPLE_PROVIDER_IMG)-$(ARCH):$(TAG)
497+
DOCKER_BUILDKIT=1 docker build --pull --build-arg goproxy=$(GOPROXY) --build-arg ARCH=$(ARCH) . -f ./cmd/example-provider/Dockerfile -t $(EXAMPLE_PROVIDER_IMG)-$(ARCH):$(TAG)
498498
sed -i'' -e 's@image: .*@image: '"${EXAMPLE_PROVIDER_IMG}-$(ARCH):$(TAG)"'@' ./config/ci/manager/manager_image_patch.yaml
499499

500500
## --------------------------------------

cmd/example-provider/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# syntax = docker/dockerfile:1-experimental
2+
13
# Copyright 2019 The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");

docs/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# syntax = docker/dockerfile:1-experimental
2+
13
# Copyright 2019 The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");

hack/boilerplate/boilerplate.Dockerfile.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# syntax = docker/dockerfile:1-experimental
2+
13
# Copyright YEAR The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");

test/infrastructure/docker/Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# syntax = docker/dockerfile:1-experimental
2+
13
# Copyright 2019 The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -41,7 +43,8 @@ COPY . .
4143
WORKDIR /workspace/test/infrastructure/docker
4244

4345
# Build the CAPD manager
44-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o /workspace/manager main.go
46+
RUN --mount=type=cache,target=/root/.cache/go-build \
47+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o /workspace/manager main.go
4548

4649
# Use alpine:latest as minimal base image to package the manager binary and its dependencies
4750
FROM alpine:latest

test/infrastructure/docker/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ modules: ## Runs go mod to ensure modules are up to date.
136136

137137
.PHONY: docker-build
138138
docker-build: ## Build the docker image for controller-manager
139-
docker build --pull --build-arg goproxy=$(GOPROXY) --build-arg ARCH=$(ARCH) ../../.. -t $(CONTROLLER_IMG)-$(ARCH):$(TAG) --file Dockerfile
139+
DOCKER_BUILDKIT=1 docker build --pull --build-arg goproxy=$(GOPROXY) --build-arg ARCH=$(ARCH) ../../.. -t $(CONTROLLER_IMG)-$(ARCH):$(TAG) --file Dockerfile
140140
MANIFEST_IMG=$(CONTROLLER_IMG)-$(ARCH) MANIFEST_TAG=$(TAG) $(MAKE) set-manifest-image
141141
$(MAKE) set-manifest-pull-policy
142142

0 commit comments

Comments
 (0)