Skip to content

Commit d9632a3

Browse files
committed
Optimize cluster-autoscaler release build process
The release process for cluster-autoscaler was taking approximately 15 minutes to complete (~5 mins/arch on my machine), largely due to redundant Go dependency downloads. This commit introduces caching optimizations: 1. Dockerfile: Reordered instructions to copy go.mod/go.sum first and added 'RUN go mod download' to cache dependencies. 2. Build Cache: Added '--mount=type=cache' to utilize Go build cache. 3. Context: Added '.dockerignore' to reduce build context size. 4. Makefile: Updated to pass 'LDFLAGS_FLAG' and 'TAGS_FLAG' as build args.
1 parent 354f4c4 commit d9632a3

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

cluster-autoscaler/.dockerignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Ignore git and GitHub folders
2+
.git
3+
.github
4+
5+
# Ignore Go binaries and build artifacts
6+
cluster-autoscaler-*
7+
8+
# Ignore test files
9+
*_test.go
10+
e2e/
11+
12+
# Ignore documentation and other non-build files
13+
*.md
14+
LICENSE
15+
OWNERS
16+
OWNERS_ALIASES
17+
CONTRIBUTING.md
18+
SECURITY_CONTACTS
19+
code-of-conduct.md
20+
cloudbuild.yaml
21+
Makefile
22+
push_image.sh
23+
update_toc.py
24+
*.txt

cluster-autoscaler/Dockerfile

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,24 @@ FROM --platform=$BUILDPLATFORM golang:1.25 AS builder
1515

1616
WORKDIR /workspace
1717

18+
# Copy go.mod and go.sum files first to cache dependencies
19+
COPY go.mod go.sum ./
20+
COPY apis/go.mod apis/go.sum ./apis/
21+
22+
# Download dependencies
23+
RUN go mod download
24+
25+
# Copy the rest of the source code
1826
COPY . .
1927

2028
ARG GOARCH
2129
ARG LDFLAGS_FLAG
2230
ARG TAGS_FLAG
2331

24-
RUN CGO_ENABLED=0 GOOS=linux go build -o cluster-autoscaler-$GOARCH $LDFLAGS_FLAG $TAGS_FLAG
32+
RUN --mount=type=cache,target=/root/.cache/go-build \
33+
--mount=type=cache,target=/go/pkg/mod \
34+
CGO_ENABLED=0 GOOS=linux go build -o cluster-autoscaler-$GOARCH $LDFLAGS_FLAG $TAGS_FLAG
35+
2536
FROM gcr.io/distroless/static:nonroot
2637
ARG GOARCH
2738
COPY --from=builder /workspace/cluster-autoscaler-$GOARCH /cluster-autoscaler

cluster-autoscaler/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ make-image: make-image-arch-$(GOARCH)
7373
make-image-arch-%:
7474
GOOS=$(GOOS) docker buildx build --pull --platform linux/$* \
7575
--build-arg "GOARCH=$*" \
76+
--build-arg "LDFLAGS_FLAG=${LDFLAGS_FLAG}" \
77+
--build-arg "TAGS_FLAG=${TAGS_FLAG}" \
7678
-t ${IMAGE}-$*:${TAG} \
7779
-f Dockerfile .
7880
@echo "Image ${TAG}${FOR_PROVIDER}-$* completed"

0 commit comments

Comments
 (0)