Fix Kubernetes publisher crash on Boolean/scalar environment variable values#15560
Fix Kubernetes publisher crash on Boolean/scalar environment variable values#15560mitchdenny wants to merge 1 commit intomainfrom
Conversation
The Kubernetes publisher's ProcessValueAsync method only handled string, resource references, and expression types. When third-party integrations (like Scalar.Aspire) set environment variables to non-string scalar values (bool, int, double, etc.), it threw NotSupportedException. Add handling for IConvertible types (which covers all primitives) by converting them to their InvariantCulture string representation. Fixes #15229 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 15560Or
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 15560" |
There was a problem hiding this comment.
Pull request overview
Fixes a crash in the Kubernetes publisher when environment variable values are provided as non-string scalar types (e.g., bool, int, double) by converting such values to an invariant-culture string representation during processing.
Changes:
- Extend
KubernetesResource.ProcessValueAsyncto handleIConvertiblescalar values by converting them to strings usingInvariantCulture. - Add a regression test that publishes a Kubernetes environment with boolean/numeric environment variables.
- Add Verify snapshots covering the generated Helm chart outputs for the new test.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/Aspire.Hosting.Kubernetes/KubernetesResource.cs | Converts IConvertible env-var values to invariant strings to avoid NotSupportedException during publish. |
| tests/Aspire.Hosting.Kubernetes.Tests/KubernetesPublisherTests.cs | Adds a publish test that sets scalar (bool/int/double) environment variable values. |
| tests/Aspire.Hosting.Kubernetes.Tests/Snapshots/KubernetesPublisherTests.PublishAsync_HandlesScalarEnvironmentVariableTypes#00.verified.yaml | Snapshot for generated Chart.yaml. |
| tests/Aspire.Hosting.Kubernetes.Tests/Snapshots/KubernetesPublisherTests.PublishAsync_HandlesScalarEnvironmentVariableTypes#01.verified.yaml | Snapshot for generated values.yaml including scalar env vars as strings. |
| tests/Aspire.Hosting.Kubernetes.Tests/Snapshots/KubernetesPublisherTests.PublishAsync_HandlesScalarEnvironmentVariableTypes#02.verified.yaml | Snapshot for generated deployment template referencing the ConfigMap. |
| tests/Aspire.Hosting.Kubernetes.Tests/Snapshots/KubernetesPublisherTests.PublishAsync_HandlesScalarEnvironmentVariableTypes#03.verified.yaml | Snapshot for generated ConfigMap template wiring env vars from values. |
| if (value is IConvertible) | ||
| { | ||
| return string.Format(CultureInfo.InvariantCulture, "{0}", value); | ||
| } |
There was a problem hiding this comment.
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);| context.EnvironmentVariables["BOOL_TRUE"] = true; | ||
| context.EnvironmentVariables["BOOL_FALSE"] = false; | ||
| context.EnvironmentVariables["INT_VALUE"] = 42; | ||
| context.EnvironmentVariables["DOUBLE_VALUE"] = 3.14; |
There was a problem hiding this comment.
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.
|
Re-running the failed jobs in the CI workflow for this pull request because 1 job was identified as retry-safe transient failures in the CI run attempt.
|
|
How are env var values converted to strings before they're given to DCP? Should double check it is the same process and see whether there is opportunity for code reuse. |
|
🎬 CLI E2E Test Recordings — 49 recordings uploaded (commit View recordings
📹 Recordings uploaded automatically from CI run #23519734284 |
Description
The Kubernetes publisher's
ProcessValueAsyncmethod inKubernetesResource.csonly handledstring, resource references, and expression types. When third-party integrations (like Scalar.Aspire) set environment variables to non-string scalar values (e.g.,bool,int,double), the method threwNotSupportedException: Unsupported value type: Boolean, crashing the publish pipeline.This PR adds handling for
IConvertibletypes (which covers all .NET primitive types) by converting them to theirInvariantCulturestring representation. This is placed right after the existingstringcheck inProcessValueAsyncfor maximum clarity.Fixes #15229
Checklist