Skip to content

Replace hyphens with underscores in Helm environment variable keys #10839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions src/Aspire.Hosting.Kubernetes/Extensions/HelmExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,14 @@ public static bool ContainsHelmExpression(this string value)

public static bool ContainsHelmSecretExpression(this string value)
=> value.Contains($"{{{{ {ValuesSegment}.{SecretsKey}.", StringComparison.Ordinal);

/// <summary>
/// Converts the specified environment variable key into a valid Helm key.
/// </summary>
/// <remarks>
/// The environment variable names in Helm values files can not contain hyphens ('-').
/// <see href="link">https://helm.sh/docs/chart_best_practices/values/</see>
/// </remarks>
public static string ToHelmEnvironmentVariable(this string key)
=> $"{key.Replace("-", "_")}";
}
2 changes: 1 addition & 1 deletion src/Aspire.Hosting.Kubernetes/KubernetesResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private async Task ProcessEnvironmentAsync(KubernetesEnvironmentContext environm

foreach (var environmentVariable in context.EnvironmentVariables)
{
var key = environmentVariable.Key;
var key = environmentVariable.Key.ToHelmEnvironmentVariable();
var value = await this.ProcessValueAsync(environmentContext, executionContext, environmentVariable.Value).ConfigureAwait(false);

switch (value)
Expand Down
33 changes: 25 additions & 8 deletions tests/Aspire.Hosting.Kubernetes.Tests/KubernetesPublisherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,23 @@ public async Task PublishAsync_HandlesSpecialResourceName()
var cs = builder.AddConnectionString("api-cs", ReferenceExpression.Create($"Url={param0}, Secret={param1}"));

var param3 = builder.AddResource(ParameterResourceBuilderExtensions.CreateDefaultPasswordParameter(builder, "param3"));
builder.AddProject<TestProject>("SpeciaL-ApP", launchProfileName: null)

var containerResource = builder.AddContainer("Special-resource-with-hyphens", "my-image")
.AsHttp2Service()
.WithEnvironment("ORIGINAL_ENV", "value")
.WithHttpEndpoint(port: 80, targetPort: 80)
.WithHttpsEndpoint(port: 443, targetPort: 443)
.PublishAsKubernetesService(serviceResource =>
{
serviceResource.Workload!.PodTemplate.Spec.Containers[0].ImagePullPolicy = "Always";
(serviceResource.Workload as Deployment)!.Spec.RevisionHistoryLimit = 5;
});

var projectResource = builder.AddProject<TestProject>("SpeciaL-ApP", launchProfileName: null)
.WithEnvironment("param3", param3)
.WithReference(cs);
.WithReference(cs)
.WithReference(containerResource.GetEndpoint("http"))
.WithReference(containerResource.GetEndpoint("https"));

var app = builder.Build();

Expand All @@ -195,12 +209,15 @@ public async Task PublishAsync_HandlesSpecialResourceName()
// Assert
var expectedFiles = new[]
{
"Chart.yaml",
"values.yaml",
"templates/SpeciaL-ApP/deployment.yaml",
"templates/SpeciaL-ApP/config.yaml",
"templates/SpeciaL-ApP/secrets.yaml"
};
"Chart.yaml",
"values.yaml",
"templates/SpeciaL-ApP/deployment.yaml",
"templates/SpeciaL-ApP/config.yaml",
"templates/SpeciaL-ApP/secrets.yaml",
"templates/Special-resource-with-hyphens/deployment.yaml",
"templates/Special-resource-with-hyphens/service.yaml",
"templates/Special-resource-with-hyphens/config.yaml"
};

SettingsTask settingsTask = default!;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
parameters:
parameters:
SpeciaL_ApP:
SpeciaL_ApP_image: "SpeciaL-ApP:latest"
secrets:
SpeciaL_ApP:
param3: ""
config:
Special_resource_with_hyphens:
ORIGINAL_ENV: "value"
SpeciaL_ApP:
OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EXCEPTION_LOG_ATTRIBUTES: "true"
OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EVENT_LOG_ATTRIBUTES: "true"
OTEL_DOTNET_EXPERIMENTAL_OTLP_RETRY: "in_memory"
services__Special_resource_with_hyphens__http__0: "http://special-resource-with-hyphens-service:80"
services__Special_resource_with_hyphens__https__0: "https://special-resource-with-hyphens-service:443"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
---
apiVersion: "v1"
kind: "ConfigMap"
metadata:
Expand All @@ -11,3 +11,5 @@ data:
OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EXCEPTION_LOG_ATTRIBUTES: "{{ .Values.config.SpeciaL_ApP.OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EXCEPTION_LOG_ATTRIBUTES }}"
OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EVENT_LOG_ATTRIBUTES: "{{ .Values.config.SpeciaL_ApP.OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EVENT_LOG_ATTRIBUTES }}"
OTEL_DOTNET_EXPERIMENTAL_OTLP_RETRY: "{{ .Values.config.SpeciaL_ApP.OTEL_DOTNET_EXPERIMENTAL_OTLP_RETRY }}"
services__Special_resource_with_hyphens__http__0: "{{ .Values.config.SpeciaL_ApP.services__Special_resource_with_hyphens__http__0 }}"
services__Special_resource_with_hyphens__https__0: "{{ .Values.config.SpeciaL_ApP.services__Special_resource_with_hyphens__https__0 }}"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
---
apiVersion: "v1"
kind: "Secret"
metadata:
Expand All @@ -9,5 +9,5 @@ metadata:
app.kubernetes.io/instance: "{{.Release.Name}}"
stringData:
param3: "{{ .Values.secrets.SpeciaL_ApP.param3 }}"
ConnectionStrings__api-cs: "Url={{ .Values.config.SpeciaL_ApP.param0 }}, Secret={{ .Values.secrets.SpeciaL_ApP.param1 }}"
ConnectionStrings__api_cs: "Url={{ .Values.config.SpeciaL_ApP.param0 }}, Secret={{ .Values.secrets.SpeciaL_ApP.param1 }}"
type: "Opaque"
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
apiVersion: "apps/v1"
kind: "Deployment"
metadata:
name: "special-resource-with-hyphens-deployment"
labels:
app.kubernetes.io/name: "my-chart"
app.kubernetes.io/component: "Special-resource-with-hyphens"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
template:
metadata:
labels:
app.kubernetes.io/name: "my-chart"
app.kubernetes.io/component: "Special-resource-with-hyphens"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
containers:
- image: "my-image:latest"
name: "Special-resource-with-hyphens"
envFrom:
- configMapRef:
name: "special-resource-with-hyphens-config"
ports:
- name: "http"
protocol: "TCP"
containerPort: 80
- name: "https"
protocol: "TCP"
containerPort: 443
imagePullPolicy: "Always"
selector:
matchLabels:
app.kubernetes.io/name: "my-chart"
app.kubernetes.io/component: "Special-resource-with-hyphens"
app.kubernetes.io/instance: "{{.Release.Name}}"
replicas: 1
revisionHistoryLimit: 5
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
type: "RollingUpdate"
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
apiVersion: "v1"
kind: "Service"
metadata:
name: "special-resource-with-hyphens-service"
labels:
app.kubernetes.io/name: "my-chart"
app.kubernetes.io/component: "Special-resource-with-hyphens"
app.kubernetes.io/instance: "{{.Release.Name}}"
spec:
type: "ClusterIP"
selector:
app.kubernetes.io/name: "my-chart"
app.kubernetes.io/component: "Special-resource-with-hyphens"
app.kubernetes.io/instance: "{{.Release.Name}}"
ports:
- name: "http"
protocol: "TCP"
port: 80
targetPort: 80
- name: "https"
protocol: "TCP"
port: 443
targetPort: 443
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
apiVersion: "v1"
kind: "ConfigMap"
metadata:
name: "special-resource-with-hyphens-config"
labels:
app.kubernetes.io/name: "my-chart"
app.kubernetes.io/component: "Special-resource-with-hyphens"
app.kubernetes.io/instance: "{{.Release.Name}}"
data:
ORIGINAL_ENV: "{{ .Values.config.Special_resource_with_hyphens.ORIGINAL_ENV }}"
Loading