Skip to content

Set a default fsGroup for Kubernetes persistent volumes - #19374

Merged
Mitch Denny (mitchdenny) merged 3 commits into
mainfrom
mitchdenny-automatic-volume-fsgroup
Aug 15, 2026
Merged

Mitch Denny (mitchdenny) merged 3 commits into
mainfrom
mitchdenny-automatic-volume-fsgroup

Conversation

@mitchdenny

@mitchdenny Mitch Denny (mitchdenny) commented Aug 14, 2026 •

Copy link
Copy Markdown
Member

Description

This is a usability fix targeted for Aspire 13.5.

The problem

A Kubernetes volume access mode such as ReadWriteOnce controls how the volume can be attached and mounted; it does not grant the container's Linux process permission to write to the mounted filesystem.

That distinction produces a confusing experience with first-class Aspire persistent volumes. The PVC can be Bound, the pod can be Running, and the volume can be mounted successfully, while the application still fails on its first write because the filesystem is owned by root:root and the container runs as a non-root user. From the application's perspective this surfaces as an I/O permission error even though the deployment otherwise appears healthy.

The workaround before this change

Today, users must understand the underlying Kubernetes ownership model and customize every affected workload themselves:

builder.AddProject<Projects.WebFrontend>("webfrontend")
    .WithPersistentVolume(data, "/data")
    .PublishAsKubernetesService(resource =>
    {
        var podSpec = resource.Workload?.PodTemplate.Spec
            ?? throw new InvalidOperationException("The Kubernetes workload was not generated.");

        podSpec.SecurityContext ??= new();
        podSpec.SecurityContext.FsGroup = 2000;
        podSpec.SecurityContext.FsGroupChangePolicy = "OnRootMismatch";
    });

The alternatives are similarly low-level, such as adding a privileged init container to run chown, changing the image to use a known UID/GID, or configuring storage-driver-specific mount options. Requiring one of these workarounds makes a newly provisioned persistent volume look broken by default and leaks Kubernetes filesystem details into otherwise straightforward AppHost code.

How this change fixes it

Workloads bound through either WithPersistentVolume(...) overload now receive this pod security context automatically:

securityContext:
  fsGroup: 2000
  fsGroupChangePolicy: OnRootMismatch

fsGroup adds a supplemental group to the processes in the pod and, for supported volume types, instructs Kubernetes or the CSI driver to make the mounted volume accessible to that group. It does not change the image-defined UID or primary GID, so Aspire does not need to know which identity the image uses.

Aspire uses a stable group ID rather than selecting a new value on each deployment because numeric ownership is persisted on the volume. OnRootMismatch avoids unnecessary recursive ownership changes when the volume already has the expected group.

Existing AppHost code therefore requires no extra configuration:

var data = k8s.AddPersistentVolume("data")
    .WithCapacity("10Gi");

builder.AddProject<Projects.WebFrontend>("webfrontend")
    .WithPersistentVolume(data, "/data");

This behavior is limited to first-class WithPersistentVolume(...) bindings. Ordinary workloads and legacy PVC generation through the Kubernetes environment's default storage type are unchanged. Read-only mounts remain read-only.

Using a different fsGroup

The default is applied before existing PublishAsKubernetesService callbacks run, so a workload or cluster that requires a specific group can replace it:

builder.AddProject<Projects.WebFrontend>("webfrontend")
    .WithPersistentVolume(data, "/data")
    .PublishAsKubernetesService(resource =>
    {
        var podSpec = resource.Workload?.PodTemplate.Spec
            ?? throw new InvalidOperationException("The Kubernetes workload was not generated.");

        podSpec.SecurityContext ??= new();
        podSpec.SecurityContext.FsGroup = 3000;
    });

The generated OnRootMismatch policy is retained unless the callback also replaces it.

Opting out

A workload can remove the generated pod security context through the same customization mechanism:

builder.AddProject<Projects.WebFrontend>("webfrontend")
    .WithPersistentVolume(data, "/data")
    .PublishAsKubernetesService(resource =>
    {
        var podSpec = resource.Workload?.PodTemplate.Spec
            ?? throw new InvalidOperationException("The Kubernetes workload was not generated.");

        podSpec.SecurityContext = null;
    });

