Skip to content

Commit 36ef500

Browse files
committed
feat(postgres): per-application allocation ratio (resourcesAllocationRatio)
Requests are derived from limits by the cluster-wide cpu/memory allocation ratios only, so an operator who wants one database to keep a high limit while reserving what it normally uses has to change the ratio for every application in the cluster. A platform that bills by use and offers a generous limit per database needs the override per instance. cozy-lib reads an optional resourcesAllocationRatio.{cpu,memory} from the application's values before the cluster-wide ratio; unset, the rendered requests are unchanged for every chart that uses sanitize. A ratio below 1 is refused because the request would exceed the limit. Postgres is the first application to expose the value. Assisted-by: LLM Signed-off-by: Yasar Rahman <[email protected]>
1 parent 588d829 commit 36ef500

9 files changed

Lines changed: 287 additions & 24 deletions

File tree

‎api/apps/v1alpha1/postgresql/types.go‎

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎api/apps/v1alpha1/postgresql/zz_generated.deepcopy.go‎

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/apps/postgres/README.md‎

Lines changed: 15 additions & 12 deletions
Large diffs are not rendered by default.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
suite: resourcesAllocationRatio overrides the cluster-wide ratios for this database only
2+
3+
templates:
4+
- templates/db.yaml
5+
6+
release:
7+
name: pg-test
8+
namespace: tenant-test
9+
10+
set:
11+
_cluster:
12+
scheduling: {}
13+
resources:
14+
cpu: "4"
15+
memory: 16Gi
16+
17+
tests:
18+
- it: unset, the memory request equals the limit (cluster default memory ratio 1)
19+
documentSelector:
20+
path: kind
21+
value: Cluster
22+
asserts:
23+
- equal:
24+
path: spec.resources.limits.memory
25+
value: 16Gi
26+
- equal:
27+
path: spec.resources.requests.memory
28+
value: "17179869184"
29+
30+
- it: a memory ratio of 8 reserves an eighth of the limit, the limit is unchanged
31+
set:
32+
resourcesAllocationRatio:
33+
memory: 8
34+
documentSelector:
35+
path: kind
36+
value: Cluster
37+
asserts:
38+
- equal:
39+
path: spec.resources.limits.memory
40+
value: 16Gi
41+
- equal:
42+
path: spec.resources.requests.memory
43+
value: "2147483648"
44+
45+
- it: a ratio below 1 is refused
46+
set:
47+
resourcesAllocationRatio:
48+
memory: 0
49+
asserts:
50+
- failedTemplate:
51+
errorPattern: "resourcesAllocationRatio.memory must be 1 or more"

