diff --git a/packages/library/cozy-lib/templates/_resourcepresets.tpl b/packages/library/cozy-lib/templates/_resourcepresets.tpl index 333b17a4..e1e95547 100644 --- a/packages/library/cozy-lib/templates/_resourcepresets.tpl +++ b/packages/library/cozy-lib/templates/_resourcepresets.tpl @@ -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) }} diff --git a/packages/library/cozy-lib/templates/_resources.tpl b/packages/library/cozy-lib/templates/_resources.tpl index 8a2ceb8a..8ea1fe10 100644 --- a/packages/library/cozy-lib/templates/_resources.tpl +++ b/packages/library/cozy-lib/templates/_resources.tpl @@ -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 }}