Fix port mismatch for bait-and-switch resources in Kubernetes publisher#14653
Merged
joperezr merged 6 commits intorelease/13.2from Feb 24, 2026
Merged
Fix port mismatch for bait-and-switch resources in Kubernetes publisher#14653joperezr merged 6 commits intorelease/13.2from
joperezr merged 6 commits intorelease/13.2from
Conversation
Contributor
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 14653Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 14653" |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a port mismatch issue in the Kubernetes publisher when resources are transformed via PublishAsDockerFile() (the "bait-and-switch" pattern). The fix applies the ResourceNameComparer pattern already used by other publishers to ensure resource lookups work correctly by name rather than object identity.
Changes:
- Modified
KubernetesEnvironmentContextto useResourceNameComparerwhen caching resources in the dictionary - Added comprehensive test with snapshot verification to ensure port consistency across generated Kubernetes manifests
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Aspire.Hosting.Kubernetes/KubernetesEnvironmentContext.cs | Updated dictionary to use ResourceNameComparer for resource cache lookups |
| tests/Aspire.Hosting.Kubernetes.Tests/KubernetesPublisherTests.cs | Added test case verifying port consistency for bait-and-switch resources |
| tests/Aspire.Hosting.Kubernetes.Tests/Snapshots/*.verified.yaml | Snapshot files verifying correct port mappings (8000) across all generated manifests |
Contributor
🎬 CLI E2E Test RecordingsThe following terminal recordings are available for commit
📹 Recordings uploaded automatically from CI run #22362675720 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cherry-pick of #14590 into release/13.2.
The original PR was accidentally retargeted from main to release/13.2, which brought all main branch commits along. That merge was reverted, and this PR contains only the original commits by @bbartels with the fix.
Description
The Kubernetes publisher uses a
Dictionary<IResource, KubernetesResource>cache inKubernetesEnvironmentContextkeyed by object identity. WhenPublishAsDockerFile()replaces anExecutableResourcewith anExecutableContainerResource(the 'bait and switch' pattern), other resources still holdEndpointReferenceobjects pointing to the original. The cache misses on the original vs replacement, creating twoKubernetesResourceobjects for the same logical resource, each allocating a different port from the sharedPortAllocator. This causes the referencing resource to get a different port than the one in the service/deployment YAML.The fix applies the same
ResourceNameComparerpattern already used by the Docker Compose, Azure App Service, and Azure Container Apps publishers.Fixes #9226