Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add labels to all generated resources, updated default labels to alig…
…n more with Kubernetes recommendations.
  • Loading branch information
hewe-saxo committed Jul 2, 2025
commit c0d1347bed3926d817fd20af6a77b821c315d71e
6 changes: 3 additions & 3 deletions src/Aspire.Hosting.Kubernetes/Extensions/HelmExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public static string ToHelmValuesSectionName(this string resourceName)
=> $"{resourceName.Replace("-", "_")}";

public static string ToHelmParameterExpression(this string parameterName, string resourceName)
=> $"{{{{ {ValuesSegment}.{ParametersKey}.{resourceName.ToHelmValuesSectionName()}.{parameterName} }}}}";
=> $"{{{{ {ValuesSegment}.{ParametersKey}.{resourceName}.{parameterName} }}}}".ToHelmValuesSectionName();

public static string ToHelmSecretExpression(this string parameterName, string resourceName)
=> $"{{{{ {ValuesSegment}.{SecretsKey}.{resourceName.ToHelmValuesSectionName()}.{parameterName} }}}}";
=> $"{{{{ {ValuesSegment}.{SecretsKey}.{resourceName}.{parameterName} }}}}".ToHelmValuesSectionName();

public static string ToHelmConfigExpression(this string parameterName, string resourceName)
=> $"{{{{ {ValuesSegment}.{ConfigKey}.{resourceName.ToHelmValuesSectionName()}.{parameterName} }}}}";
=> $"{{{{ {ValuesSegment}.{ConfigKey}.{resourceName}.{parameterName} }}}}".ToHelmValuesSectionName();

