Skip to content

[AKS] az aks command invoke/result: Add --propagate-exit-code to exit with the in-cluster command's exit code - #34498

Open
Sai Kumar Pola (alopsing) wants to merge 2 commits into
Azure:devfrom
alopsing:aks-command-invoke-exit-code
Open

Sai Kumar Pola (alopsing) wants to merge 2 commits into
Azure:devfrom
alopsing:aks-command-invoke-exit-code

Conversation

@alopsing

@alopsing Sai Kumar Pola (alopsing) commented Nov 27, 2026 •

Copy link
Copy Markdown

🤖 PR Validation — ⚠️ Review suggested

Breaking Changes Tests Suppressions
⚠️ None ️✔️ 130/130 ️✔️ None
⚠️AzureCLI-BreakingChangeTest
⚠️acs
rule cmd_name rule_message suggest_message
⚠️ 1006 - ParaAdd aks command invoke cmd aks command invoke added parameter propagate_exit_code
⚠️ 1006 - ParaAdd aks command result cmd aks command result added parameter propagate_exit_code

Related command

az aks command invoke, az aks command result

Description

Fixes #34496.

az aks command invoke and az aks command result print the exit code of the command that ran in the cluster, but always exit 0 themselves. A script or CI pipeline cannot detect a failed command without parsing the output.

This PR adds an opt-in flag, --propagate-exit-code, to both commands. It is off by default, so existing behavior does not change.

  • Without the flag: exit 0, as today.
  • With the flag: the CLI exits with the in-cluster command's exit code. If the run command itself fails (provisioning state Failed) and there is no exit code from inside the cluster, the CLI exits 1.
  • The flag works independently of -o/--output. The result is rendered in the requested format first, and the CLI exits with the code after that. This uses the Cli.SuccessfulExecute event, which the CLI raises once the output has been written.
  • --no-wait stays non-blocking and ignores the flag. Pass the flag to az aks command result to get the exit code of the finished command.
  • A command that is still running has no exit code, so az aks command result --propagate-exit-code exits 0 for it.

The first commit changed the default behavior. The second commit replaces that with the flag, as requested in review.

Example:

$ az aks command invoke -g my-rg -n my-cluster --command "kubectl get pod does-not-exist" --propagate-exit-code
command started at 2026-11-25T04:10:11Z, finished at 2026-11-25T04:10:13Z with exitcode=1
Error from server (NotFound): pods "does-not-exist" not found
$ echo $?
1

$ az aks command invoke -g my-rg -n my-cluster --command "kubectl get pod does-not-exist" --propagate-exit-code -o json
{ ... "exitCode": 1, ... }
$ echo $?
1

Testing Guide

Unit tests in acs/tests/latest/test_custom.py (TestRunCommand):

  • Flag absent, invoke and result: no exit for a failed command or a failed provisioning, with and without --output.
  • Flag present: exit code 0 gives a normal exit; exit code 3 gives 3; provisioning Failed without an exit code gives 1; provisioning Failed with an exit code gives that code.
  • Flag present with -o and --output: the result object is still returned for rendering, and the exit code is propagated.
  • Flag present with --no-wait, and with a command still running: normal exit.

I ran these tests against the changed functions locally, and confirmed on a released az build that a handler on Cli.SuccessfulExecute runs after the output is written and sets the process exit code.

Manual check against a live AKS cluster (private cluster, managed Microsoft Entra ID), with a build of this branch:

Command Flag Output CLI exit code
invoke, command fails with 1 no text 0 (unchanged)
invoke, command fails with 1 yes text 1
invoke, command fails with 1 yes -o json, rendered in full 1
invoke, command succeeds yes text 0
invoke --no-wait, command fails with 1 yes command id 0
result for that command id yes text 1
result for that command id no text 0 (unchanged)
result for that command id yes -o tsv --query exitCode 1

Not covered by the manual check: a run command whose provisioning fails. That case is covered by the unit tests only.

This PR merges cleanly with #34499. One interaction to note if both merge: #34499 returns the complete result when a command finishes between the two requests that result makes. That path does not propagate the exit code yet. I can cover it in whichever PR merges second.

History Notes

[AKS] az aks command invoke: Add --propagate-exit-code to exit with the exit code of the command that ran in the cluster
[AKS] az aks command result: Add --propagate-exit-code to exit with the exit code of the command that ran in the cluster


This checklist is used to make sure that common guidelines for a pull request are followed.

… command's exit code

`az aks command invoke` and `az aks command result` printed the exit code
of the command that ran in the cluster, but always exited 0 themselves.
Scripts and CI pipelines could not detect a failed command without parsing
the output.

Exit with the in-cluster command's exit code when it is non-zero, and exit
1 when the run command itself fails (provisioning state "Failed").
Behavior with an explicit -o/--output is unchanged: the result object is
still returned.
Copilot AI balanced review requested due to automatic review settings November 27, 2026 04:26
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@microsoft-github-policy-service microsoft-github-policy-service Bot added the customer-reported Issues that are reported by GitHub users external to the Azure organization. label Nov 27, 2026
@microsoft-github-policy-service

Copy link
Copy Markdown
Contributor

Thank you for your contribution Sai Kumar Pola (@alopsing)! We will review the pull request and get back to you soon.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

A maintainer must decide whether changing the default exit statuses of both AKS commands requires an opt-in flag or breaking-change treatment because existing scripts may be affected.

