Skip to content

Commit 7b59408

Browse files
committed
Read Go build info from the binary
1 parent c52b4da commit 7b59408

File tree

5 files changed

+35
-11
lines changed

5 files changed

+35
-11
lines changed

.goreleaser.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ builds:
1414
- all=-trimpath={{.Env.GOPATH}}
1515
asmflags:
1616
- all=-trimpath={{.Env.GOPATH}}
17+
ldflags:
18+
- -s -w -X main.version={{.Version}}
1719
main: ./cmd/gateway/
1820
binary: gateway
1921

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ TARGET ?= local ## The target of the build. Possible values: local and container
1010
KIND_KUBE_CONFIG_FOLDER = $${HOME}/.kube/kind ## The folder where the kind kubeconfig is stored
1111
OUT_DIR ?= $(shell pwd)/build/out ## The folder where the binary will be stored
1212
ARCH ?= amd64 ## The architecture of the image and/or binary. For example: amd64 or arm64
13-
override DOCKER_BUILD_OPTIONS += --build-arg VERSION=$(VERSION) --build-arg GIT_COMMIT=$(GIT_COMMIT) --build-arg DATE=$(DATE) ## The options for the docker build command. For example, --pull
13+
override DOCKER_BUILD_OPTIONS += --build-arg VERSION=$(VERSION) ## The options for the docker build command. For example, --pull
1414

1515
.DEFAULT_GOAL := help
1616

@@ -28,7 +28,7 @@ container: build ## Build the container
2828
build: ## Build the binary
2929
ifeq (${TARGET},local)
3030
@go version || (code=$$?; printf "\033[0;31mError\033[0m: unable to build locally\n"; exit $$code)
31-
CGO_ENABLED=0 GOOS=linux GOARCH=$(ARCH) go build -trimpath -a -ldflags "-s -w -X main.version=${VERSION} -X main.commit=${GIT_COMMIT} -X main.date=${DATE}" -o $(OUT_DIR)/gateway github.com/nginxinc/nginx-kubernetes-gateway/cmd/gateway
31+
CGO_ENABLED=0 GOOS=linux GOARCH=$(ARCH) go build -trimpath -a -ldflags "-s -w -X main.version=${VERSION}" -o $(OUT_DIR)/gateway github.com/nginxinc/nginx-kubernetes-gateway/cmd/gateway
3232
endif
3333

3434
.PHONY: build-goreleaser

build/Dockerfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# syntax=docker/dockerfile:1.4
22
FROM golang:1.20 as builder
33
ARG VERSION
4-
ARG GIT_COMMIT
5-
ARG DATE
64

75
WORKDIR /go/src/github.com/nginxinc/nginx-kubernetes-gateway/cmd/gateway
86

@@ -12,7 +10,7 @@ RUN go mod download
1210
COPY cmd /go/src/github.com/nginxinc/nginx-kubernetes-gateway/cmd
1311
COPY internal /go/src/github.com/nginxinc/nginx-kubernetes-gateway/internal
1412
COPY pkg /go/src/github.com/nginxinc/nginx-kubernetes-gateway/pkg
15-
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -a -ldflags "-s -w -X main.version=${VERSION} -X main.commit=${GIT_COMMIT} -X main.date=${DATE}" -o gateway .
13+
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -a -ldflags "-s -w -X main.version=${VERSION}" -o gateway .
1614

1715
FROM alpine:3.18 as capabilizer
1816
RUN apk add --no-cache libcap

cmd/gateway/commands.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"runtime/debug"
67

78
"github.com/spf13/cobra"
89
"k8s.io/apimachinery/pkg/types"
@@ -124,10 +125,12 @@ func createStaticModeCommand() *cobra.Command {
124125
Short: "Configure NGINX in the scope of a single Gateway resource",
125126
RunE: func(cmd *cobra.Command, args []string) error {
126127
logger := zap.New()
128+
commit, date, dirty := getBuildInfo()
127129
logger.Info("Starting NGINX Kubernetes Gateway in static mode",
128130
"version", version,
129131
"commit", commit,
130132
"date", date,
133+
"dirty", dirty,
131134
)
132135

133136
podIP := os.Getenv("POD_IP")
@@ -184,10 +187,12 @@ func createProvisionerModeCommand() *cobra.Command {
184187
Hidden: true,
185188
RunE: func(cmd *cobra.Command, args []string) error {
186189
logger := zap.New()
190+
commit, date, dirty := getBuildInfo()
187191
logger.Info("Starting NGINX Kubernetes Gateway Provisioner",
188192
"version", version,
189193
"commit", commit,
190194
"date", date,
195+
"dirty", dirty,
191196
)
192197

193198
return provisioner.StartManager(provisioner.Config{
@@ -198,3 +203,26 @@ func createProvisionerModeCommand() *cobra.Command {
198203
},
199204
}
200205
}
206+
207+
func getBuildInfo() (commitHash string, commitTime string, dirtyBuild bool) {
208+
commitHash = "unknown"
209+
commitTime = "unknown"
210+
dirtyBuild = true
211+
212+
info, ok := debug.ReadBuildInfo()
213+
if !ok {
214+
return
215+
}
216+
for _, kv := range info.Settings {
217+
switch kv.Key {
218+
case "vcs.revision":
219+
commitHash = kv.Value
220+
case "vcs.time":
221+
commitTime = kv.Value
222+
case "vcs.modified":
223+
dirtyBuild = kv.Value == "true"
224+
}
225+
}
226+
227+
return
228+
}

cmd/gateway/main.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@ import (
55
"os"
66
)
77

8-
var (
9-
// Set during go build
10-
version string
11-
commit string
12-
date string
13-
)
8+
// Set during go build
9+
var version string
1410

1511
func main() {
1612
rootCmd := createRootCommand()

0 commit comments

Comments
 (0)