Skip to content

backend: Fix port-forward cache key collisions#5626

Open
harrshita123 wants to merge 1 commit into
kubernetes-sigs:mainfrom
harrshita123:backend-portforward-cache-key-collision
Open

backend: Fix port-forward cache key collisions#5626
harrshita123 wants to merge 1 commit into
kubernetes-sigs:mainfrom
harrshita123:backend-portforward-cache-key-collision

Conversation

@harrshita123
Copy link
Copy Markdown
Contributor

Summary

This PR fixes a backend port-forward cache-key collision in Headlamp. Port-forward entries now use separator-safe cache keys, so clusters with similar
names no longer overlap when entries are listed, looked up, or deleted.

Related Issue

Fixes #5625

Changes

  • Updated port-forward cache key generation to use a safer, delimiter-based format
  • Made cluster matching in port-forward listing exact instead of relying on a raw prefix
  • Reused the same key generator for ID lookups to keep cache access consistent
  • Added a regression test for the foo vs foobar cluster-name collision case

Steps to Test

  1. Start Headlamp with the backend running.
  2. Create or simulate two clusters with similar names, such as foo and foobar.
  3. Create a port-forward entry for one of the clusters.
  4. List or stop port-forward entries and confirm that only the matching cluster’s entries are affected.
  5. Run the backend port-forward tests and confirm they pass.

Screenshots

Not applicable for this backend-only change.

Notes for the Reviewer

This is a focused backend fix. The change stays within the existing port-forward cache logic and adds a regression test for the collision scenario
described in the issue.

@k8s-ci-robot k8s-ci-robot requested review from ashu8912 and sniok May 14, 2026 06:07
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: harrshita123
Once this PR has been reviewed and has the lgtm label, please assign sniok 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

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels May 14, 2026
@illume illume requested a review from Copilot May 14, 2026 08:14
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 for working on this.

would you mind rebasing this branch on top of the latest main rather than merging it in?

Why this matters

Merge commits from main make the PR history harder to review. Please rebase your branch on top of the latest main instead, then update the PR with the rebased commits.

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

This PR updates the backend port-forward cache key format and lookup behavior to prevent collisions between similarly named clusters (e.g., foo vs foobar) when listing, retrieving, stopping, or deleting port-forward entries.

Changes:

  • Introduces a delimiter-based, escaped cache key format for port-forward entries and centralizes prefix generation.
  • Makes port-forward listing filter by an escaped cluster-specific prefix instead of a raw storeKeyPrefix+cluster prefix.
  • Adds a regression test for the foo vs foobar cluster-name collision case.

Reviewed changes

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

File Description
backend/pkg/portforward/store.go Adds escaped, delimiter-based key generation and updates list/lookup to use the shared key/prefix helpers.
backend/pkg/portforward/internal_test.go Updates key-generator expectations and adds a regression test for prefix-collision behavior.
backend/pkg/portforward/handler_test.go Updates integration test’s cache-key construction to match the new key format.

Comment thread backend/pkg/portforward/store.go Outdated
Comment thread backend/pkg/portforward/internal_test.go Outdated
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 for this PR.

The open review comments from Copilot still need attention — can you have a look? Once addressed, please mark them as resolved.

@harrshita123
Copy link
Copy Markdown
Contributor Author

Thanks for this PR.

The open review comments from Copilot still need attention — can you have a look? Once addressed, please mark them as resolved.

@illume I resolved the commets .

@illume illume requested a review from Copilot May 14, 2026 15:07
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 for the contribution.

it looks like there's a merge-main commit in this PR — could you rebase onto main instead?

Why this matters

Merge commits from main make the PR history harder to review. Please rebase your branch on top of the latest main instead, then update the PR with the rebased commits.

@harrshita123 harrshita123 force-pushed the backend-portforward-cache-key-collision branch from bec9774 to 7fa68a8 Compare May 14, 2026 15:10
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

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread backend/pkg/portforward/store.go Outdated
@harrshita123 harrshita123 force-pushed the backend-portforward-cache-key-collision branch 2 times, most recently from 6d05f52 to 7fa68a8 Compare May 14, 2026 15:16
@harrshita123
Copy link
Copy Markdown
Contributor Author

Thanks for the contribution.

it looks like there's a merge-main commit in this PR — could you rebase onto main instead?

Why this matters

@illume I rebased the commits .

@illume illume requested a review from Copilot May 15, 2026 06:27
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 for working on this.

would you mind rebasing this branch on top of the latest main rather than merging it in?

Why this matters

Merge commits from main make the PR history harder to review. Please rebase your branch on top of the latest main instead, then update the PR with the rebased commits.

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread backend/pkg/portforward/internal_test.go Outdated
@illume illume marked this pull request as draft May 15, 2026 10:34
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label May 15, 2026
@illume
Copy link
Copy Markdown
Contributor

illume commented May 15, 2026

@harrshita123
It might be a good idea to focus on one PR at a time.

@harrshita123 harrshita123 marked this pull request as ready for review May 17, 2026 08:09
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label May 17, 2026
@k8s-ci-robot k8s-ci-robot requested a review from kahirokunn May 17, 2026 08:09
@harrshita123 harrshita123 force-pushed the backend-portforward-cache-key-collision branch from d73d0ad to 2927230 Compare May 17, 2026 08:21
@harrshita123 harrshita123 force-pushed the backend-portforward-cache-key-collision branch from 2927230 to 66f9edc Compare May 17, 2026 08:26
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels May 17, 2026
@harrshita123
Copy link
Copy Markdown
Contributor Author

Thanks for working on this.

would you mind rebasing this branch on top of the latest main rather than merging it in?

Why this matters

@illume
I resolved all the comments .

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

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

@k8s-ci-robot k8s-ci-robot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels May 17, 2026
@harrshita123 harrshita123 force-pushed the backend-portforward-cache-key-collision branch from ec8828b to aae2f31 Compare May 17, 2026 11:20
@illume illume requested a review from Copilot May 17, 2026 18:58
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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comment thread backend/pkg/portforward/handler.go
@harrshita123 harrshita123 force-pushed the backend-portforward-cache-key-collision branch from aae2f31 to 09f2e5a Compare May 18, 2026 04:44
@illume illume requested a review from Copilot May 18, 2026 07:07
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 for the contribution.

The backend test job in CI is failing. Run cd backend && go test ./... to reproduce the errors locally.

How to run the backend tests

Run cd backend && go test ./... to see all failures. Fix the failing tests and commit the result.

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

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

Comment thread backend/pkg/portforward/store.go Outdated
Comment thread backend/pkg/portforward/handler_test.go Outdated
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 for this PR.

The open review comments from Copilot still need attention — can you have a look? Once addressed, please mark them as resolved.

@harrshita123 harrshita123 force-pushed the backend-portforward-cache-key-collision branch from 09f2e5a to cdd5013 Compare May 19, 2026 14:52
@illume illume requested a review from Copilot May 19, 2026 15:13
@harrshita123 harrshita123 force-pushed the backend-portforward-cache-key-collision branch from cdd5013 to b2c6bd9 Compare May 19, 2026 15:13
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

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

@harrshita123
Copy link
Copy Markdown
Contributor Author

Thanks for this PR.

The open review comments from Copilot still need attention — can you have a look? Once addressed, please mark them as resolved.

@illume
I resolved all the comments .

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/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.

backend: port-forward cache keys can collide across similarly named clusters

4 participants