diff --git a/components/egress/Dockerfile b/components/egress/Dockerfile index cc2c33c4..ca364106 100644 --- a/components/egress/Dockerfile +++ b/components/egress/Dockerfile @@ -16,6 +16,10 @@ FROM golang:1.24-bookworm AS builder WORKDIR /workspace +ARG VERSION=dev +ARG GIT_COMMIT=unknown +ARG BUILD_TIME=unknown + # Copy only go mod/sum first for better caching COPY components/egress/go.mod components/egress/go.sum ./components/egress/ # Bring internal module so replace ../internal works during download/build @@ -28,8 +32,12 @@ ENV CGO_ENABLED=0 RUN go mod download # Copy the rest of the egress sources -COPY components/egress ./ -RUN go build -o /out/egress . +COPY components/egress ./ +RUN CGO_ENABLED=0 go build \ + -ldflags "-X 'github.com/alibaba/opensandbox/internal/version.Version=${VERSION}' \ + -X 'github.com/alibaba/opensandbox/internal/version.BuildTime=${BUILD_TIME}' \ + -X 'github.com/alibaba/opensandbox/internal/version.GitCommit=${GIT_COMMIT}'" \ + -o /out/egress . FROM debian:bookworm-slim diff --git a/components/egress/build.sh b/components/egress/build.sh index 23434fd4..6f5bd4d5 100755 --- a/components/egress/build.sh +++ b/components/egress/build.sh @@ -16,6 +16,9 @@ set -ex TAG=${TAG:-latest} +VERSION=${VERSION:-$(git describe --tags --always --dirty 2>/dev/null || echo "dev")} +GIT_COMMIT=${GIT_COMMIT:-$(git rev-parse HEAD 2>/dev/null || echo "unknown")} +BUILD_TIME=${BUILD_TIME:-$(date -u +"%Y-%m-%dT%H:%M:%SZ")} REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null || realpath "$(dirname "$0")/../..") cd "${REPO_ROOT}" @@ -28,6 +31,9 @@ docker buildx build \ -t opensandbox/egress:${TAG} \ -t sandbox-registry.cn-zhangjiakou.cr.aliyuncs.com/opensandbox/egress:${TAG} \ -f components/egress/Dockerfile \ + --build-arg VERSION="${VERSION}" \ + --build-arg GIT_COMMIT="${GIT_COMMIT}" \ + --build-arg BUILD_TIME="${BUILD_TIME}" \ --platform linux/amd64,linux/arm64 \ --push \ . diff --git a/components/egress/main.go b/components/egress/main.go index ff46dc1c..4df0c8f7 100644 --- a/components/egress/main.go +++ b/components/egress/main.go @@ -26,9 +26,12 @@ import ( "github.com/alibaba/opensandbox/egress/pkg/iptables" "github.com/alibaba/opensandbox/egress/pkg/log" slogger "github.com/alibaba/opensandbox/internal/logger" + "github.com/alibaba/opensandbox/internal/version" ) func main() { + version.EchoVersion("OpenSandbox Egress") + ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer cancel() diff --git a/components/execd/Dockerfile b/components/execd/Dockerfile index 0e348ec0..98a3f052 100644 --- a/components/execd/Dockerfile +++ b/components/execd/Dockerfile @@ -16,6 +16,10 @@ FROM golang:1.24.0 AS builder WORKDIR /build +ARG VERSION=dev +ARG GIT_COMMIT=unknown +ARG BUILD_TIME=unknown + # Prepare local modules to satisfy replace directives. COPY components/internal/go.mod components/internal/go.sum ./components/internal/ COPY components/execd/go.mod components/execd/go.sum ./components/execd/ @@ -30,7 +34,11 @@ COPY components/execd ./components/execd WORKDIR /build/components/execd -RUN CGO_ENABLED=0 go build -o /build/execd ./main.go +RUN CGO_ENABLED=0 go build \ + -ldflags "-X 'github.com/alibaba/opensandbox/internal/version.Version=${VERSION}' \ + -X 'github.com/alibaba/opensandbox/internal/version.BuildTime=${BUILD_TIME}' \ + -X 'github.com/alibaba/opensandbox/internal/version.GitCommit=${GIT_COMMIT}'" \ + -o /build/execd ./main.go FROM alpine:latest diff --git a/components/execd/Makefile b/components/execd/Makefile index 66cc590a..d3caee63 100644 --- a/components/execd/Makefile +++ b/components/execd/Makefile @@ -26,9 +26,17 @@ install-golint: golint: fmt install-golint golangci-lint run -v --fix ./... +VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev") +GIT_COMMIT ?= $(shell git rev-parse HEAD 2>/dev/null || echo "unknown") +BUILD_TIME ?= $(shell date -u +"%Y-%m-%dT%H:%M:%SZ") +LDFLAGS := -X 'github.com/alibaba/opensandbox/internal/version.Version=$(VERSION)' \ + -X 'github.com/alibaba/opensandbox/internal/version.BuildTime=$(BUILD_TIME)' \ + -X 'github.com/alibaba/opensandbox/internal/version.GitCommit=$(GIT_COMMIT)' + .PHONY: build build: vet ## Build the binary. - go build -o bin/execd main.go + @mkdir -p bin + go build -ldflags "$(LDFLAGS)" -o bin/execd main.go .PHONY: multi-build multi-build: vet ## Cross-compile for linux/windows/darwin amd64/arm64. @@ -38,6 +46,6 @@ multi-build: vet ## Cross-compile for linux/windows/darwin amd64/arm64. out=bin/execd-$${os}-$${arch}; \ [ "$${os}" = "windows" ] && out="$${out}.exe"; \ echo ">> building $${os}/$${arch} -> $${out}"; \ - GOOS=$${os} GOARCH=$${arch} CGO_ENABLED=0 go build -o "$${out}" main.go || exit $$?; \ + GOOS=$${os} GOARCH=$${arch} CGO_ENABLED=0 go build -ldflags "$(LDFLAGS)" -o "$${out}" main.go || exit $$?; \ done; \ done diff --git a/components/execd/bootstrap.sh b/components/execd/bootstrap.sh index ce5c4272..a820ac26 100755 --- a/components/execd/bootstrap.sh +++ b/components/execd/bootstrap.sh @@ -30,7 +30,7 @@ if ! touch "$EXECD_ENVS" 2>/dev/null; then fi export EXECD_ENVS -echo "starting OpenSandbox execd daemon at $EXECD with version v1.0.6. https://github.com/alibaba/OpenSandbox/releases/tag/docker%2Fexecd%2Fv1.0.6" +echo "starting OpenSandbox Execd daemon at $EXECD." $EXECD & # Allow chained shell commands (e.g., /test1.sh && /test2.sh) diff --git a/components/execd/build.sh b/components/execd/build.sh index e80f5a78..f7f9d6ec 100755 --- a/components/execd/build.sh +++ b/components/execd/build.sh @@ -16,8 +16,12 @@ set -ex TAG=${TAG:-latest} +VERSION=${VERSION:-$(git describe --tags --always --dirty 2>/dev/null || echo "dev")} +GIT_COMMIT=${GIT_COMMIT:-$(git rev-parse HEAD 2>/dev/null || echo "unknown")} +BUILD_TIME=${BUILD_TIME:-$(date -u +"%Y-%m-%dT%H:%M:%SZ")} -REPO_ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null || realpath "$(dirname "$0")/../..") +cd "${REPO_ROOT}" docker buildx rm execd-builder || true @@ -30,7 +34,10 @@ docker buildx ls docker buildx build \ -t opensandbox/execd:${TAG} \ -t sandbox-registry.cn-zhangjiakou.cr.aliyuncs.com/opensandbox/execd:${TAG} \ + -f components/execd/Dockerfile \ + --build-arg VERSION="${VERSION}" \ + --build-arg GIT_COMMIT="${GIT_COMMIT}" \ + --build-arg BUILD_TIME="${BUILD_TIME}" \ --platform linux/amd64,linux/arm64 \ --push \ - -f components/execd/Dockerfile \ - "${REPO_ROOT}" + . diff --git a/components/execd/main.go b/components/execd/main.go index a9564505..8e09e90f 100644 --- a/components/execd/main.go +++ b/components/execd/main.go @@ -24,10 +24,13 @@ import ( _ "github.com/alibaba/opensandbox/execd/pkg/util/safego" "github.com/alibaba/opensandbox/execd/pkg/web" "github.com/alibaba/opensandbox/execd/pkg/web/controller" + "github.com/alibaba/opensandbox/internal/version" ) // main initializes and starts the execd server. func main() { + version.EchoVersion("OpenSandbox Execd") + flag.InitFlags() log.Init(flag.ServerLogLevel) diff --git a/components/ingress/Dockerfile b/components/ingress/Dockerfile index c43219d1..ed160bf6 100644 --- a/components/ingress/Dockerfile +++ b/components/ingress/Dockerfile @@ -37,9 +37,9 @@ COPY components/ingress/. ./components/ingress WORKDIR /build/components/ingress RUN CGO_ENABLED=0 go build \ - -ldflags "-X 'github.com/alibaba/opensandbox/ingress/version.Version=${VERSION}' \ - -X 'github.com/alibaba/opensandbox/ingress/version.BuildTime=${BUILD_TIME}' \ - -X 'github.com/alibaba/opensandbox/ingress/version.GitCommit=${GIT_COMMIT}'" \ + -ldflags "-X 'github.com/alibaba/opensandbox/internal/version.Version=${VERSION}' \ + -X 'github.com/alibaba/opensandbox/internal/version.BuildTime=${BUILD_TIME}' \ + -X 'github.com/alibaba/opensandbox/internal/version.GitCommit=${GIT_COMMIT}'" \ -o /build/ingress ./main.go FROM alpine:latest diff --git a/components/ingress/Makefile b/components/ingress/Makefile index 7488c7cb..f9b32aff 100644 --- a/components/ingress/Makefile +++ b/components/ingress/Makefile @@ -29,9 +29,9 @@ golint: fmt install-golint VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev") GIT_COMMIT ?= $(shell git rev-parse HEAD 2>/dev/null || echo "unknown") BUILD_TIME ?= $(shell date -u +"%Y-%m-%dT%H:%M:%SZ") -LDFLAGS := -X 'github.com/alibaba/opensandbox/router/version.Version=$(VERSION)' \ - -X 'github.com/alibaba/opensandbox/router/version.BuildTime=$(BUILD_TIME)' \ - -X 'github.com/alibaba/opensandbox/router/version.GitCommit=$(GIT_COMMIT)' +LDFLAGS := -X 'github.com/alibaba/opensandbox/internal/version.Version=$(VERSION)' \ + -X 'github.com/alibaba/opensandbox/internal/version.BuildTime=$(BUILD_TIME)' \ + -X 'github.com/alibaba/opensandbox/internal/version.GitCommit=$(GIT_COMMIT)' .PHONY: build build: vet ## Build the binary. diff --git a/components/ingress/main.go b/components/ingress/main.go index f1a5c2c4..4f649c57 100644 --- a/components/ingress/main.go +++ b/components/ingress/main.go @@ -28,12 +28,12 @@ import ( "github.com/alibaba/opensandbox/ingress/pkg/flag" "github.com/alibaba/opensandbox/ingress/pkg/proxy" "github.com/alibaba/opensandbox/ingress/pkg/sandbox" - "github.com/alibaba/opensandbox/ingress/version" slogger "github.com/alibaba/opensandbox/internal/logger" + "github.com/alibaba/opensandbox/internal/version" ) func main() { - version.EchoVersion() + version.EchoVersion("OpenSandbox Ingress") flag.InitFlags() if flag.Namespace == "" { diff --git a/components/ingress/version/version.go b/components/internal/version/version.go similarity index 68% rename from components/ingress/version/version.go rename to components/internal/version/version.go index 63239dbe..ac919c5c 100644 --- a/components/ingress/version/version.go +++ b/components/internal/version/version.go @@ -1,4 +1,4 @@ -// Copyright 2025 Alibaba Group Holding Ltd. +// Copyright 2026 Alibaba Group Holding Ltd. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -19,22 +19,21 @@ import ( "runtime" ) -// Version package values is auto-generated, the following values will be overridden at build time. +// Package values are typically overridden at build time via -ldflags. var ( - // Version represents the version of taskline suite. - Version = "1.0.0" - - // BuildTime is the time when taskline-operator binary is built + // Version is the component version. + Version = "dirty" + // BuildTime is when the binary was built. BuildTime = "assigned-at-build-time" - - // GitCommit is the commit id to build taskline-operator + // GitCommit is the commit id used to build the binary. GitCommit = "assigned-at-build-time" ) -// EchoVersion is used to echo current binary build info for diagnosing -func EchoVersion() { +// EchoVersion prints build info for the given component name (e.g. "OpenSandbox Ingress", "OpenSandbox Execd"). +// All components can use this by passing their display name. +func EchoVersion(componentName string) { fmt.Println("=====================================================") - fmt.Println(" OpenSandbox Ingress") + fmt.Printf(" %s\n", componentName) fmt.Println("-----------------------------------------------------") fmt.Printf(" Version : %s\n", Version) fmt.Printf(" Git Commit : %s\n", GitCommit)