From 70ac79d3d3435d3c544f89227a8aa92ca407b15d Mon Sep 17 00:00:00 2001 From: Arsolitt Date: Wed, 22 Apr 2026 17:21:21 +0300 Subject: [PATCH] fix(kubernetes): subtract CPU reservations in capacity annotation The CPU capacity annotation previously reported raw effective CPU without subtracting system-reserved and kube-reserved. Now it computes allocatable CPU (effective minus reservations) to match what the cluster autoscaler uses for scaling decisions. Also fix the CPU total validation block to use cozy-lib.resources.toFloat for consistent millicore parsing. Signed-off-by: Arsolitt --- .../apps/kubernetes/templates/cluster.yaml | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/packages/apps/kubernetes/templates/cluster.yaml b/packages/apps/kubernetes/templates/cluster.yaml index ba7b173f7..28f671b78 100644 --- a/packages/apps/kubernetes/templates/cluster.yaml +++ b/packages/apps/kubernetes/templates/cluster.yaml @@ -347,7 +347,8 @@ metadata: {{- $kubeCpuMillis = mulf ($kubeCpuStr | float64) 1000.0 }} {{- end }} {{- $totalCpuMillis := addf $sysCpuMillis $kubeCpuMillis }} -{{- $effectiveCpuMillis := mulf ($effectiveCpu | float64) 1000.0 }} +{{- $effectiveCpuFloatV := include "cozy-lib.resources.toFloat" $effectiveCpu | float64 }} +{{- $effectiveCpuMillis := mulf $effectiveCpuFloatV 1000.0 }} {{- if ge $totalCpuMillis $effectiveCpuMillis }} {{- fail (printf "nodeGroup %s: total reserved CPU (systemReservedCpu=%s + kubeReservedCpu=%s) must be less than effective node CPU (%s cores)" $groupName $systemReservedCpu $kubeReservedCpu $effectiveCpu) }} {{- end }} @@ -489,10 +490,26 @@ metadata: {{- $allocatableMi := divf $allocatableBytes 1048576.0 | ceil }} capacity.cluster-autoscaler.kubernetes.io/memory: "{{ $allocatableMi }}Mi" {{- end }} - {{- if and $group.resources $group.resources.cpu }} - capacity.cluster-autoscaler.kubernetes.io/cpu: "{{ $group.resources.cpu }}" - {{- else if and $instanceType $instanceType.spec $instanceType.spec.cpu $instanceType.spec.cpu.guest }} - capacity.cluster-autoscaler.kubernetes.io/cpu: "{{ $instanceType.spec.cpu.guest }}" + {{- if $effectiveCpu }} + {{- $sysCpuStr := $systemReservedCpu | toString }} + {{- $kubeCpuStr := $kubeReservedCpu | toString }} + {{- $sysCpuMillisA := 0.0 }} + {{- $kubeCpuMillisA := 0.0 }} + {{- if hasSuffix "m" $sysCpuStr }} + {{- $sysCpuMillisA = trimSuffix "m" $sysCpuStr | float64 }} + {{- else }} + {{- $sysCpuMillisA = mulf ($sysCpuStr | float64) 1000.0 }} + {{- end }} + {{- if hasSuffix "m" $kubeCpuStr }} + {{- $kubeCpuMillisA = trimSuffix "m" $kubeCpuStr | float64 }} + {{- else }} + {{- $kubeCpuMillisA = mulf ($kubeCpuStr | float64) 1000.0 }} + {{- end }} + {{- $effectiveCpuFloatA := include "cozy-lib.resources.toFloat" $effectiveCpu | float64 }} + {{- $effectiveCpuMillisA := mulf $effectiveCpuFloatA 1000.0 }} + {{- $allocatableCpuMillis := subf $effectiveCpuMillisA (addf $sysCpuMillisA $kubeCpuMillisA) }} + {{- $allocatableCpuMillisInt := $allocatableCpuMillis | ceil | int }} + capacity.cluster-autoscaler.kubernetes.io/cpu: "{{ $allocatableCpuMillisInt }}m" {{- end }} spec: clusterName: {{ $.Release.Name }}