Skip to content

Commit 505717f

Browse files
authored
Refactor CurrentVersion to use the git version string (#217)
* Previously the git version string was plumbed to cmd.Version via the linker and i2gw had a hardcoded version * Now the linker propogates the version string to i2gw and the cmd package imports it from there * This has a few effects: 1. You no longer need to manually update i2gw.CurrentVersion whenever there is a release. Simply tagging the release will work. 2. The generator annotations (gateway.networking.k8s.io/generator) will now use the git version string instead instead of just the hardcoded value. This means that during local development you will see something like "ingress2gateway-v0.4.0-5-gabcdef-dirty" in your generated annotations, letting you know exactly what local commit you were using when you generated that Gateway.
1 parent 9219873 commit 505717f

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
# Enable Go modules.
1919
export GO111MODULE=on
2020

21-
# CMDPKG is the package path for cmd. This allows us to propogate the git info
21+
# I2GWPKG is the package path for i2gw. This allows us to propogate the git info
2222
# to the binary via LDFLAGS.
23-
CMDPKG := $(shell go list .)/cmd
23+
I2GWPKG := $(shell go list .)/pkg/i2gw
2424

2525
# Get the version string from git describe.
2626
# --tags: Use annotated tags.
@@ -29,7 +29,7 @@ CMDPKG := $(shell go list .)/cmd
2929
GIT_VERSION_STRING := $(shell git describe --tags --always --dirty 2>/dev/null)
3030

3131
# Construct the LDFLAGS string to inject the version
32-
LDFLAGS := -ldflags="-X '$(CMDPKG).Version=$(GIT_VERSION_STRING)'"
32+
LDFLAGS := -ldflags="-X '$(I2GWPKG).Version=$(GIT_VERSION_STRING)'"
3333

3434
# Print the help menu.
3535
.PHONY: help

cmd/print.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (pr *PrintRunner) outputResult(gatewayResources []i2gw.GatewayResources) {
123123
if gateway.Annotations == nil {
124124
gateway.Annotations = make(map[string]string)
125125
}
126-
gateway.Annotations[i2gw.GeneratorAnnotationKey] = fmt.Sprintf("ingress2gateway-%s", i2gw.CurrentVersion)
126+
gateway.Annotations[i2gw.GeneratorAnnotationKey] = fmt.Sprintf("ingress2gateway-%s", i2gw.Version)
127127
err := pr.resourcePrinter.PrintObj(&gateway, os.Stdout)
128128
if err != nil {
129129
fmt.Printf("# Error printing %s Gateway: %v\n", gateway.Name, err)
@@ -138,7 +138,7 @@ func (pr *PrintRunner) outputResult(gatewayResources []i2gw.GatewayResources) {
138138
if httpRoute.Annotations == nil {
139139
httpRoute.Annotations = make(map[string]string)
140140
}
141-
httpRoute.Annotations[i2gw.GeneratorAnnotationKey] = fmt.Sprintf("ingress2gateway-%s", i2gw.CurrentVersion)
141+
httpRoute.Annotations[i2gw.GeneratorAnnotationKey] = fmt.Sprintf("ingress2gateway-%s", i2gw.Version)
142142
err := pr.resourcePrinter.PrintObj(&httpRoute, os.Stdout)
143143
if err != nil {
144144
fmt.Printf("# Error printing %s HTTPRoute: %v\n", httpRoute.Name, err)
@@ -153,7 +153,7 @@ func (pr *PrintRunner) outputResult(gatewayResources []i2gw.GatewayResources) {
153153
if tlsRoute.Annotations == nil {
154154
tlsRoute.Annotations = make(map[string]string)
155155
}
156-
tlsRoute.Annotations[i2gw.GeneratorAnnotationKey] = fmt.Sprintf("ingress2gateway-%s", i2gw.CurrentVersion)
156+
tlsRoute.Annotations[i2gw.GeneratorAnnotationKey] = fmt.Sprintf("ingress2gateway-%s", i2gw.Version)
157157
err := pr.resourcePrinter.PrintObj(&tlsRoute, os.Stdout)
158158
if err != nil {
159159
fmt.Printf("# Error printing %s TLSRoute: %v\n", tlsRoute.Name, err)
@@ -168,7 +168,7 @@ func (pr *PrintRunner) outputResult(gatewayResources []i2gw.GatewayResources) {
168168
if tcpRoute.Annotations == nil {
169169
tcpRoute.Annotations = make(map[string]string)
170170
}
171-
tcpRoute.Annotations[i2gw.GeneratorAnnotationKey] = fmt.Sprintf("ingress2gateway-%s", i2gw.CurrentVersion)
171+
tcpRoute.Annotations[i2gw.GeneratorAnnotationKey] = fmt.Sprintf("ingress2gateway-%s", i2gw.Version)
172172
err := pr.resourcePrinter.PrintObj(&tcpRoute, os.Stdout)
173173
if err != nil {
174174
fmt.Printf("# Error printing %s TCPRoute: %v\n", tcpRoute.Name, err)
@@ -183,7 +183,7 @@ func (pr *PrintRunner) outputResult(gatewayResources []i2gw.GatewayResources) {
183183
if udpRoute.Annotations == nil {
184184
udpRoute.Annotations = make(map[string]string)
185185
}
186-
udpRoute.Annotations[i2gw.GeneratorAnnotationKey] = fmt.Sprintf("ingress2gateway-%s", i2gw.CurrentVersion)
186+
udpRoute.Annotations[i2gw.GeneratorAnnotationKey] = fmt.Sprintf("ingress2gateway-%s", i2gw.Version)
187187
err := pr.resourcePrinter.PrintObj(&udpRoute, os.Stdout)
188188
if err != nil {
189189
fmt.Printf("# Error printing %s UDPRoute: %v\n", udpRoute.Name, err)
@@ -198,7 +198,7 @@ func (pr *PrintRunner) outputResult(gatewayResources []i2gw.GatewayResources) {
198198
if referenceGrant.Annotations == nil {
199199
referenceGrant.Annotations = make(map[string]string)
200200
}
201-
referenceGrant.Annotations[i2gw.GeneratorAnnotationKey] = fmt.Sprintf("ingress2gateway-%s", i2gw.CurrentVersion)
201+
referenceGrant.Annotations[i2gw.GeneratorAnnotationKey] = fmt.Sprintf("ingress2gateway-%s", i2gw.Version)
202202
err := pr.resourcePrinter.PrintObj(&referenceGrant, os.Stdout)
203203
if err != nil {
204204
fmt.Printf("# Error printing %s ReferenceGrant: %v\n", referenceGrant.Name, err)

cmd/version.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,10 @@ import (
2020
"fmt"
2121
"runtime/debug"
2222

23+
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
2324
"github.com/spf13/cobra"
2425
)
2526

26-
// Version holds the version string (injected by ldflags during build).
27-
// It will be populated by `git describe --tags --always --dirty`.
28-
// Examples: "v0.4.0", "v0.4.0-5-gabcdef", "v0.4.0-5-gabcdef-dirty"
29-
var Version = "dev" // Default value if not built with linker flags
30-
3127
// versionCmd represents the version command
3228
var versionCmd = &cobra.Command{
3329
Use: "version",
@@ -40,7 +36,7 @@ var versionCmd = &cobra.Command{
4036

4137
// printVersion formats and prints the version information.
4238
func printVersion() {
43-
fmt.Printf("ingress2gateway version: %s\n", Version)
39+
fmt.Printf("ingress2gateway version: %s\n", i2gw.Version)
4440

4541
// Print the golang version if it's available
4642
buildInfo, ok := debug.ReadBuildInfo()

pkg/i2gw/ingress2gateway.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ import (
3030

3131
const GeneratorAnnotationKey = "gateway.networking.k8s.io/generator"
3232

33-
var CurrentVersion = "0.4.0"
33+
// Version holds the version string (injected by ldflags during build).
34+
// It will be populated by `git describe --tags --always --dirty`.
35+
// Examples: "v0.4.0", "v0.4.0-5-gabcdef", "v0.4.0-5-gabcdef-dirty"
36+
var Version = "dev" // Default value if not built with linker flags
3437

3538
func ToGatewayAPIResources(ctx context.Context, namespace string, inputFile string, providers []string, providerSpecificFlags map[string]map[string]string) ([]GatewayResources, map[string]string, error) {
3639
var clusterClient client.Client

0 commit comments

Comments
 (0)