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 <dgrisonn@redhat.com>
This commit is contained in:
Damien Grisonnet 2021-06-02 17:03:28 +02:00
parent 815fa20931
commit 03e8eb8ddb
5 changed files with 40 additions and 29 deletions

2
.gitignore vendored
View file

@ -1,4 +1,4 @@
*.swp
*~
/_output
/vendor
/adapter

22
Dockerfile Normal file
View file

@ -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"]

View file

@ -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

View file

@ -1,6 +0,0 @@
ARG BASEIMAGE
FROM ${BASEIMAGE}
ARG ARCH
COPY ${ARCH}/adapter /
USER 1001:1001
ENTRYPOINT ["/adapter"]

View file

@ -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.