Skip to content

Commit

Permalink
feat: add functions to common.libsonnet for warpstream (#14123)
Browse files Browse the repository at this point in the history
  • Loading branch information
grobinson-grafana authored Sep 12, 2024
1 parent f226b59 commit 2bde071
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions production/ksonnet/loki/common.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,45 @@ local k = import 'ksonnet-util/kausal.libsonnet';
container.mixin.readinessProbe.httpGet.withPort($._config.http_listen_port) +
container.mixin.readinessProbe.withInitialDelaySeconds(15) +
container.mixin.readinessProbe.withTimeoutSeconds(1),

// parseCPU is used for conversion of Kubernetes CPU units to the corresponding float value of CPU cores.
// Moreover, the function assumes the input is in a correct Kubernetes format, i.e., an integer, a float,
// a string representation of an integer or a float, or a string containing a number ending with 'm'
// representing a number of millicores.
// Examples:
// parseCPU(10) = parseCPU("10") = 10
// parseCPU(4.5) = parse("4.5") = 4.5
// parseCPU("3000m") = 3000 / 1000
// parseCPU("3580m") = 3580 / 1000
// parseCPU("3980.7m") = 3980.7 / 1000
// parseCPU(0.5) = parse("0.5") = parse("500m") = 0.5
parseCPU(v)::
if std.isString(v) && std.endsWith(v, 'm') then std.parseJson(std.rstripChars(v, 'm')) / 1000
else if std.isString(v) then std.parseJson(v)
else if std.isNumber(v) then v
else 0,

// siToBytes is used to convert Kubernetes byte units to bytes.
// Only works for limited set of SI prefixes: Ki, Mi, Gi, Ti.
siToBytes(str):: (
// Utility converting the input to a (potentially decimal) number of bytes
local siToBytesDecimal(str) = (
if std.endsWith(str, 'Ki') then (
std.parseJson(std.rstripChars(str, 'Ki')) * std.pow(2, 10)
) else if std.endsWith(str, 'Mi') then (
std.parseJson(std.rstripChars(str, 'Mi')) * std.pow(2, 20)
) else if std.endsWith(str, 'Gi') then (
std.parseJson(std.rstripChars(str, 'Gi')) * std.pow(2, 30)
) else if std.endsWith(str, 'Ti') then (
std.parseJson(std.rstripChars(str, 'Ti')) * std.pow(2, 40)
) else (
std.parseJson(str)
)
);

// Round down to nearest integer
std.floor(siToBytesDecimal(str))
),
},

// functions for k8s objects
Expand Down

0 comments on commit 2bde071

Please sign in to comment.