From 819df2bb3d8bb4eb2bffa3cc2809feafa0d1d040 Mon Sep 17 00:00:00 2001 From: Chengyu Liu Date: Mon, 11 Mar 2024 12:10:54 +0800 Subject: [PATCH] feat: support building pika_exporter docker image (#2451) Co-authored-by: liuchengyu --- .dockerignore | 14 ++- .../publish_nightly_docker_image.yml | 36 +++++++ .../publish_release_docker_image.yml | 36 +++++++ docker/Dockerfile_pika_exporter | 36 +++++++ docker/build_pika_exporter_docker.sh | 101 ++++++++++++++++++ tools/pika_exporter/Makefile | 16 +-- 6 files changed, 223 insertions(+), 16 deletions(-) create mode 100644 docker/Dockerfile_pika_exporter create mode 100755 docker/build_pika_exporter_docker.sh diff --git a/.dockerignore b/.dockerignore index 5d21ed9c5a..5a550e1b58 100644 --- a/.dockerignore +++ b/.dockerignore @@ -33,7 +33,19 @@ make_config.mk log/ lib/ -tools/ +tools/aof_to_pika +tools/benchmark_client +tools/binlog_sender +tools/codis2pika +tools/kubeblocks_helm +tools/manifest_generator +tools/pika-port +tools/pika_keys_analysis +tools/pika_to_txt +tools/rdb_to_pika +tools/redis-copy +tools/txt_to_pika +docker/ output/ # DB diff --git a/.github/workflows/publish_nightly_docker_image.yml b/.github/workflows/publish_nightly_docker_image.yml index 00d35ebab6..ce84355815 100644 --- a/.github/workflows/publish_nightly_docker_image.yml +++ b/.github/workflows/publish_nightly_docker_image.yml @@ -81,3 +81,39 @@ jobs: push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} + + push_pika_exporter_to_registry: + name: Push Pika Exporter Docker image to Docker Hub + runs-on: ubuntu-latest + steps: + - name: Check out the repo + uses: actions/checkout@v3 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Log in to Docker Hub + uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: pikadb/pika-exporter-dev-nightly + + - name: Build and push Docker image + timeout-minutes: 1440 + uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671 + with: + context: . + platforms: linux/amd64,linux/arm64 + file: ./docker/Dockerfile_pika_exporter + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/.github/workflows/publish_release_docker_image.yml b/.github/workflows/publish_release_docker_image.yml index 8d6c21e583..42698b6573 100644 --- a/.github/workflows/publish_release_docker_image.yml +++ b/.github/workflows/publish_release_docker_image.yml @@ -77,3 +77,39 @@ jobs: push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} + + push_pika_exporter_to_registry: + name: Push Pika Exporter Docker image to Docker Hub + runs-on: ubuntu-latest + steps: + - name: Check out the repo + uses: actions/checkout@v3 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Log in to Docker Hub + uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: pikadb/pika-exporter + + - name: Build and push Docker image + timeout-minutes: 1440 + uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671 + with: + context: . + platforms: linux/amd64,linux/arm64 + file: ./docker/Dockerfile_pika_exporter + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/docker/Dockerfile_pika_exporter b/docker/Dockerfile_pika_exporter new file mode 100644 index 0000000000..ed0169477f --- /dev/null +++ b/docker/Dockerfile_pika_exporter @@ -0,0 +1,36 @@ +FROM golang:1.20 AS builder + +LABEL maintainer="pikiwidb@gmail.com" + +ENV PIKA=/pika \ + PIKA_BUILD_DIR=/tmp/pika \ + PIKA_EXPORTER_BUILD_DIR=/tmp/pika/tools/pika_exporter \ + PATH=${PIKA}:${PIKA}/bin:${PATH} + +ARG ENABLE_PROXY=false +RUN if [ "${ENABLE_PROXY}" = "true" ] ; \ + then go env -w GOPROXY=https://goproxy.io,direct; \ + fi + +COPY . ${PIKA_BUILD_DIR} + +WORKDIR ${PIKA_EXPORTER_BUILD_DIR} + +RUN go env && make + +FROM ubuntu:22.04 + +LABEL maintainer="pikiwidb@gmail.com" + +ENV PIKA=/pika \ + PIKA_BUILD_DIR=/tmp/pika \ + PATH=${PIKA}:${PIKA}/bin:${PATH} + +WORKDIR ${PIKA} + +COPY --from=builder ${PIKA_BUILD_DIR}/tools/pika_exporter/bin/pika_exporter ${PIKA}/bin/pika_exporter +COPY --from=builder ${PIKA_BUILD_DIR}/tools/pika_exporter/config/info.toml ${PIKA}/conf/info.toml + +EXPOSE 9121 + +CMD ["/pika/bin/pika_exporter", "--config", "/pika/conf/info.toml"] diff --git a/docker/build_pika_exporter_docker.sh b/docker/build_pika_exporter_docker.sh new file mode 100755 index 0000000000..4e9edc1bb8 --- /dev/null +++ b/docker/build_pika_exporter_docker.sh @@ -0,0 +1,101 @@ +#! /bin/bash + +# parse arguments +# -t tag, default is "pikadb/pika-exporter:" +# -p platform , it will use docker buildx, options: all, linux/amd64, linux/arm64, linux/arm, linux/arm64, darwin/amd64 more details: https://docs.docker.com/build/building/multi-platform/ +# --proxy proxy, proxy has no value, if you want to use proxy, just add --proxy. if you are in China, you may need to use proxy download the package for up speed the build process +# --help help + +while getopts "t:p:-:" opt; do + case $opt in + t) + TAG=$OPTARG + ;; + p) + PLATFORM=$OPTARG + MULTIARCHIVE=true + ;; + -) + case $OPTARG in + proxy) + proxy=1 + ;; + help) + echo "Usage: build_docker.sh [-t tag] [-p platform] [--proxy] [--help]" + echo "" + echo "Options:" + echo " -t tag default is \"pikadb/pika-exporter:\"" + echo " -p ,[] default is current docker platform. " + echo " options: all, linux/amd64, linux/arm, linux/arm64" + echo " more details: https://docs.docker.com/build/building/multi-platform " + echo " --proxy use proxy download the package for up speed the build process in CN." + echo " --help help" + echo "" + echo "eg:" + echo " ./build_docker.sh -p linux/amd64,linux/arm64 -t pikadb/pika-exporter:latest --proxy " + exit 0 + ;; + + *) + echo "Unknown option --$OPTARG" + exit 1 + ;; + esac + ;; + *) + echo "Unknown option -$opt" + exit 1 + ;; + esac +done + + +# if TAG is not set, set it "pikadb/pika-exporter" +if [ -z "$TAG" ] +then + TAG="pikadb/pika-exporter:$(git describe --tags --abbrev=0 --always)" +fi + +# if Platform is "all", set it "linux/amd64,linux/arm64,linux/arm" +if [ "$PLATFORM" = "all" ] +then + PLATFORM="linux/amd64,linux/arm,linux/arm64" +fi + +# if Platform is not set, set it "linux/amd64" +if [ -z "$PLATFORM" ] +then + PLATFORM="linux/amd64" +fi + +# if proxy is set, set it +PROXY=false +if [ -n "$proxy" ] +then + PROXY=true +fi + +# check if docker is installed +if ! [ -x "$(command -v docker)" ]; then + echo 'Error: docker is not installed.' >&2 + exit 1 +fi + + +if [ "$MULTIARCHIVE" = true ] +then + # check if `docker buildx inpsect pika-builder` is ok + if ! docker buildx inspect pika-builder > /dev/null 2>&1; then + docker buildx create --use --name=pika-builder --driver docker-container + else + docker buildx use pika-builder + fi + + docker buildx build --platform ${PLATFORM} -t ${TAG} -f Dockerfile_pika_exporter --build-arg ENABLE_PROXY=${PROXY} ../ + +else + # build single-arch image + docker build -t ${TAG} -f Dockerfile_pika_exporter --build-arg ENABLE_PROXY=${PROXY} ../ +fi + + diff --git a/tools/pika_exporter/Makefile b/tools/pika_exporter/Makefile index b8f7ed8ace..b5178df6e0 100644 --- a/tools/pika_exporter/Makefile +++ b/tools/pika_exporter/Makefile @@ -4,8 +4,6 @@ # export PATH := $(PATH):$(GOPATH)/bin -OS := $(shell uname) -ARCH := $(shell uname -m) # for mac BRANCH := $(shell git branch | sed 's/* \(.*\)/\1/p') # for Linux @@ -64,19 +62,7 @@ export TEST_COVER all: build build: deps -ifeq ($(OS), Linux) -ifeq ($(ARCH), x86_64) - CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/$(PROJNAME) -else ifeq ($(ARCH), arm64) - CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o bin/$(PROJNAME) -endif -else ifeq ($(OS), Darwin) -ifeq ($(ARCH), x86_64) - CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o bin/$(PROJNAME) -else ifeq ($(ARCH), arm64) - CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o bin/$(PROJNAME) -endif -endif + CGO_ENABLED=0 go build -o bin/$(PROJNAME) deps: generateVer @mkdir -p bin