Skip to content

frontend: project: Fix ProjectList crash when a namespace has no labels#5723

Merged
illume merged 1 commit into
kubernetes-sigs:mainfrom
govindup63:fix/5254-project-list-crash
May 18, 2026
Merged

frontend: project: Fix ProjectList crash when a namespace has no labels#5723
illume merged 1 commit into
kubernetes-sigs:mainfrom
govindup63:fix/5254-project-list-crash

Conversation

@govindup63
Copy link
Copy Markdown
Contributor

Summary

This PR fixes a TypeError that crashed the Projects page whenever the list returned from Namespace.useList contained a namespace without metadata.labels populated. The Projects view simply went blank with this error in the console:

TypeError: Cannot read properties of undefined (reading 'headlamp.dev/project-id')

The crash originated on frontend/src/components/project/ProjectList.tsx, where the groupBy iteratee dereferenced n.metadata.labels![PROJECT_ID_LABEL] with a non-null assertion. The labelSelector on the API request filters at the server side, but the array reaching groupBy can still transiently include namespaces without metadata.labels populated, for example during multi-cluster fan-out or while the react-query cache is being refreshed after a label removal.

Related Issue

Fixes #5254

Changes

  • frontend/src/components/project/ProjectList.tsx: extract the namespaces-to-projects transformation into a small exported helper groupNamespacesIntoProjects and filter out items that do not carry PROJECT_ID_LABEL before grouping. The useProjects hook now calls the helper and tolerates Namespace.useList returning items: null.
  • frontend/src/components/project/ProjectList.test.tsx: new vitest suite covering normal grouping, multi-cluster collection, and the two regression cases from Crash Report: Cannot read properties of undefined (reading 'headlamp.dev/project-id') #5254 (a namespace whose metadata.labels is undefined, and a namespace whose labels object is present but does not include PROJECT_ID_LABEL).

Steps to Test

  1. Check out this branch and run npm install under frontend/ if needed.
  2. npx vitest run src/components/project/ProjectList.test.tsx. All four tests should pass.
  3. Reverting only the filter inside groupNamespacesIntoProjects reproduces the original crash from Crash Report: Cannot read properties of undefined (reading 'headlamp.dev/project-id') #5254 with the exact error message, which the new tests catch.
  4. npx tsc --noEmit and npx eslint -c package.json src/components/project/ProjectList.tsx src/components/project/ProjectList.test.tsx are clean.

Notes for the Reviewer

  • The label selector still filters at the API layer, so under steady state the new filter is a no-op. Its only job is to keep the inner iteratee safe against transient cache contents.
  • The helper is exported rather than inlined into useProjects so it can be tested directly without an RTL harness. Following the cyclic-import workaround already used by projectUtils.test.ts (the _dont_delete_me = App import) was the cheapest way to make the new test load.
  • No behavior change for users whose namespaces are all labelled correctly: the helper output is identical to the previous expression in that case.

@k8s-ci-robot k8s-ci-robot added the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label May 17, 2026
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

Welcome @govindup63!

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 the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label May 17, 2026
@k8s-ci-robot k8s-ci-robot requested review from illume and skoeva May 17, 2026 16:44
@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label May 17, 2026
Reading n.metadata.labels![PROJECT_ID_LABEL] inside the groupBy iteratee
threw

  TypeError: Cannot read properties of undefined (reading
             'headlamp.dev/project-id')

whenever the items returned from Namespace.useList contained a namespace
without metadata.labels populated. The labelSelector on the request
filters at the API level, but the returned list can still transiently
include unlabelled items (multi-cluster fan-out, react-query cache
during a label removal), and an unguarded access there took down the
whole Projects page.

Move the grouping into a small exported helper so the null-safety guard
lives in one place and add a vitest regression covering the unlabelled
and partially-labelled cases.

Issue: kubernetes-sigs#5254
Signed-off-by: Govind Pandey <govindup63@gmail.com>
@govindup63 govindup63 force-pushed the fix/5254-project-list-crash branch from f89ddf2 to c3b0b96 Compare May 17, 2026 16:46
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label May 17, 2026
@illume illume requested a review from Copilot May 17, 2026 18:51
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 a crash in the Projects page caused by dereferencing metadata.labels[PROJECT_ID_LABEL] when a Namespace item transiently lacks metadata.labels (e.g., during cache refresh or multi-cluster fan-out).

Changes:

  • Added groupNamespacesIntoProjects helper to safely group namespaces by PROJECT_ID_LABEL, filtering out entries without that label before groupBy.
  • Updated useProjects to tolerate Namespace.useList returning items: null by defaulting to an empty list.
  • Added a new Vitest suite covering normal grouping, multi-cluster behavior, and the two regression scenarios from #5254.

Reviewed changes

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

File Description
frontend/src/components/project/ProjectList.tsx Prevents Projects page crash by filtering namespaces lacking metadata.labels[PROJECT_ID_LABEL] before grouping; handles items: null.
frontend/src/components/project/ProjectList.test.tsx Adds unit tests for the grouping helper, including regressions for missing labels / missing project-id label.

Copy link
Copy Markdown
Contributor

@illume illume left a comment

Choose a reason for hiding this comment

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

🎉 thanks!

@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: govindup63, illume

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

The pull request process is described 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

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label May 18, 2026
@illume illume merged commit 91f55c7 into kubernetes-sigs:main May 18, 2026
15 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash Report: Cannot read properties of undefined (reading 'headlamp.dev/project-id')

4 participants