‎packages/apps/postgres/values.schema.json‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@
9494
"2xlarge"
9595
]
9696
},
97+
"resourcesAllocationRatio": {
98+
"description": "Overrides the cluster-wide allocation ratios for this database only, so its limits (from `resources` or `resourcesPreset`) can stay high while its requests reflect normal use. Unset by default: the cluster-wide ratios apply and the rendered requests are unchanged. A ratio below 1 is refused, since the request would exceed the limit.",
99+
"type": "object",
100+
"default": {},
101+
"properties": {
102+
"cpu": {
103+
"description": "CPU ratio for this database; overrides the cluster-wide `cpu-allocation-ratio`. Must be 1 or more.",
104+
"type": "integer"
105+
},
106+
"memory": {
107+
"description": "Memory ratio for this database; overrides the cluster-wide `memory-allocation-ratio`. Must be 1 or more.",
108+
"type": "integer"
109+
}
110+
}
111+
},
97112
"size": {
98113
"description": "Persistent Volume Claim size available for application data.",
99114
"default": "10Gi",

‎packages/apps/postgres/values.yaml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ resources: {}
6464
## @param {ResourcesPreset} resourcesPreset="t1.micro" - Default sizing preset used when `resources` is omitted.
6565
resourcesPreset: "t1.micro"
6666

67+
## @typedef {struct} ResourcesAllocationRatio - Per-instance overcommit for this database: each request is its limit divided by the ratio.
68+
## @field {*int} [cpu] - CPU ratio for this database; overrides the cluster-wide `cpu-allocation-ratio`. Must be 1 or more.
69+
## @field {*int} [memory] - Memory ratio for this database; overrides the cluster-wide `memory-allocation-ratio`. Must be 1 or more.
70+
71+
## @param {ResourcesAllocationRatio} [resourcesAllocationRatio] - Overrides the cluster-wide allocation ratios for this database only, so its limits (from `resources` or `resourcesPreset`) can stay high while its requests reflect normal use. Unset by default: the cluster-wide ratios apply and the rendered requests are unchanged. A ratio below 1 is refused, since the request would exceed the limit.
72+
resourcesAllocationRatio: {}
73+
6774
## @param {quantity} size - Persistent Volume Claim size available for application data.
6875
size: 10Gi
6976

‎packages/library/cozy-lib/templates/_resources.tpl‎

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,52 @@
88
{{- `40` }}
99
{{- end }}
1010

11+
{{- /*
12+
The application's own allocation ratio for one resource kind ("cpu" or "memory"), read from the
13+
application's values `resourcesAllocationRatio.<kind>`; empty when the application sets none, so
14+
the cluster-wide ratio applies. A ratio below 1 would make the request exceed the limit, so it is
15+
refused. include "cozy-lib.resources.appAllocationRatio" (list "memory" $)
16+
*/}}
17+
{{- define "cozy-lib.resources.appAllocationRatio" }}
18+
{{- $kind := index . 0 }}
19+
{{- $ratios := dict }}
20+
{{- with (index . 1).Values }}{{ $ratios = .resourcesAllocationRatio | default dict }}{{ end }}
21+
{{- if and (hasKey $ratios $kind) (not (kindIs "invalid" (index $ratios $kind))) }}
22+
{{- $r := index $ratios $kind }}
23+
{{- if lt (float64 $r) 1.0 }}
24+
{{- fail (printf "resourcesAllocationRatio.%s must be 1 or more (got %v): the request would exceed the limit" $kind $r) }}
25+
{{- end }}
26+
{{- $r }}
27+
{{- end }}
28+
{{- end }}
29+
1130
{{- define "cozy-lib.resources.cpuAllocationRatio" }}
12-
{{- include "cozy-lib.loadCozyConfig" . }}
13-
{{- $cozyConfig := index . 1 "cozyConfig" }}
14-
{{- if not $cozyConfig }}
15-
{{- include "cozy-lib.resources.defaultCpuAllocationRatio" . }}
31+
{{- $app := include "cozy-lib.resources.appAllocationRatio" (list "cpu" (index . 1)) }}
32+
{{- if $app }}
33+
{{- $app }}
1634
{{- else }}
17-
{{- dig "data" "cpu-allocation-ratio" (include "cozy-lib.resources.defaultCpuAllocationRatio" dict) $cozyConfig }}
35+
{{- include "cozy-lib.loadCozyConfig" . }}
36+
{{- $cozyConfig := index . 1 "cozyConfig" }}
37+
{{- if not $cozyConfig }}
38+
{{- include "cozy-lib.resources.defaultCpuAllocationRatio" . }}
39+
{{- else }}
40+
{{- dig "data" "cpu-allocation-ratio" (include "cozy-lib.resources.defaultCpuAllocationRatio" dict) $cozyConfig }}
41+
{{- end }}
1842
{{- end }}
1943
{{- end }}
2044

2145
{{- define "cozy-lib.resources.memoryAllocationRatio" }}
22-
{{- include "cozy-lib.loadCozyConfig" . }}
23-
{{- $cozyConfig := index . 1 "cozyConfig" }}
24-
{{- if not $cozyConfig }}
25-
{{- include "cozy-lib.resources.defaultMemoryAllocationRatio" . }}
46+
{{- $app := include "cozy-lib.resources.appAllocationRatio" (list "memory" (index . 1)) }}
47+
{{- if $app }}
48+
{{- $app }}
2649
{{- else }}
27-
{{- dig "data" "memory-allocation-ratio" (include "cozy-lib.resources.defaultMemoryAllocationRatio" dict) $cozyConfig }}
50+
{{- include "cozy-lib.loadCozyConfig" . }}
51+
{{- $cozyConfig := index . 1 "cozyConfig" }}
52+
{{- if not $cozyConfig }}
53+
{{- include "cozy-lib.resources.defaultMemoryAllocationRatio" . }}
54+
{{- else }}
55+
{{- dig "data" "memory-allocation-ratio" (include "cozy-lib.resources.defaultMemoryAllocationRatio" dict) $cozyConfig }}
56+
{{- end }}
2857
{{- end }}
2958
{{- end }}
3059

‎packages/system/postgres-rd/cozyrds/postgres.yaml‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# ./tests/resources_app_ratio_test.yaml
2+
suite: per-application allocation ratio (resourcesAllocationRatio)
3+
4+
# An application may override the cluster-wide allocation ratios for itself only. Unset, the
5+
# cluster-wide ratios (or the defaults) apply exactly as before.
6+
7+
templates:
8+
- templates/tests/resources.yaml
9+
10+
release:
11+
name: myrelease
12+
namespace: default
13+
14+
tests:
15+
- it: leaves requests unchanged when the application sets no ratio (defaults cpu 10, memory 1)
16+
set:
17+
resources:
18+
cpu: "1"
19+
memory: 1Gi
20+
asserts:
21+
- equal:
22+
path: spec.containers[0].resources.requests.cpu
23+
value: "0.1"
24+
- equal:
25+
path: spec.containers[0].resources.requests.memory
26+
value: "1073741824"
27+
- equal:
28+
path: spec.containers[0].resources.limits.memory
29+
value: 1Gi
30+
31+
- it: leaves requests unchanged with an empty resourcesAllocationRatio
32+
set:
33+
resourcesAllocationRatio: {}
34+
resources:
35+
cpu: "1"
36+
memory: 1Gi
37+
asserts:
38+
- equal:
39+
path: spec.containers[0].resources.requests.cpu
40+
value: "0.1"
41+
- equal:
42+
path: spec.containers[0].resources.requests.memory
43+
value: "1073741824"
44+
45+
- it: still uses the cluster-wide ratios when the application sets none
46+
set:
47+
_cluster:
48+
cpu-allocation-ratio: 4
49+
memory-allocation-ratio: 2
50+
resources:
51+
cpu: "1"
52+
memory: 1Gi
53+
asserts:
54+
- equal:
55+
path: spec.containers[0].resources.requests.cpu
56+
value: "0.25"
57+
- equal:
58+
path: spec.containers[0].resources.requests.memory
59+
value: "536870912"
60+
61+
- it: divides the memory request by the application's memory ratio, limit unchanged
62+
set:
63+
resourcesAllocationRatio:
64+
memory: 4
65+
resources:
66+
cpu: "1"
67+
memory: 16Gi
68+
asserts:
69+
- equal:
70+
path: spec.containers[0].resources.requests.memory
71+
value: "4294967296"
72+
- equal:
73+
path: spec.containers[0].resources.limits.memory
74+
value: 16Gi
75+
# cpu keeps the default ratio
76+
- equal:
77+
path: spec.containers[0].resources.requests.cpu
78+
value: "0.1"
79+
80+
- it: the application's ratio wins over the cluster-wide ratio, per resource
81+
set:
82+
_cluster:
83+
cpu-allocation-ratio: 4
84+
memory-allocation-ratio: 2
85+
resourcesAllocationRatio:
86+
cpu: 20
87+
resources:
88+
cpu: "2"
89+
memory: 1Gi
90+
asserts:
91+
- equal:
92+
path: spec.containers[0].resources.requests.cpu
93+
value: "0.1"
94+
# memory has no application ratio, so the cluster-wide 2 applies
95+
- equal:
96+
path: spec.containers[0].resources.requests.memory
97+
value: "536870912"
98+
99+
- it: a ratio of 1 makes the request equal to the limit
100+
set:
101+
_cluster:
102+
memory-allocation-ratio: 2
103+
resourcesAllocationRatio:
104+
memory: 1
105+
resources:
106+
cpu: "1"
107+
memory: 1Gi
108+
asserts:
109+
- equal:
110+
path: spec.containers[0].resources.requests.memory
111+
value: "1073741824"
112+
113+
- it: refuses a ratio below 1, which would make the request exceed the limit
114+
set:
115+
resourcesAllocationRatio:
116+
memory: 0
117+
resources:
118+
cpu: "1"
119+
memory: 1Gi
120+
asserts:
121+
- failedTemplate:
122+
errorPattern: "resourcesAllocationRatio.memory must be 1 or more"

0 commit comments

Comments
 (0)