[cozy-lib] refactor resources (#1155)

Add. missing commits from
https://github.com/cozystack/cozystack/pull/1127, which were skipped by
mistake

- [cozy-lib, bug] divf by cpu ratio, not mulf (#1125)
- [cozy-lib] remove handler for nested resources/requests map
- [cozy-lib] Introduce memory-allocation-ratio and
ephemeral-strorage-allocation-ratio options
- [system] Recuce resources for some system apps

<!-- Thank you for making a contribution! Here are some tips for you:
- Start the PR title with the [label] of Cozystack component:
- For system components: [platform], [system], [linstor], [cilium],
[kube-ovn], [dashboard], [cluster-api], etc.
- For managed apps: [apps], [tenant], [kubernetes], [postgres],
[virtual-machine] etc.
- For development and maintenance: [tests], [ci], [docs], [maintenance].
- If it's a work in progress, consider creating this PR as a draft.
- Don't hesistate to ask for opinion and review in the community chats,
even if it's still a draft.
- Add the label `backport` if it's a bugfix that needs to be backported
to a previous version.
-->

## What this PR does


### Release note

<!--  Write a release note:
- Explain what has changed internally and for users.
- Start with the same [label] as in the PR title
- Follow the guidelines at
https://github.com/kubernetes/community/blob/master/contributors/guide/release-notes.md.
-->

```release-note
[cozy-lib] refactor resources
```

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

## Summary by CodeRabbit

* **New Features**
* Introduced support for memory and ephemeral storage allocation ratios,
allowing more flexible resource allocation.

* **Refactor**
* Simplified resource preset structure for easier configuration and
management.
* Updated resource preset logic to use a new sanitization process for
resource values.

* **Bug Fixes**
  * Improved error handling for invalid resource preset keys.

* **Chores**
* Adjusted resource requests and limits for Redis master, FluxCD
operator, and Vertical Pod Autoscaler components to optimize resource
usage.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Andrei Kvapil
2025-07-03 15:40:35 +03:00
committed by GitHub
5 changed files with 103 additions and 109 deletions

View File

@@ -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 -}}

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 -}}
@@ -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 }}

View File

@@ -27,9 +27,9 @@ kubeapps:
resources:
requests:
cpu: 20m
memory: 32Mi
memory: 128Mi
limits:
memory: 256Mi
memory: 128Mi
kubeappsapis:
resourcesPreset: "none"
qps: "250.0"

View File

@@ -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

View File

@@ -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: