From 696911efd272263c576a5470651a17b7809df402 Mon Sep 17 00:00:00 2001 From: Tanay Kothari Date: Fri, 24 Jan 2025 02:37:11 +0000 Subject: [PATCH] Add version information to binaries --- .github/workflows/publish-container-images.yaml | 8 +++++++- build/images/Dockerfile.alerter | 9 ++++++++- build/images/Dockerfile.collector | 7 ++++++- build/images/Dockerfile.ingestor | 7 ++++++- cmd/alerter/main.go | 4 ++++ cmd/collector/main.go | 5 +++++ cmd/ingestor/main.go | 5 +++++ pkg/version/version.go | 13 +++++++++++++ 8 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 pkg/version/version.go diff --git a/.github/workflows/publish-container-images.yaml b/.github/workflows/publish-container-images.yaml index df50f4610..037bfa3c8 100644 --- a/.github/workflows/publish-container-images.yaml +++ b/.github/workflows/publish-container-images.yaml @@ -4,6 +4,7 @@ on: push: branches: [ "main" ] tags: [ 'v*.*.*' ] + pull_request: # TOOD: Remove after testing env: REGISTRY: ghcr.io @@ -50,8 +51,9 @@ jobs: with: images: ${{ matrix.image }} # Use latest tag on default branch (main), otherwise use the tag + # type=raw, value=latest, enable={{is_default_branch}} tags: | - type=raw, value=latest, enable={{is_default_branch}} + type=raw, value=latest, enable=true type=ref, event=tag flavor: | latest=false @@ -63,6 +65,10 @@ jobs: with: context: . file: ${{ matrix.dockerfile }} + build-args: | + VERSION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }} + GIT_COMMIT=${{ github.sha }} + BUILD_TIME=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }} push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} diff --git a/build/images/Dockerfile.alerter b/build/images/Dockerfile.alerter index 6e954a43c..f3dc1b619 100644 --- a/build/images/Dockerfile.alerter +++ b/build/images/Dockerfile.alerter @@ -5,7 +5,14 @@ RUN tdnf install -y golang ca-certificates ADD . /code WORKDIR /code -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ./bin/alerter ./cmd/alerter +ARG VERSION GIT_COMMIT BUILD_TIME + +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \ +-ldflags=" \ +-X 'github.com/Azure/adx-mon/pkg/version.Version=${VERSION}' \ +-X 'github.com/Azure/adx-mon/pkg/version.GitCommit=${GIT_COMMIT}' \ +-X 'github.com/Azure/adx-mon/pkg/version.BuildTime=${BUILD_TIME}' \ +" -o ./bin/alerter ./cmd/alerter FROM mcr.microsoft.com/cbl-mariner/distroless/minimal:2.0 diff --git a/build/images/Dockerfile.collector b/build/images/Dockerfile.collector index 51d12e744..b4f9acbf1 100644 --- a/build/images/Dockerfile.collector +++ b/build/images/Dockerfile.collector @@ -5,7 +5,12 @@ RUN tdnf install -y golang systemd-devel gcc glibc-devel binutils kernel-headers ADD . /code WORKDIR /code -RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o ./bin/collector ./cmd/collector +RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build \ +-ldflags=" \ +-X 'github.com/Azure/adx-mon/pkg/version.Version=${VERSION}' \ +-X 'github.com/Azure/adx-mon/pkg/version.GitCommit=${GIT_COMMIT}' \ +-X 'github.com/Azure/adx-mon/pkg/version.BuildTime=${BUILD_TIME}' \ +" -o ./bin/collector ./cmd/collector # Install systemd lib to get libsystemd.so FROM mcr.microsoft.com/cbl-mariner/base/core:2.0 AS libsystemdsource diff --git a/build/images/Dockerfile.ingestor b/build/images/Dockerfile.ingestor index a6213059a..c9c8ceb35 100644 --- a/build/images/Dockerfile.ingestor +++ b/build/images/Dockerfile.ingestor @@ -5,7 +5,12 @@ RUN tdnf install -y golang ca-certificates ADD . /code WORKDIR /code -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ./bin/ingestor ./cmd/ingestor +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \ +-ldflags=" \ +-X 'github.com/Azure/adx-mon/pkg/version.Version=${VERSION}' \ +-X 'github.com/Azure/adx-mon/pkg/version.GitCommit=${GIT_COMMIT}' \ +-X 'github.com/Azure/adx-mon/pkg/version.BuildTime=${BUILD_TIME}' \ +" -o ./bin/ingestor ./cmd/ingestor FROM mcr.microsoft.com/cbl-mariner/distroless/minimal:2.0 diff --git a/cmd/alerter/main.go b/cmd/alerter/main.go index 143bf929b..b51e85475 100644 --- a/cmd/alerter/main.go +++ b/cmd/alerter/main.go @@ -11,6 +11,7 @@ import ( "github.com/Azure/adx-mon/alerter" alertrulev1 "github.com/Azure/adx-mon/api/v1" "github.com/Azure/adx-mon/pkg/logger" + "github.com/Azure/adx-mon/pkg/version" "github.com/urfave/cli/v2" // imports as package "cli" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" @@ -40,6 +41,7 @@ func main() { Commands: []*cli.Command{ NewLintCommand(), }, + Version: version.String(), } if err := app.Run(os.Args); err != nil { @@ -48,6 +50,8 @@ func main() { } func realMain(ctx *cli.Context) error { + logger.Infof("alerter version:%s", version.String()) + endpoints := make(map[string]string) endpointsArg := ctx.StringSlice("kusto-endpoint") for _, v := range endpointsArg { diff --git a/cmd/collector/main.go b/cmd/collector/main.go index 026846de4..09a1a13bf 100644 --- a/cmd/collector/main.go +++ b/cmd/collector/main.go @@ -28,6 +28,7 @@ import ( "github.com/Azure/adx-mon/pkg/k8s" "github.com/Azure/adx-mon/pkg/logger" "github.com/Azure/adx-mon/pkg/remote" + "github.com/Azure/adx-mon/pkg/version" "github.com/Azure/adx-mon/schema" "github.com/Azure/adx-mon/storage" "github.com/pelletier/go-toml/v2" @@ -68,6 +69,8 @@ func main() { Action: func(ctx *cli.Context) error { return realMain(ctx) }, + + Version: version.String(), } if err := app.Run(os.Args); err != nil { @@ -76,6 +79,8 @@ func main() { } func realMain(ctx *cli.Context) error { + logger.Infof("collector version:%s", version.String()) + runtime.MemProfileRate = 4096 runtime.SetBlockProfileRate(int(1 * time.Second)) runtime.SetMutexProfileFraction(1) diff --git a/cmd/ingestor/main.go b/cmd/ingestor/main.go index 425705a0a..f12de6637 100644 --- a/cmd/ingestor/main.go +++ b/cmd/ingestor/main.go @@ -26,6 +26,7 @@ import ( "github.com/Azure/adx-mon/pkg/limiter" "github.com/Azure/adx-mon/pkg/logger" "github.com/Azure/adx-mon/pkg/tls" + "github.com/Azure/adx-mon/pkg/version" "github.com/Azure/adx-mon/schema" "github.com/Azure/azure-kusto-go/kusto" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -85,6 +86,8 @@ func main() { Action: func(ctx *cli.Context) error { return realMain(ctx) }, + + Version: version.String(), } if err := app.Run(os.Args); err != nil { @@ -93,6 +96,8 @@ func main() { } func realMain(ctx *cli.Context) error { + logger.Infof("ingestor version:%s", version.String()) + svcCtx, cancel := context.WithCancel(context.Background()) defer cancel() diff --git a/pkg/version/version.go b/pkg/version/version.go new file mode 100644 index 000000000..d0be58282 --- /dev/null +++ b/pkg/version/version.go @@ -0,0 +1,13 @@ +package version + +import "fmt" + +var ( + GitCommit string + BuildTime string + Version string +) + +func String() string { + return fmt.Sprintf("%s, Commit:%s, Build-time:%s", Version, GitCommit, BuildTime) +}