diff --git a/packages/library/cozy-lib/templates/_resourcepresets.tpl b/packages/library/cozy-lib/templates/_resourcepresets.tpl index d26094f0..e1e95547 100644 --- a/packages/library/cozy-lib/templates/_resourcepresets.tpl +++ b/packages/library/cozy-lib/templates/_resourcepresets.tpl @@ -1,78 +1,46 @@ -{{/* -Copyright Broadcom, Inc. All Rights Reserved. -SPDX-License-Identifier: APACHE-2.0 -*/}} - {{/* vim: set filetype=mustache: */}} {{/* Return a resource request/limit object based on a given preset. -These presets are for basic testing and not meant to be used in production {{ include "cozy-lib.resources.preset" "nano" -}} */}} {{- 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" "250m" ) + "micro" (dict "cpu" "500m" ) + "small" (dict "cpu" "1" ) + "medium" (dict "cpu" "1" ) + "large" (dict "cpu" "2" ) + "xlarge" (dict "cpu" "4" ) + "2xlarge" (dict "cpu" "8" ) }} {{- $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" ) +}} +{{- $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" ) }} -{{- 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") - "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 -}} -{{- 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 -}} diff --git a/packages/library/cozy-lib/templates/_resources.tpl b/packages/library/cozy-lib/templates/_resources.tpl index ac848f48..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 -}} @@ -29,63 +56,62 @@ {{- end -}} {{- /* - A sanitized resource map is a dict with resource-name => resource-quantity. - If not in such a form, requests are used, then limits. All resources are set - 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 }}. + A sanitized resource map is a dict with resource-name to resource-quantity. + All resources are returned with equal **requests** and **limits**, except for + **cpu**, whose *request* is reduced by the CPU-allocation ratio obtained from + `cozy-lib.resources.cpuAllocationRatio`. + + The template now expects **one flat map** as input (no nested `requests:` / + `limits:` sections). Each value in that map is taken as the *limit* for the + corresponding resource. Usage example: + + {{ include "cozy-lib.resources.sanitize" list (.Values.resources $) }} + Example input: ============== - limits: - cpu: "1" - memory: 1024Mi - requests: - cpu: "2" - memory: 512Mi + cpu: "2" memory: 256Mi devices.com/nvidia: "1" - Example output: - =============== + Example output (cpuAllocationRatio = 10): + ========================================= limits: - devices.com/nvidia: "1" # only present in top level key - memory: 256Mi # value from top level key has priority over all others - cpu: "2" # value from .requests.cpu has priority over .limits.cpu + cpu: "2" + memory: 256Mi + devices.com/nvidia: "1" requests: - cpu: 200m # .limits.cpu divided by CPU allocation ratio - devices.com/nvidia: "1" # .requests == .limits - memory: 256Mi # .requests == .limits + cpu: 200m # 2 / 10 + memory: 256Mi # = limit + devices.com/nvidia: "1" # = limit */}} {{- define "cozy-lib.resources.sanitize" }} {{- $cpuAllocationRatio := include "cozy-lib.resources.cpuAllocationRatio" . | float64 }} -{{- $sanitizedMap := dict }} +{{- $memoryAllocationRatio := include "cozy-lib.resources.memoryAllocationRatio" . | float64 }} +{{- $ephemeralStorageAllocationRatio := include "cozy-lib.resources.ephemeralStorageAllocationRatio" . | float64 }} {{- $args := index . 0 }} -{{- if hasKey $args "limits" }} -{{- range $k, $v := $args.limits }} -{{- $_ := set $sanitizedMap $k $v }} -{{- end }} -{{- end }} -{{- if hasKey $args "requests" }} -{{- range $k, $v := $args.requests }} -{{- $_ := set $sanitizedMap $k $v }} -{{- end }} +{{- $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 (or (eq $k "requests") (eq $k "limits")) }} -{{- $_ := set $sanitizedMap $k $v }} -{{- end }} -{{- end }} -{{- $output := dict "requests" dict "limits" dict }} -{{- range $k, $v := $sanitizedMap }} -{{- 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 }} diff --git a/packages/system/dashboard/values.yaml b/packages/system/dashboard/values.yaml index 0be82d31..49d95970 100644 --- a/packages/system/dashboard/values.yaml +++ b/packages/system/dashboard/values.yaml @@ -27,9 +27,9 @@ kubeapps: resources: requests: cpu: 20m - memory: 32Mi + memory: 128Mi limits: - memory: 256Mi + memory: 128Mi kubeappsapis: resourcesPreset: "none" qps: "250.0" diff --git a/packages/system/fluxcd-operator/values.yaml b/packages/system/fluxcd-operator/values.yaml index 7748c042..9053689a 100644 --- a/packages/system/fluxcd-operator/values.yaml +++ b/packages/system/fluxcd-operator/values.yaml @@ -7,8 +7,8 @@ flux-operator: hostNetwork: true resources: limits: - cpu: 1000m - memory: 1Gi + cpu: 100m + memory: 350Mi requests: - cpu: 10m - memory: 64Mi + cpu: 100m + memory: 350Mi diff --git a/packages/system/vertical-pod-autoscaler/values.yaml b/packages/system/vertical-pod-autoscaler/values.yaml index 3471a8c9..df4a5d10 100644 --- a/packages/system/vertical-pod-autoscaler/values.yaml +++ b/packages/system/vertical-pod-autoscaler/values.yaml @@ -5,10 +5,10 @@ vertical-pod-autoscaler: updater: resources: limits: - memory: 1100Mi + memory: 110Mi requests: cpu: 200m - memory: 1100Mi + memory: 110Mi recommender: extraArgs: @@ -25,10 +25,10 @@ vertical-pod-autoscaler: prometheus-cadvisor-job-name: cadvisor resources: limits: - memory: 1600Mi + memory: 160Mi requests: cpu: 100m - memory: 1600Mi + memory: 160Mi admissionController: resources: