Skip to content

Add Kubernetes fileKeyRef environment variable support - #20847

Merged
Mitch Denny (mitchdenny) merged 1 commit into
mainfrom
mitchdenny-kubernetes-filekeyref-support
Oct 11, 2026
Merged

Mitch Denny (mitchdenny) merged 1 commit into
mainfrom
mitchdenny-kubernetes-filekeyref-support

Conversation

@mitchdenny

Copy link
Copy Markdown
Member

Description

Allow Kubernetes workloads to source individual environment variables from an env file written by an init container, without mounting the file into the application container or overriding its entrypoint.

Adds FileKeySelectorV1 and EnvVarSourceV1.FileKeyRef, matching Kubernetes 1.35's API schema. Snapshot tests cover omitted, false, and true optional values. A CLI E2E test builds and deploys an application to KinD, checking generated values, key renaming, optional missing keys, and the actual pod configuration. The Kubernetes node and BusyBox init-container images are digest-pinned.

Requires Kubernetes 1.34+ with EnvFiles enabled (enabled by default from 1.35). Init-container images must already be available to the cluster; this does not add image-publication primitives, AKS-specific behavior, or local-run emulation.

Fixes #19756

User-facing usage

C# workload customization can select a key from an emptyDir env file populated by an init container:

pod.Containers[0].Env.Add(new EnvVarV1
{
    Name = "APP_VALUE",
    ValueFrom = new EnvVarSourceV1
    {
        FileKeyRef = new FileKeySelectorV1
        {
            VolumeName = "generated-env",
            Path = "app.env",
            Key = "SOURCE_KEY",
            Optional = false
        }
    }
});

This is a C# Kubernetes resource-model API; no TypeScript API is introduced.

Validation

  • ./restore.sh — passed.
  • dotnet test --project tests/Aspire.Hosting.Kubernetes.Tests/Aspire.Hosting.Kubernetes.Tests.csproj --no-launch-profile -- --filter-not-trait 'quarantined=true' --filter-not-trait 'outerloop=true' — passed, 370 tests.
  • dotnet test --project tests/Aspire.Hosting.Kubernetes.Tests/Aspire.Hosting.Kubernetes.Tests.csproj --no-launch-profile -- --filter-method '*.PublishAsync_SupportsFileKeyRef' --filter-not-trait 'quarantined=true' --filter-not-trait 'outerloop=true' — passed, 3 cases with digest-pinned snapshots.
  • ASPIRE_E2E_DOTNET_IMAGE=aspire-filekeyref-built:local ASPIRE_E2E_ARCHIVE="$PWD/artifacts/filekeyref-linux-arm64.tar.gz" dotnet test --project tests/Aspire.Cli.EndToEnd.Tests/Aspire.Cli.EndToEnd.Tests.csproj --no-launch-profile -- --filter-method '*.DeployK8sWithFileKeyRef' --filter-not-trait 'quarantined=true' --filter-not-trait 'outerloop=true' — passed against local KinD/Kubernetes 1.35 with digest-pinned BusyBox.

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
      • If yes, did you have an API Review for it?
        • Yes
        • No
      • Did you add <remarks /> and <code /> elements on your triple slash comments?
        • Yes
        • No
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
      • If yes, have you done a threat model and had a security review?
        • Yes
        • No
    • No

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 37157142-2151-4210-8f9b-fc4b0c4e5138
@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
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 -- 20847

Or

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

@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Tests selector

5 / 98 PR test projects · 2 PR jobs, from 9 changed files.

Selected PR test projects (5 / 98)

Aspire.Cli.EndToEnd.Tests, Aspire.Hosting.Azure.Kubernetes.Tests, Aspire.Hosting.CodeGeneration.TypeScript.Tests, Aspire.Hosting.Docker.Tests, Aspire.Hosting.Kubernetes.Tests

Selected PR jobs (2)

polyglot, typescript-api-compat


How these were chosen — grouped by what changed

