Skip to content

fix(monitor): avoid division by zero in CPU usage when no limit is found - #1633

Merged
Rader merged 2 commits into
OpenCSGs:mainfrom
pcbeingused333:fix/cpu-usage-zero-limit
Nov 25, 2026
Merged

Rader merged 2 commits into
OpenCSGs:mainfrom
pcbeingused333:fix/cpu-usage-zero-limit

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

What is this feature?

A fix for the CPU usage chart of the deploy monitor: when the CPU limit cannot be found, the returned values are no longer huge negative numbers.

Why do we need this feature?

CPUUsage in component/monitor.go calls CPULimit and, on error, only logs a warning and continues with limit == 0. The percentage is then usage/0, which is +Inf/NaN; converting that to int64 yields MinInt64 on amd64, so the API returns about -9.2e16. CPULimit also queried m.k8sNameSpace while CPUUsage uses the namespace resolved by hasPermission, so for workloads outside the space namespace the limit was never found and this path was always taken.

The change passes the resolved namespace to CPULimit and falls back to a limit of 1 when no usable limit is returned (the same value CPULimit already returns for v <= 0).

Who is this feature for?

Users looking at the CPU chart of a deploy/workflow in the monitor page.

Which issue(s) does this PR fix?:

No issue filed. It shows up for evaluation/finetune workflow pods (their namespace is wf.Namespace, not the space namespace), and for any pod whose CPU limit metric is missing.

Special notes for your reviewer:

Added TestMonitor_CPUUsage_NoLimit (limit query returns no result): it failed before the change (value -9.22e16) and passes after. go test ./component -run TestMonitor passes.

This change was prepared with the help of an AI assistant (Claude Code), and the author is responsible for the change.

🤖 Generated with Claude Code

CPUUsage logged a failure from CPULimit and kept going with limit=0, so usage/0 became +Inf and was converted to int64, returning about -9.2e16. CPULimit also always queried the space namespace while usage uses the resolved one. Fall back to a limit of 1 (as CPULimit already does for v<=0) and pass the resolved namespace to CPULimit.

Signed-off-by: Alex Castillo <alex.castillog33@gmail.com>
@CementZhang

Copy link
Copy Markdown
Collaborator

Add a CPU regression test for the workflow namespace

  • Location: component/monitor_test.go:636
  • Rationale/Evidence: The newly added test still utilizes SpaceRepo, where both cfg.Cluster.SpaceNamespace and the expected query value are empty. Even if the production code is reverted to use m.k8sNameSpace, this test case still passes. Consequently, the specific workflow namespace mismatch addressed by this PR lacks test coverage.
  • Impact: If the default namespace is misused in the future, workflows will once again fall back to the default, preventing the actual CPU limit from being correctly reflected in charts.
  • Recommended Approach: Use an evaluation/finetune request, set SpaceNamespace=spaces and wf.Namespace=workflows, and assert that both usage and limit queries include workflows. Additionally, provide a valid limit and assert that the calculated ratio is precise.
  • Classification: A verification gap that should be addressed before merging; this does not necessarily imply a confirmed production failure.

Use an evaluation request with SpaceNamespace=spaces and the workflow in
namespace workflows, and assert both the usage and limit queries use
workflows and that the ratio is computed from the returned limit.

Signed-off-by: Alex Castillo <alex.castillog33@gmail.com>
@pcbeingused333

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Added TestMonitor_CPUUsage_Evaluation_WorkflowNamespace: an evaluation request with SpaceNamespace=spaces and the workflow in workflows, asserting that both the limit and usage queries use workflows, with limit 2 and usage 0.5 giving exactly 25. It fails if CPULimit goes back to m.k8sNameSpace and passes with the fix.

This comment was drafted with Claude Code.

@HaiHui886 HaiHui886 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

lgtm

Rader commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for fixing the namespace lookup and adding the cross-namespace regression test. One question about the CPU chart: is the percentage meant to be relative to the configured CPU limit? If so, falling back to 1 core when the limit is unknown avoids division by zero, but a workload using 2 cores will show 100% after clamping, even when it has no configured limit.

Could we distinguish a workload that is confirmed to have no CPU limit from one whose limit metric is unavailable? In the first case, show “No limit”; in the second, show “No data” for the percentage. If we still want to show CPU usage in either case, we can report it in cores. A missing metric alone shouldn't be treated as proof that the workload has no limit.

@pcbeingused333

Copy link
Copy Markdown
Contributor Author

Fair point, and you're right that the limit = 1 fallback is a guard against +Inf/NaN, not a statement about the workload: a 2-core workload with no resolvable limit would show 100% after clamping.

Telling "confirmed no limit" apart from "limit metric unavailable" needs a change to the MonitorData response contract (for example a null percentage plus a flag the UI can render as "No limit" / "No data"), and the frontend would have to consume it. To keep this PR reviewable I'd leave it as the division-by-zero + namespace fix, and open a separate issue with that contract proposal so you can pick the shape there. Does that work for you?

This comment was drafted with Claude Code.

Rader commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Yes, let’s keep this PR scoped to the namespace fix and division-by-zero guard. Could you open a separate issue for the CPU chart contract and link it here? The follow-up should distinguish “no CPU limit configured” from “CPU limit metric unavailable,” and make sure CPU usage and its limit refer to the same container. We can settle the response shape and UI behavior there without changing the existing percentage contract in this PR. Thanks!

@pcbeingused333

Copy link
Copy Markdown
Contributor Author

Opened #1644 for the CPU chart contract (no-limit vs. metric-unavailable, same-container check), so this PR stays scoped to the namespace fix and the division-by-zero guard.

This comment was drafted with Claude Code.

@Rader
Rader merged commit 478504d into OpenCSGs:main Nov 25, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants