diff --git a/aks-node-controller/app.go b/aks-node-controller/app.go index d82adbafbc1..781ea0358a5 100644 --- a/aks-node-controller/app.go +++ b/aks-node-controller/app.go @@ -97,6 +97,10 @@ type ProvisionStatusFiles struct { } func (a *App) Run(ctx context.Context, args []string) int { + if handled := handleInfoCommand(args); handled { + return 0 + } + slog.Info("aks-node-controller started", "args", args) err := a.run(ctx, args) exitCode := errToExitCode(err) @@ -108,6 +112,30 @@ func (a *App) Run(ctx context.Context, args []string) int { return exitCode } +// handleInfoCommand handles --version, version, --help, and help commands +// without logging noise. Returns true if handled. +func handleInfoCommand(args []string) bool { + if len(args) < 2 { + return false + } + switch args[1] { + case "--version", "version": + _, _ = fmt.Fprintln(os.Stdout, Version) + return true + case "--help", "help": + _, _ = fmt.Fprintln(os.Stdout, "Usage: aks-node-controller [options]") + _, _ = fmt.Fprintln(os.Stdout) + _, _ = fmt.Fprintln(os.Stdout, "Commands:") + _, _ = fmt.Fprintln(os.Stdout, " provision Run node provisioning") + _, _ = fmt.Fprintln(os.Stdout, " provision-wait Wait for provisioning to complete") + _, _ = fmt.Fprintln(os.Stdout, " version Print the version") + _, _ = fmt.Fprintln(os.Stdout, " help Print this help message") + return true + default: + return false + } +} + func (a *App) run(ctx context.Context, args []string) error { command := "" if len(args) >= 2 { @@ -117,12 +145,6 @@ func (a *App) run(ctx context.Context, args []string) error { return errors.New("missing command argument") } - if command == "--version" || command == "version" { - //nolint:forbidigo // stdout is part of the interface - fmt.Println(Version) - return nil - } - cmd, ok := getCommandRegistry()[command] if !ok { return fmt.Errorf("unknown command: %s", command) diff --git a/aks-node-controller/app_test.go b/aks-node-controller/app_test.go index dc80b711bc7..6915bd3356e 100644 --- a/aks-node-controller/app_test.go +++ b/aks-node-controller/app_test.go @@ -78,6 +78,18 @@ func TestApp_Run(t *testing.T) { assert.Equal(t, 0, exitCode) }) + t.Run("--help flag returns success exit code", func(t *testing.T) { + tt := NewTestApp(t, TestAppConfig{}) + exitCode := tt.App.Run(context.Background(), []string{"aks-node-controller", "--help"}) + assert.Equal(t, 0, exitCode) + }) + + t.Run("help command returns success exit code", func(t *testing.T) { + tt := NewTestApp(t, TestAppConfig{}) + exitCode := tt.App.Run(context.Background(), []string{"aks-node-controller", "help"}) + assert.Equal(t, 0, exitCode) + }) + t.Run("provision command with missing flag", func(t *testing.T) { tt := NewTestApp(t, TestAppConfig{}) exitCode := tt.App.Run(context.Background(), []string{"aks-node-controller", "provision"})