KubernetesPersistentVolumeExtensions Methods
ClassMethods14 members
Provides extension methods for configuring Kubernetes
KubernetesPersistentVolumeResource resources and binding workloads to them. AddPersistentVolume(IResourceBuilder<KubernetesEnvironmentResource>, string) Section titled AddPersistentVolume(IResourceBuilder<KubernetesEnvironmentResource>, string)extensionIResourceBuilder<KubernetesPersistentVolumeResource> Adds a Kubernetes PersistentVolumeClaim resource to the application model as a child of the specified Kubernetes environment. The resource generates a
v1.PersistentVolumeClaim manifest in the Helm chart output at publish time. public static class KubernetesPersistentVolumeExtensions{ public static IResourceBuilder<KubernetesPersistentVolumeResource> AddPersistentVolume( this IResourceBuilder<KubernetesEnvironmentResource> builder, string name) { // ... }}Parameters
builderIResourceBuilder<KubernetesEnvironmentResource> The Kubernetes environment resource builder.namestringThe name of the persistent volume resource. Used as the generated PVC's metadata.name after lower-casing. To bind a workload using the name-match overload of KubernetesPersistentVolumeExtensions.WithPersistentVolume , add a WithVolume("name", "/path") on the workload using the same name.Returns
IResourceBuilder<KubernetesPersistentVolumeResource> A builder for the new KubernetesPersistentVolumeResource .Examples
var k8s = builder.AddKubernetesEnvironment("k8s");var data = k8s.AddPersistentVolume("data") .WithStorageClass("managed-csi") .WithCapacity("20Gi");WithAccessMode(IResourceBuilder<KubernetesPersistentVolumeResource>, PersistentVolumeAccessMode) Section titled WithAccessMode(IResourceBuilder<KubernetesPersistentVolumeResource>, PersistentVolumeAccessMode)extensionIResourceBuilder<KubernetesPersistentVolumeResource> Adds an access mode to the PVC's
spec.accessModes . Call multiple times to declare more than one mode. When unset, the environment's KubernetesEnvironmentResource.DefaultStorageReadWritePolicy is used. public static class KubernetesPersistentVolumeExtensions{ public static IResourceBuilder<KubernetesPersistentVolumeResource> WithAccessMode( this IResourceBuilder<KubernetesPersistentVolumeResource> builder, PersistentVolumeAccessMode accessMode) { // ... }}Parameters
builderIResourceBuilder<KubernetesPersistentVolumeResource> The persistent volume resource builder.accessMode PersistentReturns
IResourceBuilder<KubernetesPersistentVolumeResource> The same builder for chaining.WithCapacity(IResourceBuilder<KubernetesPersistentVolumeResource>, string) Section titled WithCapacity(IResourceBuilder<KubernetesPersistentVolumeResource>, string)extensionIResourceBuilder<KubernetesPersistentVolumeResource> Sets the requested storage capacity on the PVC's
spec.resources.requests.storage field. public static class KubernetesPersistentVolumeExtensions{ public static IResourceBuilder<KubernetesPersistentVolumeResource> WithCapacity( this IResourceBuilder<KubernetesPersistentVolumeResource> builder, string capacity) { // ... }}Parameters
builderIResourceBuilder<KubernetesPersistentVolumeResource> The persistent volume resource builder.capacitystringA Kubernetes quantity string (e.g. "10Gi" , "500Mi" ).Returns
IResourceBuilder<KubernetesPersistentVolumeResource> The same builder for chaining.WithCapacity(IResourceBuilder<KubernetesPersistentVolumeResource>, IResourceBuilder<ParameterResource>) Section titled WithCapacity(IResourceBuilder<KubernetesPersistentVolumeResource>, IResourceBuilder<ParameterResource>)extensionIResourceBuilder<KubernetesPersistentVolumeResource> Sets the requested storage capacity using a parameter resolved at deploy time.
public static class KubernetesPersistentVolumeExtensions{ public static IResourceBuilder<KubernetesPersistentVolumeResource> WithCapacity( this IResourceBuilder<KubernetesPersistentVolumeResource> builder, IResourceBuilder<ParameterResource> capacity) { // ... }}Parameters
builderIResourceBuilder<KubernetesPersistentVolumeResource> The persistent volume resource builder.capacityIResourceBuilder<ParameterResource> A parameter resource builder for the capacity quantity string.Returns
IResourceBuilder<KubernetesPersistentVolumeResource> The same builder for chaining.WithConfiguration(IResourceBuilder<KubernetesPersistentVolumeResource>, Action<PersistentVolumeClaim>) Section titled WithConfiguration(IResourceBuilder<KubernetesPersistentVolumeResource>, Action<PersistentVolumeClaim>)extensionIResourceBuilder<KubernetesPersistentVolumeResource> Configures the Kubernetes PersistentVolumeClaim generated for the persistent volume.
public static class KubernetesPersistentVolumeExtensions{ public static IResourceBuilder<KubernetesPersistentVolumeResource> WithConfiguration( this IResourceBuilder<KubernetesPersistentVolumeResource> builder, Action<PersistentVolumeClaim> configure) { // ... }}Parameters
builderIResourceBuilder<KubernetesPersistentVolumeResource> The persistent volume resource builder.configureAction<PersistentVolumeClaim> The action used to configure the generated persistent volume claim.Returns
IResourceBuilder<KubernetesPersistentVolumeResource> The same builder for chaining.Remarks
This callback runs when the Kubernetes manifest is generated and can be used to set properties that are not exposed by the other persistent volume configuration methods. Callbacks run in registration order after the fluent settings and environment defaults have been applied. TypeScript callbacks use asynchronous property and collection operations to customize labels, annotations, storage class, volume name, volume mode, access modes, and resource requests. Other Kubernetes manifest fields remain C#-only. Changing
claim.Metadata.Name changes the identity of the generated PVC; if you do so, ensure that any workload references are updated accordingly. Examples
var data = k8s.AddPersistentVolume("data") .WithConfiguration(claim => { claim.Metadata.Labels["example.com/retention"] = "retain"; claim.Spec.VolumeMode = "Filesystem"; });WithoutStorageClass(IResourceBuilder<KubernetesPersistentVolumeResource>) Section titled WithoutStorageClass(IResourceBuilder<KubernetesPersistentVolumeResource>)extensionIResourceBuilder<KubernetesPersistentVolumeResource> Omits
spec.storageClassName from the generated PVC so Kubernetes can assign the cluster's default StorageClass. To request a classless volume, call KubernetesPersistentVolumeExtensions.WithStorageClass with an empty string. public static class KubernetesPersistentVolumeExtensions{ public static IResourceBuilder<KubernetesPersistentVolumeResource> WithoutStorageClass( this IResourceBuilder<KubernetesPersistentVolumeResource> builder) { // ... }}Parameters
builderIResourceBuilder<KubernetesPersistentVolumeResource> The persistent volume resource builder.Returns
IResourceBuilder<KubernetesPersistentVolumeResource> The same builder for chaining.WithPersistentVolume(IResourceBuilder<T>, IResourceBuilder<KubernetesPersistentVolumeResource>) Section titled WithPersistentVolume(IResourceBuilder<T>, IResourceBuilder<KubernetesPersistentVolumeResource>)extensionIResourceBuilder<T> Binds a workload to a Kubernetes
KubernetesPersistentVolumeResource using name matching. The workload must declare a volume with a matching source name (typically via WithVolume("name", "/path") or an integration helper such as Postgres' WithDataVolume() ). The publisher rewrites that volume's pod-spec entry to reference the generated PVC and promotes the workload to a StatefulSet . public static class KubernetesPersistentVolumeExtensions{ public static IResourceBuilder<T> WithPersistentVolume<T>( this IResourceBuilder<T> builder, IResourceBuilder<KubernetesPersistentVolumeResource> volume) { // ... }}Parameters
builderIResourceBuilder<T> The workload resource builder.volumeIResourceBuilder<KubernetesPersistentVolumeResource> The persistent volume resource to bind to.Returns
IResourceBuilder<T> The same builder for chaining.Remarks
To bind a workload that does not have a matching named mount (for example a
ProjectResource ), use the overload that accepts a mountPath instead. The generated pod uses an Aspire-managed fsGroup of 2000 with an OnRootMismatch change policy so non-root containers can access supported volumes without matching the image's primary group. Use KubernetesServiceExtensions.PublishAsKubernetesService to customize the pod security context when a different group or policy is required. Examples
var pgData = k8s.AddPersistentVolume("pg-data") .WithStorageClass("managed-csi") .WithCapacity("20Gi");
var pg = builder.AddPostgres("pg") .WithDataVolume("pg-data") // ContainerMountAnnotation source = "pg-data" .WithPersistentVolume(pgData); // matches by name "pg-data"WithPersistentVolume(IResourceBuilder<T>, IResourceBuilder<KubernetesPersistentVolumeResource>, string, bool) Section titled WithPersistentVolume(IResourceBuilder<T>, IResourceBuilder<KubernetesPersistentVolumeResource>, string, bool)extensionIResourceBuilder<T> Binds a workload to a Kubernetes
KubernetesPersistentVolumeResource and mounts it at the specified path inside the workload's container. Unlike the name-match overload this one creates the underlying mount itself, so it works for workloads that don't already declare a named volume — including ApplicationModel.ProjectResource . public static class KubernetesPersistentVolumeExtensions{ public static IResourceBuilder<T> WithPersistentVolume<T>( this IResourceBuilder<T> builder, IResourceBuilder<KubernetesPersistentVolumeResource> volume, string mountPath, bool isReadOnly = false) { // ... }}Parameters
builderIResourceBuilder<T> The workload resource builder.volumeIResourceBuilder<KubernetesPersistentVolumeResource> The persistent volume resource to bind to.mountPath stringThe path inside the container where the volume will be mounted (e.g. "/var/lib/postgresql/data" ).isReadOnly booloptionalWhen true, mounts the volume read-only.Returns
IResourceBuilder<T> The same builder for chaining.Remarks
The generated pod uses an Aspire-managed
fsGroup of 2000 with an OnRootMismatch change policy so non-root containers can access supported volumes without matching the image's primary group. Use KubernetesServiceExtensions.PublishAsKubernetesService to customize the pod security context when a different group or policy is required. Examples
var media = k8s.AddPersistentVolume("media") .WithStorageClass("azurefile-csi") .WithCapacity("100Gi") .WithAccessMode(PersistentVolumeAccessMode.ReadWriteMany);
builder.AddDotnetProject("api", "../Api/Api.csproj") .WithPersistentVolume(media, "/srv/media");WithPersistentVolume(IResourceBuilder<T>, IResourceBuilder<KubernetesPersistentVolumeResource>, string, string, bool) Section titled WithPersistentVolume(IResourceBuilder<T>, IResourceBuilder<KubernetesPersistentVolumeResource>, string, string, bool)extensionIResourceBuilder<T> Binds a workload to a Kubernetes
KubernetesPersistentVolumeResource , mounts it at the specified path when deployed, and exposes the effective storage path through an environment variable. public static class KubernetesPersistentVolumeExtensions{ public static IResourceBuilder<T> WithPersistentVolume<T>( this IResourceBuilder<T> builder, IResourceBuilder<KubernetesPersistentVolumeResource> volume, string mountPath, string env, bool isReadOnly = false) { // ... }}Parameters
builderIResourceBuilder<T> The workload resource builder.volumeIResourceBuilder<KubernetesPersistentVolumeResource> The persistent volume resource to bind to.mountPath stringThe path inside the deployed container where the volume is mounted.envstringThe environment variable that receives the effective storage path.isReadOnly booloptionalWhen true, mounts the deployed volume read-only.Returns
IResourceBuilder<T> The same builder for chaining.Remarks
In run mode, projects and executables receive a deterministic host directory under the AppHost's
ApplicationModel.IAspireStore . Containers receive the in-container mountPath . In publish and deploy modes, every workload receives mountPath . Examples
var data = k8s.AddPersistentVolume("data") .WithCapacity("20Gi");
builder.AddDotnetProject("api", "../Api/Api.csproj") .WithPersistentVolume(data, "/srv/data", env: "DATA_PATH");WithPersistentVolumeName(IResourceBuilder<KubernetesPersistentVolumeResource>, string) Section titled WithPersistentVolumeName(IResourceBuilder<KubernetesPersistentVolumeResource>, string)extensionIResourceBuilder<KubernetesPersistentVolumeResource> Sets the name of the existing PersistentVolume to bind to in the generated PVC's
spec.volumeName . public static class KubernetesPersistentVolumeExtensions{ public static IResourceBuilder<KubernetesPersistentVolumeResource> WithPersistentVolumeName( this IResourceBuilder<KubernetesPersistentVolumeResource> builder, string persistentVolumeName) { // ... }}Parameters
builderIResourceBuilder<KubernetesPersistentVolumeResource> The persistent volume resource builder.persistentVolumeName stringThe name of the existing PersistentVolume.Returns
IResourceBuilder<KubernetesPersistentVolumeResource> The same builder for chaining.Exceptions
ArgumentNullExceptionThe builder or volume name is null.ArgumentExceptionThe volume name is empty or consists only of whitespace.Remarks
The named PersistentVolume must satisfy the claim's storage class, access modes, and requested capacity; otherwise, the claim remains unbound. Configure these values to match the existing volume.
WithStorageClass(IResourceBuilder<KubernetesPersistentVolumeResource>, string) Section titled WithStorageClass(IResourceBuilder<KubernetesPersistentVolumeResource>, string)extensionIResourceBuilder<KubernetesPersistentVolumeResource> Sets the Kubernetes storage class name on the PVC's
spec.storageClassName . When unset, the cluster's default storage class is used. An empty string explicitly disables storage class assignment. public static class KubernetesPersistentVolumeExtensions{ public static IResourceBuilder<KubernetesPersistentVolumeResource> WithStorageClass( this IResourceBuilder<KubernetesPersistentVolumeResource> builder, string storageClassName) { // ... }}Parameters
builderIResourceBuilder<KubernetesPersistentVolumeResource> The persistent volume resource builder.storageClassName stringThe storage class name (e.g. "managed-csi" , "gp3").Returns
IResourceBuilder<KubernetesPersistentVolumeResource> The same builder for chaining.WithStorageClass(IResourceBuilder<KubernetesPersistentVolumeResource>, IResourceBuilder<ParameterResource>) Section titled WithStorageClass(IResourceBuilder<KubernetesPersistentVolumeResource>, IResourceBuilder<ParameterResource>)extensionIResourceBuilder<KubernetesPersistentVolumeResource> Sets the Kubernetes storage class name using a parameter resolved at deploy time.
public static class KubernetesPersistentVolumeExtensions{ public static IResourceBuilder<KubernetesPersistentVolumeResource> WithStorageClass( this IResourceBuilder<KubernetesPersistentVolumeResource> builder, IResourceBuilder<ParameterResource> storageClassName) { // ... }}Parameters
builderIResourceBuilder<KubernetesPersistentVolumeResource> The persistent volume resource builder.storageClassName IResourceBuilder<ParameterResource> A parameter resource builder for the storage class name.Returns
IResourceBuilder<KubernetesPersistentVolumeResource> The same builder for chaining.WithVolumeAnnotation(IResourceBuilder<KubernetesPersistentVolumeResource>, string, string) Section titled WithVolumeAnnotation(IResourceBuilder<KubernetesPersistentVolumeResource>, string, string)extensionIResourceBuilder<KubernetesPersistentVolumeResource> Adds a Kubernetes metadata annotation to the generated PVC. These flush to
metadata.annotations on the rendered Kubernetes resource — not Aspire ApplicationModel.IResourceAnnotation instances. Common uses: CSI driver hints, dynamic provisioner parameters, external-secrets selectors, or backup tooling tags. public static class KubernetesPersistentVolumeExtensions{ public static IResourceBuilder<KubernetesPersistentVolumeResource> WithVolumeAnnotation( this IResourceBuilder<KubernetesPersistentVolumeResource> builder, string key, string value) { // ... }}Parameters
builderIResourceBuilder<KubernetesPersistentVolumeResource> The persistent volume resource builder.keystringThe annotation key (e.g. "volume.beta.kubernetes.io/storage-provisioner" ).valuestringThe annotation value.Returns
IResourceBuilder<KubernetesPersistentVolumeResource> The same builder for chaining.WithVolumeAnnotation(IResourceBuilder<KubernetesPersistentVolumeResource>, string, IResourceBuilder<ParameterResource>) Section titled WithVolumeAnnotation(IResourceBuilder<KubernetesPersistentVolumeResource>, string, IResourceBuilder<ParameterResource>)extensionIResourceBuilder<KubernetesPersistentVolumeResource> Adds a Kubernetes metadata annotation with a parameter value resolved at deploy time.
public static class KubernetesPersistentVolumeExtensions{ public static IResourceBuilder<KubernetesPersistentVolumeResource> WithVolumeAnnotation( this IResourceBuilder<KubernetesPersistentVolumeResource> builder, string key, IResourceBuilder<ParameterResource> value) { // ... }}Parameters
builderIResourceBuilder<KubernetesPersistentVolumeResource> The persistent volume resource builder.keystringThe annotation key.valueIResourceBuilder<ParameterResource> A parameter resource builder for the annotation value.Returns
IResourceBuilder<KubernetesPersistentVolumeResource> The same builder for chaining.