Skip to content

Commit

Permalink
feat: provide CPU as milliCPU units
Browse files Browse the repository at this point in the history
BREAKING CHANGE: CPU is provided as milliCPU units (1 CPU = 1000 milliCPU units).
  • Loading branch information
gajus committed Jul 22, 2019
1 parent 281f992 commit f7ce704
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 18 deletions.
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ const main = async () => {
// name: 'authentication-proxy',
// resources: {
// limits: {
// cpu: '500m',
// cpu: 500',
// memory: 536870912
// },
// requests: {
// cpu: '250m',
// cpu: 250,
// memory: 268435456
// }
// }
Expand All @@ -68,11 +68,11 @@ const main = async () => {
// name: 'monitoring-proxy',
// resources: {
// limits: {
// cpu: '1',
// cpu: 1000',
// memory: 536870912
// },
// requests: {
// cpu: '500m',
// cpu: 500,
// memory: 268435456
// }
// }
Expand All @@ -81,11 +81,11 @@ const main = async () => {
// name: 'showtime-api',
// resources: {
// limits: {
// cpu: '2',
// cpu: 2000,
// memory: 2147483648
// },
// requests: {
// cpu: '1',
// cpu: 1000,
// memory: 1073741824
// }
// }
Expand All @@ -101,21 +101,21 @@ const main = async () => {
// {
// name: 'authentication-proxy',
// usage: {
// cpu: '0',
// cpu: 0,
// memory: 101044224
// }
// },
// {
// name: 'monitoring-proxy',
// usage: {
// cpu: '1m',
// cpu: 1000,
// memory: 42151936
// }
// },
// {
// name: 'showtime-api',
// usage: {
// cpu: '0',
// cpu: 0,
// memory: 1349738496
// }
// }
Expand Down Expand Up @@ -176,6 +176,11 @@ main();

```

## Units

* CPUs are reported as milliCPU units (1000 = 1 CPU).
* Memory is reported in bytes.

## Related projects

* [Iapetus](https://github.com/gajus/iapetus) – Prometheus metrics server.
Expand Down
7 changes: 4 additions & 3 deletions src/services/getPodResourceSpecification.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// @flow

import {
parseBytes
parseBytes,
parseCpu
} from '../utilities';
import type {
PodResourceSpecificationType,
Expand All @@ -19,14 +20,14 @@ export default async (httpClient: HttpClientType, serviceUrl: string, podNamespa

if (container.resources.limits) {
limits = {
cpu: container.resources.limits.cpu || null,
cpu: container.resources.limits.cpu ? parseCpu(container.resources.limits.cpu) : null,
memory: container.resources.limits.memory ? parseBytes(container.resources.limits.memory) : null
};
}

if (container.resources.requests) {
requests = {
cpu: container.resources.requests.cpu || null,
cpu: container.resources.requests.cpu ? parseCpu(container.resources.requests.cpu) : null,
memory: container.resources.requests.memory ? parseBytes(container.resources.requests.memory) : null
};
}
Expand Down
5 changes: 3 additions & 2 deletions src/services/getPodResourceUsage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// @flow

import {
parseBytes
parseBytes,
parseCpu
} from '../utilities';
import type {
PodResourceUsageType,
Expand All @@ -17,7 +18,7 @@ export default async (httpClient: HttpClientType, serviceUrl: string, podNamespa
containers.push({
name: container.name,
usage: {
cpu: container.usage.cpu,
cpu: parseCpu(container.usage.cpu),
memory: parseBytes(container.usage.memory)
}
});
Expand Down
8 changes: 4 additions & 4 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export type HttpClientType = (url: string) => Promise<Object>;
type ContainerResourceUsageType = {|
+name: string,
+usage: {|
+cpu: string,
+memory: string
+cpu: number,
+memory: number
|}
|};

Expand All @@ -19,8 +19,8 @@ export type PodResourceUsageType = {|
|};

type ResourceType = {|
+cpu: string | null,
+memory: string | null
+cpu: number | null,
+memory: number | null
|};

type ContainerResourceSpecificationType = {|
Expand Down
1 change: 1 addition & 0 deletions src/utilities/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

export {default as isKubernetesCredentialsPresent} from './isKubernetesCredentialsPresent';
export {default as parseBytes} from './parseBytes';
export {default as parseCpu} from './parseCpu';
export {default as read} from './read';
11 changes: 11 additions & 0 deletions src/utilities/parseCpu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @flow

export default (input: string): number => {
// '500m'
if (String(input).endsWith('m')) {
return parseInt(input.slice(0, -1), 10);
}

// '1' or '0.1'
return Math.round(parseFloat(String(input)) * 1000);
};
10 changes: 10 additions & 0 deletions test/preoom/utilities/parseCpu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @flow

import test from 'ava';
import parseCpu from '../../../src/utilities/parseCpu';

test('parses input to milliCPU units', (t) => {
t.assert(parseCpu('1') === 1000);
t.assert(parseCpu('0.1') === 100);
t.assert(parseCpu('500m') === 500);
});

0 comments on commit f7ce704

Please sign in to comment.