From db7cb38ccb6f822a023488472e6da23370c435be Mon Sep 17 00:00:00 2001 From: Hugo Hromic Date: Tue, 21 Nov 2023 09:28:21 +0000 Subject: [PATCH] Fix and optimize generator Docker image building (#1045) * Fix Docker image of generator always pointing to latest tag * new `REPO_TAG` variable can be used to specify the tag/branch to use for a build * the Makefile default is to use the branch of the current repository `HEAD` * the Dockerfile default (if building manually with Docker) is `main` * updated CircleCI configuration to apply the correct `REPO_TAG` values Signed-off-by: Hugo Hromic * Optimize Docker image of generator * use multi-stage build to reduce final image size * ensure to use the same Debian version (`bookworm`) for the builder and final images * use `--no-install-recommends` to reduce unnecessary dependency downloads * removed unnecessary `unzip` dependency in the image * use modern syntax (using equal sign) for `ENV` Dockerfile directive Signed-off-by: Hugo Hromic --------- Signed-off-by: Hugo Hromic --- .circleci/config.yml | 8 ++++---- generator/Dockerfile | 22 ++++++++++++++++------ generator/Makefile | 6 ++++-- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 2445ec87..6ac62edc 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -53,8 +53,8 @@ jobs: steps: - prometheus/setup_build_environment: docker_version: "" - - run: make -C generator docker DOCKER_IMAGE_TAG=main - - run: make -C generator docker DOCKER_REPO=quay.io/prometheus DOCKER_IMAGE_TAG=main + - run: make -C generator docker REPO_TAG=main DOCKER_IMAGE_TAG=main + - run: make -C generator docker REPO_TAG=main DOCKER_REPO=quay.io/prometheus DOCKER_IMAGE_TAG=main - run: docker images - run: docker login -u $DOCKER_LOGIN -p $DOCKER_PASSWORD - run: docker login -u $QUAY_LOGIN -p $QUAY_PASSWORD quay.io @@ -67,8 +67,8 @@ jobs: steps: - prometheus/setup_build_environment: docker_version: "" - - run: make -C generator docker DOCKER_IMAGE_TAG=$CIRCLE_TAG - - run: make -C generator docker DOCKER_IMAGE_TAG=$CIRCLE_TAG DOCKER_REPO=quay.io/prometheus + - run: make -C generator docker REPO_TAG=$CIRCLE_TAG DOCKER_IMAGE_TAG=$CIRCLE_TAG + - run: make -C generator docker REPO_TAG=$CIRCLE_TAG DOCKER_IMAGE_TAG=$CIRCLE_TAG DOCKER_REPO=quay.io/prometheus - run: docker login -u $DOCKER_LOGIN -p $DOCKER_PASSWORD - run: docker login -u $QUAY_LOGIN -p $QUAY_PASSWORD quay.io - run: | diff --git a/generator/Dockerfile b/generator/Dockerfile index b1281394..7be2e0d5 100644 --- a/generator/Dockerfile +++ b/generator/Dockerfile @@ -1,13 +1,23 @@ -FROM golang:latest +FROM golang:bookworm AS builder -RUN apt-get update && \ - apt-get install -y libsnmp-dev unzip && \ - go install github.com/prometheus/snmp_exporter/generator@latest +RUN apt-get update \ + && apt-get install -y --no-install-recommends libsnmp-dev + +ARG REPO_TAG=main +RUN go install github.com/prometheus/snmp_exporter/generator@"$REPO_TAG" + +FROM debian:bookworm-slim WORKDIR "/opt" -ENTRYPOINT ["/go/bin/generator"] +ENTRYPOINT ["/bin/generator"] -ENV MIBDIRS mibs +ENV MIBDIRS=mibs CMD ["generate"] + +RUN apt-get update \ + && apt-get install -y --no-install-recommends libsnmp40 \ + && rm -rf /var/lib/apt/lists/* + +COPY --from=builder /go/bin/generator /bin/generator diff --git a/generator/Makefile b/generator/Makefile index f7f113ea..16703e61 100644 --- a/generator/Makefile +++ b/generator/Makefile @@ -16,8 +16,10 @@ MIB_PATH := 'mibs' CURL_OPTS ?= -L --no-progress-meter --retry 3 --retry-delay 3 --fail +REPO_TAG ?= $(shell git rev-parse --abbrev-ref HEAD) + DOCKER_IMAGE_NAME ?= snmp-generator -DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD)) +DOCKER_IMAGE_TAG ?= $(subst /,-,$(REPO_TAG)) DOCKER_REPO ?= prom SANITIZED_DOCKER_IMAGE_TAG := $(subst +,-,$(DOCKER_IMAGE_TAG)) @@ -79,7 +81,7 @@ parse_errors: generator mibs .PHONY: docker docker: - docker build -t "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(SANITIZED_DOCKER_IMAGE_TAG)" . + docker build --build-arg REPO_TAG="$(REPO_TAG)" -t "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(SANITIZED_DOCKER_IMAGE_TAG)" . .PHONY: docker-generate docker-generate: docker mibs