Skip to content

Read Go build info from the binary #2319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ builds:
ldflags:
- -s -w
- -X main.version={{.Version}}
- -X main.commit={{.Commit}}
- -X main.date={{.Date}}
- -X main.telemetryReportPeriod=24h
- -X main.telemetryEndpoint={{.Env.TELEMETRY_ENDPOINT}}
- -X main.telemetryEndpointInsecure={{.Env.TELEMETRY_ENDPOINT_INSECURE}}
Expand Down
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# variables that should not be overridden by the user
VERSION = edge
GIT_COMMIT = $(shell git rev-parse HEAD || echo "unknown")
DATE = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
SELF_DIR := $(dir $(lastword $(MAKEFILE_LIST)))
MANIFEST_DIR = $(CURDIR)/deploy/manifests
CHART_DIR = $(SELF_DIR)charts/nginx-gateway-fabric
Expand All @@ -22,7 +20,7 @@ ENABLE_EXPERIMENTAL ?= false
NODE_VERSION = $(shell cat .nvmrc)

# go build flags - should not be overridden by the user
GO_LINKER_FlAGS_VARS = -X main.version=${VERSION} -X main.commit=${GIT_COMMIT} -X main.date=${DATE} -X main.telemetryReportPeriod=${TELEMETRY_REPORT_PERIOD} -X main.telemetryEndpoint=${TELEMETRY_ENDPOINT} -X main.telemetryEndpointInsecure=${TELEMETRY_ENDPOINT_INSECURE}
GO_LINKER_FlAGS_VARS = -X main.version=${VERSION} -X main.telemetryReportPeriod=${TELEMETRY_REPORT_PERIOD} -X main.telemetryEndpoint=${TELEMETRY_ENDPOINT} -X main.telemetryEndpointInsecure=${TELEMETRY_ENDPOINT_INSECURE}
GO_LINKER_FLAGS_OPTIMIZATIONS = -s -w
GO_LINKER_FLAGS = $(GO_LINKER_FLAGS_OPTIMIZATIONS) $(GO_LINKER_FlAGS_VARS)

Expand Down
32 changes: 32 additions & 0 deletions cmd/gateway/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"errors"
"fmt"
"os"
"runtime/debug"
"strconv"
"time"

Expand Down Expand Up @@ -124,11 +125,13 @@
atom := zap.NewAtomicLevel()

logger := ctlrZap.New(ctlrZap.Level(atom))
commit, date, dirty := getBuildInfo()

Check warning on line 128 in cmd/gateway/commands.go

View check run for this annotation

Codecov / codecov/patch

cmd/gateway/commands.go#L128

Added line #L128 was not covered by tests
logger.Info(
"Starting NGINX Gateway Fabric in static mode",
"version", version,
"commit", commit,
"date", date,
"dirty", dirty,

Check warning on line 134 in cmd/gateway/commands.go

View check run for this annotation

Codecov / codecov/patch

cmd/gateway/commands.go#L134

Added line #L134 was not covered by tests
)
log.SetLogger(logger)

Expand Down Expand Up @@ -410,11 +413,13 @@
Hidden: true,
RunE: func(_ *cobra.Command, _ []string) error {
logger := ctlrZap.New()
commit, date, dirty := getBuildInfo()

Check warning on line 416 in cmd/gateway/commands.go

View check run for this annotation

Codecov / codecov/patch

cmd/gateway/commands.go#L416

Added line #L416 was not covered by tests
logger.Info(
"Starting NGINX Gateway Fabric Provisioner",
"version", version,
"commit", commit,
"date", date,
"dirty", dirty,

Check warning on line 422 in cmd/gateway/commands.go

View check run for this annotation

Codecov / codecov/patch

cmd/gateway/commands.go#L422

Added line #L422 was not covered by tests
)

return provisioner.StartManager(provisioner.Config{
Expand Down Expand Up @@ -495,3 +500,30 @@

return flagKeys, flagValues
}

type buildInfoFunc func() (info *debug.BuildInfo, ok bool)

var buildInfo buildInfoFunc = debug.ReadBuildInfo

func getBuildInfo() (commitHash string, commitTime string, dirtyBuild string) {
commitHash = "unknown"
commitTime = "unknown"
dirtyBuild = "unknown"

info, ok := buildInfo()
if !ok {
return
}
for _, kv := range info.Settings {
switch kv.Key {
case "vcs.revision":
commitHash = kv.Value
case "vcs.time":
commitTime = kv.Value
case "vcs.modified":
dirtyBuild = kv.Value
}
}

return
}
57 changes: 57 additions & 0 deletions cmd/gateway/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"io"
"runtime/debug"
"testing"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -582,3 +583,59 @@ func TestParseFlags(t *testing.T) {
g.Expect(flagKeys).Should(Equal(expectedKeys))
g.Expect(flagValues).Should(Equal(expectedValues))
}

func TestGetBuildInfo(t *testing.T) {
g := NewWithT(t)
stubBuildInfo := func() (info *debug.BuildInfo, ok bool) {
return &debug.BuildInfo{
Settings: []debug.BuildSetting{
{Key: "vcs.revision", Value: "abc123"},
{Key: "vcs.time", Value: "2024-07-30T12:34:56Z"},
{Key: "vcs.modified", Value: "true"},
},
}, true
}

buildInfo = stubBuildInfo

commitHash, commitTime, dirtyBuild := getBuildInfo()

g.Expect(commitHash).To(Equal("abc123"))
g.Expect(commitTime).To(Equal("2024-07-30T12:34:56Z"))
g.Expect(dirtyBuild).To(Equal("true"))
}

func TestGetBuildInfoNoBuildInfo(t *testing.T) {
g := NewWithT(t)
stubBuildInfo := func() (info *debug.BuildInfo, ok bool) {
return nil, false
}

buildInfo = stubBuildInfo

commitHash, commitTime, dirtyBuild := getBuildInfo()

g.Expect(commitHash).To(Equal("unknown"))
g.Expect(commitTime).To(Equal("unknown"))
g.Expect(dirtyBuild).To(Equal("unknown"))
}

func TestGetBuildInfoMissingValue(t *testing.T) {
g := NewWithT(t)
stubBuildInfo := func() (info *debug.BuildInfo, ok bool) {
return &debug.BuildInfo{
Settings: []debug.BuildSetting{
{Key: "vcs.time", Value: "2024-07-30T12:34:56Z"},
{Key: "vcs.modified", Value: "true"},
},
}, true
}

buildInfo = stubBuildInfo

commitHash, commitTime, dirtyBuild := getBuildInfo()

g.Expect(commitHash).To(Equal("unknown"))
g.Expect(commitTime).To(Equal("2024-07-30T12:34:56Z"))
g.Expect(dirtyBuild).To(Equal("true"))
}
2 changes: 0 additions & 2 deletions cmd/gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
// Set during go build.
var (
version string
commit string
date string

// telemetryReportPeriod is the period at which telemetry reports are sent.
telemetryReportPeriod string
Expand Down
Loading