Skip to content

Commit 2bde071

Browse files
feat: add functions to common.libsonnet for warpstream (#14123)
1 parent f226b59 commit 2bde071

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

production/ksonnet/loki/common.libsonnet

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,45 @@ local k = import 'ksonnet-util/kausal.libsonnet';
4444
container.mixin.readinessProbe.httpGet.withPort($._config.http_listen_port) +
4545
container.mixin.readinessProbe.withInitialDelaySeconds(15) +
4646
container.mixin.readinessProbe.withTimeoutSeconds(1),
47+
48+
// parseCPU is used for conversion of Kubernetes CPU units to the corresponding float value of CPU cores.
49+
// Moreover, the function assumes the input is in a correct Kubernetes format, i.e., an integer, a float,
50+
// a string representation of an integer or a float, or a string containing a number ending with 'm'
51+
// representing a number of millicores.
52+
// Examples:
53+
// parseCPU(10) = parseCPU("10") = 10
54+
// parseCPU(4.5) = parse("4.5") = 4.5
55+
// parseCPU("3000m") = 3000 / 1000
56+
// parseCPU("3580m") = 3580 / 1000
57+
// parseCPU("3980.7m") = 3980.7 / 1000
58+
// parseCPU(0.5) = parse("0.5") = parse("500m") = 0.5
59+
parseCPU(v)::
60+
if std.isString(v) && std.endsWith(v, 'm') then std.parseJson(std.rstripChars(v, 'm')) / 1000
61+
else if std.isString(v) then std.parseJson(v)
62+
else if std.isNumber(v) then v
63+
else 0,
64+
65+
// siToBytes is used to convert Kubernetes byte units to bytes.
66+
// Only works for limited set of SI prefixes: Ki, Mi, Gi, Ti.
67+
siToBytes(str):: (
68+
// Utility converting the input to a (potentially decimal) number of bytes
69+
local siToBytesDecimal(str) = (
70+
if std.endsWith(str, 'Ki') then (
71+
std.parseJson(std.rstripChars(str, 'Ki')) * std.pow(2, 10)
72+
) else if std.endsWith(str, 'Mi') then (
73+
std.parseJson(std.rstripChars(str, 'Mi')) * std.pow(2, 20)
74+
) else if std.endsWith(str, 'Gi') then (
75+
std.parseJson(std.rstripChars(str, 'Gi')) * std.pow(2, 30)
76+
) else if std.endsWith(str, 'Ti') then (
77+
std.parseJson(std.rstripChars(str, 'Ti')) * std.pow(2, 40)
78+
) else (
79+
std.parseJson(str)
80+
)
81+
);
82+
83+
// Round down to nearest integer
84+
std.floor(siToBytesDecimal(str))
85+
),
4786
},
4887

4988
// functions for k8s objects

0 commit comments

Comments
 (0)