[cozy-lib, bug] divf by cpu ratio, not mulf (#1125)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

* **Refactor**
* Updated the structure of resource presets for improved clarity and
processing.
* Adjusted template logic to streamline resource handling and removed
previous resource limit calculations.
* Modified template parameters to enhance flexibility in resource
processing.
* **Chores**
* Improved internal template invocation for better compatibility with
resource data.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
This commit is contained in:
Andrei Kvapil
2025-06-27 13:16:49 +02:00
parent 0d46393e8c
commit 22cda073b9
2 changed files with 18 additions and 23 deletions

View File

@@ -13,33 +13,27 @@ These presets are for basic testing and not meant to be used in production
{{- define "cozy-lib.resources.preset" -}}
{{- $cpuAllocationRatio := include "cozy-lib.resources.cpuAllocationRatio" . | float64 }}
{{- $args := index . 0 }}
{{- $global := index . 1 }}
{{- $baseCPU := dict
"nano" (dict "requests" (dict "cpu" "100m" ))
"micro" (dict "requests" (dict "cpu" "250m" ))
"small" (dict "requests" (dict "cpu" "500m" ))
"medium" (dict "requests" (dict "cpu" "500m" ))
"large" (dict "requests" (dict "cpu" "1" ))
"xlarge" (dict "requests" (dict "cpu" "2" ))
"2xlarge" (dict "requests" (dict "cpu" "4" ))
"nano" (dict "cpu" "100m" )
"micro" (dict "cpu" "250m" )
"small" (dict "cpu" "500m" )
"medium" (dict "cpu" "500m" )
"large" (dict "cpu" "1" )
"xlarge" (dict "cpu" "2" )
"2xlarge" (dict "cpu" "4" )
}}
{{- $baseMemory := dict
"nano" (dict "requests" (dict "memory" "128Mi" ))
"micro" (dict "requests" (dict "memory" "256Mi" ))
"small" (dict "requests" (dict "memory" "512Mi" ))
"medium" (dict "requests" (dict "memory" "1Gi" ))
"large" (dict "requests" (dict "memory" "2Gi" ))
"xlarge" (dict "requests" (dict "memory" "4Gi" ))
"2xlarge" (dict "requests" (dict "memory" "8Gi" ))
"nano" (dict "memory" "128Mi" )
"micro" (dict "memory" "256Mi" )
"small" (dict "memory" "512Mi" )
"medium" (dict "memory" "1Gi" )
"large" (dict "memory" "2Gi" )
"xlarge" (dict "memory" "4Gi" )
"2xlarge" (dict "memory" "8Gi" )
}}
{{- range $baseCPU }}
{{- $_ := set . "limits" (dict "cpu" (include "cozy-lib.resources.toFloat" .requests.cpu | float64 | mulf $cpuAllocationRatio | toString)) }}
{{- end }}
{{- range $baseMemory }}
{{- $_ := set . "limits" (dict "memory" .requests.memory) }}
{{- end }}
{{- $presets := dict
"nano" (dict
"requests" (dict "ephemeral-storage" "50Mi")
@@ -72,7 +66,8 @@ These presets are for basic testing and not meant to be used in production
}}
{{- $_ := merge $presets $baseCPU $baseMemory }}
{{- if hasKey $presets $args -}}
{{- index $presets $args | toYaml -}}
{{- $flatDict := index $presets $args }}
{{- include "cozy-lib.resources.sanitize" (list $flatDict $global) }}
{{- else -}}
{{- printf "ERROR: Preset key '%s' invalid. Allowed values are %s" . (join "," (keys $presets)) | fail -}}
{{- end -}}

View File

@@ -34,7 +34,7 @@
to have equal requests and limits, except CPU, where the limit is increased
by a factor of the CPU allocation ratio. The template expects to receive a
dict {"requests":{...}, "limits":{...}} as input, e.g.
{{ include "cozy-lib.resources.sanitize" .Values.resources }}.
{{ include "cozy-lib.resources.sanitize" list (.Values.resources $) }}.
Example input:
==============
limits: