diff --git a/.goreleaser.yml b/.goreleaser.yml index 81db615838..e820898467 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -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 diff --git a/Makefile b/Makefile index c43a932709..9fb97f4bb0 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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 diff --git a/build/Dockerfile b/build/Dockerfile index 9f0fcf7ba8..26b4048c73 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -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 @@ -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 diff --git a/cmd/gateway/commands.go b/cmd/gateway/commands.go index fa0f9423ce..ba6e3c4e1c 100644 --- a/cmd/gateway/commands.go +++ b/cmd/gateway/commands.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "runtime/debug" "github.com/spf13/cobra" "k8s.io/apimachinery/pkg/types" @@ -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") @@ -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{ @@ -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 +} diff --git a/cmd/gateway/main.go b/cmd/gateway/main.go index 12b2a89517..913e0a35a0 100644 --- a/cmd/gateway/main.go +++ b/cmd/gateway/main.go @@ -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()