From 03e8eb8ddbbb4bf3223e70ff1630548b33f8a9ee Mon Sep 17 00:00:00 2001 From: Damien Grisonnet Date: Wed, 2 Jun 2021 17:03:28 +0200 Subject: [PATCH] Makefile: consolidate docker-build As part of this commit, I upgraded the golang image used for building to 1.16 and consolidated how the docker-build rule was working. Previously, it was failing in master's CI because the go modules were not downloaded in the build image. To improve that, I replaced the combination of docker run and docker build by a multi-stage Dockerfile responsible for building the adapter and running it. In addition to that, I removed the `_output` directory completely as it wasn't really meaningful to have it anymore. I also removed the `build-local-image` rule as it was a duplicate of the `docker-build` rule with the only different of using a scratch base image. Also, since all the base images that we are using by default are based on busybox, I change the UID used in the image to 65534 which correspond to the nobody user in busybox. Signed-off-by: Damien Grisonnet --- .gitignore | 2 +- Dockerfile | 22 ++++++++++++++++++++++ Makefile | 36 ++++++++++++++++-------------------- deploy/Dockerfile | 6 ------ docs/walkthrough.md | 3 +-- 5 files changed, 40 insertions(+), 29 deletions(-) create mode 100644 Dockerfile delete mode 100644 deploy/Dockerfile diff --git a/.gitignore b/.gitignore index 3bb214d6..1b9f0ec0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ *.swp *~ -/_output /vendor +/adapter diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..92eee6d0 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,22 @@ +ARG BASEIMAGE +ARG GOIMAGE + +FROM ${GOIMAGE} as build + +WORKDIR /go/src/sigs.k8s.io/prometheus-adapter +COPY go.mod . +COPY go.sum . +RUN go mod download + +COPY pkg pkg +COPY cmd cmd +COPY Makefile Makefile + +ARG ARCH +RUN make prometheus-adapter + +FROM ${BASEIMAGE} + +COPY --from=build /go/src/sigs.k8s.io/prometheus-adapter/adapter / +USER 65534 +ENTRYPOINT ["/adapter"] diff --git a/Makefile b/Makefile index adc154f0..00362cd9 100644 --- a/Makefile +++ b/Makefile @@ -3,12 +3,9 @@ IMAGE?=k8s-prometheus-adapter ARCH?=$(shell go env GOARCH) ALL_ARCH=amd64 arm arm64 ppc64le s390x ML_PLATFORMS=linux/amd64,linux/arm,linux/arm64,linux/ppc64le,linux/s390x -OUT_DIR?=$(PWD)/_output VERSION?=latest -GOIMAGE=golang:1.15 -GO111MODULE=on -export GO111MODULE +GOIMAGE=golang:1.16 ifeq ($(ARCH),amd64) BASEIMAGE?=busybox @@ -26,31 +23,26 @@ ifeq ($(ARCH),s390x) BASEIMAGE?=s390x/busybox endif -.PHONY: all docker-build push-% push test verify-gofmt gofmt verify build-local-image +.PHONY: all +all: prometheus-adapter -all: $(OUT_DIR)/$(ARCH)/adapter +# Build +# ----- src_deps=$(shell find pkg cmd -type f -name "*.go") -$(OUT_DIR)/%/adapter: $(src_deps) - CGO_ENABLED=0 GOARCH=$* go build -tags netgo -o $(OUT_DIR)/$*/adapter sigs.k8s.io/prometheus-adapter/cmd/adapter +prometheus-adapter: $(src_deps) + CGO_ENABLED=0 GOARCH=$(ARCH) go build sigs.k8s.io/prometheus-adapter/cmd/adapter -docker-build: $(OUT_DIR)/Dockerfile - docker run -it -v $(OUT_DIR):/build -v $(PWD):/go/src/sigs.k8s.io/prometheus-adapter -e GOARCH=$(ARCH) $(GOIMAGE) /bin/bash -c "\ - CGO_ENABLED=0 go build -tags netgo -o /build/$(ARCH)/adapter sigs.k8s.io/prometheus-adapter/cmd/adapter" - - docker build -t $(REGISTRY)/$(IMAGE)-$(ARCH):$(VERSION) --build-arg ARCH=$(ARCH) --build-arg BASEIMAGE=$(BASEIMAGE) $(OUT_DIR) - -$(OUT_DIR)/Dockerfile: deploy/Dockerfile - mkdir -p $(OUT_DIR) - cp deploy/Dockerfile $(OUT_DIR)/Dockerfile - -build-local-image: $(OUT_DIR)/Dockerfile $(OUT_DIR)/$(ARCH)/adapter - docker build -t $(REGISTRY)/$(IMAGE)-$(ARCH):$(VERSION) --build-arg ARCH=$(ARCH) --build-arg BASEIMAGE=scratch $(OUT_DIR) +.PHONY: docker-build +docker-build: + docker build -t $(REGISTRY)/$(IMAGE)-$(ARCH):$(VERSION) --build-arg ARCH=$(ARCH) --build-arg BASEIMAGE=$(BASEIMAGE) --build-arg GOIMAGE=$(GOIMAGE) . +.PHONY: push-% push-%: $(MAKE) ARCH=$* docker-build docker push $(REGISTRY)/$(IMAGE)-$*:$(VERSION) +.PHONY: push push: ./manifest-tool $(addprefix push-,$(ALL_ARCH)) ./manifest-tool push from-args --platforms $(ML_PLATFORMS) --template $(REGISTRY)/$(IMAGE)-ARCH:$(VERSION) --target $(REGISTRY)/$(IMAGE):$(VERSION) @@ -58,15 +50,19 @@ push: ./manifest-tool $(addprefix push-,$(ALL_ARCH)) curl -sSL https://github.com/estesp/manifest-tool/releases/download/v0.5.0/manifest-tool-linux-amd64 > manifest-tool chmod +x manifest-tool +.PHONY: test test: CGO_ENABLED=0 go test ./cmd/... ./pkg/... +.PHONY: verify-gofmt verify-gofmt: ./hack/gofmt-all.sh -v +.PHONY: gofmt gofmt: ./hack/gofmt-all.sh +.PHONY: verify verify: verify-gofmt verify-deps verify-generated test .PHONY: update diff --git a/deploy/Dockerfile b/deploy/Dockerfile deleted file mode 100644 index 07890708..00000000 --- a/deploy/Dockerfile +++ /dev/null @@ -1,6 +0,0 @@ -ARG BASEIMAGE -FROM ${BASEIMAGE} -ARG ARCH -COPY ${ARCH}/adapter / -USER 1001:1001 -ENTRYPOINT ["/adapter"] diff --git a/docs/walkthrough.md b/docs/walkthrough.md index 6af6f9f0..9b0a9ec6 100644 --- a/docs/walkthrough.md +++ b/docs/walkthrough.md @@ -46,8 +46,7 @@ an x86_64 machine, use the `directxman12/k8s-prometheus-adapter-amd64` image. If you're feeling adventurous, you can build the latest version of the -custom metrics adapter by running `make docker-build` or `make -build-local-image`. +custom metrics adapter by running `make docker-build`. Special thanks to [@luxas](https://github.com/luxas) for providing the demo application for this walkthrough.