Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 6 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,16 @@ 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();

public static string ToHelmChartName(this string applicationName)
=> applicationName.ToLower().Replace("_", "-").Replace(".", "-");

/// <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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Kubernetes;
using Aspire.Hosting.Kubernetes.Extensions;
using Aspire.Hosting.Lifecycle;

namespace Aspire.Hosting;
Expand Down Expand Up @@ -34,7 +35,10 @@ public static IResourceBuilder<KubernetesEnvironmentResource> AddKubernetesEnvir

builder.AddKubernetesInfrastructureCore();

var resource = new KubernetesEnvironmentResource(name);
var resource = new KubernetesEnvironmentResource(name)
{
HelmChartName = builder.Environment.ApplicationName.ToHelmChartName()
};
if (builder.ExecutionContext.IsRunMode)
{

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,8 @@ 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")
.WithProperties(k => k.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
@@ -1,5 +1,5 @@
apiVersion: "v2"
name: "aspire"
name: "aspire-hosting-tests"
version: "0.1.0"
kubeVersion: ">= 1.18.0-0"
description: "Aspire Helm Chart"
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-hosting-tests"
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-hosting-tests"
app.kubernetes.io/component: "ServiceA"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
template:
metadata:
labels:
app: "aspire"
component: "ServiceA"
app.kubernetes.io/name: "aspire-hosting-tests"
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-hosting-tests"
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-hosting-tests"
app.kubernetes.io/component: "ServiceA"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
type: "ClusterIP"
selector:
app: "aspire"
component: "ServiceA"
app.kubernetes.io/name: "aspire-hosting-tests"
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
@@ -1,5 +1,5 @@
apiVersion: "v2"
name: "aspire"
name: "aspire-hosting-tests"
version: "0.1.0"
kubeVersion: ">= 1.18.0-0"
description: "Aspire Helm Chart"
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-hosting-tests"
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-hosting-tests"
app.kubernetes.io/component: "ServiceB"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
template:
metadata:
labels:
app: "aspire"
component: "ServiceB"
app.kubernetes.io/name: "aspire-hosting-tests"
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-hosting-tests"
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-hosting-tests"
app.kubernetes.io/component: "ServiceB"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
type: "ClusterIP"
selector:
app: "aspire"
component: "ServiceB"
app.kubernetes.io/name: "aspire-hosting-tests"
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
@@ -1,5 +1,5 @@
apiVersion: "v2"
name: "aspire"
name: "aspire-hosting-tests"
version: "0.1.0"
kubeVersion: ">= 1.18.0-0"
description: "Aspire Helm Chart"
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-hosting-tests"
app.kubernetes.io/component: "service"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
template:
metadata:
labels:
app: "aspire"
component: "service"
app.kubernetes.io/name: "aspire-hosting-tests"
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-hosting-tests"
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-hosting-tests"
app.kubernetes.io/component: "service"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
template:
metadata:
labels:
app: "aspire"
component: "service"
app.kubernetes.io/name: "aspire-hosting-tests"
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-hosting-tests"
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
@@ -1,5 +1,5 @@
apiVersion: "v2"
name: "aspire"
name: "aspire-hosting-tests"
version: "0.1.0"
kubeVersion: ">= 1.18.0-0"
description: "Aspire Helm Chart"
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-hosting-tests"
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-hosting-tests"
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-hosting-tests"
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-hosting-tests"
app.kubernetes.io/component: "myapp"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
type: "ClusterIP"
selector:
app: "aspire"
component: "myapp"
app.kubernetes.io/name: "aspire-hosting-tests"
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-hosting-tests"
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-hosting-tests"
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
@@ -1,5 +1,5 @@
apiVersion: "v2"
name: "aspire"
name: "aspire-hosting-tests"
version: "0.1.0"
kubeVersion: ">= 1.18.0-0"
description: "Aspire Helm Chart"
Expand Down
Loading