[cozy-lib] Introduce memory-allocation-ratio and ephemeral-strorage-allocation-ratio options

Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
This commit is contained in:
Andrei Kvapil
2025-06-26 21:14:02 +02:00
parent 6256e40169
commit bb46aa4b7d
2 changed files with 53 additions and 35 deletions

View File

@@ -27,38 +27,17 @@ Return a resource request/limit object based on a given preset.
"xlarge" (dict "memory" "4Gi" )
"2xlarge" (dict "memory" "8Gi" )
}}
{{- $baseEphemeralStorage := dict
"nano" (dict "ephemeral-storage" "2Gi" )
"micro" (dict "ephemeral-storage" "2Gi" )
"small" (dict "ephemeral-storage" "2Gi" )
"medium" (dict "ephemeral-storage" "2Gi" )
"large" (dict "ephemeral-storage" "2Gi" )
"xlarge" (dict "ephemeral-storage" "2Gi" )
"2xlarge" (dict "ephemeral-storage" "2Gi" )
}}
{{- $presets := dict
"nano" (dict
"requests" (dict "ephemeral-storage" "50Mi")
"limits" (dict "ephemeral-storage" "2Gi")
)
"micro" (dict
"requests" (dict "ephemeral-storage" "50Mi")
"limits" (dict "ephemeral-storage" "2Gi")
)
"small" (dict
"requests" (dict "ephemeral-storage" "50Mi")
"limits" (dict "ephemeral-storage" "2Gi")
)
"medium" (dict
"requests" (dict "ephemeral-storage" "50Mi")
"limits" (dict "ephemeral-storage" "2Gi")
)
"large" (dict
"requests" (dict "ephemeral-storage" "50Mi")
"limits" (dict "ephemeral-storage" "2Gi")
)
"xlarge" (dict
"requests" (dict "ephemeral-storage" "50Mi")
"limits" (dict "ephemeral-storage" "2Gi")
)
"2xlarge" (dict
"requests" (dict "ephemeral-storage" "50Mi")
"limits" (dict "ephemeral-storage" "2Gi")
)
}}
{{- $_ := merge $presets $baseCPU $baseMemory }}
{{- $presets := merge $baseCPU $baseMemory $baseEphemeralStorage }}
{{- if hasKey $presets $args -}}
{{- $flatDict := index $presets $args }}
{{- include "cozy-lib.resources.sanitize" (list $flatDict $global) }}

View File

@@ -1,6 +1,12 @@
{{- define "cozy-lib.resources.defaultCpuAllocationRatio" }}
{{- `10` }}
{{- end }}
{{- define "cozy-lib.resources.defaultMemoryAllocationRatio" }}
{{- `1` }}
{{- end }}
{{- define "cozy-lib.resources.defaultEphemeralStorageAllocationRatio" }}
{{- `40` }}
{{- end }}
{{- define "cozy-lib.resources.cpuAllocationRatio" }}
{{- include "cozy-lib.loadCozyConfig" . }}
@@ -12,6 +18,27 @@
{{- end }}
{{- end }}
{{- define "cozy-lib.resources.memoryAllocationRatio" }}
{{- include "cozy-lib.loadCozyConfig" . }}
{{- $cozyConfig := index . 1 "cozyConfig" }}
{{- if not $cozyConfig }}
{{- include "cozy-lib.resources.defaultMemoryAllocationRatio" . }}
{{- else }}
{{- dig "data" "memory-allocation-ratio" (include "cozy-lib.resources.defaultMemoryAllocationRatio" dict) $cozyConfig }}
{{- end }}
{{- end }}
{{- define "cozy-lib.resources.ephemeralStorageAllocationRatio" }}
{{- include "cozy-lib.loadCozyConfig" . }}
{{- $cozyConfig := index . 1 "cozyConfig" }}
{{- if not $cozyConfig }}
{{- include "cozy-lib.resources.defaultEphemeralStorageAllocationRatio" . }}
{{- else }}
{{- dig "data" "ephemeral-storage-allocation-ratio" (include "cozy-lib.resources.defaultEphemeralStorageAllocationRatio" dict) $cozyConfig }}
{{- end }}
{{- end }}
{{- define "cozy-lib.resources.toFloat" -}}
{{- $value := . -}}
{{- $unit := 1.0 -}}
@@ -59,20 +86,32 @@
*/}}
{{- define "cozy-lib.resources.sanitize" }}
{{- $cpuAllocationRatio := include "cozy-lib.resources.cpuAllocationRatio" . | float64 }}
{{- $memoryAllocationRatio := include "cozy-lib.resources.memoryAllocationRatio" . | float64 }}
{{- $ephemeralStorageAllocationRatio := include "cozy-lib.resources.ephemeralStorageAllocationRatio" . | float64 }}
{{- $args := index . 0 }}
{{- $output := dict "requests" dict "limits" dict }}
{{- if or (hasKey $args "limits") (hasKey $args "requests") }}
{{- fail "ERROR: A flat map of resources expected, not nested `requests:` or `limits:` sections." -}}
{{- end }}
{{- range $k, $v := $args }}
{{- if not (eq $k "cpu") }}
{{- $_ := set $output.requests $k $v }}
{{- $_ := set $output.limits $k $v }}
{{- else }}
{{- if eq $k "cpu" }}
{{- $vcpuRequestF64 := (include "cozy-lib.resources.toFloat" $v) | float64 }}
{{- $cpuRequestF64 := divf $vcpuRequestF64 $cpuAllocationRatio }}
{{- $_ := set $output.requests $k ($cpuRequestF64 | toString) }}
{{- $_ := set $output.limits $k $v }}
{{- else if eq $k "memory" }}
{{- $vMemoryRequestF64 := (include "cozy-lib.resources.toFloat" $v) | float64 }}
{{- $memoryRequestF64 := divf $vMemoryRequestF64 $memoryAllocationRatio }}
{{- $_ := set $output.requests $k ($memoryRequestF64 | int) }}
{{- $_ := set $output.limits $k $v }}
{{- else if eq $k "ephemeral-storage" }}
{{- $vEphemeralStorageRequestF64 := (include "cozy-lib.resources.toFloat" $v) | float64 }}
{{- $ephemeralStorageRequestF64 := divf $vEphemeralStorageRequestF64 $ephemeralStorageAllocationRatio }}
{{- $_ := set $output.requests $k ($ephemeralStorageRequestF64 | int) }}
{{- $_ := set $output.limits $k $v }}
{{- else }}
{{- $_ := set $output.requests $k $v }}
{{- $_ := set $output.limits $k $v }}
{{- end }}
{{- end }}
{{- $output | toYaml }}