Skip to content

Fix port mismatch for bait-and-switch resources in Kubernetes publisher (13.2)#14627

Open
mitchdenny wants to merge 6 commits intorelease/13.2from
k8s-bait-switch-fix-13.2
Open

Fix port mismatch for bait-and-switch resources in Kubernetes publisher (13.2)#14627
mitchdenny wants to merge 6 commits intorelease/13.2from
k8s-bait-switch-fix-13.2

Conversation

@mitchdenny
Copy link
Member

Cherry-pick of #14590 targeting release/13.2 for deployment testing.

Description

The Kubernetes publisher uses a Dictionary 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
Based on community PR #14590 by @bbartels

Copilot AI review requested due to automatic review settings February 23, 2026 22:13
@mitchdenny
Copy link
Member Author

/deployment-test

@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 -- 14627

Or

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

@github-actions
Copy link
Contributor

🚀 Deployment tests starting on PR #14627...

This will deploy to real Azure infrastructure. Results will be posted here when complete.

View workflow run

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 is a cherry-pick of #14590 targeting the release/13.2 branch. It fixes a port mismatch bug in the Kubernetes publisher that occurs when resources are replaced during the "bait-and-switch" pattern (e.g., when PublishAsDockerFile() replaces an ExecutableResource with an ExecutableContainerResource). The fix applies the same ResourceNameComparer pattern already used successfully by the Docker Compose, Azure App Service, and Azure Container Apps publishers.

Changes:

  • Modified KubernetesEnvironmentContext to use ResourceNameComparer for resource dictionary lookups by name instead of object identity
  • Added comprehensive test coverage with snapshot verification for the bait-and-switch scenario

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 Added ResourceNameComparer to the _kubernetesComponents dictionary to enable name-based resource lookups
tests/Aspire.Hosting.Kubernetes.Tests/KubernetesPublisherTests.cs Added new test KubernetesMapsPortsForBaitAndSwitchResources to verify consistent port allocation
tests/Aspire.Hosting.Kubernetes.Tests/Snapshots/*.verified.yaml Seven snapshot files verifying correct YAML generation with consistent port 8000 across all configurations

@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 22:19 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 22:19 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 22:19 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 22:19 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 22:19 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 22:19 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 22:19 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 22:19 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 22:19 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 22:19 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 22:19 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 22:19 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 22:19 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 22:19 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 22:19 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 22:19 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 22:19 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 22:19 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 22:19 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 22:19 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 22:19 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 22:19 Inactive
@github-actions
Copy link
Contributor

🎬 CLI E2E Test Recordings

The following terminal recordings are available for commit 8df7446:

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
DoctorCommand_DetectsDeprecatedAgentConfig ▶️ View Recording
DoctorCommand_WithSslCertDir_ShowsTrusted ▶️ View Recording
DoctorCommand_WithoutSslCertDir_ShowsPartiallyTrusted ▶️ View Recording
LogsCommandShowsResourceLogs ▶️ View Recording
PsCommandListsRunningAppHost ▶️ View Recording
ResourcesCommandShowsRunningResources ▶️ 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 #22327060851

@github-actions
Copy link
Contributor

Deployment E2E Tests failed

Summary: 22 passed, 1 failed, 0 cancelled

View workflow run

Passed Tests

  • ✅ AppServiceReactDeploymentTests
  • ✅ AuthenticationTests
  • ✅ AcaExistingRegistryDeploymentTests
  • ✅ AksStarterWithRedisDeploymentTests
  • ✅ AcaCompactNamingUpgradeDeploymentTests
  • ✅ AzureLogAnalyticsDeploymentTests
  • ✅ AzureAppConfigDeploymentTests
  • ✅ AcaStarterDeploymentTests
  • ✅ AzureKeyVaultDeploymentTests
  • ✅ AzureServiceBusDeploymentTests
  • ✅ AcaCustomRegistryDeploymentTests
  • ✅ AzureContainerRegistryDeploymentTests
  • ✅ PythonFastApiDeploymentTests
  • ✅ AksStarterDeploymentTests
  • ✅ AppServicePythonDeploymentTests
  • ✅ AzureStorageDeploymentTests
  • ✅ VnetKeyVaultConnectivityDeploymentTests
  • ✅ VnetStorageBlobInfraDeploymentTests
  • ✅ VnetSqlServerInfraDeploymentTests
  • ✅ AzureEventHubsDeploymentTests
  • ✅ VnetKeyVaultInfraDeploymentTests
  • ✅ VnetStorageBlobConnectivityDeploymentTests

Failed Tests

  • ❌ AcaCompactNamingDeploymentTests

🎬 Terminal Recordings

Test Recording
DeployAzureAppConfigResource ▶️ View Recording
DeployAzureContainerRegistryResource ▶️ View Recording
DeployAzureEventHubsResource ▶️ View Recording
DeployAzureKeyVaultResource ▶️ View Recording
DeployAzureLogAnalyticsResource ▶️ View Recording
DeployAzureServiceBusResource ▶️ View Recording
DeployAzureStorageResource ▶️ View Recording
DeployPythonFastApiTemplateToAzureAppService ▶️ View Recording
DeployPythonFastApiTemplateToAzureContainerApps ▶️ View Recording
DeployReactTemplateToAzureAppService ▶️ View Recording
DeployStarterTemplateToAks ▶️ View Recording
DeployStarterTemplateToAzureContainerApps ▶️ View Recording
DeployStarterTemplateWithCustomRegistry ▶️ View Recording
DeployStarterTemplateWithExistingRegistry ▶️ View Recording
DeployStarterTemplateWithKeyVaultPrivateEndpoint ▶️ View Recording
DeployStarterTemplateWithRedisToAks ▶️ View Recording
DeployStarterTemplateWithStorageBlobPrivateEndpoint ▶️ View Recording
DeployVnetKeyVaultInfrastructure ▶️ View Recording
DeployVnetSqlServerInfrastructure ▶️ View Recording
DeployVnetStorageBlobInfrastructure ▶️ View Recording
DeployWithCompactNamingFixesStorageCollision ▶️ View Recording
UpgradeFromGaToDevDoesNotDuplicateStorageAccounts ▶️ View Recording

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