Repository navigation
[AKS] az aks command invoke/result: Add --propagate-exit-code to exit with the in-cluster command's exit code - #34498
Sai Kumar Pola (alopsing) wants to merge 2 commits into
Conversation
… 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.
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
|
Thank you for your contribution Sai Kumar Pola (@alopsing)! We will review the pull request and get back to you soon. |
There was a problem hiding this comment.
🔵 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
|
@microsoft-github-policy-service agree |
|
/azp run |
|
Azure Pipelines: Successfully started running 3 pipeline(s). |
|
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
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.
az aks command invoke: Return the in-cluster command's exit codeaz aks command invoke/result: Add --propagate-exit-code to exit with the in-cluster command's exit code
|
Thanks for the review FumingZhang. Agreed, an opt-in flag is the safer change. I pushed a second commit that does this:
Tests cover flag present and absent, successful and failed commands, provisioning failures with and without an exit code, I also ran both commands with and without the flag against a live cluster, including I updated the PR title and description to match. |
|
/azp run |
|
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)) |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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.
|
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 |
|
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! |
🤖 PR Validation —⚠️ Review suggested
Related command
az aks command invoke,az aks command resultDescription
Fixes #34496.
az aks command invokeandaz aks command resultprint 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.Failed) and there is no exit code from inside the cluster, the CLI exits 1.-o/--output. The result is rendered in the requested format first, and the CLI exits with the code after that. This uses theCli.SuccessfulExecuteevent, which the CLI raises once the output has been written.--no-waitstays non-blocking and ignores the flag. Pass the flag toaz aks command resultto get the exit code of the finished command.az aks command result --propagate-exit-codeexits 0 for it.The first commit changed the default behavior. The second commit replaces that with the flag, as requested in review.
Example:
Testing Guide
Unit tests in
acs/tests/latest/test_custom.py(TestRunCommand):invokeandresult: no exit for a failed command or a failed provisioning, with and without--output.Failedwithout an exit code gives 1; provisioningFailedwith an exit code gives that code.-oand--output: the result object is still returned for rendering, and the exit code is propagated.--no-wait, and with a command still running: normal exit.I ran these tests against the changed functions locally, and confirmed on a released
azbuild that a handler onCli.SuccessfulExecuteruns 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:
invoke, command fails with 1invoke, command fails with 1invoke, command fails with 1-o json, rendered in fullinvoke, command succeedsinvoke --no-wait, command fails with 1resultfor that command idresultfor that command idresultfor that command id-o tsv --query exitCodeNot 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
resultmakes. 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-codeto exit with the exit code of the command that ran in the cluster[AKS]
az aks command result: Add--propagate-exit-codeto exit with the exit code of the command that ran in the clusterThis checklist is used to make sure that common guidelines for a pull request are followed.
The PR title and description has followed the guideline in Submitting Pull Requests.
I adhere to the Command Guidelines.
I adhere to the Error Handling Guidelines.