This is useful when ownership is managed by the image, an admission controller, or storage-specific configuration. Workloads that need other pod security-context settings can instead clear or replace only FsGroup and FsGroupChangePolicy.

Security and compatibility considerations

The group ID 2000 is an Aspire-managed default, not a Kubernetes-reserved value. Some storage drivers do not support fsGroup, CSI drivers may apply the group at mount time themselves, and cluster admission policies can restrict allowed group ranges. The existing customization callback remains the escape hatch for those environments.

This change intentionally grants processes in the pod supplemental group access to supported mounted volumes and can update persisted POSIX group ownership. It does not change the image-defined UID or primary GID, and it does not make a read-only mount writable.

Validation

  • Publisher tests verify the generated defaults for first-class persistent-volume bindings, including read-only mounts.
  • Publisher tests verify that Kubernetes customization can override the group to 3000 or remove the generated security context entirely.
  • The live AKS deployment test DeployAksPersistentVolumeSurvivesRedeploy passed against commit 7b565f163e. It verifies that the application runs as non-root UID 1654, receives supplemental group 2000, sees /srv/data owned by group 2000, and can write. It then redeploys with fsGroup: 3000, confirms the same PVC is reused, verifies UID 1654 now has supplemental group 3000 and /srv/data is owned by group 3000, reads the persisted data, and creates a new file.

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: efbea589-4f81-4baf-9e17-4f1af07af5ee
Copilot AI balanced review requested due to automatic review settings August 14, 2026 01:50
@github-actions

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 -- 19374

Or

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

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Deployment tests starting on PR #19374...

This will deploy to real Azure infrastructure. Results will be posted here when complete.

View workflow run

@github-actions

This comment has been minimized.

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.

Pull request overview

Adds default Kubernetes filesystem-group permissions for workloads using first-class persistent volumes.

Changes:

  • Emits fsGroup: 2000 with OnRootMismatch.
  • Documents customization through PublishAsKubernetesService.
  • Adds publisher snapshots and AKS deployment coverage for defaults and overrides.
Show a summary per file
File Description
src/Aspire.Hosting.Kubernetes/KubernetesResource.cs Applies the default pod security context.
src/Aspire.Hosting.Kubernetes/KubernetesPersistentVolumeExtensions.cs Documents the new behavior.
tests/Aspire.Hosting.Kubernetes.Tests/KubernetesPublisherTests.cs Tests defaults, overrides, and removal.
tests/Aspire.Deployment.EndToEnd.Tests/AksPersistentVolumeDeploymentTests.cs Verifies deployed AKS volume access.
tests/Aspire.Hosting.Kubernetes.Tests/Snapshots/KubernetesPublisherTests.PublishAsync_WithFirstClassPersistentVolume_OnProject_BindsViaMountPathOverload#00.verified.yaml Captures the project manifest default.
tests/Aspire.Hosting.Kubernetes.Tests/Snapshots/KubernetesPublisherTests.PublishAsync_WithFirstClassPersistentVolume_KubernetesCustomizationOverridesDefaultFsGroup.verified.yaml Captures an overridden group.
tests/Aspire.Hosting.Kubernetes.Tests/Snapshots/KubernetesPublisherTests.PublishAsync_WithFirstClassPersistentVolume_KubernetesCustomizationCanRemoveDefaultSecurityContext.verified.yaml Captures removal of the context.
tests/Aspire.Hosting.Kubernetes.Tests/Snapshots/KubernetesPublisherTests.PublishAsync_WithFirstClassPersistentVolume_FallsThroughForUnboundVolumes#00.verified.yaml Captures mixed-volume behavior.
tests/Aspire.Hosting.Kubernetes.Tests/Snapshots/KubernetesPublisherTests.PublishAsync_WithFirstClassPersistentVolume_BindsByName_PromotesToStatefulSet#00.verified.yaml Captures name-based binding behavior.

Review details

Suppressed comments (1)

