diff --git a/Dockerfile b/Dockerfile index c1b1178..5135c57 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,7 @@ -FROM centos:7.6.1810 +FROM alpine:3.18.2 -COPY nebula-stats-exporter / -RUN chmod +x /nebula-stats-exporter +ARG TARGETPLATFORM +ARG TARGETARCH + +COPY bin/${TARGETPLATFORM}/nebula-stats-exporter / ENTRYPOINT [ "/nebula-stats-exporter" ] diff --git a/Makefile b/Makefile index 63c291c..63b09b6 100644 --- a/Makefile +++ b/Makefile @@ -1,37 +1,61 @@ PROJECT="nebula-exporter" GO ?= go -VERSION ?= v3.3.0 -DockerUser=vesoft +IMAGE_TAG ?= v3.3.0 +DOCKER_REPO=vesoft +export GO111MODULE := on +LDFLAGS = $(if $(DEBUGGER),,-s -w) +GOOS := $(if $(GOOS),$(GOOS),linux) +GOARCH := $(if $(GOARCH),$(GOARCH),amd64) +GOENV := GO15VENDOREXPERIMENT="1" CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) +GO := $(GOENV) go +GO_BUILD := $(GO) build -trimpath +TARGETDIR := "$(GOOS)/$(GOARCH)" +# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) ifeq (,$(shell go env GOBIN)) GOBIN=$(shell go env GOPATH)/bin else GOBIN=$(shell go env GOBIN) endif -all: check build docker-build docker-push clean +PLATFORMS = arm64 amd64 +BUILDX_PLATFORMS = linux/arm64,linux/amd64 + +ifeq (,$(shell go env GOBIN)) +GOBIN=$(shell go env GOPATH)/bin +else +GOBIN=$(shell go env GOBIN) +endif + +all: check build-exporter helm-chart image-multiarch clean check: fmt vet lint -build: - go build -o nebula-stats-exporter main.go +build-exporter: + $(GO_BUILD) -ldflags '$(LDFLAGS)' -o bin/$(TARGETDIR)/nebula-stats-exporter main.go -build-helm: +helm-chart: helm repo index charts --url https://vesoft-inc.github.io/nebula-stats-exporter/charts helm package charts/nebula-exporter mv nebula-exporter-*.tgz charts/ -docker-build: - CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o nebula-stats-exporter main.go - docker build -t $(DockerUser)/nebula-stats-exporter:$(VERSION) . - -docker-push: - docker push $(DockerUser)/nebula-stats-exporter:$(VERSION) +image-multiarch: + $(foreach PLATFORM,$(PLATFORMS), echo -n "$(PLATFORM)..."; GOARCH=$(PLATFORM) make build-exporter;) + echo "Building and pushing nebula-exporter image... $(BUILDX_PLATFORMS)" + docker buildx rm exporter || true + docker buildx create --driver-opt network=host --use --name=exporter + docker buildx build \ + --no-cache \ + --pull \ + --push \ + --progress plain \ + --platform $(BUILDX_PLATFORMS) \ + -t "${DOCKER_REPO}/nebula-stats-exporter:${IMAGE_TAG}" . clean: rm -rf nebula-stats-exporter - docker rmi -f $(DockerUser)/nebula-stats-exporter:$(VERSION) + docker rmi -f $(DOCKER_REPO)/nebula-stats-exporter:$(IMAGE_TAG) fmt: go fmt .