From 363d01392ac8e31da66f926d8ef9d537eb8b3d27 Mon Sep 17 00:00:00 2001 From: Kelvie Wong Date: Sun, 13 Feb 2022 00:12:04 -0800 Subject: [PATCH 1/2] Create multi-arch image For linux arm64 and amd64, as per #586. I moved the builder image into the same dockerfile, and bumped the Go version on it. It didn't seem like the builder dockerfile worked with the latest code anyway (the go modules require go 1.15 and higher). This requires a recent enough docker install with buildx, as well as an arm64 builder. BASEIMAGE is changed to not specify an arch, so that the image will build on its native arch in buildx. Example image is on docker hub as: kelvie/node-problem-detector:v0.8.10-5-gb0fa610 --- Dockerfile | 22 +++++++++++++++++++--- Makefile | 15 ++++++++++----- builder/Dockerfile | 25 ------------------------- 3 files changed, 29 insertions(+), 33 deletions(-) delete mode 100644 builder/Dockerfile diff --git a/Dockerfile b/Dockerfile index 7ec0591da..b5e973340 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,6 +11,22 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +ARG BASEIMAGE + +FROM golang:1.17.7 as builder +LABEL maintainer="Andy Xie " + +ENV GOPATH /gopath/ +ENV PATH $GOPATH/bin:$PATH + +RUN apt-get update && apt-get --yes install libsystemd-dev +RUN go version +RUN go get github.com/tools/godep +RUN godep version + +COPY . /gopath/src/k8s.io/node-problem-detector/ +WORKDIR /gopath/src/k8s.io/node-problem-detector +RUN make bin/node-problem-detector bin/health-checker bin/log-counter ARG BASEIMAGE FROM ${BASEIMAGE} @@ -22,10 +38,10 @@ RUN clean-install util-linux libsystemd0 bash # Avoid symlink of /etc/localtime. RUN test -h /etc/localtime && rm -f /etc/localtime && cp /usr/share/zoneinfo/UTC /etc/localtime || true -COPY ./bin/node-problem-detector /node-problem-detector +COPY --from=builder /gopath/src/k8s.io/node-problem-detector/bin/node-problem-detector /node-problem-detector ARG LOGCOUNTER -COPY ./bin/health-checker ${LOGCOUNTER} /home/kubernetes/bin/ +COPY --from=builder /gopath/src/k8s.io/node-problem-detector/bin/health-checker /gopath/src/k8s.io/node-problem-detector/${LOGCOUNTER} /home/kubernetes/bin/ -COPY config /config +COPY --from=builder /gopath/src/k8s.io/node-problem-detector/config/ /config ENTRYPOINT ["/node-problem-detector", "--config.system-log-monitor=/config/kernel-monitor.json"] diff --git a/Makefile b/Makefile index 84caddc7a..5c5df779d 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,7 @@ all: build # PLATFORMS is the set of OS_ARCH that NPD can build against. LINUX_PLATFORMS=linux_amd64 linux_arm64 +DOCKER_PLATFORMS=linux/amd64,linux/arm64 PLATFORMS=$(LINUX_PLATFORMS) windows_amd64 # VERSION is the version of the binary. @@ -74,7 +75,7 @@ endif # The debian-base:v1.0.0 image built from kubernetes repository is based on # Debian Stretch. It includes systemd 232 with support for both +XZ and +LZ4 # compression. +LZ4 is needed on some os distros such as COS. -BASEIMAGE:=k8s.gcr.io/debian-base-amd64:v2.0.0 +BASEIMAGE:=k8s.gcr.io/debian-base:v2.0.0 # Disable cgo by default to make the binary statically linked. CGO_ENABLED:=0 @@ -239,8 +240,8 @@ $(NPD_NAME_VERSION)-%.tar.gz: $(ALL_BINARIES) test/e2e-install.sh build-binaries: $(ALL_BINARIES) -build-container: build-binaries Dockerfile - docker build -t $(IMAGE) --build-arg BASEIMAGE=$(BASEIMAGE) --build-arg LOGCOUNTER=$(LOGCOUNTER) . +build-container: clean Dockerfile + docker buildx build --platform $(DOCKER_PLATFORMS) -t $(IMAGE) --build-arg BASEIMAGE=$(BASEIMAGE) --build-arg LOGCOUNTER=$(LOGCOUNTER) . $(TARBALL): ./bin/node-problem-detector ./bin/log-counter ./bin/health-checker ./test/bin/problem-maker tar -zcvf $(TARBALL) bin/ config/ test/e2e-install.sh test/bin/problem-maker @@ -252,7 +253,7 @@ build-tar: $(TARBALL) $(ALL_TARBALLS) build: build-container build-tar docker-builder: - docker build -t npd-builder ./builder + docker build -t npd-builder . --target=builder build-in-docker: clean docker-builder docker run \ @@ -260,8 +261,12 @@ build-in-docker: clean docker-builder -c 'cd /gopath/src/k8s.io/node-problem-detector/ && make build-binaries' push-container: build-container + # So we can push to docker hub by setting REGISTRY +ifneq (,$(findstring gcr.io,$(REGISTRY))) gcloud auth configure-docker - docker push $(IMAGE) +endif + # Build should be cached from build-container + docker buildx build --push --platform $(DOCKER_PLATFORMS) -t $(IMAGE) --build-arg BASEIMAGE=$(BASEIMAGE) --build-arg LOGCOUNTER=$(LOGCOUNTER) . push-tar: build-tar gsutil cp $(TARBALL) $(UPLOAD_PATH)/node-problem-detector/ diff --git a/builder/Dockerfile b/builder/Dockerfile deleted file mode 100644 index 7f9279ff3..000000000 --- a/builder/Dockerfile +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2018 The Kubernetes Authors. All rights reserved -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -FROM golang:1.11.0 -LABEL maintainer="Andy Xie " - -ENV GOPATH /gopath/ -ENV PATH $GOPATH/bin:$PATH - -RUN apt-get update && apt-get --yes install libsystemd-dev -RUN go version -RUN go get github.com/tools/godep -RUN godep version -CMD ["/bin/bash"] From 04e8d009d49e87467c8ea5e0d6fb1990fc46c921 Mon Sep 17 00:00:00 2001 From: Sonu Kumar Singh Date: Tue, 5 Jul 2022 09:37:52 +0530 Subject: [PATCH 2/2] Use buildx for docker builder --- Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile b/Makefile index 5c5df779d..db99831ab 100644 --- a/Makefile +++ b/Makefile @@ -241,6 +241,7 @@ $(NPD_NAME_VERSION)-%.tar.gz: $(ALL_BINARIES) test/e2e-install.sh build-binaries: $(ALL_BINARIES) build-container: clean Dockerfile + docker buildx create --use docker buildx build --platform $(DOCKER_PLATFORMS) -t $(IMAGE) --build-arg BASEIMAGE=$(BASEIMAGE) --build-arg LOGCOUNTER=$(LOGCOUNTER) . $(TARBALL): ./bin/node-problem-detector ./bin/log-counter ./bin/health-checker ./test/bin/problem-maker @@ -266,6 +267,7 @@ ifneq (,$(findstring gcr.io,$(REGISTRY))) gcloud auth configure-docker endif # Build should be cached from build-container + docker buildx create --use docker buildx build --push --platform $(DOCKER_PLATFORMS) -t $(IMAGE) --build-arg BASEIMAGE=$(BASEIMAGE) --build-arg LOGCOUNTER=$(LOGCOUNTER) . push-tar: build-tar