src/Aspire.Hosting.Kubernetes/KubernetesPersistentVolumeExtensions.cs:289

  • This is a public API reference, so it should use see cref rather than code formatting; otherwise IntelliSense and generated API documentation cannot link users to the customization method.
    /// <c>PublishAsKubernetesService</c> to customize the pod security context when
  • Files reviewed: 9/9 changed files
  • Comments generated: 2
  • Review effort level: Balanced

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Deployment tests starting on PR #19374...

This will deploy to real Azure infrastructure. Results will be posted here when complete.

View workflow run

@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

@mitchdenny Mitch Denny (mitchdenny) added this to the 13.5 milestone Aug 14, 2026
Co-authored-by: Copilot App <[email protected]>
Copilot-Session: efbea589-4f81-4baf-9e17-4f1af07af5ee
Copilot AI review requested due to automatic review settings August 14, 2026 04:31
@github-actions

This comment has been minimized.

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.

Review details

Suppressed comments (1)

src/Aspire.Hosting.Kubernetes/KubernetesResource.cs:192

  • Using one global GID changes more than volume ownership: Kubernetes defines fsGroup as a supplemental group for every process in every container, so any image files or other mounts already group-readable/writable by numeric GID 2000 become accessible too. Because GIDs are not namespaced and 2000 is commonly available for image-defined users/groups, this security-sensitive access expansion can silently cross an image's intended permission boundary. Avoid assigning an arbitrary universal group by default; require an explicit/opt-in group (ideally through a first-class binding option) or otherwise obtain a group guaranteed by the target cluster/workload.
            securityContext.FsGroup ??= DefaultPersistentVolumeFsGroup;
  • Files reviewed: 9/9 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Deployment tests starting on PR #19374...

This will deploy to real Azure infrastructure. Results will be posted here when complete.

View workflow run

@adamint Adam Ratzman (adamint) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the fixed fsGroup: 2000 default needs another pass before this is ready. It is rejected by OpenShift’s default restricted SCC because the value is outside the namespace-allocated range, and it causes a one-time recursive permission rewrite for existing PVCs on upgrade. I also left a smaller test comment to assert the workload is actually non-root.

The implementation itself worked in the targeted publisher tests and the live AKS deployment path. That AKS run was at the parent commit, but the only delta to the current head is the XML-doc link fix.

@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: efbea589-4f81-4baf-9e17-4f1af07af5ee
Copilot AI review requested due to automatic review settings August 14, 2026 05:11
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Deployment tests starting on PR #19374...

This will deploy to real Azure infrastructure. Results will be posted here when complete.

View workflow run

@mitchdenny
Mitch Denny (mitchdenny) marked this pull request as ready for review August 14, 2026 08:41
@adamint

Copy link
Copy Markdown
Member

/backport to release/13.5

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/13.5 (link to workflow run)

@mitchdenny
Mitch Denny (mitchdenny) merged commit 002abcf into main Aug 15, 2026
372 checks passed
@mitchdenny
Mitch Denny (mitchdenny) deleted the mitchdenny-automatic-volume-fsgroup branch August 15, 2026 01:47
@github-actions github-actions Bot modified the milestones: 13.5, 13.6 Aug 15, 2026
@mitchdenny

Copy link
Copy Markdown
Member Author

/backport to release/13.5

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/13.5 (link to workflow run)

@aspire-repo-bot

Copy link
Copy Markdown
Contributor

Pull request created: #1502

Generated by PR Documentation Check · auto · 41.8 AIC · ⌖ 15.6 AIC · ⊞ 19.6K

@aspire-repo-bot

Copy link
Copy Markdown
Contributor

📝 Documentation has been drafted in microsoft/aspire.dev#1502 targeting release/13.5.

Added a new "Default pod security context" section to deployment/kubernetes/persistent-volumes.mdx documenting the automatic fsGroup: 2000 / fsGroupChangePolicy: OnRootMismatch pod security context applied to WithPersistentVolume(...) workloads, including how to override the group or remove the security context via PublishAsKubernetesService, and caveats about storage-driver/CSI/admission-policy support.

Note

This draft PR needs human review before merging.

This branch was previously deployed

1 inactive deployment
deployment-testing — 7b565f16 Deployed Aug 14, 2026 by mitchdenny via Deploy (Deployment.EndToEnd-Deployment) #692
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.

3 participants