microsoft / microsoft/aspire

Bug: aspire publish kubernetes Fails with Duplicate Endpoint Names and Port Resolution Errors

Open
#11,183 1 comment 0 reactions 0 assignees View on GitHub
area-deployment kubernetes
Dominant language
C#
Stars
6.3k
Forks
991
Avg merge
2d 15h
Merged PRs (30d)
196

Description

Description
When running aspire publish, the build/pack step crashes with two related errors in endpoint handling:
- Duplicate Default Endpoint Name
The first error surfaces if you call WithHttpEndpoint multiple times without explicit names:
Unhandled exception. Aspire.Hosting.DistributedApplicationException: Endpoint with name 'http' already exists.
Endpoint name may not have been explicitly specified and was derived automatically from scheme argument...
- Unable to Resolve Port for Endpoint
Even when you name your endpoint (e.g. "admin"), the Kubernetes generator fails during publish:
System.InvalidOperationException: Unable to resolve port for endpoint http on resource datanex-master


Both errors occur during the aspire publish --project command and prevent producing the Docker files and Kubernetes manifests.

Reproduction Steps
- Add Aspire to your project:
dotnet add package Aspire.Hosting
- In your Program.cs, configure endpoints without unique names:
var builder = DistributedApplication.CreateBuilder(args)
.AddKubernetesEnvironment("k8s");

// MongoDB resource omitted for brevity...

```
var webApp = builder
.AddProject("datanex-master")
.WithReference(mongo)
.WaitFor(mongo)
// First endpoint: defaults to name "http"
.WithHttpEndpoint(port: 8080)
// Second endpoint: also defaults to "http" → collision
.WithHttpEndpoint(port: 8081)
.PublishAsDockerFile(cb => { /* ... */ });
```

builder.Build().Run();
- Run publish:
`aspire publish --project ./Datanex.Master.AppHost/Datanex.Master.AppHost.csproj`
- Observe the duplicate-name exception.
- Rename the endpoints to avoid the first error:
```
.WithHttpEndpoint(name: "main", port: 8080)
.WithHttpEndpoint(name: "admin", port: 8081)
```
- Re-run publish. Now you hit the port-resolution error:
Unable to resolve port for endpoint http on resource datanex-master

Expected Behavior
- Publish should succeed, generating Dockerfiles and Kubernetes YAML with both ports exposed.
- If no name is provided, each endpoint should derive a unique identifier (e.g. "http-8080", "http-8081").
- Named endpoints should always map their port into the generated Kubernetes Service/Pod spec.

Actual Behavior
- First aspire publish run fails on duplicate default endpoint name.
- Second run (after naming endpoints) fails on missing port resolution for the "http" scheme.

Workaround
Explicitly name and port each endpoint, then manually adjust the generated YAML:
.WithHttpEndpoint(name: "main-8080", port: 8080)
.WithHttpEndpoint(name: "admin-8081", port: 8081)

After aspire publish, open the Service manifest and add the missing port entries by hand.

Suggested Fix
- Automatic Unique Naming
In ResourceBuilderExtensions.WithEndpoint:
```
- var endpointName = string.IsNullOrEmpty(name) ? scheme : name;
+ var endpointName = string.IsNullOrEmpty(name)
+ ? $"{scheme}-{port.GetValueOrDefault()}"
+ : name;
+ // ensure uniqueness
+ var original = endpointName;
+ var index = 2;
+ while (builder.Resource.Endpoints.ContainsKey(endpointName))
+ {
+ endpointName = $"{original}-{index++}";
+ }

```
- Port Propagation to Kubernetes
In KubernetesResource.ProcessEndpoints, ensure that each HTTP endpoint’s Port value is captured and written into the Service spec, regardless of whether it came from:
- WithHttpEndpoint(...)
- WithExternalHttpEndpoints() via Dockerfile
- Default Kestrel settings

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.