Repository: cdk8s-team/cdk8s-plus
Pull request #1449 (2022-11-24 21:37:48)

fix(volume): names can become too long
For some volume types we default its name by prepending the volume type to the name of the volume source (configmap, pvc...). Problem is that this can surpass the 63 character limit kubernetes imposes on names.

This PR trims the volume name if necessary, by slicing characters from the end. 

Fixes https://github.com/cdk8s-team/cdk8s-plus/issues/1130


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();