/// <summary>
/// Converts the specified resource name into a Kubernetes resource name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal static Deployment ToDeployment(this IResource resource, KubernetesResou
Metadata =
{
Name = resource.Name.ToDeploymentName(),
Labels = context.Labels.ToDictionary(),
},
Spec =
{
Expand Down Expand Up @@ -43,6 +44,7 @@ internal static StatefulSet ToStatefulSet(this IResource resource, KubernetesRes
Metadata =
{
Name = resource.Name.ToStatefulSetName(),
Labels = context.Labels.ToDictionary(),
},
Spec =
{
Expand Down Expand Up @@ -125,6 +127,7 @@ internal static StatefulSet ToStatefulSet(this IResource resource, KubernetesRes
Metadata =
{
Name = resource.Name.ToServiceName(),
Labels = context.Labels.ToDictionary(),
},
Spec =
{
Expand Down
10 changes: 8 additions & 2 deletions src/Aspire.Hosting.Kubernetes/KubernetesResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ internal IEnumerable<BaseKubernetesResource> GetTemplatedResources()

foreach (var resource in AdditionalResources)
{
foreach(var label in Labels)
{
resource.Metadata.Labels.TryAdd(label.Key, label.Value);
}

yield return resource;
}
}
Expand All @@ -105,8 +110,9 @@ private void SetLabels()
{
Labels = new()
{
["app"] = "aspire",
["component"] = resource.Name,
["app.kubernetes.io/name"] = Parent.HelmChartName,
["app.kubernetes.io/component"] = resource.Name,
["app.kubernetes.io/instance"] = "{{.Release.Name}}",
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public async Task PublishAsync_HandlesSpecialResourceName()
using var tempDir = new TempDirectory();
var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish, "default", outputPath: tempDir.Path);

builder.AddKubernetesEnvironment("env");
builder.AddKubernetesEnvironment("env").Resource.HelmChartName = "my-chart";

var param0 = builder.AddParameter("param0");
var param1 = builder.AddParameter("param1", secret: true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ kind: "ConfigMap"
metadata:
name: "servicea-config"
labels:
app: "aspire"
component: "ServiceA"
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "ServiceA"
app.kubernetes.io/instance: "{{.Release.Name}}"
data:
ASPNETCORE_URLS: "{{ .Values.config.ServiceA.ASPNETCORE_URLS }}"
OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EXCEPTION_LOG_ATTRIBUTES: "{{ .Values.config.ServiceA.OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EXCEPTION_LOG_ATTRIBUTES }}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ apiVersion: "apps/v1"
kind: "Deployment"
metadata:
name: "servicea-deployment"
labels:
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "ServiceA"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
template:
metadata:
labels:
app: "aspire"
component: "ServiceA"
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "ServiceA"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
containers:
- image: "{{ .Values.parameters.ServiceA.ServiceA_image }}"
Expand All @@ -26,8 +31,9 @@ spec:
imagePullPolicy: "IfNotPresent"
selector:
matchLabels:
app: "aspire"
component: "ServiceA"
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "ServiceA"
app.kubernetes.io/instance: "{{.Release.Name}}"
replicas: 1
revisionHistoryLimit: 3
strategy:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ apiVersion: "v1"
kind: "Service"
metadata:
name: "servicea-service"
labels:
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "ServiceA"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
type: "ClusterIP"
selector:
app: "aspire"
component: "ServiceA"
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "ServiceA"
app.kubernetes.io/instance: "{{.Release.Name}}"
ports:
- name: "http"
protocol: "TCP"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ kind: "ConfigMap"
metadata:
name: "serviceb-config"
labels:
app: "aspire"
component: "ServiceB"
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "ServiceB"
app.kubernetes.io/instance: "{{.Release.Name}}"
data:
ASPNETCORE_URLS: "{{ .Values.config.ServiceB.ASPNETCORE_URLS }}"
OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EXCEPTION_LOG_ATTRIBUTES: "{{ .Values.config.ServiceB.OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EXCEPTION_LOG_ATTRIBUTES }}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ apiVersion: "apps/v1"
kind: "Deployment"
metadata:
name: "serviceb-deployment"
labels:
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "ServiceB"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
template:
metadata:
labels:
app: "aspire"
component: "ServiceB"
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "ServiceB"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
containers:
- image: "{{ .Values.parameters.ServiceB.ServiceB_image }}"
Expand All @@ -26,8 +31,9 @@ spec:
imagePullPolicy: "IfNotPresent"
selector:
matchLabels:
app: "aspire"
component: "ServiceB"
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "ServiceB"
app.kubernetes.io/instance: "{{.Release.Name}}"
replicas: 1
revisionHistoryLimit: 3
strategy:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ apiVersion: "v1"
kind: "Service"
metadata:
name: "serviceb-service"
labels:
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "ServiceB"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
type: "ClusterIP"
selector:
app: "aspire"
component: "ServiceB"
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "ServiceB"
app.kubernetes.io/instance: "{{.Release.Name}}"
ports:
- name: "http"
protocol: "TCP"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@ apiVersion: "apps/v1"
kind: "Deployment"
metadata:
name: "service-deployment"
labels:
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "service"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
template:
metadata:
labels:
app: "aspire"
component: "service"
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "service"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
containers:
- image: "nginx:latest"
name: "service"
imagePullPolicy: "IfNotPresent"
selector:
matchLabels:
app: "aspire"
component: "service"
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "service"
app.kubernetes.io/instance: "{{.Release.Name}}"
replicas: 1
revisionHistoryLimit: 3
strategy:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ apiVersion: "apps/v1"
kind: "Deployment"
metadata:
name: "service-deployment"
labels:
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "service"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
template:
metadata:
labels:
app: "aspire"
component: "service"
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "service"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
containers:
- image: "nginx:latest"
Expand All @@ -19,8 +24,9 @@ spec:
imagePullPolicy: "Always"
selector:
matchLabels:
app: "aspire"
component: "service"
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "service"
app.kubernetes.io/instance: "{{.Release.Name}}"
replicas: 1
revisionHistoryLimit: 5
strategy:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ kind: "Rollout"
metadata:
name: "myapp-rollout"
labels:
app: "aspire"
component: "myapp"
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "myapp"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
replicas: 1
template:
metadata:
labels:
app: "aspire"
component: "myapp"
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "myapp"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
containers:
- image: "mcr.microsoft.com/dotnet/aspnet:8.0"
Expand All @@ -27,5 +29,6 @@ spec:
imagePullPolicy: "IfNotPresent"
selector:
matchLabels:
app: "aspire"
component: "myapp"
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "myapp"
app.kubernetes.io/instance: "{{.Release.Name}}"
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ apiVersion: "v1"
kind: "Service"
metadata:
name: "myapp-service"
labels:
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "myapp"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
type: "ClusterIP"
selector:
app: "aspire"
component: "myapp"
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "myapp"
app.kubernetes.io/instance: "{{.Release.Name}}"
ports:
- name: "http"
protocol: "TCP"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ kind: "ConfigMap"
metadata:
name: "myapp-config"
labels:
app: "aspire"
component: "myapp"
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "myapp"
app.kubernetes.io/instance: "{{.Release.Name}}"
data:
ASPNETCORE_ENVIRONMENT: "{{ .Values.config.myapp.ASPNETCORE_ENVIRONMENT }}"
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ apiVersion: "keda.sh/v1alpha1"
kind: "ScaledObject"
metadata:
name: "myapp-scaler"
labels:
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "myapp"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
scaleTargetRef:
name: "myapp-rollout"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ apiVersion: "apps/v1"
kind: "Deployment"
metadata:
name: "project1-deployment"
labels:
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "project1"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
template:
metadata:
labels:
app: "aspire"
component: "project1"
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "project1"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
containers:
- image: "{{ .Values.parameters.project1.project1_image }}"
Expand All @@ -19,8 +24,9 @@ spec:
imagePullPolicy: "IfNotPresent"
selector:
matchLabels:
app: "aspire"
component: "project1"
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "project1"
app.kubernetes.io/instance: "{{.Release.Name}}"
replicas: 1
revisionHistoryLimit: 3
strategy:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ kind: "ConfigMap"
metadata:
name: "project1-config"
labels:
app: "aspire"
component: "project1"
app.kubernetes.io/name: "aspire"
app.kubernetes.io/component: "project1"
app.kubernetes.io/instance: "{{.Release.Name}}"
data:
OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EXCEPTION_LOG_ATTRIBUTES: "{{ .Values.config.project1.OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EXCEPTION_LOG_ATTRIBUTES }}"
OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EVENT_LOG_ATTRIBUTES: "{{ .Values.config.project1.OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EVENT_LOG_ATTRIBUTES }}"
Expand Down
Loading
Loading