Skip to content

Commit 01c46ae

Browse files
authored
Read Go build info from the binary (#2319)
Problem: We're injecting information about the environment at build time. This could be unreliable and doesn't need to be done since `go` has all this info at build time already. Solution: Get the info directly from the build.
1 parent 99a4532 commit 01c46ae

File tree

5 files changed

+90
-7
lines changed

5 files changed

+90
-7
lines changed

.goreleaser.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ builds:
1818
ldflags:
1919
- -s -w
2020
- -X main.version={{.Version}}
21-
- -X main.commit={{.Commit}}
22-
- -X main.date={{.Date}}
2321
- -X main.telemetryReportPeriod=24h
2422
- -X main.telemetryEndpoint={{.Env.TELEMETRY_ENDPOINT}}
2523
- -X main.telemetryEndpointInsecure={{.Env.TELEMETRY_ENDPOINT_INSECURE}}

Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# variables that should not be overridden by the user
22
VERSION = edge
3-
GIT_COMMIT = $(shell git rev-parse HEAD || echo "unknown")
4-
DATE = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
53
SELF_DIR := $(dir $(lastword $(MAKEFILE_LIST)))
64
MANIFEST_DIR = $(CURDIR)/deploy/manifests
75
CHART_DIR = $(SELF_DIR)charts/nginx-gateway-fabric
@@ -22,7 +20,7 @@ ENABLE_EXPERIMENTAL ?= false
2220
NODE_VERSION = $(shell cat .nvmrc)
2321

2422
# go build flags - should not be overridden by the user
25-
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}
23+
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}
2624
GO_LINKER_FLAGS_OPTIMIZATIONS = -s -w
2725
GO_LINKER_FLAGS = $(GO_LINKER_FLAGS_OPTIMIZATIONS) $(GO_LINKER_FlAGS_VARS)
2826

cmd/gateway/commands.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"os"
7+
"runtime/debug"
78
"strconv"
89
"time"
910

@@ -124,11 +125,13 @@ func createStaticModeCommand() *cobra.Command {
124125
atom := zap.NewAtomicLevel()
125126

126127
logger := ctlrZap.New(ctlrZap.Level(atom))
128+
commit, date, dirty := getBuildInfo()
127129
logger.Info(
128130
"Starting NGINX Gateway Fabric in static mode",
129131
"version", version,
130132
"commit", commit,
131133
"date", date,
134+
"dirty", dirty,
132135
)
133136
log.SetLogger(logger)
134137

@@ -410,11 +413,13 @@ func createProvisionerModeCommand() *cobra.Command {
410413
Hidden: true,
411414
RunE: func(_ *cobra.Command, _ []string) error {
412415
logger := ctlrZap.New()
416+
commit, date, dirty := getBuildInfo()
413417
logger.Info(
414418
"Starting NGINX Gateway Fabric Provisioner",
415419
"version", version,
416420
"commit", commit,
417421
"date", date,
422+
"dirty", dirty,
418423
)
419424

420425
return provisioner.StartManager(provisioner.Config{
@@ -495,3 +500,30 @@ func parseFlags(flags *pflag.FlagSet) ([]string, []string) {
495500

496501
return flagKeys, flagValues
497502
}
503+
504+
type buildInfoFunc func() (info *debug.BuildInfo, ok bool)
505+
506+
var buildInfo buildInfoFunc = debug.ReadBuildInfo
507+
508+
func getBuildInfo() (commitHash string, commitTime string, dirtyBuild string) {
509+
commitHash = "unknown"
510+
commitTime = "unknown"
511+
dirtyBuild = "unknown"
512+
513+
info, ok := buildInfo()
514+
if !ok {
515+
return
516+
}
517+
for _, kv := range info.Settings {
518+
switch kv.Key {
519+
case "vcs.revision":
520+
commitHash = kv.Value
521+
case "vcs.time":
522+
commitTime = kv.Value
523+
case "vcs.modified":
524+
dirtyBuild = kv.Value
525+
}
526+
}
527+
528+
return
529+
}

cmd/gateway/commands_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"io"
5+
"runtime/debug"
56
"testing"
67

78
. "github.com/onsi/gomega"
@@ -582,3 +583,59 @@ func TestParseFlags(t *testing.T) {
582583
g.Expect(flagKeys).Should(Equal(expectedKeys))
583584
g.Expect(flagValues).Should(Equal(expectedValues))
584585
}
586+
587+
func TestGetBuildInfo(t *testing.T) {
588+
g := NewWithT(t)
589+
stubBuildInfo := func() (info *debug.BuildInfo, ok bool) {
590+
return &debug.BuildInfo{
591+
Settings: []debug.BuildSetting{
592+
{Key: "vcs.revision", Value: "abc123"},
593+
{Key: "vcs.time", Value: "2024-07-30T12:34:56Z"},
594+
{Key: "vcs.modified", Value: "true"},
595+
},
596+
}, true
597+
}
598+
599+
buildInfo = stubBuildInfo
600+
601+
commitHash, commitTime, dirtyBuild := getBuildInfo()
602+
603+
g.Expect(commitHash).To(Equal("abc123"))
604+
g.Expect(commitTime).To(Equal("2024-07-30T12:34:56Z"))
605+
g.Expect(dirtyBuild).To(Equal("true"))
606+
}
607+
608+
func TestGetBuildInfoNoBuildInfo(t *testing.T) {
609+
g := NewWithT(t)
610+
stubBuildInfo := func() (info *debug.BuildInfo, ok bool) {
611+
return nil, false
612+
}
613+
614+
buildInfo = stubBuildInfo
615+
616+
commitHash, commitTime, dirtyBuild := getBuildInfo()
617+
618+
g.Expect(commitHash).To(Equal("unknown"))
619+
g.Expect(commitTime).To(Equal("unknown"))
620+
g.Expect(dirtyBuild).To(Equal("unknown"))
621+
}
622+
623+
func TestGetBuildInfoMissingValue(t *testing.T) {
624+
g := NewWithT(t)
625+
stubBuildInfo := func() (info *debug.BuildInfo, ok bool) {
626+
return &debug.BuildInfo{
627+
Settings: []debug.BuildSetting{
628+
{Key: "vcs.time", Value: "2024-07-30T12:34:56Z"},
629+
{Key: "vcs.modified", Value: "true"},
630+
},
631+
}, true
632+
}
633+
634+
buildInfo = stubBuildInfo
635+
636+
commitHash, commitTime, dirtyBuild := getBuildInfo()
637+
638+
g.Expect(commitHash).To(Equal("unknown"))
639+
g.Expect(commitTime).To(Equal("2024-07-30T12:34:56Z"))
640+
g.Expect(dirtyBuild).To(Equal("true"))
641+
}

cmd/gateway/main.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
// Set during go build.
99
var (
1010
version string
11-
commit string
12-
date string
1311

1412
// telemetryReportPeriod is the period at which telemetry reports are sent.
1513
telemetryReportPeriod string

0 commit comments

Comments
 (0)