Repository: cdk8s-team/cdk8s-plus
Pull request #1828 (2023-03-16 12:37:55)

feat(volume): CSI drivers (#1826)
# Backport

This will backport the following commits from `k8s-26/main` to `k8s-25/main`:
 - [feat(volume): CSI drivers (#1826)](https://github.com/cdk8s-team/cdk8s-plus/pull/1826)

<!--- Backport version: 8.5.0 -->

### Questions ?
Please refer to the [Backport tool documentation](https://github.com/sqren/backport)


Patch:
diff --git a/src/volume.ts b/src/volume.ts
index 5ea6a178..8f7e63e7 100644
--- a/src/volume.ts
+++ b/src/volume.ts
@@ -1,4 +1,4 @@
-import { Size } from 'cdk8s';
+import { Names, Size } from 'cdk8s';
 import { IConstruct, Construct } from 'constructs';
 import * as configmap from './config-map';
 import * as k8s from './imports/k8s';
@@ -221,6 +221,27 @@ export class Volume extends Construct implements IStorage {
     });
   }
 
+  /**
+   * Populate the volume from a CSI driver, for example the Secrets Store CSI
+   * Driver: https://secrets-store-csi-driver.sigs.k8s.io/introduction.html.
+   * Which in turn needs an associated provider to source the secrets, such as
+   * the AWS Secrets Manager and Systems Manager Parameter Store provider:
+   * https://aws.github.io/secrets-store-csi-driver-provider-aws/.
+   *
+   * @param driver The name of the CSI driver to use to populate the volume.
+   * @param options Options for the CSI volume, including driver-specific ones.
+   */
+  public static fromCsi(scope: Construct, id: string, driver: string, options: CsiVolumeOptions = { }): Volume {
+    return new Volume(scope, id, options.name ?? Names.toDnsLabel(scope, { extra: [id] }), {
+      csi: {
+        driver: driver,
+        fsType: options.fsType,
+        readOnly: options.readOnly,
+        volumeAttributes: options.attributes,
+      },
+    });
+  }
+
   /**
     * @internal
    */
@@ -656,4 +677,39 @@ export enum HostPathVolumeType {
    * A block device must exist at the given path.
    */
   BLOCK_DEVICE = 'BlockDevice'
+}
+
+/**
+ * Options for the CSI driver based volume.
+ */
+export interface CsiVolumeOptions {
+  /**
+   * The volume name.
+   *
+   * @default - auto-generated
+   */
+  readonly name?: string;
+
+  /**
+   * The filesystem type to mount. Ex. "ext4", "xfs", "ntfs". If not provided,
+   * the empty value is passed to the associated CSI driver, which will
+   * determine the default filesystem to apply.
+   *
+   * @default - driver-dependent
+   */
+  readonly fsType?: string;
+
+  /**
+   * Whether the mounted volume should be read-only or not.
+   *
+   * @default - false
+   */
+  readonly readOnly?: boolean;
+
+  /**
+   * Any driver-specific attributes to pass to the CSI volume builder.
+   *
+   * @default - undefined
+   */
+  readonly attributes?: {[key: string]: string};
 }
\ No newline at end of file


Test patch:
diff --git a/test/volume.test.ts b/test/volume.test.ts
index 96c04f10..0ea9d105 100644
--- a/test/volume.test.ts
+++ b/test/volume.test.ts
@@ -489,4 +489,57 @@ describe('fromHostPath', () => {
 
   });
 
+});
+
+describe('fromCsi', () => {
+  test('minimal definition', () => {
+    // GIVEN
+    const chart = Testing.chart();
+
+    // WHEN
+    const vol = Volume.fromCsi(chart, 'Csi', 'my-csi-driver');
+
+    // THEN
+    expect(vol._toKube()).toMatchInlineSnapshot(`
+      Object {
+        "csi": Object {
+          "driver": "my-csi-driver",
+          "fsType": undefined,
+          "readOnly": undefined,
+          "volumeAttributes": undefined,
+        },
+        "name": "test-csi-c8e2763d",
+      }
+    `);
+  });
+
+  test('custom', () => {
+    // GIVEN
+    const chart = Testing.chart();
+
+    // WHEN
+    const vol = Volume.fromCsi(chart, 'Csi', 'secrets-store.csi.k8s.io', {
+      attributes: {
+        secretProviderClass: 'my-csi',
+      },
+      fsType: 'ext4',
+      name: 'filesystem',
+      readOnly: true,
+    });
+
+    // THEN
+    expect(vol._toKube()).toMatchInlineSnapshot(`
+      Object {
+        "csi": Object {
+          "driver": "secrets-store.csi.k8s.io",
+          "fsType": "ext4",
+          "readOnly": true,
+          "volumeAttributes": Object {
+            "secretProviderClass": "my-csi",
+          },
+        },
+        "name": "filesystem",
+      }
+    `);
+  });
 });
\ No newline at end of file