Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add functions to common.libsonnet for warpstream #14123

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions production/ksonnet/loki/common.libsonnet
Original file line number Diff line number Diff line change
@@ -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