Skip to content

Commit

Permalink
feat: Size.asString that preserves original units (#1494)
Browse files Browse the repository at this point in the history
We currently do not have a way to return the unit in a way that would work in Kubernetes. This PR would be a prerequisite PR for this issue: cdk8s-team/cdk8s#1166. 

We are converting `memory` units to `mebibytes` and `ephemeral-storage` to `gibibyte` even if user passes something else. This does not seem correct. For instance, [here](https://github.com/cdk8s-team/cdk8s-plus/blob/k8s-27/main/src/container.ts#L880). So this PR would help resolving this issue.
I believe this [issue](cdk8s-team/cdk8s-plus#1950) is another instance of being impacted by us converting the inputs.
  • Loading branch information
vinayak-kukreja authored Sep 10, 2024
1 parent 721b340 commit b8dc5c2
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 13 deletions.
11 changes: 10 additions & 1 deletion docs/java.md
Original file line number Diff line number Diff line change
Expand Up @@ -2330,7 +2330,7 @@ If true, this reference points to the managing controller.

### SizeConversionOptions <a name="SizeConversionOptions" id="cdk8s.SizeConversionOptions"></a>

Options for how to convert time to a different unit.
Options for how to convert size to a different unit.

#### Initializer <a name="Initializer" id="cdk8s.SizeConversionOptions.Initializer"></a>

Expand Down Expand Up @@ -4039,6 +4039,7 @@ When the amount is passed as a token, unit conversion is not possible.

| **Name** | **Description** |
| --- | --- |
| <code><a href="#cdk8s.Size.asString">asString</a></code> | Returns amount with abbreviated storage unit. |
| <code><a href="#cdk8s.Size.toGibibytes">toGibibytes</a></code> | Return this storage as a total number of gibibytes. |
| <code><a href="#cdk8s.Size.toKibibytes">toKibibytes</a></code> | Return this storage as a total number of kibibytes. |
| <code><a href="#cdk8s.Size.toMebibytes">toMebibytes</a></code> | Return this storage as a total number of mebibytes. |
Expand All @@ -4047,6 +4048,14 @@ When the amount is passed as a token, unit conversion is not possible.

---

##### `asString` <a name="asString" id="cdk8s.Size.asString"></a>

```java
public java.lang.String asString()
```

Returns amount with abbreviated storage unit.

##### `toGibibytes` <a name="toGibibytes" id="cdk8s.Size.toGibibytes"></a>

```java
Expand Down
11 changes: 10 additions & 1 deletion docs/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -2366,7 +2366,7 @@ If true, this reference points to the managing controller.

### SizeConversionOptions <a name="SizeConversionOptions" id="cdk8s.SizeConversionOptions"></a>

Options for how to convert time to a different unit.
Options for how to convert size to a different unit.

#### Initializer <a name="Initializer" id="cdk8s.SizeConversionOptions.Initializer"></a>

Expand Down Expand Up @@ -4334,6 +4334,7 @@ When the amount is passed as a token, unit conversion is not possible.

| **Name** | **Description** |
| --- | --- |
| <code><a href="#cdk8s.Size.asString">as_string</a></code> | Returns amount with abbreviated storage unit. |
| <code><a href="#cdk8s.Size.toGibibytes">to_gibibytes</a></code> | Return this storage as a total number of gibibytes. |
| <code><a href="#cdk8s.Size.toKibibytes">to_kibibytes</a></code> | Return this storage as a total number of kibibytes. |
| <code><a href="#cdk8s.Size.toMebibytes">to_mebibytes</a></code> | Return this storage as a total number of mebibytes. |
Expand All @@ -4342,6 +4343,14 @@ When the amount is passed as a token, unit conversion is not possible.

---

##### `as_string` <a name="as_string" id="cdk8s.Size.asString"></a>

```python
def as_string() -> str
```

Returns amount with abbreviated storage unit.

##### `to_gibibytes` <a name="to_gibibytes" id="cdk8s.Size.toGibibytes"></a>

```python
Expand Down
11 changes: 10 additions & 1 deletion docs/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -2035,7 +2035,7 @@ If true, this reference points to the managing controller.

### SizeConversionOptions <a name="SizeConversionOptions" id="cdk8s.SizeConversionOptions"></a>

Options for how to convert time to a different unit.
Options for how to convert size to a different unit.

#### Initializer <a name="Initializer" id="cdk8s.SizeConversionOptions.Initializer"></a>

Expand Down Expand Up @@ -3555,6 +3555,7 @@ When the amount is passed as a token, unit conversion is not possible.

| **Name** | **Description** |
| --- | --- |
| <code><a href="#cdk8s.Size.asString">asString</a></code> | Returns amount with abbreviated storage unit. |
| <code><a href="#cdk8s.Size.toGibibytes">toGibibytes</a></code> | Return this storage as a total number of gibibytes. |
| <code><a href="#cdk8s.Size.toKibibytes">toKibibytes</a></code> | Return this storage as a total number of kibibytes. |
| <code><a href="#cdk8s.Size.toMebibytes">toMebibytes</a></code> | Return this storage as a total number of mebibytes. |
Expand All @@ -3563,6 +3564,14 @@ When the amount is passed as a token, unit conversion is not possible.

---

##### `asString` <a name="asString" id="cdk8s.Size.asString"></a>

```typescript
public asString(): string
```

Returns amount with abbreviated storage unit.

##### `toGibibytes` <a name="toGibibytes" id="cdk8s.Size.toGibibytes"></a>

```typescript
Expand Down
28 changes: 18 additions & 10 deletions src/size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ export class Size {
this.unit = unit;
}

/**
* Returns amount with abbreviated storage unit
*/
public asString(): string {
return `${this.amount}${this.unit.abbr}`;
}

/**
* Return this storage as a total number of kibibytes.
*/
Expand Down Expand Up @@ -104,11 +111,10 @@ export enum SizeRoundingBehavior {
FLOOR,
/** Don't round. Return even if the result is a fraction. */
NONE,

}

/**
* Options for how to convert time to a different unit.
* Options for how to convert size to a different unit.
*/
export interface SizeConversionOptions {
/**
Expand All @@ -119,13 +125,13 @@ export interface SizeConversionOptions {
}

class StorageUnit {
public static readonly Kibibytes = new StorageUnit('kibibytes', 1);
public static readonly Mebibytes = new StorageUnit('mebibytes', 1024);
public static readonly Gibibytes = new StorageUnit('gibibytes', 1024 * 1024);
public static readonly Tebibytes = new StorageUnit('tebibytes', 1024 * 1024 * 1024);
public static readonly Pebibytes = new StorageUnit('pebibytes', 1024 * 1024 * 1024 * 1024);
public static readonly Kibibytes = new StorageUnit('kibibytes', 1, 'Ki');
public static readonly Mebibytes = new StorageUnit('mebibytes', 1024, 'Mi');
public static readonly Gibibytes = new StorageUnit('gibibytes', 1024 * 1024, 'Gi');
public static readonly Tebibytes = new StorageUnit('tebibytes', 1024 * 1024 * 1024, 'Ti');
public static readonly Pebibytes = new StorageUnit('pebibytes', 1024 * 1024 * 1024 * 1024, 'Pi');

private constructor(public readonly label: string, public readonly inKibiBytes: number) {
private constructor(public readonly label: string, public readonly inKibiBytes: number, public readonly abbr: string) {
// MAX_SAFE_INTEGER is 2^53, so by representing storage in kibibytes,
// the highest storage we can represent is 8 exbibytes.
}
Expand All @@ -137,7 +143,9 @@ class StorageUnit {

function convert(amount: number, fromUnit: StorageUnit, toUnit: StorageUnit, options: SizeConversionOptions = {}) {
const rounding = options.rounding ?? SizeRoundingBehavior.FAIL;
if (fromUnit.inKibiBytes === toUnit.inKibiBytes) { return amount; }
if (fromUnit.inKibiBytes === toUnit.inKibiBytes) {
return amount;
}

const multiplier = fromUnit.inKibiBytes / toUnit.inKibiBytes;
const value = amount * multiplier;
Expand All @@ -153,4 +161,4 @@ function convert(amount: number, fromUnit: StorageUnit, toUnit: StorageUnit, opt
}
return value;
}
}
}
7 changes: 7 additions & 0 deletions test/size.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,10 @@ test('rounding behavior', () => {
expect(size.toKibibytes({ rounding: SizeRoundingBehavior.NONE })).toEqual(5_324_800);
});

test('asString function gives abbreviated units', () => {
expect(Size.kibibytes(10).asString()).toEqual('10Ki');
expect(Size.mebibytes(10).asString()).toEqual('10Mi');
expect(Size.gibibytes(10).asString()).toEqual('10Gi');
expect(Size.tebibytes(10).asString()).toEqual('10Ti');
expect(Size.pebibyte(10).asString()).toEqual('10Pi');
});

0 comments on commit b8dc5c2

Please sign in to comment.