Skip to content

Commit 533d7f5

Browse files
authored
feat: add --version flag for webhook and proxy (#1629)
Signed-off-by: Anish Ramasekar <anish.ramasekar@gmail.com>
1 parent fabb122 commit 533d7f5

File tree

6 files changed

+123
-4
lines changed

6 files changed

+123
-4
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ IMAGE_VERSION ?= v1.5.1
66

77
ORG_PATH := github.com/Azure
88
PROJECT_NAME := azure-workload-identity
9-
BUILD_COMMIT := $(shell git rev-parse --short HEAD)
9+
BUILD_COMMIT := $(shell git describe --always --dirty --abbrev=7)
1010
REPO_PATH := $(ORG_PATH)/$(PROJECT_NAME)
1111

1212
# build variables

cmd/proxy/main.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ import (
1212
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
1313

1414
"github.com/Azure/azure-workload-identity/pkg/proxy"
15+
"github.com/Azure/azure-workload-identity/pkg/version"
1516
)
1617

1718
var (
18-
proxyPort int
19-
probe bool
20-
logLevel string
19+
proxyPort int
20+
probe bool
21+
logLevel string
22+
versionInfo bool
2123
)
2224

2325
func main() {
@@ -33,8 +35,13 @@ func mainErr() error {
3335
flag.BoolVar(&probe, "probe", false, "Run a readyz probe on the proxy")
3436
flag.StringVar(&logLevel, "log-level", "",
3537
"In order of increasing verbosity: unset (empty string), info, debug, trace and all.")
38+
flag.BoolVar(&versionInfo, "version", false, "Print version information and exit")
3639
flag.Parse()
3740

41+
if versionInfo {
42+
return version.PrintVersionToStdout()
43+
}
44+
3845
if err := mlog.ValidateAndSetLogLevelAndFormatGlobally(signals.SetupSignalHandler(), mlog.LogSpec{
3946
Level: mlog.LogLevel(logLevel),
4047
Format: mlog.FormatJSON,

cmd/webhook/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ var (
4848
disableCertRotation bool
4949
metricsBackend string
5050
logLevel string
51+
versionInfo bool
5152

5253
// DNSName is <service name>.<namespace>.svc
5354
dnsName = fmt.Sprintf("%s.%s.svc", serviceName, util.GetNamespace())
@@ -78,8 +79,13 @@ func mainErr() error {
7879
flag.StringVar(&metricsBackend, "metrics-backend", "prometheus", "Backend used for metrics")
7980
flag.StringVar(&logLevel, "log-level", "",
8081
"In order of increasing verbosity: unset (empty string), info, debug, trace and all.")
82+
flag.BoolVar(&versionInfo, "version", false, "Print version information and exit")
8183
flag.Parse()
8284

85+
if versionInfo {
86+
return version.PrintVersionToStdout()
87+
}
88+
8389
ctx := signals.SetupSignalHandler()
8490

8591
if err := mlog.ValidateAndSetLogLevelAndFormatGlobally(ctx, mlog.LogSpec{

pkg/version/version.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package version
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"io"
7+
"os"
58
"runtime"
69
)
710

@@ -18,3 +21,32 @@ var (
1821
func GetUserAgent(component string) string {
1922
return fmt.Sprintf("azure-workload-identity/%s/%s (%s/%s) %s/%s", component, BuildVersion, runtime.GOOS, runtime.GOARCH, Vcs, BuildTime)
2023
}
24+
25+
// PrintVersionToStdout prints the current driver version to stdout
26+
func PrintVersionToStdout() error {
27+
return printVersion(os.Stdout)
28+
}
29+
30+
func printVersion(w io.Writer) error {
31+
pv := struct {
32+
BuildVersion string `json:"buildVersion"`
33+
GitCommit string `json:"gitCommit"`
34+
BuildDate string `json:"buildDate"`
35+
GoVersion string `json:"goVersion"`
36+
Platform string `json:"platform"`
37+
}{
38+
BuildDate: BuildTime,
39+
BuildVersion: BuildVersion,
40+
GitCommit: Vcs,
41+
GoVersion: runtime.Version(),
42+
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
43+
}
44+
45+
res, err := json.Marshal(pv)
46+
if err != nil {
47+
return err
48+
}
49+
50+
_, err = fmt.Fprintf(w, "%s\n", res)
51+
return err
52+
}

pkg/version/version_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package version
22

33
import (
4+
"bytes"
45
"fmt"
56
"runtime"
67
"strings"
@@ -18,3 +19,21 @@ func TestGetUserAgent(t *testing.T) {
1819
t.Fatalf("expected: %s, got: %s", expected, actual)
1920
}
2021
}
22+
23+
func TestPrintVersion(t *testing.T) {
24+
BuildTime = "Now"
25+
BuildVersion = "version"
26+
Vcs = "hash"
27+
28+
var buf bytes.Buffer
29+
err := printVersion(&buf)
30+
if err != nil {
31+
t.Fatalf("PrintVersion() failed: %v", err)
32+
}
33+
34+
out := strings.TrimSpace(buf.String())
35+
expected := fmt.Sprintf(`{"buildVersion":"version","gitCommit":"hash","buildDate":"Now","goVersion":"%s","platform":"%s/%s"}`, runtime.Version(), runtime.GOOS, runtime.GOARCH)
36+
if out != expected {
37+
t.Fatalf("expected %q, got %q", expected, out)
38+
}
39+
}

scripts/ci-e2e.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ main() {
7979
make uninstall-deploy
8080
test_helm_chart
8181
fi
82+
83+
test_version_flag "webhook"
84+
test_version_flag "proxy"
8285
}
8386

8487
test_helm_chart() {
@@ -115,4 +118,56 @@ poll_webhook_readiness() {
115118
${KUBECTL} wait --for=condition=available --timeout=5m deployment/azure-wi-webhook-controller-manager -n azure-workload-identity-system
116119
}
117120

121+
test_version_flag() {
122+
echo "Testing version flag for $1 image..."
123+
local image_name="$1"
124+
message=$(docker run --platform linux/amd64 --quiet "${REGISTRY:-mcr.microsoft.com/oss/azure/workload-identity}/${image_name}:${IMAGE_VERSION}" --version 2>&1)
125+
126+
# Validate that it's valid JSON
127+
if ! echo "$message" | jq empty > /dev/null 2>&1; then
128+
echo "Invalid JSON output: $message"
129+
return 1
130+
fi
131+
132+
# Extract and validate fields
133+
build_version=$(echo "$message" | jq -r .buildVersion)
134+
git_commit=$(echo "$message" | jq -r .gitCommit)
135+
build_date=$(echo "$message" | jq -r .buildDate)
136+
go_version=$(echo "$message" | jq -r .goVersion)
137+
platform=$(echo "$message" | jq -r .platform)
138+
139+
if [[ -z "$build_version" || "$build_version" == "null" ]]; then
140+
echo "Missing BuildVersion"
141+
return 1
142+
fi
143+
144+
if [[ "$build_version" != "$IMAGE_VERSION" ]]; then
145+
echo "BuildVersion does not match IMAGE_VERSION: $build_version != $IMAGE_VERSION"
146+
return 1
147+
fi
148+
149+
if [[ -z "$git_commit" || "$git_commit" == "null" ]]; then
150+
echo "Missing GitCommit"
151+
return 1
152+
fi
153+
154+
if [[ -z "$build_date" || "$build_date" == "null" ]]; then
155+
echo "Missing BuildDate"
156+
return 1
157+
fi
158+
159+
if [[ -z "$go_version" || "$go_version" == "null" ]]; then
160+
echo "Missing GoVersion"
161+
return 1
162+
fi
163+
164+
if [[ -z "$platform" || "$platform" == "null" ]]; then
165+
echo "Missing Platform"
166+
return 1
167+
fi
168+
169+
echo "Version info looks good:"
170+
echo "$message"
171+
}
172+
118173
main

0 commit comments

Comments
 (0)