Skip to content

Commit 94eea44

Browse files
committed
Replace limited K8s quantity parser with a full parser
1 parent 963d1ec commit 94eea44

1 file changed

Lines changed: 47 additions & 16 deletions

File tree

gitlab/commodore-compile.jsonnet

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,26 +105,57 @@ local gitInsteadOf(cluster) =
105105

106106
local cpu_limit(cluster) = std.get(cpu_limits, cluster, '2');
107107

108-
// Compute Commodore process count based on CPU limit for the cluster by
109-
// rounding down to the next nearest integer, clamped at 1.
110-
local proc_count(cluster) =
111-
local parseK8sCPU(cl) =
112-
local val = if std.endsWith(cl, 'm') then
108+
// K8s quantity parser.
109+
// Supported suffices as listed in `kubectl explain pod.spec.containers.resources.requests`.
110+
local parseK8sQuantity(q) =
111+
local decimalSIMap = {
112+
m: 1 / 1000,
113+
k: 1000,
114+
M: 1000 * 1000,
115+
G: 1000 * 1000 * 1000,
116+
T: 1000 * 1000 * 1000 * 1000,
117+
P: 1000 * 1000 * 1000 * 1000 * 1000,
118+
E: 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
119+
};
120+
local binarySIMap = {
121+
Ki: 1000,
122+
Mi: 1000 * 1000,
123+
Gi: 1000 * 1000 * 1000,
124+
Ti: 1000 * 1000 * 1000 * 1000,
125+
Pi: 1000 * 1000 * 1000 * 1000 * 1000,
126+
Ei: 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
127+
};
128+
local qlen = std.length(q);
129+
local decSuffix = if qlen > 1 then q[-1:] else '';
130+
local binSuffix = if qlen > 2 then q[-2:] else '';
131+
local val =
132+
if std.member(std.objectFields(decimalSIMap), decSuffix) then
133+
{
134+
scale: decimalSIMap[decSuffix],
135+
num: q[:-1],
136+
}
137+
else if std.member(std.objectFields(binarySIMap), binSuffix) then
113138
{
114-
isMilli: true,
115-
num: cl[:-1],
139+
scale: binarySIMap[binSuffix],
140+
num: q[:-2],
116141
}
117-
else {
118-
isMilli: false,
119-
num: cl,
120-
};
121-
local clnum = std.parseYaml(val.num);
122-
if std.isString(clnum) then
123-
error 'Failed to parse K8s CPU limit %s: the parser currently only supports unsuffixed values and milli-CPU values' % cl
124142
else
125-
if val.isMilli then clnum / 1000 else clnum;
143+
{
144+
scale: 1,
145+
num: q,
146+
};
147+
148+
local num = std.parseYaml(val.num);
149+
if !std.isNumber(num) then
150+
error 'Failed to parse K8s quantity %s: parser got %s' % [ q, val ]
151+
else
152+
num * val.scale;
153+
154+
// Compute Commodore process count based on CPU limit for the cluster by
155+
// rounding down to the next nearest integer, clamped at 1.
156+
local proc_count(cluster) =
126157
if std.extVar('commodore_proc_count_from_cpu_limit') != '' then (
127-
local cl = parseK8sCPU(cpu_limit(cluster));
158+
local cl = parseK8sQuantity(cpu_limit(cluster));
128159
if cl >= 1 then std.floor(cl) else 1
129160
) else
130161
// Commodore auto-selects the number of worker processes when the value of

0 commit comments

Comments
 (0)