(code provided) Minimal k8s aspire application generates invalid YAML assets: duplicate ports, invalid values, etc.
Is there an existing issue for this?
- [x] I have searched the existing issues
Describe the bug
Use minimal aspire app created with aspire new and configured to be deployed to k8s provided here).
- Create a free tier AKCS cluster named
mi-k8s - Create container registry
aspireapp - Execute a simple BASH shell script in here which does
aspire deploy,helm pack .andhelm install
Note the following issues
-
.WithContainerRegistryin AppHost's code has no effect: the passed private registry is ignored, and instead images are pulled from docker.io which results in an image pull error. -
Note a number of errors pointing that YAML assets generated by aspire deploy are invalid:
Upgrade "aspire-app" failed: failed to create resource: Service "apiservice-service" is invalid: [spec.ports[0].targetPort: Invalid value: "8080": must contain at least one letter (a-z), spec.ports[1].targetPort: Invalid value: "8080": must contain at least one letter (a-z), spec.ports[1]: Duplicate value: core.ServicePort{Name:"", Protocol:"TCP", AppProtocol:(*string)(nil), Port:8080, TargetPort:intstr.IntOrString{Type:0, IntVal:0, StrVal:""}, NodePort:0}]
Error: UPGRADE FAILED: failed to create resource: Service "apiservice-service" is invalid: [spec.ports[0].targetPort: Invalid value: "8080": must contain at least one letter (a-z), spec.ports[1].targetPort: Invalid value: "8080": must contain at least one letter (a-z), spec.ports[1]: Duplicate value: core.ServicePort{Name:"", Protocol:"TCP", AppProtocol:(*string)(nil), Port:8080, TargetPort:intstr.IntOrString{Type:0, IntVal:0, StrVal:""}, NodePort:0}]
helm.go:92: 2026-01-21 15:43:35.593857 +1100 AEDT m=+1.191704401 [debug] Service "apiservice-service" is invalid: [spec.ports[0].targetPort: Invalid value: "8080": must contain at least one letter (a-z), spec.ports[1].targetPort: Invalid value: "8080": must contain at least one letter (a-z), spec.ports[1]: Duplicate value: core.ServicePort{Name:"", Protocol:"TCP", AppProtocol:(*string)(nil), Port:8080, TargetPort:intstr.IntOrString{Type:0, IntVal:0, StrVal:""}, NodePort:0}]
failed to create resource
- The YARP pod is crashing
k -n test logs gateway-deployment-6d4b94464c-prddm
warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]
Storing keys in a directory '/home/app/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed. For more information go to https://aka.ms/aspnet/dataprotectionwarning
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[62]
User profile is available. Using '/home/app/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
info: Yarp.ReverseProxy.Configuration.ConfigProvider.ConfigurationConfigProvider[1]
Loading proxy data from config.
fail: Microsoft.Extensions.ServiceDiscovery.ServiceEndpointWatcher[4]
Error resolving endpoints for service 'https+http://apiservice'.
System.Collections.Generic.KeyNotFoundException: The endpoint configuration section for service 'apiservice' endpoint 'https' has an invalid value with key '0'.
at Microsoft.Extensions.ServiceDiscovery.Configuration.ConfigurationServiceEndpointProvider.AddEndpoint(List`1 endpoints, IConfigurationSection section, String endpointName) in /_/src/Microsoft.Extensions.ServiceDiscovery/Configuration/ConfigurationServiceEndpointProvider.cs:line 196
at Microsoft.Extensions.ServiceDiscovery.Configuration.ConfigurationServiceEndpointProvider.PopulateAsync(IServiceEndpointBuilder endpoints, CancellationToken cancellationToken) in /_/src/Microsoft.Extensions.ServiceDiscovery/Configuration/ConfigurationServiceEndpointProvider.cs:line 126
at Microsoft.Extensions.ServiceDiscovery.ServiceEndpointWatcher.RefreshAsyncInternal() in /_/src/Microsoft.Extensions.ServiceDiscovery/ServiceEndpointWatcher.cs:line 150
Expected Behavior
The helm chart is generated and installed to my mi-k8s k8s cluster
Steps To Reproduce
Follow the describe section.
Exceptions (if any)
No response
.NET Version info
dotnet --version 10.0.102 aspire --version 13.1.0+8a4db1775c3fbae1c602022b636299cb04971fde
Anything else?
No response
Piling onto this a tiny bit, because there's an extra layer to this:
As you said, builder.AddKubernetesEnvironment() does not take builder.AddContainerRegistry() into account.
It also ignores resource.WithImagePushOptions() (and by extension resource.WithRemoteImageTag|Name). It will in almost all cases use resource.Name to determine the image name to use.
From the outside there's no "clean" way to fix this, since everything is internal. Here's my horribleness:
internal static class KubernetesResourceAccessor
{
private static readonly PropertyInfo dictProp = typeof(KubernetesResource)
.GetProperty("Parameters", BindingFlags.Instance | BindingFlags.NonPublic)!;
private static Type _valueType = typeof(KubernetesResource)
.GetNestedType("HelmExpressionWithValue", BindingFlags.NonPublic)!;
private static FieldInfo _backingField = _valueType.GetField("<Value>k__BackingField",
BindingFlags.Instance | BindingFlags.NonPublic)!;
/// <summary>
/// Replaces the image name in the KubernetesResource's Parameters dictionary.<br/>
/// In a... quite terrible way.
/// </summary>
/// <param name="resource">The resource to update the image name for</param>
/// <param name="imageName">The new name to set. Fully qualified if you need to.</param>
public static void Hax(KubernetesResource resource, string imageName)
{
var key = $"{resource.Name}_image";
var parameterDict = (System.Collections.IDictionary?)dictProp.GetValue(resource);
if (parameterDict is null || !parameterDict.Contains(key)) return;
var imageHelmExpression = parameterDict[key];
var existingValue = (string?)_backingField!.GetValue(imageHelmExpression);
if (existingValue is null) return;
var tag = existingValue.Split(':')[1];
var newValue = $"{imageName}:{tag}";
_backingField.SetValue(imageHelmExpression, newValue);
}
}
use by e.g. attaching a KubernetesServiceCustomizationAnnotation that reads your ContainerImagePushOptions and RegistryTargetAnnotation then doing Hax(...).
Re: the missing endpoint config, there's a bit of funny-business in the generation of the root values.yaml, the configmaps for the services, and the helm stuff.
e.g. you for your gateway:
gateway/config.yaml has data: APISERVICE_HTTPS: "{{ .Values.config.gateway.APISERVICE_HTTPS }}"
values.yaml does not have that value. Maybe it'd work of you supply it?
You'll also encounter issues if/when you add services with secrets: values.yaml will have things like secrets.cache.REDIS_PASSWORD: "SOMETHING", but the secrets.yaml will reference REDIS_PASSWORD: "{{ .Values.secrets.cache.cache_password }}".
Additional Kubernetes publishing issues encountered (Aspire 13.1.0)
We've been working on deploying an Aspire application to k3s and encountered several additional issues beyond what's already reported:
1. Port values rendered as strings instead of integers
Aspire generates Helm templates with quoted port expressions:
port: "{{ .Values.parameters.gateway.port_http }}"
This renders as port: "8080" (string) instead of port: 8080 (integer), causing Kubernetes validation errors. We had to post-process with sed to remove the quotes.
2. Secrets structure mismatch between templates and values.yaml
Templates reference per-service secret paths:
{{ .Values.secrets.otel_endpoint.redis_password }}
{{ .Values.secrets.gateway.postgres_password }}
But values.yaml only generates global secrets:
secrets:
redis:
REDIS_PASSWORD: ""
postgres:
POSTGRES_PASSWORD: ""
This causes nil pointer evaluating interface {} errors when running helm template.
3. Redis container command broken (TLS-related)
Aspire generates:
containers:
- command:
- "/bin/sh"
args:
- "redis-server"
- "--requirepass"
- "$REDIS_PASSWORD"
This fails with /bin/sh: 0: cannot open redis-server: No such file because /bin/sh without -c interprets the first arg as a script file to execute.
4. No imagePullSecrets in deployments
Generated deployments don't include imagePullSecrets, so pods can't pull from private registries (like GHCR). We had to patch the default ServiceAccount:
kubectl patch serviceaccount default -n <namespace> -p '{"imagePullSecrets":[{"name":"ghcr-pull-secret"}]}'
Environment:
- Aspire 13.1.0
- .NET 9
- Target: k3s on Hetzner
We're currently using post-processing (yq/sed) on the generated Helm chart to work around these issues. Happy to provide more details or a repro if helpful.
I searched the code and found https://github.com/dotnet/aspire/pull/13800 targeting 13.2.
This is addressed in #14351 — the Kubernetes publisher now skips the DefaultHttpsEndpoint when generating port mappings, matching the core framework's SetBothPortsEnvVariables() behavior. The https endpoint still gets an EndpointMapping for service discovery but reuses the http endpoint's HelmValue, so no duplicate K8s Service/container port is generated.