Skip to content

Read Go build info from the binary #179

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

Closed
wants to merge 1 commit into from
Closed
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: 2 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ builds:
- all=-trimpath={{.Env.GOPATH}}
asmflags:
- all=-trimpath={{.Env.GOPATH}}
ldflags:
- -s -w -X main.version={{.Version}}
main: ./cmd/gateway/
binary: gateway

Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ TARGET ?= local ## The target of the build. Possible values: local and container
KIND_KUBE_CONFIG_FOLDER = $${HOME}/.kube/kind ## The folder where the kind kubeconfig is stored
OUT_DIR ?= $(shell pwd)/build/out ## The folder where the binary will be stored
ARCH ?= amd64 ## The architecture of the image and/or binary. For example: amd64 or arm64
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
override DOCKER_BUILD_OPTIONS += --build-arg VERSION=$(VERSION) ## The options for the docker build command. For example, --pull

.DEFAULT_GOAL := help

Expand All @@ -28,7 +28,7 @@ container: build ## Build the container
build: ## Build the binary
ifeq (${TARGET},local)
@go version || (code=$$?; printf "\033[0;31mError\033[0m: unable to build locally\n"; exit $$code)
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
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
endif

.PHONY: build-goreleaser
Expand Down
4 changes: 1 addition & 3 deletions build/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# syntax=docker/dockerfile:1.4
FROM golang:1.20 as builder
ARG VERSION
ARG GIT_COMMIT
ARG DATE

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

Expand All @@ -12,7 +10,7 @@ RUN go mod download
COPY cmd /go/src/github.com/nginxinc/nginx-kubernetes-gateway/cmd
COPY internal /go/src/github.com/nginxinc/nginx-kubernetes-gateway/internal
COPY pkg /go/src/github.com/nginxinc/nginx-kubernetes-gateway/pkg
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 .
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -a -ldflags "-s -w -X main.version=${VERSION}" -o gateway .

FROM alpine:3.18 as capabilizer
RUN apk add --no-cache libcap
Expand Down
28 changes: 28 additions & 0 deletions cmd/gateway/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"runtime/debug"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -124,10 +125,12 @@ func createStaticModeCommand() *cobra.Command {
Short: "Configure NGINX in the scope of a single Gateway resource",
RunE: func(cmd *cobra.Command, args []string) error {
logger := zap.New()
commit, date, dirty := getBuildInfo()
logger.Info("Starting NGINX Kubernetes Gateway in static mode",
"version", version,
"commit", commit,
"date", date,
"dirty", dirty,
)

podIP := os.Getenv("POD_IP")
Expand Down Expand Up @@ -184,10 +187,12 @@ func createProvisionerModeCommand() *cobra.Command {
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
logger := zap.New()
commit, date, dirty := getBuildInfo()
logger.Info("Starting NGINX Kubernetes Gateway Provisioner",
"version", version,
"commit", commit,
"date", date,
"dirty", dirty,
)

return provisioner.StartManager(provisioner.Config{
Expand All @@ -198,3 +203,26 @@ func createProvisionerModeCommand() *cobra.Command {
},
}
}

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

info, ok := debug.ReadBuildInfo()
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 == "true"
}
}

return
}
8 changes: 2 additions & 6 deletions cmd/gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ import (
"os"
)

var (
// Set during go build
version string
commit string
date string
)
// Set during go build
var version string

func main() {
rootCmd := createRootCommand()
Expand Down