Skip to content

Commit 4fd644f

Browse files
K8SPS-301 improve multi arch builds (#666)
* K8SPS-301 improve multi arch builds * Golang builds faster in native architecture. * Update e2e-tests/build Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent f236e49 commit 4fd644f

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

Jenkinsfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ pipeline {
340340
docker login -u '${USER}' -p '${PASS}'
341341
export RELEASE=0
342342
export IMAGE=\$DOCKER_TAG
343+
docker buildx create --use
343344
./e2e-tests/build
344345
docker logout
345346
"

build/Dockerfile

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.22 AS go_builder
1+
FROM --platform=${BUILDPLATFORM} golang:1.22 AS go_builder
22
WORKDIR /go/src/github.com/percona/percona-server-mysql-operator
33

44
COPY go.mod go.sum ./
@@ -9,38 +9,38 @@ ARG BUILD_TIME
99
ARG BUILD_TIME
1010
ARG GO_LDFLAGS
1111
ARG GOOS=linux
12-
ARG GOARCH=amd64
12+
ARG TARGETARCH
1313
ARG CGO_ENABLED=0
1414

1515
COPY . .
1616

1717
RUN mkdir -p build/_output/bin \
18-
&& GOOS=$GOOS GOARCH=$GOARCH CGO_ENABLED=$CGO_ENABLED GO_LDFLAGS=$GO_LDFLAGS \
18+
&& GOOS=$GOOS GOARCH=$TARGETARCH CGO_ENABLED=$CGO_ENABLED GO_LDFLAGS=$GO_LDFLAGS \
1919
go build -ldflags "-w -s -X main.GitCommit=$GIT_COMMIT -X main.GitBranch=$GIT_BRANCH -X main.BuildTime=$BUILD_TIME" \
2020
-o build/_output/bin/percona-server-mysql-operator \
2121
cmd/manager/main.go \
2222
&& cp -r build/_output/bin/percona-server-mysql-operator /usr/local/bin/percona-server-mysql-operator
23-
RUN GOOS=$GOOS GOARCH=$GOARCH CGO_ENABLED=$CGO_ENABLED GO_LDFLAGS=$GO_LDFLAGS \
23+
RUN GOOS=$GOOS GOARCH=$TARGETARCH CGO_ENABLED=$CGO_ENABLED GO_LDFLAGS=$GO_LDFLAGS \
2424
go build -ldflags "-w -s -X main.GitCommit=$GIT_COMMIT -X main.GitBranch=$GIT_BRANCH -X main.BuildTime=$BUILD_TIME" \
2525
-o build/_output/bin/bootstrap \
2626
./cmd/bootstrap/ \
2727
&& cp -r build/_output/bin/bootstrap /usr/local/bin/bootstrap
28-
RUN GOOS=$GOOS GOARCH=$GOARCH CGO_ENABLED=$CGO_ENABLED GO_LDFLAGS=$GO_LDFLAGS \
28+
RUN GOOS=$GOOS GOARCH=$TARGETARCH CGO_ENABLED=$CGO_ENABLED GO_LDFLAGS=$GO_LDFLAGS \
2929
go build -ldflags "-w -s -X main.GitCommit=$GIT_COMMIT -X main.GitBranch=$GIT_BRANCH -X main.BuildTime=$BUILD_TIME" \
3030
-o build/_output/bin/healthcheck \
3131
cmd/healthcheck/main.go \
3232
&& cp -r build/_output/bin/healthcheck /usr/local/bin/healthcheck
33-
RUN GOOS=$GOOS GOARCH=$GOARCH CGO_ENABLED=$CGO_ENABLED GO_LDFLAGS=$GO_LDFLAGS \
33+
RUN GOOS=$GOOS GOARCH=$TARGETARCH CGO_ENABLED=$CGO_ENABLED GO_LDFLAGS=$GO_LDFLAGS \
3434
go build -ldflags "-w -s -X main.GitCommit=$GIT_COMMIT -X main.GitBranch=$GIT_BRANCH -X main.BuildTime=$BUILD_TIME" \
3535
-o build/_output/bin/sidecar \
3636
./cmd/sidecar \
3737
&& cp -r build/_output/bin/sidecar /usr/local/bin/sidecar
38-
RUN GOOS=$GOOS GOARCH=$GOARCH CGO_ENABLED=$CGO_ENABLED GO_LDFLAGS=$GO_LDFLAGS \
38+
RUN GOOS=$GOOS GOARCH=$TARGETARCH CGO_ENABLED=$CGO_ENABLED GO_LDFLAGS=$GO_LDFLAGS \
3939
go build -ldflags "-w -s -X main.GitCommit=$GIT_COMMIT -X main.GitBranch=$GIT_BRANCH -X main.BuildTime=$BUILD_TIME" \
4040
-o build/_output/bin/peer-list \
4141
cmd/peer-list/main.go \
4242
&& cp -r build/_output/bin/peer-list /usr/local/bin/peer-list
43-
RUN GOOS=$GOOS GOARCH=$GOARCH CGO_ENABLED=$CGO_ENABLED GO_LDFLAGS=$GO_LDFLAGS \
43+
RUN GOOS=$GOOS GOARCH=$TARGETARCH CGO_ENABLED=$CGO_ENABLED GO_LDFLAGS=$GO_LDFLAGS \
4444
go build -ldflags "-w -s -X main.GitCommit=$GIT_COMMIT -X main.GitBranch=$GIT_BRANCH -X main.BuildTime=$BUILD_TIME" \
4545
-o build/_output/bin/orc-handler \
4646
cmd/orc-handler/main.go \

e2e-tests/build

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@ GIT_COMMIT=$(git rev-parse HEAD)
1313
IMAGE=${IMAGE/#percona\//perconalab/}
1414
DOCKER_DEFAULT_PLATFORM=${DOCKER_DEFAULT_PLATFORM:-"linux/amd64"}
1515

16-
build_options='--squash'
17-
if [[ ${DOCKER_SQUASH} == 0 ]]; then
18-
build_options=''
16+
if [[ ${DOCKER_SQUASH:-1} == 1 ]]; then
17+
squash="--squash"
1918
fi
2019
if [[ ${DOCKER_NOCACHE:-0} == 1 ]]; then
21-
build_options="$build_options --no-cache"
20+
no_cache="--no-cache"
21+
fi
22+
23+
if [[ ${DOCKER_PUSH:-1} == 1 ]]; then
24+
imgresult="--push=true"
25+
else
26+
imgresult="--load"
2227
fi
2328

2429
build_operator() {
@@ -29,26 +34,25 @@ build_operator() {
2934
export IMAGE
3035
export DOCKER_DEFAULT_PLATFORM
3136
export GO_LDFLAGS="-w -s -trimpath $GO_LDFLAGS"
32-
pushd "${ROOT_REPO}" || exit
3337

34-
build_command='build'
3538
if echo "$DOCKER_DEFAULT_PLATFORM" | grep -q ','; then
36-
build_command='buildx build'
37-
build_options="$build_options --push=true"
3839
if [ "${DOCKER_PUSH:-1}" = 0 ]; then
3940
echo "'docker $build_command' doesn't support DOCKER_PUSH=0 option in case of multi-arch builds, please use DOCKER_PUSH=1"
4041
exit 1
4142
fi
4243
fi
4344

44-
docker $build_command \
45+
pushd "${ROOT_REPO}" || exit
46+
docker buildx build \
4547
--platform $DOCKER_DEFAULT_PLATFORM \
4648
--build-arg GIT_COMMIT=$GIT_COMMIT \
4749
--build-arg GIT_BRANCH=$GIT_BRANCH \
4850
--build-arg BUILD_TIME=$BUILD_TIME \
4951
--build-arg GO_LDFLAGS="$GO_LDFLAGS" \
5052
--progress plain \
51-
$build_options \
53+
$squash \
54+
$imgresult \
55+
$no_cache \
5256
-t "${IMAGE}" -f build/Dockerfile .
5357
popd
5458

0 commit comments

Comments
 (0)