[cozy-lib] remove handler for nested resources/requests map

Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
This commit is contained in:
Andrei Kvapil
2025-06-26 20:24:51 +02:00
parent 22cda073b9
commit 6256e40169
2 changed files with 30 additions and 49 deletions

View File

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

View File

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