Skip to content
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
7 changes: 7 additions & 0 deletions src/Aspire.Hosting.Kubernetes/KubernetesResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,13 @@ private async Task<object> ProcessValueAsync(KubernetesEnvironmentContext contex
return s;
}

// Handle scalar/primitive types (bool, int, long, double, etc.)
// These can appear when third-party integrations set environment variables to non-string values.
if (value is IConvertible)
{
return string.Format(CultureInfo.InvariantCulture, "{0}", value);
}
Comment on lines +391 to +394
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use IConvertible. It has a limited range of support. For example, DateTimeOffset doesn't implement it.

I think IFormattable is what you want here, e.g.

if (value is IFormattable formattable) return formattable.ToString(null, CultureInfo.InvariantInfo);


if (value is EndpointReference ep)
{
var referencedResource = ep.Resource == this
Expand Down
49 changes: 49 additions & 0 deletions tests/Aspire.Hosting.Kubernetes.Tests/KubernetesPublisherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,55 @@ public async Task PublishAsync_ConditionalWithParameterBranch_UsesIfElseSyntax()
await settingsTask;
}

[Fact]
public async Task PublishAsync_HandlesScalarEnvironmentVariableTypes()
{
using var tempDir = new TestTempDirectory();
var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish, tempDir.Path);

builder.AddKubernetesEnvironment("env");

var api = builder.AddContainer("myapp", "mcr.microsoft.com/dotnet/aspnet:8.0")
.WithEnvironment(context =>
{
context.EnvironmentVariables["BOOL_TRUE"] = true;
context.EnvironmentVariables["BOOL_FALSE"] = false;
context.EnvironmentVariables["INT_VALUE"] = 42;
context.EnvironmentVariables["DOUBLE_VALUE"] = 3.14;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add DateTimeOffset, Uri, TimeSpan.

What happens if someone sets a nullable int (both with a value and null). I think it gets converted to a boxed int or null, depending on its value. But something to double check.

})
.WithEnvironment("STRING_VALUE", "hello");

var app = builder.Build();
app.Run();

var expectedFiles = new[]
{
"Chart.yaml",
"values.yaml",
"templates/myapp/deployment.yaml",
"templates/myapp/config.yaml",
};

SettingsTask settingsTask = default!;

foreach (var expectedFile in expectedFiles)
{
var filePath = Path.Combine(tempDir.Path, expectedFile);
var fileExtension = Path.GetExtension(filePath)[1..];

if (settingsTask is null)
{
settingsTask = Verify(File.ReadAllText(filePath), fileExtension);
}
else
{
settingsTask = settingsTask.AppendContentAsFile(File.ReadAllText(filePath), fileExtension);
}
}

await settingsTask;
}

private sealed class TestConditionProvider(string value) : IValueProvider, IManifestExpressionProvider
{
public string ValueExpression => "test-condition";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: "v2"
name: "aspire-hosting-tests"
version: "0.1.0"
kubeVersion: ">= 1.18.0-0"
description: "Aspire Helm Chart"
type: "application"
keywords:
- "aspire"
- "kubernetes"
appVersion: "0.1.0"
deprecated: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
parameters: {}
secrets: {}
config:
myapp:
BOOL_TRUE: "True"
BOOL_FALSE: "False"
INT_VALUE: "42"
DOUBLE_VALUE: "3.14"
STRING_VALUE: "hello"
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
apiVersion: "apps/v1"
kind: "Deployment"
metadata:
name: "myapp-deployment"
labels:
app.kubernetes.io/name: "aspire-hosting-tests"
app.kubernetes.io/component: "myapp"
app.kubernetes.io/instance: "{{ .Release.Name }}"
spec:
template:
metadata:
labels:
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"
name: "myapp"
envFrom:
- configMapRef:
name: "myapp-config"
imagePullPolicy: "IfNotPresent"
selector:
matchLabels:
app.kubernetes.io/name: "aspire-hosting-tests"
app.kubernetes.io/component: "myapp"
app.kubernetes.io/instance: "{{ .Release.Name }}"
replicas: 1
revisionHistoryLimit: 3
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
type: "RollingUpdate"
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
apiVersion: "v1"
kind: "ConfigMap"
metadata:
name: "myapp-config"
labels:
app.kubernetes.io/name: "aspire-hosting-tests"
app.kubernetes.io/component: "myapp"
app.kubernetes.io/instance: "{{ .Release.Name }}"
data:
BOOL_TRUE: "{{ .Values.config.myapp.BOOL_TRUE }}"
BOOL_FALSE: "{{ .Values.config.myapp.BOOL_FALSE }}"
INT_VALUE: "{{ .Values.config.myapp.INT_VALUE }}"
DOUBLE_VALUE: "{{ .Values.config.myapp.DOUBLE_VALUE }}"
STRING_VALUE: "{{ .Values.config.myapp.STRING_VALUE }}"
Loading