🔧 src/Aspire.Hosting.Kubernetes/Resources/EnvVarSourceV1.cs (changed source)
→ 1 directly: Aspire.Hosting.Kubernetes.Tests
→ 3 via the project graph: Aspire.Hosting.Azure.Kubernetes.Tests (2 hops), Aspire.Hosting.CodeGeneration.TypeScript.Tests, Aspire.Hosting.Docker.Tests

📦 affected project Aspire.Hosting.Kubernetes
→ 1 test: Aspire.Cli.EndToEnd.Tests

🔧 src/Aspire.Hosting.Kubernetes/Resources/FileKeySelectorV1.cs (changed source)
→ 1 directly: Aspire.Hosting.Kubernetes.Tests

🧪 tests/Aspire.Cli.EndToEnd.Tests/Helpers/KubernetesDeployTestHelpers.cs (changed test)
→ 1 directly: Aspire.Cli.EndToEnd.Tests

🧪 tests/Aspire.Cli.EndToEnd.Tests/KubernetesDeployWithFileKeyRefTests.cs (changed test)
→ 1 directly: Aspire.Cli.EndToEnd.Tests

🧪 tests/Aspire.Cli.EndToEnd.Tests/RadiusDeployTests.cs (changed test)
→ 1 directly: Aspire.Cli.EndToEnd.Tests

🧪 tests/Aspire.Hosting.Kubernetes.Tests/KubernetesPublisherTests.cs (changed test)
→ 1 directly: Aspire.Hosting.Kubernetes.Tests

🧪 tests/Aspire.Hosting.Kubernetes.Tests/Snapshots/KubernetesPublisherTests.PublishAsync_SupportsFileKeyRef_optional=False.verified.yaml (changed test)
→ 1 directly: Aspire.Hosting.Kubernetes.Tests

🧪 tests/Aspire.Hosting.Kubernetes.Tests/Snapshots/KubernetesPublisherTests.PublishAsync_SupportsFileKeyRef_optional=True.verified.yaml (changed test)
→ 1 directly: Aspire.Hosting.Kubernetes.Tests

🧪 tests/Aspire.Hosting.Kubernetes.Tests/Snapshots/KubernetesPublisherTests.PublishAsync_SupportsFileKeyRef_optional=null.verified.yaml (changed test)
→ 1 directly: Aspire.Hosting.Kubernetes.Tests

Job reasons

Job Triggered by
polyglot • affected project Aspire.Hosting.Kubernetes
• affected project Aspire.Hosting.Azure.Provisioning.ContainerService
typescript-api-compat affected project Aspire.Hosting.Kubernetes

Selection computed for commit cdb7971.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The API matches Kubernetes 1.35’s schema and has focused serialization and real deployment coverage.

0 open findings

What changed in this PR

Adds Kubernetes 1.34+ fileKeyRef support to the Kubernetes deployment resource model.

Changes:

  • Adds FileKeySelectorV1 and EnvVarSourceV1.FileKeyRef.
  • Verifies nullable optional serialization through snapshots.
  • Adds KinD deployment coverage for generated values, renamed keys, and optional missing keys.
File Description
src/​Aspire.Hosting.Kubernetes/​Resources/​FileKeySelectorV1.cs Defines the Kubernetes file-key selector model.
src/​Aspire.Hosting.Kubernetes/​Resources/​EnvVarSourceV1.cs Exposes fileKeyRef for environment values.
tests/​Aspire.Hosting.Kubernetes.Tests/​KubernetesPublisherTests.cs Tests generated Kubernetes YAML.
tests/​Aspire.Hosting.Kubernetes.Tests/​Snapshots/​KubernetesPublisherTests.PublishAsync_SupportsFileKeyRef_optional=null.verified.yaml Verifies omitted optional.
tests/​Aspire.Hosting.Kubernetes.Tests/​Snapshots/​KubernetesPublisherTests.PublishAsync_SupportsFileKeyRef_optional=False.verified.yaml Verifies explicit false.
tests/​Aspire.Hosting.Kubernetes.Tests/​Snapshots/​KubernetesPublisherTests.PublishAsync_SupportsFileKeyRef_optional=True.verified.yaml Verifies explicit true.
tests/​Aspire.Cli.EndToEnd.Tests/​KubernetesDeployWithFileKeyRefTests.cs Validates real Kubernetes deployment behavior.
tests/​Aspire.Cli.EndToEnd.Tests/​Helpers/​KubernetesDeployTestHelpers.cs Supports selecting a pinned KinD node image.
tests/​Aspire.Cli.EndToEnd.Tests/​RadiusDeployTests.cs Disambiguates an XML documentation reference.

