Skip to content

Commit 70691a9

Browse files
authored
Faster K3s Binary Build Option (#7805)
* Add local build option Signed-off-by: Derek Nola <[email protected]>
1 parent 2215870 commit 70691a9

File tree

5 files changed

+86
-7
lines changed

5 files changed

+86
-7
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
./.cache
66
./.dapper
77
./.trash-cache
8+
./.git/objects/pack

Dockerfile.local

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
ARG GOLANG=golang:1.20.4-alpine3.18
2+
FROM ${GOLANG} as infra
3+
4+
ARG http_proxy=$http_proxy
5+
ARG https_proxy=$https_proxy
6+
ARG no_proxy=$no_proxy
7+
ENV http_proxy=$http_proxy
8+
ENV https_proxy=$https_proxy
9+
ENV no_proxy=$no_proxy
10+
11+
RUN apk -U --no-cache add bash git gcc musl-dev docker vim less file curl wget ca-certificates jq linux-headers \
12+
zlib-dev tar zip squashfs-tools npm coreutils python3 py3-pip openssl-dev libffi-dev libseccomp libseccomp-dev \
13+
libseccomp-static make libuv-static sqlite-dev sqlite-static libselinux libselinux-dev zlib-dev zlib-static \
14+
zstd pigz alpine-sdk binutils-gold btrfs-progs-dev btrfs-progs-static gawk yq \
15+
&& \
16+
if [ "$(go env GOARCH)" = "amd64" ]; then \
17+
apk -U --no-cache add mingw-w64-gcc; \
18+
fi
19+
20+
RUN python3 -m pip install awscli
21+
22+
# this works for both go 1.17 and 1.18
23+
RUN GOPROXY=direct go install golang.org/x/tools/cmd/goimports@gopls/v0.11.0
24+
RUN rm -rf /go/src /go/pkg
25+
26+
RUN if [ "$(go env GOARCH)" = "amd64" ]; then \
27+
curl -sL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.51.2; \
28+
fi
29+
30+
ARG SELINUX=true
31+
ENV SELINUX $SELINUX
32+
ENV STATIC_BUILD true
33+
ENV SRC_DIR=/go/src/github.com/k3s-io/k3s
34+
WORKDIR ${SRC_DIR}/
35+
36+
37+
FROM infra as build
38+
39+
ARG SKIP_VALIDATE
40+
41+
COPY ./scripts/ ./scripts
42+
COPY ./go.mod ./go.sum ./main.go ./
43+
COPY ./manifests ./manifests
44+
RUN mkdir -p bin dist
45+
RUN --mount=type=cache,id=gomod,target=/go/pkg/mod \
46+
./scripts/download
47+
48+
COPY ./cmd ./cmd
49+
COPY ./pkg ./pkg
50+
COPY ./tests ./tests
51+
COPY ./.git ./.git
52+
RUN --mount=type=cache,id=gomod,target=/go/pkg/mod \
53+
--mount=type=cache,id=gobuild,target=/root/.cache/go-build \
54+
./scripts/build
55+
56+
COPY ./contrib ./contrib
57+
RUN --mount=type=cache,id=gomod,target=/go/pkg/mod \
58+
--mount=type=cache,id=gobuild,target=/root/.cache/go-build \
59+
./scripts/package-cli
60+
61+
RUN ./scripts/binary_size_check.sh
62+
63+
FROM scratch as result
64+
ENV SRC_DIR=/go/src/github.com/k3s-io/k3s
65+
COPY --from=build ${SRC_DIR}/dist /dist
66+
COPY --from=build ${SRC_DIR}/bin /bin
67+
COPY --from=build ${SRC_DIR}/build/out /build/out
68+
COPY --from=build ${SRC_DIR}/build/static /build/static
69+
COPY --from=build ${SRC_DIR}/pkg/static /pkg/static
70+
COPY --from=build ${SRC_DIR}/pkg/deploy /pkg/deploy

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,10 @@ image-scan:
3636

3737
format:
3838
gofmt -s -l -w $(GO_FILES)
39-
goimports -w $(GO_FILES)
39+
goimports -w $(GO_FILES)
40+
41+
.PHONY: local
42+
local:
43+
DOCKER_BUILDKIT=1 docker build \
44+
--build-arg="REPO TAG GITHUB_TOKEN GOLANG GOCOVER DEBUG" \
45+
-t k3s-local -f Dockerfile.local --output=. .

scripts/binary_size_check.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
set -e
44

5+
GO=${GO-go}
6+
ARCH=${ARCH:-$("${GO}" env GOARCH)}
7+
58
if [ "${DEBUG}" = 1 ]; then
69
set -x
710
fi
811

9-
. ./scripts/version.sh
10-
1112
# Try to keep the K3s binary under 70 megabytes.
1213
# "64M ought to be enough for anybody"
1314
MAX_BINARY_MB=70

scripts/validate

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#!/bin/bash
22
set -e
33

4+
if [ -n "$SKIP_VALIDATE" ]; then
5+
echo Skipping validation
6+
exit
7+
fi
8+
49
cd $(dirname $0)/..
510

611
echo Running: go mod tidy
@@ -9,10 +14,6 @@ go mod tidy
914
echo Running: go generate
1015
go generate
1116

12-
if [ -n "$SKIP_VALIDATE" ]; then
13-
echo Skipping validation
14-
exit
15-
fi
1617
echo Running validation
1718

1819
. ./scripts/version.sh

0 commit comments

Comments
 (0)