From 48ac20885ffebad89ec3208fee3305dd212da29b Mon Sep 17 00:00:00 2001 From: Prashant Shahi Date: Tue, 1 Feb 2022 23:03:16 +0530 Subject: [PATCH] =?UTF-8?q?refactor(query-service):=20=E2=99=BB=EF=B8=8F?= =?UTF-8?q?=20=20Update=20ldflags=20and=20Makefile=20for=20dynamic=20versi?= =?UTF-8?q?oning=20(#655)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor(query-service): ♻️ Update ldflags and Makefile for dynamic versioning Signed-off-by: Prashant Shahi * chore: 🎨 Use blacnk spaces indentation in build details * chore(query-service): 🎨 small build details format changes * refactor(query-service): ♻️ refactor ldflags for go build --- Makefile | 31 +++++++++++++++++--- pkg/query-service/Dockerfile | 17 +++++++---- pkg/query-service/main.go | 3 +- pkg/query-service/version/version.go | 42 ++++++++++++++++++++++------ 4 files changed, 74 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index b63d793131..21edec847d 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,14 @@ +# # Reference Guide - https://www.gnu.org/software/make/manual/make.html # + +# Build variables +BUILD_VERSION ?= $(shell git describe --always --tags) +BUILD_HASH ?= $(shell git rev-parse --short HEAD) +BUILD_TIME ?= $(shell date -u +"%Y-%m-%dT%H:%M:%SZ") +BUILD_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD) + # Internal variables or constants. -# FRONTEND_DIRECTORY ?= frontend FLATTENER_DIRECTORY ?= pkg/processors/flattener QUERY_SERVICE_DIRECTORY ?= pkg/query-service @@ -13,6 +20,15 @@ FRONTEND_DOCKER_IMAGE ?= frontend QUERY_SERVICE_DOCKER_IMAGE ?= query-service FLATTERNER_DOCKER_IMAGE ?= flattener-processor +# Build-time Go variables +PACKAGE?=go.signoz.io/query-service +buildVersion=${PACKAGE}/version.buildVersion +buildHash=${PACKAGE}/version.buildHash +buildTime=${PACKAGE}/version.buildTime +gitBranch=${PACKAGE}/version.gitBranch + +LD_FLAGS="-X ${buildHash}=${BUILD_HASH} -X ${buildTime}=${BUILD_TIME} -X ${buildVersion}=${BUILD_VERSION} -X ${gitBranch}=${BUILD_BRANCH}" + all: build-push-frontend build-push-query-service build-push-flattener # Steps to build and push docker image of frontend .PHONY: build-frontend-amd64 build-push-frontend @@ -47,7 +63,9 @@ build-query-service-amd64: @echo "--> Building query-service docker image for amd64" @echo "------------------" @cd $(QUERY_SERVICE_DIRECTORY) && \ - docker build -f Dockerfile --no-cache -t $(REPONAME)/$(QUERY_SERVICE_DOCKER_IMAGE):$(DOCKER_TAG) . --build-arg TARGETPLATFORM="linux/amd64" + docker build -f Dockerfile --no-cache -t $(REPONAME)/$(QUERY_SERVICE_DOCKER_IMAGE):$(DOCKER_TAG) . \ + --build-arg TARGETPLATFORM="linux/amd64" \ + --build-arg LD_FLAGS=$(LD_FLAGS) # Step to build and push docker image of query in amd64 and arm64 (used in push pipeline) build-push-query-service: @@ -56,10 +74,15 @@ build-push-query-service: @echo "------------------" ifndef DOCKER_SECOND_TAG @cd $(QUERY_SERVICE_DIRECTORY) && \ - docker buildx build --file Dockerfile --progress plane --no-cache --push --platform linux/arm64,linux/amd64 --tag $(REPONAME)/$(QUERY_SERVICE_DOCKER_IMAGE):$(DOCKER_TAG) . + docker buildx build --file Dockerfile --progress plane --no-cache --push \ + --platform linux/arm64,linux/amd64 . \ + --build-arg LD_FLAGS=$(LD_FLAGS) \ + --tag $(REPONAME)/$(QUERY_SERVICE_DOCKER_IMAGE):$(DOCKER_TAG) else @cd $(QUERY_SERVICE_DIRECTORY) && \ - docker buildx build --file Dockerfile --progress plane --no-cache --push --platform linux/arm64,linux/amd64 . \ + docker buildx build --file Dockerfile --progress plane --no-cache \ + --push --platform linux/arm64,linux/amd64 . \ + --build-arg LD_FLAGS=$(LD_FLAGS) \ --tag $(REPONAME)/$(QUERY_SERVICE_DOCKER_IMAGE):$(DOCKER_TAG) \ --tag $(REPONAME)/$(QUERY_SERVICE_DOCKER_IMAGE):$(DOCKER_SECOND_TAG) endif diff --git a/pkg/query-service/Dockerfile b/pkg/query-service/Dockerfile index 26352bf734..fe93291f79 100644 --- a/pkg/query-service/Dockerfile +++ b/pkg/query-service/Dockerfile @@ -1,8 +1,7 @@ FROM golang:1.14-buster AS builder -# Add Maintainer Info -LABEL maintainer="signoz" - +# LD_FLAGS is passed as argument from Makefile. It will be empty, if no argument passed +ARG LD_FLAGS ARG TARGETPLATFORM ENV CGO_ENABLED=1 @@ -21,15 +20,22 @@ RUN go mod download -x # Add the sources and proceed with build ADD . . -RUN go build -a -ldflags "-linkmode external -extldflags '-static' -s -w" -o ./bin/query-service ./main.go +RUN go build -a -ldflags "-linkmode external -extldflags '-static' -s -w $LD_FLAGS" -o ./bin/query-service ./main.go RUN chmod +x ./bin/query-service + # use a minimal alpine image FROM alpine:3.7 + +# Add Maintainer Info +LABEL maintainer="signoz" + # add ca-certificates in case you need them RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/* + # set working directory WORKDIR /root + # copy the binary from builder COPY --from=builder /go/src/github.com/signoz/signoz/pkg/query-service/bin/query-service . @@ -37,7 +43,8 @@ COPY config/prometheus.yml /root/config/prometheus.yml # run the binary ENTRYPOINT ["./query-service"] + CMD ["-config", "/root/config/prometheus.yml"] # CMD ["./query-service -config /root/config/prometheus.yml"] -EXPOSE 8080 +EXPOSE 8080 diff --git a/pkg/query-service/main.go b/pkg/query-service/main.go index b8e614868e..3418154d79 100644 --- a/pkg/query-service/main.go +++ b/pkg/query-service/main.go @@ -10,6 +10,7 @@ import ( "go.signoz.io/query-service/app" "go.signoz.io/query-service/constants" + "go.signoz.io/query-service/version" "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -31,7 +32,7 @@ func main() { defer loggerMgr.Sync() // flushes buffer, if any logger := loggerMgr.Sugar() - logger.Debug("STARTING!") + version.PrintVersion() serverOptions := &app.ServerOptions{ // HTTPHostPort: v.GetString(app.HTTPHostPort), diff --git a/pkg/query-service/version/version.go b/pkg/query-service/version/version.go index cad1bfa6f0..f53f42c544 100644 --- a/pkg/query-service/version/version.go +++ b/pkg/query-service/version/version.go @@ -1,24 +1,48 @@ package version import ( + "fmt" + "runtime" + "go.uber.org/zap" ) // These fields are set during an official build // Global vars set from command-line arguments var ( - version = "--" - buildhash = "--" - buildtime = "--" + buildVersion = "--" + buildHash = "--" + buildTime = "--" + gitBranch = "--" ) -//PrintVersionInfo displays the kyverno version - git version -func PrintVersionInfo() { - zap.S().Info("Version: ", version) - zap.S().Info("BuildHash: ", buildhash) - zap.S().Info("BuildTime: ", buildtime) +// BuildDetails returns a string containing details about the SigNoz query-service binary. +func BuildDetails() string { + licenseInfo := `Licensed under the MIT License` + + return fmt.Sprintf(` +SigNoz version : %v +Commit SHA-1 : %v +Commit timestamp : %v +Branch : %v +Go version : %v + +For SigNoz Official Documentation, visit https://signoz.io/docs +For SigNoz Community Slack, visit http://signoz.io/slack +For discussions about SigNoz, visit https://community.signoz.io + +%s. +Copyright 2022 SigNoz +`, + buildVersion, buildHash, buildTime, gitBranch, + runtime.Version(), licenseInfo) +} + +// PrintVersion prints version and other helpful information. +func PrintVersion() { + zap.S().Infof("\n%s\n", BuildDetails()) } func GetVersion() string { - return version + return buildVersion }