From 22cda073b9d42f6c9719243f373fc5caec073d72 Mon Sep 17 00:00:00 2001 From: Andrei Kvapil Date: Fri, 27 Jun 2025 13:16:49 +0200 Subject: [PATCH 1/4] [cozy-lib, bug] divf by cpu ratio, not mulf (#1125) * **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. Signed-off-by: Andrei Kvapil --- .../cozy-lib/templates/_resourcepresets.tpl | 39 ++++++++----------- .../library/cozy-lib/templates/_resources.tpl | 2 +- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/packages/library/cozy-lib/templates/_resourcepresets.tpl b/packages/library/cozy-lib/templates/_resourcepresets.tpl index d26094f0..0f3e3d4a 100644 --- a/packages/library/cozy-lib/templates/_resourcepresets.tpl +++ b/packages/library/cozy-lib/templates/_resourcepresets.tpl @@ -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 -}} diff --git a/packages/library/cozy-lib/templates/_resources.tpl b/packages/library/cozy-lib/templates/_resources.tpl index ac848f48..91d9990c 100644 --- a/packages/library/cozy-lib/templates/_resources.tpl +++ b/packages/library/cozy-lib/templates/_resources.tpl @@ -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: From 6256e401698d3ea1fc1b3bdfc430845fc325d546 Mon Sep 17 00:00:00 2001 From: Andrei Kvapil Date: Thu, 26 Jun 2025 20:24:51 +0200 Subject: [PATCH 2/4] [cozy-lib] remove handler for nested resources/requests map Signed-off-by: Andrei Kvapil --- .../cozy-lib/templates/_resourcepresets.tpl | 20 +++---- .../library/cozy-lib/templates/_resources.tpl | 59 ++++++++----------- 2 files changed, 30 insertions(+), 49 deletions(-) diff --git a/packages/library/cozy-lib/templates/_resourcepresets.tpl b/packages/library/cozy-lib/templates/_resourcepresets.tpl index 0f3e3d4a..333b17a4 100644 --- a/packages/library/cozy-lib/templates/_resourcepresets.tpl +++ b/packages/library/cozy-lib/templates/_resourcepresets.tpl @@ -1,13 +1,7 @@ -{{/* -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" -}} @@ -16,13 +10,13 @@ These presets are for basic testing and not meant to be used in production {{- $global := index . 1 }} {{- $baseCPU := dict - "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" ) + "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 "memory" "128Mi" ) diff --git a/packages/library/cozy-lib/templates/_resources.tpl b/packages/library/cozy-lib/templates/_resources.tpl index 91d9990c..8a2ceb8a 100644 --- a/packages/library/cozy-lib/templates/_resources.tpl +++ b/packages/library/cozy-lib/templates/_resources.tpl @@ -29,55 +29,42 @@ {{- 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" list (.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 }} {{- $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 }} From bb46aa4b7d63d67cfd77120e965f26af680810da Mon Sep 17 00:00:00 2001 From: Andrei Kvapil Date: Thu, 26 Jun 2025 21:14:02 +0200 Subject: [PATCH 3/4] [cozy-lib] Introduce memory-allocation-ratio and ephemeral-strorage-allocation-ratio options Signed-off-by: Andrei Kvapil --- .../cozy-lib/templates/_resourcepresets.tpl | 41 ++++------------ .../library/cozy-lib/templates/_resources.tpl | 47 +++++++++++++++++-- 2 files changed, 53 insertions(+), 35 deletions(-) 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 }} From 0b7bbb1ba97e0692df29bc3848c4a3d6e7d3e7eb Mon Sep 17 00:00:00 2001 From: Andrei Kvapil Date: Fri, 27 Jun 2025 13:36:53 +0200 Subject: [PATCH 4/4] [system] Recuce resources for some system apps Signed-off-by: Andrei Kvapil --- packages/system/dashboard/values.yaml | 4 ++-- packages/system/fluxcd-operator/values.yaml | 8 ++++---- packages/system/vertical-pod-autoscaler/values.yaml | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) 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: