Repository: cdk8s-team/cdk8s-plus
Pull request #1846 (2023-03-21 11:14:13)

fix(volume): names can become too long (#1449)
# Backport

This will backport the following commits from `k8s-26/main` to `k8s-24/main`:
 - [fix(volume): names can become too long (#1449)](https://github.com/cdk8s-team/cdk8s-plus/pull/1449)

<!--- 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 8f7e63e7..a881f4b5 100644
--- a/src/volume.ts
+++ b/src/volume.ts
@@ -263,6 +263,7 @@ export class Volume extends Construct implements IStorage {
     public readonly name: string,
     private readonly config: Omit<k8s.Volume, 'name'>) {
     super(scope, id);
+    this.name = name.slice(0, 63);
   }
 
   public asVolume(): Volume {


Test patch:
diff --git a/test/volume.test.ts b/test/volume.test.ts
index 0ea9d105..1a931b62 100644
--- a/test/volume.test.ts
+++ b/test/volume.test.ts
@@ -24,6 +24,22 @@ describe('fromSecret', () => {
     `);
   });
 
+  test('volume name is trimmed if needed', () => {
+
+    const chart = Testing.chart();
+    const secret = new Secret(chart, 'my-secret', {
+      metadata: { name: 'veryveryveryveryveryveryveryveryveryveryveryveryveryveryverylong' },
+    });
+
+    // WHEN
+    const vol = Volume.fromSecret(chart, 'Secret', secret);
+
+    const expectedName = 'secret-veryveryveryveryveryveryveryveryveryveryveryveryveryvery';
+    expect(vol.name).toEqual(expectedName);
+    expect(vol._toKube().name).toEqual(expectedName);
+
+  });
+
   test('custom volume name', () => {
     // GIVEN
     const chart = Testing.chart();
@@ -123,6 +139,23 @@ describe('fromSecret', () => {
 });
 
 describe('fromConfigMap', () => {
+
+  test('volume name is trimmed if needed', () => {
+
+    const chart = Testing.chart();
+    const configMap = new ConfigMap(chart, 'my-config-map', {
+      metadata: { name: 'veryveryveryveryveryveryveryveryveryveryveryveryveryveryverylong' },
+    });
+
+    // WHEN
+    const vol = Volume.fromConfigMap(chart, 'ConfigMap', configMap);
+
+    const expectedName = 'configmap-veryveryveryveryveryveryveryveryveryveryveryveryveryv';
+    expect(vol.name).toEqual(expectedName);
+    expect(vol._toKube().name).toEqual(expectedName);
+
+  });
+
   test('minimal definition', () => {
     // GIVEN
     const chart = Testing.chart();
@@ -280,6 +313,20 @@ describe('fromEmptyDir', () => {
 
 describe('fromPersistentVolumeClaim', () => {
 
+  test('volume name is trimmed if needed', () => {
+
+    const chart = Testing.chart();
+    const pvc = PersistentVolumeClaim.fromClaimName(chart, 'Claim', 'veryveryveryveryveryveryveryveryveryveryveryveryveryveryverylong');
+
+    // WHEN
+    const volume = Volume.fromPersistentVolumeClaim(chart, 'Volume', pvc);
+
+    const expectedName = 'pvc-veryveryveryveryveryveryveryveryveryveryveryveryveryveryver';
+    expect(volume.name).toEqual(expectedName);
+    expect(volume._toKube().name).toEqual(expectedName);
+
+  });
+
   test('defaults', () => {
 
     const chart = Testing.chart();
@@ -321,6 +368,19 @@ describe('fromPersistentVolumeClaim', () => {
 
 describe('fromAwsElasticBlockStore', () => {
 
+  test('volume name is trimmed if needed', () => {
+
+    const chart = Testing.chart();
+
+    // WHEN
+    const volume = Volume.fromAwsElasticBlockStore(chart, 'Volume', 'veryveryveryveryveryveryveryveryveryveryveryveryveryveryverylong');
+
+    const expectedName = 'ebs-veryveryveryveryveryveryveryveryveryveryveryveryveryveryver';
+    expect(volume.name).toEqual(expectedName);
+    expect(volume._toKube().name).toEqual(expectedName);
+
+  });
+
   test('defaults', () => {
 
     const chart = Testing.chart();
@@ -363,6 +423,19 @@ describe('fromAwsElasticBlockStore', () => {
 
 describe('fromGcePersistentDisk', () => {
 
+  test('volume name is trimmed if needed', () => {
+
+    const chart = Testing.chart();
+
+    // WHEN
+    const volume = Volume.fromGcePersistentDisk(chart, 'Volume', 'veryveryveryveryveryveryveryveryveryveryveryveryveryveryverylong');
+
+    const expectedName = 'gcedisk-veryveryveryveryveryveryveryveryveryveryveryveryveryver';
+    expect(volume.name).toEqual(expectedName);
+    expect(volume._toKube().name).toEqual(expectedName);
+
+  });
+
   test('defaults', () => {
 
     const chart = Testing.chart();
@@ -405,6 +478,19 @@ describe('fromGcePersistentDisk', () => {
 
 describe('fromAzureDisk', () => {
 
+  test('volume name is trimmed if needed', () => {
+
+    const chart = Testing.chart();
+
+    // WHEN
+    const volume = Volume.fromAzureDisk(chart, 'Volume', 'veryveryveryveryveryveryveryveryveryveryveryveryveryveryverylong', 'uri');
+
+    const expectedName = 'azuredisk-veryveryveryveryveryveryveryveryveryveryveryveryveryv';
+    expect(volume.name).toEqual(expectedName);
+    expect(volume._toKube().name).toEqual(expectedName);
+
+  });
+
   test('defaults', () => {
 
     const chart = Testing.chart();