Skip to content

Commit 70ac79d

Browse files
committed
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 <[email protected]>
1 parent 76def54 commit 70ac79d

1 file changed

Lines changed: 22 additions & 5 deletions

File tree

‎packages/apps/kubernetes/templates/cluster.yaml‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,8 @@ metadata:
347347
{{- $kubeCpuMillis = mulf ($kubeCpuStr | float64) 1000.0 }}
348348
{{- end }}
349349
{{- $totalCpuMillis := addf $sysCpuMillis $kubeCpuMillis }}
350-
{{- $effectiveCpuMillis := mulf ($effectiveCpu | float64) 1000.0 }}
350+
{{- $effectiveCpuFloatV := include "cozy-lib.resources.toFloat" $effectiveCpu | float64 }}
351+
{{- $effectiveCpuMillis := mulf $effectiveCpuFloatV 1000.0 }}
351352
{{- if ge $totalCpuMillis $effectiveCpuMillis }}
352353
{{- fail (printf "nodeGroup %s: total reserved CPU (systemReservedCpu=%s + kubeReservedCpu=%s) must be less than effective node CPU (%s cores)" $groupName $systemReservedCpu $kubeReservedCpu $effectiveCpu) }}
353354
{{- end }}
@@ -489,10 +490,26 @@ metadata:
489490
{{- $allocatableMi := divf $allocatableBytes 1048576.0 | ceil }}
490491
capacity.cluster-autoscaler.kubernetes.io/memory: "{{ $allocatableMi }}Mi"
491492
{{- end }}
492-
{{- if and $group.resources $group.resources.cpu }}
493-
capacity.cluster-autoscaler.kubernetes.io/cpu: "{{ $group.resources.cpu }}"
494-
{{- else if and $instanceType $instanceType.spec $instanceType.spec.cpu $instanceType.spec.cpu.guest }}
495-
capacity.cluster-autoscaler.kubernetes.io/cpu: "{{ $instanceType.spec.cpu.guest }}"
493+
{{- if $effectiveCpu }}
494+
{{- $sysCpuStr := $systemReservedCpu | toString }}
495+
{{- $kubeCpuStr := $kubeReservedCpu | toString }}
496+
{{- $sysCpuMillisA := 0.0 }}
497+
{{- $kubeCpuMillisA := 0.0 }}
498+
{{- if hasSuffix "m" $sysCpuStr }}
499+
{{- $sysCpuMillisA = trimSuffix "m" $sysCpuStr | float64 }}
500+
{{- else }}
501+
{{- $sysCpuMillisA = mulf ($sysCpuStr | float64) 1000.0 }}
502+
{{- end }}
503+
{{- if hasSuffix "m" $kubeCpuStr }}
504+
{{- $kubeCpuMillisA = trimSuffix "m" $kubeCpuStr | float64 }}
505+
{{- else }}
506+
{{- $kubeCpuMillisA = mulf ($kubeCpuStr | float64) 1000.0 }}
507+
{{- end }}
508+
{{- $effectiveCpuFloatA := include "cozy-lib.resources.toFloat" $effectiveCpu | float64 }}
509+
{{- $effectiveCpuMillisA := mulf $effectiveCpuFloatA 1000.0 }}
510+
{{- $allocatableCpuMillis := subf $effectiveCpuMillisA (addf $sysCpuMillisA $kubeCpuMillisA) }}
511+
{{- $allocatableCpuMillisInt := $allocatableCpuMillis | ceil | int }}
512+
capacity.cluster-autoscaler.kubernetes.io/cpu: "{{ $allocatableCpuMillisInt }}m"
496513
{{- end }}
497514
spec:
498515
clusterName: {{ $.Release.Name }}

0 commit comments

Comments
 (0)