Skip to content

Commit e9922b0

Browse files
committed
Fix docker build for multi-arch
1 parent d8e9d55 commit e9922b0

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

Dockerfile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
# limitations under the License.
1414
ARG BASEIMAGE
1515

16-
FROM golang:1.20.3@sha256:bcc311ec9655c350df3899611fdf134806f97a3e3b2c06c2b5c0696428503814 as builder
16+
FROM golang:1.20.8-bookworm@sha256:81e1690c528627eeb6314f2fbd15964608e6769746459ce9727c77ec988cb28d as builder
1717
LABEL maintainer="Andy Xie <[email protected]>"
1818

19+
ARG TARGETARCH
20+
1921
ENV GOPATH /gopath/
2022
ENV PATH $GOPATH/bin:$PATH
2123

@@ -24,10 +26,10 @@ RUN go version
2426

2527
COPY . /gopath/src/k8s.io/node-problem-detector/
2628
WORKDIR /gopath/src/k8s.io/node-problem-detector
27-
RUN make bin/node-problem-detector bin/health-checker bin/log-counter
29+
RUN GOARCH=${TARGETARCH} make bin/node-problem-detector bin/health-checker bin/log-counter
2830

2931
ARG BASEIMAGE
30-
FROM ${BASEIMAGE}
32+
FROM --platform=${TARGETPLATFORM} ${BASEIMAGE}
3133

3234
LABEL maintainer="Random Liu <[email protected]>"
3335

Makefile

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ else ifeq ($(shell go env GOHOSTOS), windows)
7171
ENABLE_JOURNALD=0
7272
endif
7373

74-
# TODO(random-liu): Support different architectures.
75-
# The debian-base:v2.0.0 image built from kubernetes repository is based on
76-
# Debian Stretch. It includes systemd 241 with support for both +XZ and +LZ4
77-
# compression. +LZ4 is needed on some os distros such as COS.
74+
# Set default base image to Debian 12 (Bookworm)
7875
BASEIMAGE:=registry.k8s.io/build-image/debian-base:bookworm-v1.0.0
7976

8077
# Disable cgo by default to make the binary statically linked.
8178
CGO_ENABLED:=0
8279

80+
# Set default Go architecture to AMD64.
81+
GOARCH ?= amd64
82+
8383
# Construct the "-tags" parameter used by "go build".
8484
BUILD_TAGS?=
8585

@@ -185,7 +185,7 @@ output/linux_arm64/test/bin/%: $(PKG_SOURCES)
185185
# In the future these targets should be deprecated.
186186
./bin/log-counter: $(PKG_SOURCES)
187187
ifeq ($(ENABLE_JOURNALD), 1)
188-
CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GO111MODULE=on go build \
188+
CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GOARCH=$(GOARCH) GO111MODULE=on go build \
189189
-mod vendor \
190190
-o bin/log-counter \
191191
-ldflags '-X $(PKG)/pkg/version.version=$(VERSION)' \
@@ -196,7 +196,7 @@ else
196196
endif
197197

198198
./bin/node-problem-detector: $(PKG_SOURCES)
199-
CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GO111MODULE=on go build \
199+
CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GOARCH=$(GOARCH) GO111MODULE=on go build \
200200
-mod vendor \
201201
-o bin/node-problem-detector \
202202
-ldflags '-X $(PKG)/pkg/version.version=$(VERSION)' \
@@ -205,13 +205,13 @@ endif
205205

206206
./test/bin/problem-maker: $(PKG_SOURCES)
207207
cd test && \
208-
CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GO111MODULE=on go build \
208+
CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GOARCH=$(GOARCH) GO111MODULE=on go build \
209209
-o bin/problem-maker \
210210
-tags "$(LINUX_BUILD_TAGS)" \
211211
./e2e/problemmaker/problem_maker.go
212212

213213
./bin/health-checker: $(PKG_SOURCES)
214-
CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GO111MODULE=on go build \
214+
CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GOARCH=$(GOARCH) GO111MODULE=on go build \
215215
-mod vendor \
216216
-o bin/health-checker \
217217
-ldflags '-X $(PKG)/pkg/version.version=$(VERSION)' \
@@ -242,8 +242,7 @@ $(NPD_NAME_VERSION)-%.tar.gz: $(ALL_BINARIES) test/e2e-install.sh
242242
build-binaries: $(ALL_BINARIES)
243243

244244
build-container: clean Dockerfile
245-
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
246-
docker buildx create --use
245+
docker buildx create --platform $(DOCKER_PLATFORMS) --use
247246
docker buildx build --platform $(DOCKER_PLATFORMS) -t $(IMAGE) --build-arg BASEIMAGE=$(BASEIMAGE) --build-arg LOGCOUNTER=$(LOGCOUNTER) .
248247

249248
$(TARBALL): ./bin/node-problem-detector ./bin/log-counter ./bin/health-checker ./test/bin/problem-maker
@@ -269,7 +268,6 @@ ifneq (,$(findstring gcr.io,$(REGISTRY)))
269268
gcloud auth configure-docker
270269
endif
271270
# Build should be cached from build-container
272-
docker buildx create --use
273271
docker buildx build --push --platform $(DOCKER_PLATFORMS) -t $(IMAGE) --build-arg BASEIMAGE=$(BASEIMAGE) --build-arg LOGCOUNTER=$(LOGCOUNTER) .
274272

275273
push-tar: build-tar

0 commit comments

Comments
 (0)