diff --git a/packages/apps/tcp-balancer/templates/configmap.yaml b/packages/apps/tcp-balancer/templates/configmap.yaml index 122c10254..327b8200e 100644 --- a/packages/apps/tcp-balancer/templates/configmap.yaml +++ b/packages/apps/tcp-balancer/templates/configmap.yaml @@ -11,6 +11,9 @@ default-server observe layer4 error-limit 10 on-error mark-down check send-proxy {{- fail (printf "mode %s is not supported" .) }} {{- end }} {{- end }} +{{- $resources := include "cozy-lib.resources.defaultingSanitize" (list .Values.resourcesPreset .Values.resources $) | fromYaml }} +{{- $memoryMi := div (include "cozy-lib.resources.toFloat" $resources.limits.memory | float64 | int64) 1048576 }} +{{- $cpu := include "cozy-lib.resources.toFloat" $resources.limits.cpu | float64 }} apiVersion: v1 kind: ConfigMap @@ -21,6 +24,10 @@ metadata: app.kubernetes.io/managed-by: {{ .Release.Service }} data: haproxy.cfg: | + global + fd-hard-limit {{ mul $memoryMi 32 }} + thread-hard-limit {{ max (ceil $cpu | int64) 1 }} + defaults mode tcp option dontlognull diff --git a/packages/apps/tcp-balancer/tests/haproxy-config_test.yaml b/packages/apps/tcp-balancer/tests/haproxy-config_test.yaml index 4c8844583..d1abbee44 100644 --- a/packages/apps/tcp-balancer/tests/haproxy-config_test.yaml +++ b/packages/apps/tcp-balancer/tests/haproxy-config_test.yaml @@ -130,3 +130,39 @@ tests: - matchRegex: path: data["haproxy.cfg"] pattern: '(?m)^frontend talos$\n(?:[ \t]+.*\n)*?[ \t]+acl whitelist src 10\.0\.0\.0/8\n[ \t]+acl all src 0\.0\.0\.0\n[ \t]+tcp-request content accept if whitelist\n[ \t]+tcp-request content reject$' + + - it: caps the automatic fd and thread counts by the default preset + asserts: + - matchRegex: + path: data["haproxy.cfg"] + pattern: '\Aglobal\n[ \t]+fd-hard-limit 4096\n[ \t]+thread-hard-limit 1\n\n' + - notMatchRegex: + path: data["haproxy.cfg"] + pattern: '\Aglobal\n(?:[ \t]+.*\n)*?[ \t]+(?:maxconn|nbthread)\b' + + - it: scales the caps with the preset + set: + resourcesPreset: t1.medium + asserts: + - matchRegex: + path: data["haproxy.cfg"] + pattern: '\Aglobal\n[ \t]+fd-hard-limit 32768\n[ \t]+thread-hard-limit 2\n\n' + + - it: takes the caps from explicit resources and rounds the CPU limit up + set: + resources: + cpu: 2500m + memory: 768Mi + asserts: + - matchRegex: + path: data["haproxy.cfg"] + pattern: '\Aglobal\n[ \t]+fd-hard-limit 24576\n[ \t]+thread-hard-limit 3\n\n' + + - it: never renders a thread cap of zero, which HAProxy refuses to parse + set: + resources: + cpu: "0" + asserts: + - matchRegex: + path: data["haproxy.cfg"] + pattern: '(?m)^[ \t]+thread-hard-limit 1$'