From af298da4a3357c6496f5c3f3fedbfdabee6082d7 Mon Sep 17 00:00:00 2001 From: bitliu Date: Thu, 3 Nov 2022 10:59:20 +0800 Subject: [PATCH 1/2] feat: support user-facing version Signed-off-by: bitliu --- docs/latest/conf.py | 1 + internal/cmd/root.go | 5 +++-- internal/cmd/version/version.go | 37 +++++++++++++++++++++++++++++++++ internal/cmd/versions.go | 36 +++++--------------------------- tools/make/golang.mk | 9 +++++++- tools/make/tools.mk | 1 - tools/src/goversion/go.mod | 13 ------------ tools/src/goversion/go.sum | 16 -------------- tools/src/goversion/pin.go | 11 ---------- 9 files changed, 54 insertions(+), 75 deletions(-) create mode 100644 internal/cmd/version/version.go delete mode 100644 tools/src/goversion/go.mod delete mode 100644 tools/src/goversion/go.sum delete mode 100644 tools/src/goversion/pin.go diff --git a/docs/latest/conf.py b/docs/latest/conf.py index c90c4a49fe..e5d4e3e423 100644 --- a/docs/latest/conf.py +++ b/docs/latest/conf.py @@ -31,6 +31,7 @@ } variables_to_export = [ + "version", "envoyVersion", "gatewayAPIVersion", ] diff --git a/internal/cmd/root.go b/internal/cmd/root.go index ae4c96eb8e..7b57c72d7a 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -14,13 +14,14 @@ import ( func GetRootCommand() *cobra.Command { cmd := &cobra.Command{ Use: "envoy-gateway", - Short: "Manages Envoy Proxy as a standalone or Kubernetes-based application gateway", + Short: "Envoy Gateway", + Long: "Manages Envoy Proxy as a standalone or Kubernetes-based application gateway", } cmd.AddCommand(getServerCommand()) + cmd.AddCommand(getVersionsCommand()) cmd.AddCommand(getxDSTestCommand()) cmd.AddCommand(getCertGenCommand()) - cmd.AddCommand(getVersionsCommand()) return cmd } diff --git a/internal/cmd/version/version.go b/internal/cmd/version/version.go new file mode 100644 index 0000000000..8a38bb4dae --- /dev/null +++ b/internal/cmd/version/version.go @@ -0,0 +1,37 @@ +package version + +import ( + "fmt" + "runtime/debug" + "strings" + + "github.com/envoyproxy/gateway/internal/ir" +) + +var ( + EnvoyGatewayVersion string + GatewayAPIVersion string + EnvoyVersion = strings.Split(ir.DefaultProxyImage, ":")[1] + GitCommitID string +) + +func init() { + bi, ok := debug.ReadBuildInfo() + if ok { + for _, dep := range bi.Deps { + if dep.Path == "sigs.k8s.io/gateway-api" { + GatewayAPIVersion = dep.Version + } + } + } +} + +// Print shows the versions of the Envoy Gateway. +func Print() error { + fmt.Printf("ENVOY_GATEWAY_VERSION: %s\n", EnvoyGatewayVersion) + fmt.Printf("ENVOY_VERSION: %s\n", EnvoyVersion) + fmt.Printf("GATEWAYAPI_VERSION: %s\n", GatewayAPIVersion) + fmt.Printf("GIT_COMMIT_ID: %s\n", GitCommitID) + + return nil +} diff --git a/internal/cmd/versions.go b/internal/cmd/versions.go index b95cbade35..59a96bbe51 100644 --- a/internal/cmd/versions.go +++ b/internal/cmd/versions.go @@ -7,10 +7,8 @@ package cmd import ( "fmt" - "runtime/debug" - "strings" - "github.com/envoyproxy/gateway/internal/ir" + "github.com/envoyproxy/gateway/internal/cmd/version" "github.com/spf13/cobra" ) @@ -36,36 +34,12 @@ func getVersionsCommand() *cobra.Command { // versions shows the versions of the Envoy Gateway. func versions(envOutput bool) error { - envoyVersion := strings.Split(ir.DefaultProxyImage, ":")[1] - if envOutput { - fmt.Printf("ENVOY_VERSION=\"%s\"\n", envoyVersion) + fmt.Printf("ENVOY_VERSION=\"%s\"\n", version.EnvoyVersion) + fmt.Printf("GATEWAYAPI_VERSION=\"%s\"\n", version.GatewayAPIVersion) + fmt.Printf("ENVOY_GATEWAY_VERSION=\"%s\"\n", version.EnvoyGatewayVersion) } else { - fmt.Printf("Envoy: %s\n", envoyVersion) - } - - bi, ok := debug.ReadBuildInfo() - if !ok { - return fmt.Errorf("could not read build info") - } - - foundGatewayAPI := false - - for _, dep := range bi.Deps { - if dep.Path == "sigs.k8s.io/gateway-api" { - if envOutput { - fmt.Printf("GATEWAYAPI_VERSION=\"%s\"\n", dep.Version) - } else { - fmt.Printf("Gateway API: %s\n", dep.Version) - } - - foundGatewayAPI = true - break - } - } - - if !foundGatewayAPI { - return fmt.Errorf("could not find Gateway API version") + return version.Print() } return nil diff --git a/tools/make/golang.mk b/tools/make/golang.mk index d6fc6a97d0..9bb4a55846 100644 --- a/tools/make/golang.mk +++ b/tools/make/golang.mk @@ -2,6 +2,13 @@ # # All make targets related to golang are defined in this file. +VERSION_PACKAGE := github.com/envoyproxy/gateway/internal/cmd/version + +GO_LDFLAGS += -X $(VERSION_PACKAGE).EnvoyGatewayVersion=$(shell cat VERSION) \ + -X $(VERSION_PACKAGE).GitCommitID=$(GIT_COMMIT) + +GIT_COMMIT:=$(shell git rev-parse HEAD) + GOPATH := $(shell go env GOPATH) ifeq ($(origin GOBIN), undefined) GOBIN := $(GOPATH)/bin @@ -20,7 +27,7 @@ go.build.%: $(eval OS := $(word 1,$(subst _, ,$(PLATFORM)))) $(eval ARCH := $(word 2,$(subst _, ,$(PLATFORM)))) @$(call log, "Building binary $(COMMAND) with commit $(REV) for $(OS) $(ARCH)") - CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) go build -o $(OUTPUT_DIR)/$(OS)/$(ARCH)/$(COMMAND) $(ROOT_PACKAGE)/cmd/$(COMMAND) + CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) go build -o $(OUTPUT_DIR)/$(OS)/$(ARCH)/$(COMMAND) -ldflags "$(GO_LDFLAGS)" $(ROOT_PACKAGE)/cmd/$(COMMAND) # Build the envoy-gateway binaries in the hosted platforms. .PHONY: go.build diff --git a/tools/make/tools.mk b/tools/make/tools.mk index 6495120ea4..d945caf79e 100644 --- a/tools/make/tools.mk +++ b/tools/make/tools.mk @@ -14,7 +14,6 @@ $(tools.bindir)/%: $(tools.srcdir)/%.sh # tools/controller-gen = $(tools.bindir)/controller-gen tools/golangci-lint = $(tools.bindir)/golangci-lint -tools/goversion = $(tools.bindir)/goversion tools/kustomize = $(tools.bindir)/kustomize tools/kind = $(tools.bindir)/kind tools/setup-envtest = $(tools.bindir)/setup-envtest diff --git a/tools/src/goversion/go.mod b/tools/src/goversion/go.mod deleted file mode 100644 index 68a8691075..0000000000 --- a/tools/src/goversion/go.mod +++ /dev/null @@ -1,13 +0,0 @@ -module local - -go 1.19 - -require github.com/emissary-ingress/goversion v0.0.0-20220825220041-6870ba273e76 - -require ( - github.com/inconshreveable/mousetrap v1.0.0 // indirect - github.com/spf13/cobra v1.5.0 // indirect - github.com/spf13/pflag v1.0.5 // indirect - golang.org/x/mod v0.5.1 // indirect - golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 // indirect -) diff --git a/tools/src/goversion/go.sum b/tools/src/goversion/go.sum deleted file mode 100644 index b1054153db..0000000000 --- a/tools/src/goversion/go.sum +++ /dev/null @@ -1,16 +0,0 @@ -github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/emissary-ingress/goversion v0.0.0-20220825220041-6870ba273e76 h1:r5LJ+pvXBngyIBaL9u10pXNfd332eD5JfjR5duY8TEk= -github.com/emissary-ingress/goversion v0.0.0-20220825220041-6870ba273e76/go.mod h1:O3FGqM30w7Xc2n6cZg7mdZa6o+u1AsxEfcdnqKNFDic= -github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= -github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= -github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU= -github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM= -github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= -github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38= -golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/tools/src/goversion/pin.go b/tools/src/goversion/pin.go deleted file mode 100644 index 83faf7ad75..0000000000 --- a/tools/src/goversion/pin.go +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright Envoy Gateway Authors -// SPDX-License-Identifier: Apache-2.0 -// The full text of the Apache license is available in the LICENSE file at -// the root of the repo. - -//go:build pin -// +build pin - -package ignore - -import "github.com/emissary-ingress/goversion" From adf2c01f216a16c6879e0d4fa67f5a78c0f75f76 Mon Sep 17 00:00:00 2001 From: bitliu Date: Thu, 3 Nov 2022 11:01:46 +0800 Subject: [PATCH 2/2] update Signed-off-by: bitliu --- internal/cmd/version/version.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/internal/cmd/version/version.go b/internal/cmd/version/version.go index 8a38bb4dae..7315b27db0 100644 --- a/internal/cmd/version/version.go +++ b/internal/cmd/version/version.go @@ -1,3 +1,8 @@ +// Copyright Envoy Gateway Authors +// SPDX-License-Identifier: Apache-2.0 +// The full text of the Apache license is available in the LICENSE file at +// the root of the repo. + package version import (