No open issues 🎉

What changed in this PR

Fixes #34496 by making az aks command invoke and az aks command result report failures through their process exit status, allowing scripts and CI jobs to detect them without parsing output.

Changes:

  • Propagates nonzero in-cluster exit codes and exits with status 1 when provisioning fails.
  • Preserves existing behavior for successful commands, commands still running, and explicit output formats.
  • Adds unit tests for failure propagation and unchanged behavior.
File Description
src/​azure-cli/​azure/​cli/​command_modules/​acs/​tests/​latest/​test_custom.py Adds five tests covering exit statuses and preserved result handling.
src/​azure-cli/​azure/​cli/​command_modules/​acs/​custom.py Updates the shared result printer to exit nonzero on command failures.

Review effort: Balanced

@alopsing

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@yonzhan

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).

@FumingZhang

Copy link
Copy Markdown
Member

Thanks for addressing this issue. Changing the default exit status of existing commands can break scripts and CI workflows. Please make this behavior opt-in via a boolean --propagate-exit-code flag, disabled by default, on both az aks command invoke and az aks command result.

  • Without the flag, preserve the existing behavior.
  • With the flag, propagate the in-cluster command’s exit code independently of -o/--output, while preserving the requested output format. If Run Command provisioning fails without an inner exit code, return 1.
  • Keep --no-wait nonblocking; users can propagate the final exit code when retrieving the completed result.

Please add focused tests for flag presence/absence, successful and failed commands, provisioning failures, and explicit output formats. This would provide reliable failure detection without introducing a default-behavior breaking change.

Address review feedback: changing the default exit status could break
existing scripts and CI workflows.

- Without the flag, `az aks command invoke` and `az aks command result`
  exit 0 as before.
- With `--propagate-exit-code`, the CLI exits with the in-cluster
  command's exit code, or 1 when the run command fails without one. The
  exit happens after the output is rendered, so it works with any
  -o/--output format.
- `--no-wait` and a command that is still running are unaffected.
@alopsing Sai Kumar Pola (alopsing) changed the title [AKS] Fix #34496: az aks command invoke: Return the in-cluster command's exit code [AKS] az aks command invoke/result: Add --propagate-exit-code to exit with the in-cluster command's exit code Nov 30, 2026
@alopsing

Copy link
Copy Markdown
Author

Thanks for the review FumingZhang. Agreed, an opt-in flag is the safer change. I pushed a second commit that does this:

  • --propagate-exit-code on both az aks command invoke and az aks command result, off by default. Without it, both commands behave as before.
  • With the flag, the CLI exits with the in-cluster command's exit code, whatever -o/--output says. The result is rendered in the requested format first and the exit follows, using the Cli.SuccessfulExecute event. If provisioning fails without an inner exit code, the exit code is 1.
  • --no-wait is unchanged and non-blocking. The flag on az aks command result gives the exit code of the finished command; for a command that is still running it exits 0.

Tests cover flag present and absent, successful and failed commands, provisioning failures with and without an exit code, -o and --output, --no-wait, and a command still running.

I also ran both commands with and without the flag against a live cluster, including -o json and --no-wait followed by result; the exit codes are in the description. A provisioning failure is covered by unit tests only.

I updated the PR title and description to match.

@FumingZhang

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).

exit_code = exit_code or 1
if exit_code:
from knack.events import EVENT_CLI_SUCCESSFUL_EXECUTE
cli_ctx.register_event(EVENT_CLI_SUCCESSFUL_EXECUTE, lambda *_, **__: sys.exit(exit_code))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking robustness suggestion: The Cli.SuccessfulExecute callback remains registered after execution. If Python code catches SystemExit and reuses the same CLI instance, subsequent successful commands—even without --propagate-exit-code—can exit with the previous command’s code. This does not affect normal usage through separate az processes.

Consider making the callback invocation-scoped: unregister it before exiting, with Cli.PostExecute cleanup for cases where output rendering fails before the success event. A same-instance regression test would help cover this.


def aks_runcommand(cmd, client, resource_group_name, name, command_string="", command_files=None, no_wait=False):
def aks_runcommand(cmd, client, resource_group_name, name, command_string="", command_files=None, no_wait=False,
propagate_exit_code=False):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking style nit: Consider placing propagate_exit_code=False before no_wait=False in the handler signature, following the common convention of keeping no_wait last. This is purely a consistency suggestion, not a framework requirement; CLI dispatch uses keyword arguments, so the current order works. If reordering would affect existing positional callers, preserving compatibility takes precedence.

@alopsing

Copy link
Copy Markdown
Author

Thanks for the review FumingZhang and for running CI. Both suggestions make sense. Since they are non-blocking, I'd rather not hold this PR for another CI run: I can send a small follow-up that scopes the callback to one invocation (with a same-instance regression test) and moves propagate_exit_code before no_wait. If you would prefer them in this PR, say so and I will push here.

@FumingZhang

Copy link
Copy Markdown
Member

Hi Sai Kumar Pola (@alopsing), that sounds good! Both suggestions can be addressed in a follow-up PR. No need to hold this PR for these non-blocking changes. Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

act-observability-squad AKS az aks/acs/openshift Auto-Assign Auto assign by bot customer-reported Issues that are reported by GitHub users external to the Azure organization.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

az aks command invoke exits 0 when the command run in the cluster fails

6 participants