Repository: cdk8s-team/cdk8s-plus
Pull request #1085 (2022-08-30 08:57:59)

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

This will backport the following commits from `k8s-24/main` to `k8s-22/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 9ef33aa6..8b2bcb50 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) {
@@ -850,6 +858,7 @@ export enum MountPropagation {
 export interface ContainerResources {
   readonly cpu?: CpuResources;
   readonly memory?: MemoryResources;
+  readonly ephemeralStorage?: EphemeralStorageResources;
 }
 
 /**
@@ -885,6 +894,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 a4e6f685..097fa59e 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' });