Repository: cdk8s-team/cdk8s-plus
Pull request #1086 (2022-08-30 08:58:05)

feat(container): ephemeral storage resource requirements (#962)
# Backport

This will backport the following commits from `k8s-24/main` to `k8s-23/main`:
 - [feat(container): ephemeral storage resource requirements (#962)](https://github.com/cdk8s-team/cdk8s-plus/pull/962)

<!--- Backport version: 8.9.3 -->

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


Patch:
diff --git a/src/container.ts b/src/container.ts
index 599ddbb8..d8e50e59 100644
--- a/src/container.ts
+++ b/src/container.ts
@@ -684,6 +684,8 @@ export class Container {
     const cpuRequest = this.resources?.cpu?.request?.amount;
     const memoryLimit = this.resources?.memory?.limit;
     const memoryRequest = this.resources?.memory?.request;
+    const ephemeralStorageLimit = this.resources?.ephemeralStorage?.limit;
+    const ephemeralStorageRequest = this.resources?.ephemeralStorage?.request;
 
     const limits: { [key: string]: k8s.Quantity } = {};
     const requests: { [key: string]: k8s.Quantity } = {};
@@ -694,12 +696,18 @@ export class Container {
     if (memoryLimit) {
       limits.memory = k8s.Quantity.fromString(memoryLimit.toMebibytes().toString() + 'Mi');
     }
+    if (ephemeralStorageLimit) {
+      limits['ephemeral-storage'] = k8s.Quantity.fromString(ephemeralStorageLimit.toGibibytes().toString() + 'Gi');
+    }
     if (cpuRequest) {
       requests.cpu = k8s.Quantity.fromString(cpuRequest);
     }
     if (memoryRequest) {
       requests.memory = k8s.Quantity.fromString(memoryRequest.toMebibytes().toString() + 'Mi');
     }
+    if (ephemeralStorageRequest) {
+      requests['ephemeral-storage'] = k8s.Quantity.fromString(ephemeralStorageRequest.toGibibytes().toString() + 'Gi');
+    }
 
     let resourceRequirements: k8s.ResourceRequirements | undefined = undefined;
     if (Object.keys(limits).length > 0 || Object.keys(requests).length > 0) {
@@ -863,6 +871,7 @@ export enum MountPropagation {
 export interface ContainerResources {
   readonly cpu?: CpuResources;
   readonly memory?: MemoryResources;
+  readonly ephemeralStorage?: EphemeralStorageResources;
 }
 
 /**
@@ -898,6 +907,14 @@ export interface MemoryResources {
   readonly limit?: container;
 }
 
+/**
+ * Emphemeral storage request and limit
+ */
+export interface EphemeralStorageResources {
+  readonly request?: container;
+  readonly limit?: container;
+}
+
 /**
  * A collection of env variables defined in other resources.
  */


Test patch:
diff --git a/test/container.test.ts b/test/container.test.ts
index 147f5047..9f026621 100644
--- a/test/container.test.ts
+++ b/test/container.test.ts
@@ -381,18 +381,24 @@ describe('Container', () => {
           request: Size.mebibytes(256),
           limit: Size.mebibytes(384),
         },
+        ephemeralStorage: {
+          request: Size.mebibytes(1024),
+          limit: Size.mebibytes(2048),
+        },
       },
       image: 'image',
     });
 
     expect(container._toKube().resources).toEqual({
       limits: {
-        cpu: k8s.Quantity.fromString('0.5'),
-        memory: k8s.Quantity.fromString('384Mi'),
+        'cpu': k8s.Quantity.fromString('0.5'),
+        'memory': k8s.Quantity.fromString('384Mi'),
+        'ephemeral-storage': k8s.Quantity.fromString('2Gi'),
       },
       requests: {
-        cpu: k8s.Quantity.fromString('300m'),
-        memory: k8s.Quantity.fromString('256Mi'),
+        'cpu': k8s.Quantity.fromString('300m'),
+        'memory': k8s.Quantity.fromString('256Mi'),
+        'ephemeral-storage': k8s.Quantity.fromString('1Gi'),
       },
     });
 
@@ -484,6 +490,44 @@ test('Can add only limits and requests on cpu', () => {
   });
 });
 
+test('Can add only limits and requests on emphemeral-storage', () => {
+  const container = new kplus.Container({
+    resources: {
+      ephemeralStorage: {
+        limit: Size.gibibytes(2),
+        request: Size.gibibytes(1),
+      },
+    },
+    image: 'image',
+  });
+
+  expect(container._toKube().resources).toEqual({
+    limits: {
+      'ephemeral-storage': k8s.Quantity.fromString('2Gi'),
+    },
+    requests: {
+      'ephemeral-storage': k8s.Quantity.fromString('1Gi'),
+    },
+  });
+});
+
+test('Can add only limits on emphemeral-storage', () => {
+  const container = new kplus.Container({
+    resources: {
+      ephemeralStorage: {
+        limit: Size.gibibytes(2),
+      },
+    },
+    image: 'image',
+  });
+
+  expect(container._toKube().resources).toEqual({
+    limits: {
+      'ephemeral-storage': k8s.Quantity.fromString('2Gi'),
+    },
+  });
+});
+
 test('default security context', () => {
 
   const container = new Container({ image: 'image' });