Skip to content

Fix Kubernetes publisher crash on Boolean/scalar environment variable values#15560

Open
mitchdenny wants to merge 1 commit intomainfrom
fix/15229-k8s-boolean-env
Open

Fix Kubernetes publisher crash on Boolean/scalar environment variable values#15560
mitchdenny wants to merge 1 commit intomainfrom
fix/15229-k8s-boolean-env

Conversation

@mitchdenny
Copy link
Member

Description

The Kubernetes publisher's ProcessValueAsync method in KubernetesResource.cs only handled string, 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 threw NotSupportedException: Unsupported value type: Boolean, crashing the publish pipeline.

This PR adds handling for IConvertible types (which covers all .NET primitive types) by converting them to their InvariantCulture string representation. This is placed right after the existing string check in ProcessValueAsync for maximum clarity.

Fixes #15229

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
    • No
  • Does the change require an update in our Aspire docs?
    • Yes
    • No

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>
Copilot AI review requested due to automatic review settings March 25, 2026 00:59
@github-actions
Copy link
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 15560

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 15560"

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.ProcessValueAsync to handle IConvertible scalar values by converting them to strings using InvariantCulture.
  • 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.

@mitchdenny mitchdenny requested a review from davidfowl March 25, 2026 01:47
@mitchdenny mitchdenny self-assigned this Mar 25, 2026
@mitchdenny mitchdenny added this to the 13.3 milestone Mar 25, 2026
Comment on lines +391 to +394
if (value is IConvertible)
{
return string.Format(CultureInfo.InvariantCulture, "{0}", value);
}
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);

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.

@github-actions
Copy link
Contributor

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.
GitHub was asked to rerun all failed jobs for that attempt, and the rerun is being tracked in the rerun attempt.
The job links below point to the failed attempt jobs that matched the retry-safe transient failure rules.

@JamesNK
Copy link
Member

JamesNK commented Mar 25, 2026

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.

@github-actions
Copy link
Contributor

🎬 CLI E2E Test Recordings — 49 recordings uploaded (commit 5d1c234)

View recordings
Test Recording
AddPackageInteractiveWhileAppHostRunningDetached ▶️ View Recording
AddPackageWhileAppHostRunningDetached ▶️ View Recording
AgentCommands_AllHelpOutputs_AreCorrect ▶️ View Recording
AgentInitCommand_DefaultSelection_InstallsSkillOnly ▶️ View Recording
AgentInitCommand_MigratesDeprecatedConfig ▶️ View Recording
AspireAddPackageVersionToDirectoryPackagesProps ▶️ View Recording
AspireUpdateRemovesAppHostPackageVersionFromDirectoryPackagesProps ▶️ View Recording
Banner_DisplayedOnFirstRun ▶️ View Recording
Banner_DisplayedWithExplicitFlag ▶️ View Recording
CertificatesClean_RemovesCertificates ▶️ View Recording
CertificatesTrust_WithNoCert_CreatesAndTrustsCertificate ▶️ View Recording
CertificatesTrust_WithUntrustedCert_TrustsCertificate ▶️ View Recording
ConfigSetGet_CreatesNestedJsonFormat ▶️ View Recording
CreateAndRunAspireStarterProject ▶️ View Recording
CreateAndRunAspireStarterProjectWithBundle ▶️ View Recording
CreateAndRunEmptyAppHostProject ▶️ View Recording
CreateAndRunJsReactProject ▶️ View Recording
CreateAndRunPythonReactProject ▶️ View Recording
CreateAndRunTypeScriptEmptyAppHostProject ▶️ View Recording
CreateAndRunTypeScriptStarterProject ▶️ View Recording
CreateStartAndStopAspireProject ▶️ View Recording
CreateTypeScriptAppHostWithViteApp ▶️ View Recording
DescribeCommandResolvesReplicaNames ▶️ View Recording
DescribeCommandShowsRunningResources ▶️ View Recording
DetachFormatJsonProducesValidJson ▶️ View Recording
DoctorCommand_DetectsDeprecatedAgentConfig ▶️ View Recording
DoctorCommand_WithSslCertDir_ShowsTrusted ▶️ View Recording
DoctorCommand_WithoutSslCertDir_ShowsPartiallyTrusted ▶️ View Recording
GlobalMigration_HandlesCommentsAndTrailingCommas ▶️ View Recording
GlobalMigration_HandlesMalformedLegacyJson ▶️ View Recording
GlobalMigration_PreservesAllValueTypes ▶️ View Recording
GlobalMigration_SkipsWhenNewConfigExists ▶️ View Recording
GlobalSettings_MigratedFromLegacyFormat ▶️ View Recording
InvalidAppHostPathWithComments_IsHealedOnRun ▶️ View Recording
LogsCommandShowsResourceLogs ▶️ View Recording
PsCommandListsRunningAppHost ▶️ View Recording
PsFormatJsonOutputsOnlyJsonToStdout ▶️ View Recording
PublishWithDockerComposeServiceCallbackSucceeds ▶️ View Recording
RestoreGeneratesSdkFiles ▶️ View Recording
RunWithMissingAwaitShowsHelpfulError ▶️ View Recording
SecretCrudOnDotNetAppHost ▶️ View Recording
SecretCrudOnTypeScriptAppHost ▶️ View Recording
StagingChannel_ConfigureAndVerifySettings_ThenSwitchChannels ▶️ View Recording
StopAllAppHostsFromAppHostDirectory ▶️ View Recording
StopAllAppHostsFromUnrelatedDirectory ▶️ View Recording
StopNonInteractiveMultipleAppHostsShowsError ▶️ View Recording
StopNonInteractiveSingleAppHost ▶️ View Recording
StopWithNoRunningAppHostExitsSuccessfully ▶️ View Recording
TypeScriptAppHostWithProjectReferenceIntegration ▶️ View Recording

📹 Recordings uploaded automatically from CI run #23519734284

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Kubernetes publisher fails with Unsupported value type: Boolean for Scalar env vars

3 participants