Skip to content

Commit 74583d3

Browse files
committed
Add multiarch support
Signed-off-by: Patrik Cyvoct <[email protected]>
1 parent 633ced6 commit 74583d3

File tree

7 files changed

+133
-28
lines changed

7 files changed

+133
-28
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
/bin/
1+
/bin/*
22
/Dockerfile
33
/test/bin/
44
/*.tar.gz
55
ci.env
66
pr.env
77
junit*.xml
8+
qemu*

Dockerfile.in

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
FROM @BASEIMAGE@
1616
MAINTAINER Random Liu <[email protected]>
1717

18+
CROSS_BUILD_COPY qemu-ARCH-static /usr/bin/
1819
RUN clean-install util-linux libsystemd0 bash
1920

2021
# Avoid symlink of /etc/localtime.
2122
RUN test -h /etc/localtime && rm -f /etc/localtime && cp /usr/share/zoneinfo/UTC /etc/localtime || true
2223

23-
ADD ./bin/node-problem-detector /node-problem-detector
24+
ADD ./bin/node-problem-detector-BINARCH /node-problem-detector
2425

2526
# Below command depends on ENABLE_JOURNAL=1.
26-
ADD ./bin/log-counter /home/kubernetes/bin/log-counter
27-
27+
ADD ./bin/log-counter-BINARCH /home/kubernetes/bin/log-counter
2828
ADD config /config
2929
ENTRYPOINT ["/node-problem-detector", "--system-log-monitors=/config/kernel-monitor.json"]

Makefile

Lines changed: 105 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@
1515
# Build the node-problem-detector image.
1616

1717
.PHONY: all build-container build-tar build push-container push-tar push \
18-
clean vet fmt version \
19-
Dockerfile build-binaries docker-builder build-in-docker
18+
clean vet fmt version \
19+
Dockerfile build-binaries docker-builder build-in-docker \
20+
build-all build-all-container \
21+
build-tar-all push-all push-all-container push-manifest push-tar-all qemu-register \
22+
release-staging
23+
24+
TEMP_DIR := $(shell mktemp -d)
2025

2126
all: build
2227

@@ -26,9 +31,19 @@ VERSION?=$(shell if [ -d .git ]; then echo `git describe --tags --dirty`; else e
2631
# TAG is the tag of the container image, default to binary version.
2732
TAG?=$(VERSION)
2833

34+
ARCH ?= $(shell go env GOARCH)
35+
ALL_ARCH = amd64 arm arm64 ppc64le s390x
36+
QEMUVERSION=v4.0.0-4
37+
38+
# This option is for running docker manifest command
39+
export DOCKER_CLI_EXPERIMENTAL := enabled
40+
2941
# REGISTRY is the container registry to push into.
3042
REGISTRY?=staging-k8s.gcr.io
3143

44+
# STAGING_REGISTRY is the new registry for staging
45+
STAGING_REGISTRY?=gcr.io/k8s-staging-npd
46+
3247
# UPLOAD_PATH is the cloud storage path to upload release tar.
3348
UPLOAD_PATH?=gs://kubernetes-release
3449
# Trim the trailing '/' in the path
@@ -44,20 +59,45 @@ PKG_SOURCES:=$(shell find pkg cmd -name '*.go')
4459
PARALLEL?=3
4560

4661
# TARBALL is the name of release tar. Include binary version by default.
47-
TARBALL?=node-problem-detector-$(VERSION).tar.gz
62+
TARBALL:=node-problem-detector-$(VERSION).tar.gz
63+
TARBALL-ALL:=node-problem-detector-$(VERSION)-all.tar.gz
4864

4965
# IMAGE is the image name of the node problem detector container image.
5066
IMAGE:=$(REGISTRY)/node-problem-detector:$(TAG)
67+
IMAGE_WITH_ARCH:=$(IMAGE)-$(ARCH)
5168

5269
# ENABLE_JOURNALD enables build journald support or not. Building journald
5370
# support needs libsystemd-dev or libsystemd-journal-dev.
5471
ENABLE_JOURNALD?=1
5572

56-
# TODO(random-liu): Support different architectures.
5773
# The debian-base:v1.0.0 image built from kubernetes repository is based on
5874
# Debian Stretch. It includes systemd 232 with support for both +XZ and +LZ4
5975
# compression. +LZ4 is needed on some os distros such as COS.
60-
BASEIMAGE:=k8s.gcr.io/debian-base-amd64:v1.0.0
76+
# Set the (cross) compiler, BASEIMAGE to use for different architectures
77+
ifeq ($(ARCH),amd64)
78+
CC=gcc
79+
BASEIMAGE:=k8s.gcr.io/debian-base-amd64:v1.0.0
80+
endif
81+
ifeq ($(ARCH),arm)
82+
QEMUARCH=arm
83+
CC=arm-linux-gnueabihf-gcc
84+
BASEIMAGE:=k8s.gcr.io/debian-base-arm:v1.0.0
85+
endif
86+
ifeq ($(ARCH),arm64)
87+
QEMUARCH=aarch64
88+
CC=aarch64-linux-gnu-gcc
89+
BASEIMAGE:=k8s.gcr.io/debian-base-arm64:v1.0.0
90+
endif
91+
ifeq ($(ARCH),ppc64le)
92+
QEMUARCH=ppc64le
93+
CC=powerpc64le-linux-gnu-gcc
94+
BASEIMAGE:=k8s.gcr.io/debian-base-ppc64le:v1.0.0
95+
endif
96+
ifeq ($(ARCH),s390x)
97+
QEMUARCH=s390x
98+
CC=s390x-linux-gnu-gcc
99+
BASEIMAGE:=k8s.gcr.io/debian-base-s390x:v1.0.0
100+
endif
61101

62102
# Disable cgo by default to make the binary statically linked.
63103
CGO_ENABLED:=0
@@ -88,9 +128,9 @@ version:
88128

89129
./bin/log-counter: $(PKG_SOURCES)
90130
ifeq ($(ENABLE_JOURNALD), 1)
91-
CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GO111MODULE=on go build \
131+
CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GOARCH=$(ARCH) GO111MODULE=on go build \
92132
-mod vendor \
93-
-o bin/log-counter \
133+
-o bin/log-counter-$(ARCH) \
94134
-ldflags '-X $(PKG)/pkg/version.version=$(VERSION)' \
95135
-tags "$(BUILD_TAGS)" \
96136
cmd/logcounter/log_counter.go
@@ -99,27 +139,38 @@ else
99139
endif
100140

101141
./bin/node-problem-detector: $(PKG_SOURCES)
102-
CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GO111MODULE=on go build \
142+
CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GOARCH=$(ARCH) GO111MODULE=on go build \
103143
-mod vendor \
104-
-o bin/node-problem-detector \
144+
-o bin/node-problem-detector-$(ARCH) \
105145
-ldflags '-X $(PKG)/pkg/version.version=$(VERSION)' \
106146
-tags "$(BUILD_TAGS)" \
107147
./cmd/nodeproblemdetector
108148

109149
./test/bin/problem-maker: $(PKG_SOURCES)
110-
CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GO111MODULE=on go build \
150+
CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GOARCH=$(ARCH) GO111MODULE=on go build \
111151
-mod vendor \
112152
-o test/bin/problem-maker \
113153
-tags "$(BUILD_TAGS)" \
114154
./test/e2e/problemmaker/problem_maker.go
115155

156+
qemu-register:
157+
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
158+
116159
Dockerfile: Dockerfile.in
117160
sed -e 's|@BASEIMAGE@|$(BASEIMAGE)|g' $< >$@
118161
ifneq ($(ENABLE_JOURNALD), 1)
119162
sed -i '/Below command depends on ENABLE_JOURNAL=1/,+2d' $@
120163
echo "Warning: log-counter requires journald, skipping."
121164
endif
122-
165+
sed -i "s|-ARCH|-$(QEMUARCH)|g" $@
166+
sed -i "s|-BINARCH|-$(ARCH)|g" $@
167+
ifeq ($(ARCH),amd64)
168+
# When building "normally" for amd64, remove the whole line, it has no part in the amd64 image
169+
sed -i "/CROSS_BUILD_/d" Dockerfile
170+
else
171+
curl -sSL https://github.com/multiarch/qemu-user-static/releases/download/$(QEMUVERSION)/x86_64_qemu-$(QEMUARCH)-static.tar.gz | tar -xz -C .
172+
sed -i "s/CROSS_BUILD_//g" Dockerfile
173+
endif
123174

124175
test: vet fmt
125176
GO111MODULE=on go test -mod vendor -timeout=1m -v -race -short -tags "$(BUILD_TAGS)" ./...
@@ -136,35 +187,71 @@ e2e-test: vet fmt build-tar
136187

137188
build-binaries: ./bin/node-problem-detector ./bin/log-counter
138189

139-
build-container: build-binaries Dockerfile
140-
docker build -t $(IMAGE) .
190+
build-all-container: $(addprefix build-container-,$(ALL_ARCH))
191+
192+
build-container-%: qemu-register
193+
$(MAKE) ARCH=$* build-container
194+
195+
build-container: clean build-in-docker Dockerfile
196+
docker build -t $(IMAGE_WITH_ARCH) .
141197

142198
build-tar: ./bin/node-problem-detector ./bin/log-counter ./test/bin/problem-maker
143199
tar -zcvf $(TARBALL) bin/ config/ test/e2e-install.sh test/bin/problem-maker
144200
sha1sum $(TARBALL)
145201
md5sum $(TARBALL)
146202

203+
build-tar-all: clean
204+
@for arch in $(ALL_ARCH); do $(MAKE) ARCH=$${arch} build-in-docker; done
205+
@for arch in $(ALL_ARCH); do cp bin/node-problem-detector-$(ARCH) bin/node-problem-detector; done
206+
@for arch in $(ALL_ARCH); do cp bin/log-counter-$(ARCH) bin/log-counter; done
207+
tar -zcvf $(TARBALL-ALL) bin/ config/ test/e2e-install.sh
208+
sha1sum $(TARBALL-ALL)
209+
md5sum $(TARBALL-ALL)
210+
211+
build-all: build-all-container build-tar-all
212+
147213
build: build-container build-tar
148214

149215
docker-builder:
150216
docker build -t npd-builder ./builder
151217

152-
build-in-docker: clean docker-builder
218+
build-in-docker: docker-builder
153219
docker run \
220+
-e CC=$(CC) -e GOARM=$(GOARM) -e GOARCH=$(ARCH) -e GOCACHE=/go/.cache/go-build \
221+
-u $(shell id -u ${USER}):$(shell id -g ${USER}) \
154222
-v `pwd`:/gopath/src/k8s.io/node-problem-detector/ npd-builder:latest bash \
155223
-c 'cd /gopath/src/k8s.io/node-problem-detector/ && make build-binaries'
156224

225+
push-all-container: build-all-container $(addprefix push-container-,$(ALL_ARCH))
226+
227+
push-container-%:
228+
$(MAKE) ARCH=$* push-container
229+
157230
push-container: build-container
158231
gcloud auth configure-docker
159-
docker push $(IMAGE)
232+
docker push $(IMAGE_WITH_ARCH)
233+
234+
push-manifest:
235+
gcloud auth configure-docker
236+
docker manifest create --amend $(IMAGE) $(shell echo $(ALL_ARCH) | sed -e "s~[^ ]*~$(IMAGE)\-&~g")
237+
@for arch in $(ALL_ARCH); do docker manifest annotate --arch $${arch} ${IMAGE} ${IMAGE}-$${arch}; done
238+
docker manifest push --purge ${IMAGE}
160239

161240
push-tar: build-tar
162241
gsutil cp $(TARBALL) $(UPLOAD_PATH)/node-problem-detector/
163242

243+
push-tar-all: build-tar-all
244+
gsutil cp $(TARBALL-ALL) $(UPLOAD_PATH)/node-problem-detector/
245+
246+
push-all: push-all-container push-manifest push-tar-all push-tar
247+
164248
push: push-container push-tar
165249

250+
release-staging:
251+
REGISTRY=$(STAGING_REGISTRY) $(MAKE) push-all
252+
166253
clean:
167-
rm -f bin/log-counter
168-
rm -f bin/node-problem-detector
169-
rm -f test/bin/problem-maker
254+
rm -f bin/log-counter*
255+
rm -f bin/node-problem-detector*
256+
rm -f test/bin/problem-maker*
170257
rm -f node-problem-detector-*.tar.gz

builder/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
FROM golang:1.11.0
15+
FROM gcr.io/google_containers/kube-cross:v1.12.9-1
1616
LABEL maintainer="Andy Xie <[email protected]>"
1717

1818
ENV GOPATH /gopath/

cloudbuild.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
timeout: 1200s
2+
options:
3+
substitution_option: ALLOW_LOOSE
4+
steps:
5+
- name: 'gcr.io/k8s-testimages/gcb-docker-gcloud:v20190906-745fed4'
6+
entrypoint: make
7+
env:
8+
- TAG=$_GIT_TAG
9+
- BASE_REF=$_PULL_BASE_REF
10+
- DOCKER_CLI_EXPERIMENTAL=enabled
11+
args:
12+
- release-staging
13+
substitutions:
14+
# _GIT_TAG will be filled with a git-based tag for the image, of the form vYYYYMMDD-hash, and
15+
# can be used as a substitution
16+
_GIT_TAG: '12345'
17+
_PULL_BASE_REF: 'dev'

test/build.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ function build-pr() {
131131
export REGISTRY="${NPD_STAGING_REGISTRY}/pr/${PR}"
132132
export VERSION=$(get-version)
133133
export TAG="${VERSION}"
134-
make push
134+
make push-all
135135
write-env-file ${PR_ENV_FILENAME}
136136
}
137137

@@ -141,7 +141,7 @@ function build-ci() {
141141
export REGISTRY="${NPD_STAGING_REGISTRY}/ci"
142142
export VERSION="$(get-version)-$(date +%Y%m%d.%H%M)"
143143
export TAG="${VERSION}"
144-
make push
144+
make push-all
145145

146146
# Create the env file with and without custom flags at the same time.
147147
build-npd-custom-flags

test/e2e-install.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ function install-npd() {
5555
mount -o remount,exec "${BIN_DIR}"
5656

5757
echo "Installing NPD binary."
58-
cp "${workdir}"/bin/node-problem-detector "${BIN_DIR}"
58+
cp "${workdir}"/bin/node-problem-detector-amd64 "${BIN_DIR}"/node-problem-detector
5959

6060
echo "Installing log-counter binary."
61-
cp "${workdir}"/bin/log-counter "${BIN_DIR}"
61+
cp "${workdir}"/bin/log-counter-amd64 "${BIN_DIR}"/log-counter
6262

6363
echo "Installing NPD configurations."
6464
mkdir -p "${CONFIG_DIR}"
@@ -97,4 +97,4 @@ done
9797
shift "$((OPTIND-1))"
9898

9999

100-
main "${@}"
100+
main "${@}"

0 commit comments

Comments
 (0)