Skip to content

refactor(ctl): replace os.Exit with proper error handling via RunE - #1913

Open
devGPP23 wants to merge 1 commit into
kmesh-net:mainfrom
devGPP23:fix-ctl-os-exit-rune
Open

devGPP23 wants to merge 1 commit into
kmesh-net:mainfrom
devGPP23:fix-ctl-os-exit-rune

Conversation

@devGPP23

@devGPP23 devGPP23 commented Aug 11, 2026

Copy link
Copy Markdown

Problem

I noticed any network error triggers a hard os.Exit(1) deep inside the Kmesh codebase. This abruptly crashes the entire MCP server process instead of gracefully returning the error to the caller.
Before this fix, an error fetching pods would print a raw logger message and forcefully terminate the CLI process without passing the error back to the Cobra framework:

$ ./kmeshctl authz status
client version: v0.0.0-master
time="2026-08-11T12:42:41Z" level=error msg="failed to get kmesh podList: Get \"https://127.0.0.1:34255/api/v1/namespaces/kmesh-system/pods?labelSelector=app%3Dkmesh\": dial tcp 127.0.0.1:34255: connect: connection refused"

Solution

To ensure kmeshctl is completely safe to embed in long-running processes, I audited the remaining subcommands (authz, version, secret, monitoring) and performed a full migration.
Specifically, I did the following:
1)Migrated the Run field to RunE in all subcommands so Cobra can natively handle returned errors.
2)Systematically removed all os.Exit(1) calls.
3)Refactored helper functions to return standard Go error objects rather than logging and crashing the process.
4)Replaced the hard dependency on standard stdout/stderr by using io.Writer and cmd.OutOrStdout() where applicable.

How it Improves
After my changes, errors are gracefully propagated up through fmt.Errorf rather than forcefully killing the application.we can safely import these CLI functions without the risk of an unexpected fatal exit.

In the CLI itself, Cobra now successfully catches the returned error and formats it cleanly with an Error: prefix instead of abruptly terminating:
Now Terminal Output is like below with Error: prefic

$ ./kmeshctl authz status
time="2026-08-11T12:42:41Z" level=warning msg="failed to create log directory: mkdir /var/run/kmesh/: permission denied, consider running with root user"
Error: failed to get kmesh podList: Get "https://127.0.0.1:34255/api/v1/namespaces/kmesh-system/pods?labelSelector=app%3Dkmesh": dial tcp 127.0.0.1:34255: connect: connection refused

This makes kmeshctl much safer, more idiomatic, and fully embeddable!

Copilot AI lite review requested due to automatic review settings August 11, 2026 13:27
@kmesh-bot

Copy link
Copy Markdown
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign yaozengzeng for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@devGPP23 devGPP23 changed the title **Title:** Refactor kmeshctl to use RunE and eliminate os.Exit calls for MCP safety refactor(ctl): replace os.Exit with proper error handling via RunE Aug 11, 2026
Copilot AI review requested due to automatic review settings August 11, 2026 13:37

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@codecov

codecov Bot commented Aug 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 39.58%. Comparing base (c6357aa) to head (08150f8).
⚠️ Report is 14 commits behind head on main.
see 2 files with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ec501a9...08150f8. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@devGPP23
devGPP23 force-pushed the fix-ctl-os-exit-rune branch 2 times, most recently from c9ce290 to 8b6b657 Compare August 12, 2026 19:39
Copilot AI review requested due to automatic review settings August 12, 2026 19:39

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@kmesh-bot kmesh-bot added size/L and removed size/XL labels Aug 12, 2026
Copilot AI review requested due to automatic review settings August 15, 2026 09:14

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings August 15, 2026 10:23

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@devGPP23
devGPP23 force-pushed the fix-ctl-os-exit-rune branch 2 times, most recently from 642f3ff to e149f2d Compare August 15, 2026 13:48
Comment thread ctl/dump/dump.go
return fmt.Errorf("failed to create cli client: %v", err)
}

fw, err := utils.CreateKmeshPortForwarder(cli, podName)

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.

Missing fw.Close()

Copilot AI review requested due to automatic review settings August 20, 2026 18:42
@devGPP23
devGPP23 force-pushed the fix-ctl-os-exit-rune branch 2 times, most recently from 76106a9 to df89c07 Compare August 21, 2026 08:52
Copilot AI review requested due to automatic review settings August 21, 2026 08:52

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings August 21, 2026 09:05
@devGPP23
devGPP23 force-pushed the fix-ctl-os-exit-rune branch from df89c07 to 538a51e Compare August 21, 2026 09:05

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@devGPP23
devGPP23 force-pushed the fix-ctl-os-exit-rune branch from 538a51e to 6b3f0b0 Compare August 21, 2026 09:23
Copilot AI review requested due to automatic review settings August 21, 2026 09:23

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings August 21, 2026 20:03
@devGPP23
devGPP23 force-pushed the fix-ctl-os-exit-rune branch from 6b3f0b0 to 110e6f5 Compare August 21, 2026 20:03

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@devGPP23
devGPP23 force-pushed the fix-ctl-os-exit-rune branch from 110e6f5 to 99621dc Compare August 21, 2026 20:44
Copilot AI review requested due to automatic review settings August 21, 2026 20:44

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings August 21, 2026 21:06
@devGPP23
devGPP23 force-pushed the fix-ctl-os-exit-rune branch from 99621dc to f669c55 Compare August 21, 2026 21:06

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings August 22, 2026 03:27
@devGPP23
devGPP23 force-pushed the fix-ctl-os-exit-rune branch from f669c55 to 664b0b6 Compare August 22, 2026 03:27

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings August 22, 2026 03:45
@devGPP23
devGPP23 force-pushed the fix-ctl-os-exit-rune branch from 664b0b6 to f8ebbdd Compare August 22, 2026 03:45

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Signed-off-by: devGPP23 <gauravpatil232005@gmail.com>
Copilot AI review requested due to automatic review settings August 22, 2026 04:57
@devGPP23
devGPP23 force-pushed the fix-ctl-os-exit-rune branch from f8ebbdd to 08150f8 Compare August 22, 2026 04:57

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@devGPP23

Copy link
Copy Markdown
Author

Hi @LiZhenCheng9527
I've addressed the feedback:

1 ) Added defer fw.Close() across all ctl/ port-forwarder helpers to prevent any connection/goroutine leaks.
2) Refactored remaining os.Exit(1) calls to bubble errors through RunE.
3)All tests and CI checks are green.
please take a look whenever u are free

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants