From b77d44af581c15a97e5c74f3b05e292a99c19f86 Mon Sep 17 00:00:00 2001 From: Debasish Biswas Date: Fri, 7 Nov 2025 14:14:54 +0530 Subject: [PATCH] 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 --- cmd/main.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 02de00b03a..2b45b75bb3 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -93,6 +93,21 @@ func main() { klog.ErrorS(err, "failed to validate and apply logging configuration") } + // If only the --version flag was requested, print version and exit before + // performing any initialization that may require contacting external + // services (IMDS/Kubernetes API). This allows `--version` to work in + // environments without metadata available (e.g., local/lab machines). + if *version { + versionInfo, versionErr := driver.GetVersionJSON() + if versionErr != nil { + klog.ErrorS(err, "failed to get version") + klog.FlushAndExit(klog.ExitFlushTimeout, 1) + } + //nolint:forbidigo // Print version info without klog/timestamp + fmt.Println(versionInfo) + os.Exit(0) + } + var cloud cloudPkg.Cloud var k8sClient kubernetes.Interface var md metadata.MetadataService @@ -160,17 +175,6 @@ func main() { klog.FlushAndExit(klog.ExitFlushTimeout, 0) } - if *version { - versionInfo, versionErr := driver.GetVersionJSON() - if versionErr != nil { - klog.ErrorS(err, "failed to get version") - klog.FlushAndExit(klog.ExitFlushTimeout, 1) - } - //nolint:forbidigo // Print version info without klog/timestamp - fmt.Println(versionInfo) - os.Exit(0) - } - if *toStderr { klog.SetOutput(os.Stderr) }