Skip to content

Fix port mismatch for bait-and-switch resources in Kubernetes publisher#14653

Merged
joperezr merged 6 commits intorelease/13.2from
cherry-pick/k8s-bait-and-switch-fix
Feb 24, 2026
Merged

Fix port mismatch for bait-and-switch resources in Kubernetes publisher#14653
joperezr merged 6 commits intorelease/13.2from
cherry-pick/k8s-bait-and-switch-fix

Conversation

@joperezr
Copy link
Member

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 in KubernetesEnvironmentContext keyed by object identity. When PublishAsDockerFile() replaces an ExecutableResource with an ExecutableContainerResource (the 'bait and switch' pattern), other resources still hold EndpointReference objects pointing to the original. The cache misses on the original vs replacement, creating two KubernetesResource objects for the same logical resource, each allocating a different port from the shared PortAllocator. This causes the referencing resource to get a different port than the one in the service/deployment YAML.

The fix applies the same ResourceNameComparer pattern already used by the Docker Compose, Azure App Service, and Azure Container Apps publishers.

Fixes #9226

Copilot AI review requested due to automatic review settings February 24, 2026 17:37
@joperezr joperezr added this to the 13.2 milestone Feb 24, 2026
@github-actions
Copy link
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 14653

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 14653"

Copy link
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 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 KubernetesEnvironmentContext to use ResourceNameComparer when 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

@github-actions
Copy link
Contributor

🎬 CLI E2E Test Recordings

The following terminal recordings are available for commit 314a1ae:

Test Recording
AddPackageInteractiveWhileAppHostRunningDetached ▶️ View Recording
AddPackageWhileAppHostRunningDetached ▶️ View Recording
AgentCommands_AllHelpOutputs_AreCorrect ▶️ View Recording
AgentInitCommand_MigratesDeprecatedConfig ▶️ View Recording
AgentInitCommand_WithMalformedMcpJson_ShowsErrorAndExitsNonZero ▶️ View Recording
AspireUpdateRemovesAppHostPackageVersionFromDirectoryPackagesProps ▶️ View Recording
Banner_DisplayedOnFirstRun ▶️ View Recording
Banner_DisplayedWithExplicitFlag ▶️ View Recording
CreateAndDeployToDockerCompose ▶️ View Recording
CreateAndDeployToDockerComposeInteractive ▶️ View Recording
CreateAndPublishToKubernetes ▶️ View Recording
CreateAndRunAspireStarterProject ▶️ View Recording
CreateAndRunAspireStarterProjectWithBundle ▶️ View Recording
CreateAndRunJsReactProject ▶️ View Recording
CreateAndRunPythonReactProject ▶️ View Recording
CreateEmptyAppHostProject ▶️ View Recording
CreateStartAndStopAspireProject ▶️ View Recording
CreateStartWaitAndStopAspireProject ▶️ View Recording
CreateTypeScriptAppHostWithViteApp ▶️ View Recording
DescribeCommandShowsRunningResources ▶️ View Recording
DetachFormatJsonProducesValidJson ▶️ View Recording
DoctorCommand_DetectsDeprecatedAgentConfig ▶️ View Recording
DoctorCommand_WithSslCertDir_ShowsTrusted ▶️ View Recording
DoctorCommand_WithoutSslCertDir_ShowsPartiallyTrusted ❌ Upload failed
LogsCommandShowsResourceLogs ▶️ View Recording
PsCommandListsRunningAppHost ▶️ View Recording
StagingChannel_ConfigureAndVerifySettings_ThenSwitchChannels ▶️ View Recording
StopAllAppHostsFromAppHostDirectory ▶️ View Recording
StopAllAppHostsFromUnrelatedDirectory ▶️ View Recording
StopNonInteractiveMultipleAppHostsShowsError ▶️ View Recording
StopNonInteractiveSingleAppHost ▶️ View Recording
StopWithNoRunningAppHostExitsSuccessfully ▶️ View Recording

📹 Recordings uploaded automatically from CI run #22362675720

@joperezr joperezr merged commit be6509a into release/13.2 Feb 24, 2026
348 checks passed
@joperezr joperezr deleted the cherry-pick/k8s-bait-and-switch-fix branch February 24, 2026 18:09
@dotnet-policy-service dotnet-policy-service bot modified the milestone: 13.2 Feb 24, 2026
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.

3 participants