Skip to content

fix: Pod metrics matching missing namespace (#4927)#4933

Open
Utkarshpandey0001 wants to merge 1 commit intokubernetes-sigs:mainfrom
Utkarshpandey0001:fix/pod-metrics-namespace
Open

fix: Pod metrics matching missing namespace (#4927)#4933
Utkarshpandey0001 wants to merge 1 commit intokubernetes-sigs:mainfrom
Utkarshpandey0001:fix/pod-metrics-namespace

Conversation

@Utkarshpandey0001
Copy link
Copy Markdown

Fixes issue #4927

Changes:

  • Added/Updated [component/file/logic]
  • Fixed [bug/issue/typo]
  • Refactored [code/module] for clarity/performance

This PR fixes a bug where Headlamp displays incorrect CPU and memory metrics for pods that share the same name across different namespaces (e.g., in the Workloads > Pods view).

Previously, the
getCpuUsage and getMemoryUsage functions in List.tsx matched metrics to pods using only the pod name (it.getName() === pod.getName()). This caused all pods with the same name to display the metrics of the first matched pod.

This fix updates the lookup logic to also compare the namespace (it.getNamespace() === pod.getNamespace()), ensuring each pod correctly displays its own individual metrics.

@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Utkarshpandey0001
Once this PR has been reviewed and has the lgtm label, please assign joaquimrocha for approval. For more information see the 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

@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla bot commented Mar 21, 2026

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: Utkarshpandey0001 / name: Utkarsh raj (b6f0e35)

@k8s-ci-robot
Copy link
Copy Markdown
Contributor

Welcome @Utkarshpandey0001!

It looks like this is your first PR to kubernetes-sigs/headlamp 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/headlamp has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Mar 21, 2026
@Utkarshpandey0001 Utkarshpandey0001 force-pushed the fix/pod-metrics-namespace branch from ff529c0 to b6f0e35 Compare March 21, 2026 11:52
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Mar 21, 2026
@illume illume requested a review from Copilot March 22, 2026 18:35
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes incorrect pod CPU/memory metrics attribution in the Pods list when multiple pods share the same name across namespaces, by tightening the frontend’s metrics-to-pod matching logic.

Changes:

  • Update pod metrics lookup in PodListRenderer to match by both pod name and namespace.
  • Update Storybook mock PodMetrics data to include the namespace field.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
frontend/src/components/pod/List.tsx Adjusts PodMetrics matching logic used to compute CPU/memory columns.
frontend/src/components/pod/PodList.stories.tsx Updates MSW metrics mock to include metadata.namespace so the story continues to show metrics.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +203 to +205
const metric = metrics?.find(
it => it.getName() === pod.getName() && it.getNamespace() === pod.getNamespace()
);
Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

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

Metrics lookup still isn’t fully disambiguated in multi-cluster mode. Pod.useList() / PodMetrics.useList() can return items from multiple clusters (note the cluster column), so matching only by name+namespace can still attach the wrong metrics when the same pod name/namespace exists in different clusters. Include cluster in the predicate as well (e.g., compare it.cluster with pod.cluster).

Copilot uses AI. Check for mistakes.
Comment on lines +214 to +216
const metric = metrics?.find(
it => it.getName() === pod.getName() && it.getNamespace() === pod.getNamespace()
);
Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

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

Same issue as CPU lookup: to avoid cross-cluster collisions, the metrics match should include cluster in addition to name+namespace (both lists can span multiple clusters). Otherwise memory metrics can be taken from another cluster that has the same pod name/namespace.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants