Skip to content

Commit b77d44a

Browse files
committed
cmd: handle --version before metadata initialization
Print the version and exit before any cloud or metadata setup so that invoking '--version' doesn't attempt IMDS or in-cluster Kubernetes API calls. Previously those initialization attempts could time out or fail in CI, local, or isolated build environments and cause '--version' to return a non-zero status. Normal startup behavior is unchanged when '--version' is not used. Signed-off-by: Debasish Biswas <[email protected]>
1 parent d8a0e8b commit b77d44a

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

cmd/main.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@ func main() {
9393
klog.ErrorS(err, "failed to validate and apply logging configuration")
9494
}
9595

96+
// If only the --version flag was requested, print version and exit before
97+
// performing any initialization that may require contacting external
98+
// services (IMDS/Kubernetes API). This allows `--version` to work in
99+
// environments without metadata available (e.g., local/lab machines).
100+
if *version {
101+
versionInfo, versionErr := driver.GetVersionJSON()
102+
if versionErr != nil {
103+
klog.ErrorS(err, "failed to get version")
104+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
105+
}
106+
//nolint:forbidigo // Print version info without klog/timestamp
107+
fmt.Println(versionInfo)
108+
os.Exit(0)
109+
}
110+
96111
var cloud cloudPkg.Cloud
97112
var k8sClient kubernetes.Interface
98113
var md metadata.MetadataService
@@ -160,17 +175,6 @@ func main() {
160175
klog.FlushAndExit(klog.ExitFlushTimeout, 0)
161176
}
162177

163-
if *version {
164-
versionInfo, versionErr := driver.GetVersionJSON()
165-
if versionErr != nil {
166-
klog.ErrorS(err, "failed to get version")
167-
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
168-
}
169-
//nolint:forbidigo // Print version info without klog/timestamp
170-
fmt.Println(versionInfo)
171-
os.Exit(0)
172-
}
173-
174178
if *toStderr {
175179
klog.SetOutput(os.Stderr)
176180
}

0 commit comments

Comments
 (0)