🧠 Review effort: Balanced


💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@mitchdenny

Copy link
Copy Markdown
Member Author

Code review and PR testing report

PR information

Code review

No high-confidence correctness or regression findings.

Reviewed all nine changed files and the shared KinD setup/cleanup helpers. The new selector matches Kubernetes 1.35's upstream API/OpenAPI schema: required string fields volumeName, path, and key; an optional boolean optional; and the nullable EnvVarSource.fileKeyRef reference.

Impact analysis covered YAML serialization, optional/default handling, existing manifests when FileKeyRef is unset, actual container environment values, the helper's existing default-node-image behavior, and test isolation/cleanup. Snapshot coverage exercises all three optional states; the deployment test checks real environment values and pod configuration. The existing Aspire.Hosting.Kubernetes runtime-package rule already routes changes to CLI E2E tests, so no trigger-map change is necessary.

This is a generic code review, not a formal public API review or security review.

Artifact version verification

  • Expected commit: cdb79718e0a783ff528ae7d86590b269cba0bb15
  • Installed CLI: 17.0.0-pr.20847.gcdb79718
  • Hosting package: Aspire.Hosting.Kubernetes::17.0.0-pr.20847.gcdb79718
  • Result: Verified against the PR head.
  • Artifacts: CI run 37885753324.

Local Linux ARM64 testing used the PR's downloaded native archive and NuGet artifacts, not the earlier locally built archive. CI independently ran the committed test on Linux x64.

Changes analyzed

  • Hosting: EnvVarSourceV1.FileKeyRef and FileKeySelectorV1.
  • Tests: publisher theory and three snapshots; CLI KinD deployment test; explicit node-image helper overload and corresponding XML reference.
  • No CLI product, dashboard, template, client integration, extension, or CI infrastructure changes.

Test scenarios executed

Scenario Result Evidence
Full Kubernetes integration suite at PR head Passed: 370 tests Includes existing publishing regressions and the three new optional-state snapshots
Fresh CLI scaffold, add integration, build/push application image, and deploy through Helm to KinD/Kubernetes 1.35 Passed PR-version CLI and packages; pipeline succeeded
Required key selection and key renaming Passed APP_VALUE equals the generated init-container value; SECOND_VALUE equals second value
Missing optional key Passed OPTIONAL_VALUE remains unset; HTTP response ends with optional-unset
Actual pod configuration Passed Init container exited with code 0; application has no generated-env volume mount or command override; deployed fileKeyRef fields match
Missing required key Passed: startup blocked as expected Temporary deployment patch selected MISSING_REQUIRED_KEY with optional=false; matching replacement pod reached CreateContainerConfigError
Committed CLI E2E test in PR CI Passed: 1 test Linux x64 test job; TRX and recording inspected

Exact source-test command

dotnet test --project tests/Aspire.Hosting.Kubernetes.Tests/Aspire.Hosting.Kubernetes.Tests.csproj --no-launch-profile -- --filter-not-trait 'quarantined=true' --filter-not-trait 'outerloop=true'

Result: 370 passed, 0 failed.

Exact local artifact-test command

GITHUB_PR_NUMBER=20847 \
GITHUB_PR_HEAD_SHA=cdb79718e0a783ff528ae7d86590b269cba0bb15 \
ASPIRE_E2E_CLI_ARCHIVE_DIR="$PWD/artifacts/pr20847-testing/archive-flat" \
ASPIRE_E2E_DOTNET_IMAGE=aspire-pr20847-testing:local \
ASPIRE_E2E_CLI_VERSION_OUTPUT_DIR=/tmp/pr20847-cli-version \
dotnet test --project tests/Aspire.Cli.EndToEnd.Tests/Aspire.Cli.EndToEnd.Tests.csproj --no-launch-profile -- --filter-method '*.DeployK8sWithFileKeyRef' --filter-not-trait 'quarantined=true' --filter-not-trait 'outerloop=true'

