Skip to content

Commit 26578ac

Browse files
Merge pull request #7 from Kong/feat/version-command-and-handling
feat(build): add version metadata injection and display throughout the system
2 parents 0aa6767 + c5fd84c commit 26578ac

5 files changed

Lines changed: 18 additions & 7 deletions

File tree

.github/workflows/release.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
BINARY_BASE="migrator_${VERSION}_${GOOS}_${GOARCH}"
4747
4848
mkdir -p dist
49-
go build -o "dist/${BINARY_BASE}" ./main.go
49+
go build -ldflags="-X main.Version=${VERSION}" -o "dist/${BINARY_BASE}" ./main.go
5050
5151
(cd dist && tar -czf "${BINARY_BASE}.tar.gz" "${BINARY_BASE}")
5252
@@ -87,6 +87,7 @@ jobs:
8787
context: .
8888
platforms: linux/amd64,linux/arm64
8989
push: true
90+
build-args: VERSION=${{ github.ref_name }}
9091
tags: |
9192
kong/nginx-kong-migrator:${{ github.ref_name }}
9293
kong/nginx-kong-migrator:latest

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ WORKDIR /app
33
COPY go.mod go.sum ./
44
RUN go mod download
55
COPY . .
6-
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -o /migrator .
6+
ARG VERSION=dev
7+
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-X main.Version=${VERSION}" -o /migrator .
78

89
FROM gcr.io/distroless/static-debian12:nonroot
910
COPY --from=builder /migrator /migrator

main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,22 @@ import (
1212
"nginx-kong-migrator/pkg/ui"
1313
)
1414

15+
var Version = "dev"
16+
1517
func main() {
18+
if len(os.Args) > 1 && os.Args[1] == "version" {
19+
fmt.Println(Version)
20+
return
21+
}
22+
1623
// ui subcommand — spins up the local dashboard server
1724
if len(os.Args) > 1 && os.Args[1] == "ui" {
1825
uiFlags := flag.NewFlagSet("ui", flag.ExitOnError)
1926
port := uiFlags.Int("port", 8080, "Port to listen on")
2027
namespace := uiFlags.String("namespace", "", "Kubernetes namespace to watch (empty = all namespaces)")
2128
kubeconfig := uiFlags.String("kubeconfig", "", "Path to kubeconfig file (default: $KUBECONFIG or ~/.kube/config)")
2229
uiFlags.Parse(os.Args[2:])
23-
if err := ui.Start(*port, *namespace, *kubeconfig); err != nil {
30+
if err := ui.Start(*port, *namespace, *kubeconfig, Version); err != nil {
2431
log.Fatalf("ui server error: %v", err)
2532
}
2633
return

pkg/ui/server.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var templateFS embed.FS
3434

3535
type srv struct {
3636
namespace string
37+
version string
3738
client kubernetes.Interface
3839
dynClient dynamic.Interface
3940
}
@@ -52,7 +53,7 @@ var (
5253
)
5354

5455
// Start builds Kubernetes clients and starts the HTTP server on the given port.
55-
func Start(port int, namespace string, kubeconfig string) error {
56+
func Start(port int, namespace string, kubeconfig string, version string) error {
5657
config, err := buildConfig(kubeconfig)
5758
if err != nil {
5859
return fmt.Errorf("failed to build kubeconfig: %w", err)
@@ -68,7 +69,7 @@ func Start(port int, namespace string, kubeconfig string) error {
6869
return fmt.Errorf("failed to create dynamic client: %w", err)
6970
}
7071

71-
s := &srv{namespace: namespace, client: client, dynClient: dynClient}
72+
s := &srv{namespace: namespace, version: version, client: client, dynClient: dynClient}
7273

7374
mux := http.NewServeMux()
7475
mux.HandleFunc("/", s.handleIndex)
@@ -119,8 +120,9 @@ func (s *srv) handleIndex(w http.ResponseWriter, r *http.Request) {
119120
http.Error(w, "template not found", http.StatusInternalServerError)
120121
return
121122
}
123+
content := strings.ReplaceAll(string(data), "__APP_VERSION__", s.version)
122124
w.Header().Set("Content-Type", "text/html; charset=utf-8")
123-
w.Write(data)
125+
w.Write([]byte(content))
124126
}
125127

126128
func (s *srv) handleIngresses(w http.ResponseWriter, r *http.Request) {

pkg/ui/templates/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107

108108
<div class="mt-auto px-4 py-4 border-t border-sidebar-border text-xs text-sidebar-section">
109109
<span class="block mb-0.5 text-sidebar-text font-medium">NGINX → Kong Migrator</span>
110-
v0.1.0
110+
__APP_VERSION__
111111
</div>
112112
</aside>
113113

0 commit comments

Comments
 (0)