Result: 1 passed, 0 failed in approximately 1 minute 38 seconds, including the temporary required-missing-key probe. The probe was removed after testing; the worktree is clean and the PR head is unchanged.

Representative observed output:

CLI_VERSION_EXACT:17.0.0-pr.20847.gcdb79718
Pipeline succeeded
PASSED: generated-<unique-cluster>|second value|optional-unset
REQUIRED_MISSING_KEY_BLOCKED_STARTUP

Setup issues and limitations

The first isolated dogfood install ran before artifacts were available. After upload, the local harness's PullRequest mode encountered an existing installer/PATH mismatch: the installer placed the CLI under ~/.aspire/dogfood/pr-20847/bin, while the helper exported ~/.aspire/bin. No feature scenario ran in those failed attempts.

Validation subsequently succeeded with the CI-equivalent LocalArchive mode using exact-head artifacts and a helper image containing the PR-head installer. An initial archive staging attempt also contained both Linux RIDs; staging only the ARM64 archive resolved that setup error. No product repairs or PR changes were made to work around these harness setup issues.

This report verifies the relevant scenarios, not the overall result of every job in the CI workflow. AKS registry mirroring and local-run emulation are outside this PR's scope.

Evidence and cleanup

  • CI evidence: logs-Cli.EndToEnd-KubernetesDeployWithFileKeyRefTests-ubuntu-latest from the linked run, including TRX results and an asciinema recording.
  • Local evidence: artifacts/pr20847-testing/, including install/test logs, downloaded CI diagnostics, and extracted terminal evidence.
  • Local CLI recording: artifacts/bin/Aspire.Cli.EndToEnd.Tests/Debug/net10.0/TestResults/recordings/aspire-cli-e2e/DeployK8sWithFileKeyRef.cast.
  • Test-owned KinD clusters, registry, workload images, and helper containers were cleaned up. Ignored diagnostic artifacts were retained locally; temporary test-source edits were removed.

Overall result

Relevant PR scenarios verified. No code-review findings. The publisher suite and actual deployment test pass, and the additional required-missing-key probe produces the expected blocked startup.

@mitchdenny
Mitch Denny (mitchdenny) merged commit a1f8cbd into main Oct 11, 2026
157 checks passed
@microsoft-github-policy-service microsoft-github-policy-service Bot added this to the 17.0 milestone Oct 11, 2026
@aspire-repo-bot

Copy link
Copy Markdown
Contributor

Pull request created: #1868

Generated by PR Documentation Check · copilot · auto · 58.7 AIC · ⌖ 10.5 AIC · ⊞ 20.4K

@aspire-repo-bot

Copy link
Copy Markdown
Contributor

📝 Documentation has been drafted in microsoft/aspire.dev#1868 targeting release/17.0.

Documented the new Kubernetes fileKeyRef environment variable support added in #20847. Added a new "Select environment variables from a generated file" section to src/frontend/src/content/docs/integrations/compute/kubernetes.mdx covering EnvVarSourceV1.FileKeyRef and FileKeySelectorV1, with a runnable C# example and the Kubernetes 1.34+/EnvFiles version requirement. Triggered signals: new_public_type (new FileKeySelectorV1 class in src/Aspire.Hosting.Kubernetes/Resources/FileKeySelectorV1.cs), pr_body_has_user_facing_section (PR body's "User-facing usage" section showing the FileKeyRef/FileKeySelectorV1 C# sample, which the new docs section mirrors), and pr_body_has_cli_flag_mention (validation commands citing the test project, not an actual CLI flag — no CLI surface changed, confirmed by inspecting all 9 changed files, none of which touch CLI commands/options).

Note

This draft PR needs human review before merging.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for fileKeyRef to the Kubernetes Publisher

3 participants