Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion charts/helm_lib/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: v2
type: library
name: deckhouse_lib_helm
version: 1.72.23
version: 1.72.24
description: "Helm utils template definitions for Deckhouse modules."
14 changes: 10 additions & 4 deletions charts/helm_lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1879,8 +1879,11 @@ list:
### helm_lib_resources_management_cpu_units_to_millicores

helper for converting cpu units to millicores
Accepts any Kubernetes CPU quantity, including a fractional one such as "0.5".
A fractional result is rounded up, so a limit is never smaller than the value asked for.
Accepts any Kubernetes CPU quantity: an optional sign, a decimal mantissa, and any of the
n, u, m, k, M, G, T, P, E and Ki...Ei suffixes or a decimal exponent. A fractional result is
rounded up. An unparsable value yields 0 and never aborts the rendering.
It converts, it does not validate: a negative quantity comes back as a negative number, and a
result that does not fit an int64 comes back as 0. Rejecting either is the caller's business.

#### Usage

Expand All @@ -1891,8 +1894,11 @@ list:
### helm_lib_resources_management_memory_units_to_bytes

helper for converting memory units to bytes
Accepts any Kubernetes memory quantity, including a fractional one such as "0.5Gi".
A fractional result is rounded up, so a limit is never smaller than the value asked for.
Accepts any Kubernetes memory quantity: an optional sign, a decimal mantissa, and any of the
n, u, m, k, M, G, T, P, E and Ki...Ei suffixes or a decimal exponent. A fractional result is
rounded up. A value carrying a known suffix but an unparsable mantissa yields 0, as it did
before, and so does a result that does not fit an int64. It converts, it does not validate.
The `fail` is reached only by a value that carries neither a known suffix nor a number.

#### Usage

Expand Down
94 changes: 76 additions & 18 deletions charts/helm_lib/templates/_resources_management.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -94,34 +94,77 @@ updatePolicy:

{{- /* Usage: {{ include "helm_lib_resources_management_cpu_units_to_millicores" <cpu units> }} */ -}}
{{- /* helper for converting cpu units to millicores */ -}}
{{- /* Accepts any Kubernetes CPU quantity, including a fractional one such as "0.5". */ -}}
{{- /* A fractional result is rounded up, so a limit is never smaller than the value asked for. */ -}}
{{- /* Accepts any Kubernetes CPU quantity: an optional sign, a decimal mantissa, and any of the */ -}}
{{- /* n, u, m, k, M, G, T, P, E and Ki...Ei suffixes or a decimal exponent. A fractional result is */ -}}
{{- /* rounded up. An unparsable value yields 0 and never aborts the rendering. */ -}}
{{- /* It converts, it does not validate: a negative quantity comes back as a negative number, and a */ -}}
{{- /* result that does not fit an int64 comes back as 0. Rejecting either is the caller's business. */ -}}
{{- define "helm_lib_resources_management_cpu_units_to_millicores" -}}
{{- $units := . | toString -}}
{{- $mantissa := $units -}}
{{- $factor := 1000 -}}
{{- if hasSuffix "m" $units -}}
{{- $mantissa = trimSuffix "m" $units -}}
{{- $factor = 1 -}}
{{- end -}}
{{- if regexMatch "^[0-9]+$" $mantissa -}}
{{- $mantissa | atoi | mul $factor -}}
{{- else if regexMatch "^[0-9]*[.]?[0-9]+([eE][+-]?[0-9]+)?$" $mantissa -}}
{{- $mantissa | float64 | mulf (float64 $factor) | ceil | int64 -}}
{{- if regexMatch "^[+-]?[0-9]+m$" $units -}}
{{- $units | trimSuffix "m" | atoi -}}
{{- else if regexMatch "^[+-]?[0-9]+$" $units -}}
{{- $units | atoi | mul 1000 -}}
{{- else -}}
{{- cat "ERROR: unknown cpu format:" $units | fail -}}
{{- $mantissa := $units -}}
{{- $cores := 1.0 -}}
{{- if hasSuffix "Ki" $units -}}
{{- $mantissa = trimSuffix "Ki" $units -}}{{- $cores = 1024.0 -}}
{{- else if hasSuffix "Mi" $units -}}
{{- $mantissa = trimSuffix "Mi" $units -}}{{- $cores = 1048576.0 -}}
{{- else if hasSuffix "Gi" $units -}}
{{- $mantissa = trimSuffix "Gi" $units -}}{{- $cores = 1073741824.0 -}}
{{- else if hasSuffix "Ti" $units -}}
{{- $mantissa = trimSuffix "Ti" $units -}}{{- $cores = 1099511627776.0 -}}
{{- else if hasSuffix "Pi" $units -}}
{{- $mantissa = trimSuffix "Pi" $units -}}{{- $cores = 1125899906842624.0 -}}
{{- else if hasSuffix "Ei" $units -}}
{{- $mantissa = trimSuffix "Ei" $units -}}{{- $cores = 1152921504606846976.0 -}}
{{- else if hasSuffix "n" $units -}}
{{- $mantissa = trimSuffix "n" $units -}}{{- $cores = 0.000000001 -}}
{{- else if hasSuffix "u" $units -}}
{{- $mantissa = trimSuffix "u" $units -}}{{- $cores = 0.000001 -}}
{{- else if hasSuffix "m" $units -}}
{{- $mantissa = trimSuffix "m" $units -}}{{- $cores = 0.001 -}}
{{- else if hasSuffix "k" $units -}}
{{- $mantissa = trimSuffix "k" $units -}}{{- $cores = 1000.0 -}}
{{- else if hasSuffix "M" $units -}}
{{- $mantissa = trimSuffix "M" $units -}}{{- $cores = 1000000.0 -}}
{{- else if hasSuffix "G" $units -}}
{{- $mantissa = trimSuffix "G" $units -}}{{- $cores = 1000000000.0 -}}
{{- else if hasSuffix "T" $units -}}
{{- $mantissa = trimSuffix "T" $units -}}{{- $cores = 1000000000000.0 -}}
{{- else if hasSuffix "P" $units -}}
{{- $mantissa = trimSuffix "P" $units -}}{{- $cores = 1000000000000000.0 -}}
{{- else if hasSuffix "E" $units -}}
{{- $mantissa = trimSuffix "E" $units -}}{{- $cores = 1000000000000000000.0 -}}
{{- end -}}
{{- if regexMatch "^[+-]?(([0-9]+([.][0-9]*)?)|([.][0-9]+))([eE][+-]?[0-9]+)?$" $mantissa -}}
{{- $millicores := $mantissa | float64 | mulf $cores | mulf 1000.0 | ceil -}}
{{- if or (gt $millicores 9223372036854775000.0) (lt $millicores -9223372036854775000.0) -}}
{{- 0 -}}
{{- else -}}
{{- $millicores | int64 -}}
{{- end -}}
{{- else -}}
{{- 0 -}}
{{- end -}}
{{- end }}
{{- end }}


{{- /* Usage: {{ include "helm_lib_resources_management_memory_units_to_bytes" <memory units> }} */ -}}
{{- /* helper for converting memory units to bytes */ -}}
{{- /* Accepts any Kubernetes memory quantity, including a fractional one such as "0.5Gi". */ -}}
{{- /* A fractional result is rounded up, so a limit is never smaller than the value asked for. */ -}}
{{- /* Accepts any Kubernetes memory quantity: an optional sign, a decimal mantissa, and any of the */ -}}
{{- /* n, u, m, k, M, G, T, P, E and Ki...Ei suffixes or a decimal exponent. A fractional result is */ -}}
{{- /* rounded up. A value carrying a known suffix but an unparsable mantissa yields 0, as it did */ -}}
{{- /* before, and so does a result that does not fit an int64. It converts, it does not validate. */ -}}
{{- /* The `fail` is reached only by a value that carries neither a known suffix nor a number. */ -}}
{{- define "helm_lib_resources_management_memory_units_to_bytes" }}
{{- $units := . | toString -}}
{{- $mantissa := $units -}}
{{- $factor := 1 -}}
{{- $suffixed := true -}}
{{- if hasSuffix "Ki" $units -}}
{{- $mantissa = trimSuffix "Ki" $units -}}{{- $factor = 1024 -}}
{{- else if hasSuffix "Mi" $units -}}
Expand All @@ -134,6 +177,12 @@ updatePolicy:
{{- $mantissa = trimSuffix "Pi" $units -}}{{- $factor = 1125899906842624 -}}
{{- else if hasSuffix "Ei" $units -}}
{{- $mantissa = trimSuffix "Ei" $units -}}{{- $factor = 1152921504606846976 -}}
{{- else if hasSuffix "n" $units -}}
{{- $mantissa = trimSuffix "n" $units -}}{{- $factor = 0.000000001 -}}
{{- else if hasSuffix "u" $units -}}
{{- $mantissa = trimSuffix "u" $units -}}{{- $factor = 0.000001 -}}
{{- else if hasSuffix "m" $units -}}
{{- $mantissa = trimSuffix "m" $units -}}{{- $factor = 0.001 -}}
{{- else if hasSuffix "k" $units -}}
{{- $mantissa = trimSuffix "k" $units -}}{{- $factor = 1000 -}}
{{- else if hasSuffix "M" $units -}}
Expand All @@ -146,11 +195,20 @@ updatePolicy:
{{- $mantissa = trimSuffix "P" $units -}}{{- $factor = 1000000000000000 -}}
{{- else if hasSuffix "E" $units -}}
{{- $mantissa = trimSuffix "E" $units -}}{{- $factor = 1000000000000000000 -}}
{{- else -}}
{{- $suffixed = false -}}
Comment thread
Suselz marked this conversation as resolved.
Resolved
{{- end -}}
{{- if regexMatch "^[0-9]+$" $mantissa -}}
{{- if and (regexMatch "^[+-]?[0-9]+$" $mantissa) (kindIs "int" $factor) -}}
{{- $mantissa | atoi | mul $factor -}}
{{- else if regexMatch "^[0-9]*[.]?[0-9]+([eE][+-]?[0-9]+)?$" $mantissa -}}
{{- $mantissa | float64 | mulf (float64 $factor) | ceil | int64 -}}
{{- else if regexMatch "^[+-]?(([0-9]+([.][0-9]*)?)|([.][0-9]+))([eE][+-]?[0-9]+)?$" $mantissa -}}
{{- $bytes := $mantissa | float64 | mulf (float64 $factor) | ceil -}}
{{- if or (gt $bytes 9223372036854775000.0) (lt $bytes -9223372036854775000.0) -}}
{{- 0 -}}
{{- else -}}
{{- $bytes | int64 -}}
{{- end -}}
{{- else if $suffixed -}}
{{- 0 -}}
{{- else -}}
{{- cat "ERROR: unknown memory format:" $units | fail -}}
{{- end }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,27 @@ result_dot_25: {{ $result }}
result_number_0_5: {{ $result }}
{{- $result := include "helm_lib_resources_management_cpu_units_to_millicores" "0.5m" }}
result_0_5m: {{ $result }}
{{- $result := include "helm_lib_resources_management_cpu_units_to_millicores" "+1" }}
result_plus_1: {{ $result }}
{{- $result := include "helm_lib_resources_management_cpu_units_to_millicores" "-1" }}
result_minus_1: {{ $result }}
{{- $result := include "helm_lib_resources_management_cpu_units_to_millicores" "1." }}
result_1_dot: {{ $result }}
{{- $result := include "helm_lib_resources_management_cpu_units_to_millicores" "100u" }}
result_100u: {{ $result }}
{{- $result := include "helm_lib_resources_management_cpu_units_to_millicores" "100n" }}
result_100n: {{ $result }}
{{- $result := include "helm_lib_resources_management_cpu_units_to_millicores" "1k" }}
result_1k: {{ $result }}
{{- $result := include "helm_lib_resources_management_cpu_units_to_millicores" "1e3" }}
result_1e3: {{ $result }}
{{- $result := include "helm_lib_resources_management_cpu_units_to_millicores" "abc" }}
result_garbage: {{ $result }}
{{- $result := include "helm_lib_resources_management_cpu_units_to_millicores" "1E" }}
result_1E: {{ $result }}
{{- $result := include "helm_lib_resources_management_cpu_units_to_millicores" "1Ei" }}
result_1Ei: {{ $result }}
{{- $result := include "helm_lib_resources_management_cpu_units_to_millicores" "10P" }}
result_10P: {{ $result }}
{{- $result := include "helm_lib_resources_management_cpu_units_to_millicores" "1P" }}
result_1P: {{ $result }}
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,17 @@ result_1_5Gi: {{ $result }}
result_0_25Mi: {{ $result }}
{{- $result := include "helm_lib_resources_management_memory_units_to_bytes" "0.5" }}
result_0_5: {{ $result }}
{{- $result := include "helm_lib_resources_management_memory_units_to_bytes" "+1Gi" }}
result_plus_1Gi: {{ $result }}
{{- $result := include "helm_lib_resources_management_memory_units_to_bytes" "1e3" }}
result_1e3: {{ $result }}
{{- $result := include "helm_lib_resources_management_memory_units_to_bytes" "abcGi" }}
result_garbage_suffixed: {{ $result }}
{{- $result := include "helm_lib_resources_management_memory_units_to_bytes" "100m" }}
result_100m: {{ $result }}
{{- $result := include "helm_lib_resources_management_memory_units_to_bytes" "100n" }}
result_100n: {{ $result }}
{{- $result := include "helm_lib_resources_management_memory_units_to_bytes" "100u" }}
result_100u: {{ $result }}
{{- $result := include "helm_lib_resources_management_memory_units_to_bytes" "1Ei" }}
result_1Ei: {{ $result }}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@
result_static: {{ $result | toString | quote }}
{{- $config := dict "mode" "VPA" "vpa" (dict "cpu" (dict "min" "100m" "limitRatio" 2) "memory" (dict "min" "128Mi" "limitRatio" 2)) }}
{{- $result := include "helm_lib_resources_management_original_pod_resources" $config }}
result_vpa: {{ $result | toString | quote }}
result_vpa: {{ $result | toString | quote }}
{{- $config := dict "mode" "VPA" "vpa" (dict "cpu" (dict "min" 0.5 "limitRatio" 1.5) "memory" (dict "min" "0.5Gi" "limitRatio" 1.5)) }}
{{- $parsed := include "helm_lib_resources_management_original_pod_resources" $config | fromYaml }}
result_fractional_limits_cpu: {{ $parsed.limits.cpu | quote }}
result_fractional_limits_memory: {{ $parsed.limits.memory | quote }}
result_fractional_requests_cpu: {{ $parsed.requests.cpu | quote }}
result_fractional_requests_memory: {{ $parsed.requests.memory | quote }}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,62 @@ tests:
- equal:
path: result_number_0_5
value: 500
- equal:
path: result_1_dot
value: 1000

- it: should round a fractional millicore value up
asserts:
- equal:
path: result_0_5m
value: 1

- it: should convert every Kubernetes suffix
asserts:
- equal:
path: result_100u
value: 1
- equal:
path: result_100n
value: 1
- equal:
path: result_1k
value: 1000000
- equal:
path: result_1e3
value: 1000000
- equal:
path: result_1P
value: 1000000000000000000

# The helper converts, it does not validate. A negative quantity is passed through as a
# negative number, exactly as before, and rejecting it belongs to the caller.
- it: should propagate a sign rather than validate it
asserts:
- equal:
path: result_plus_1
value: 1000
- equal:
path: result_minus_1
value: -1000

- it: should yield zero instead of aborting the rendering
asserts:
- equal:
path: result_garbage
value: 0

# 10^18 cores does not fit an int64 once expressed in millicores. Saturating or wrapping
# would hand the caller a limit the API server rejects, so the result is zero, as it was
# before these suffixes reached the CPU helper.
- it: should yield zero for a result that does not fit an int64
asserts:
- equal:
path: result_1E
value: 0
- equal:
path: result_1Ei
value: 0
- equal:
path: result_10P
value: 0
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,39 @@ tests:
- equal:
path: result_0_5
value: 1

# Sub-byte suffixes are legal in a Kubernetes quantity and used to abort the rendering here,
# while the CPU helper accepted them.
- it: should convert the sub-byte suffixes
asserts:
- equal:
path: result_100m
value: 1
- equal:
path: result_100n
value: 1
- equal:
path: result_100u
value: 1

# The largest binary suffix stays on the integer path, where it is exact.
- it: should keep the largest suffix exact
asserts:
- equal:
path: result_1Ei
value: 1152921504606846976

- it: should keep the sign and the exponent form
asserts:
- equal:
path: result_plus_1Gi
value: 1073741824
- equal:
path: result_1e3
value: 1000

- it: should yield zero for a known suffix with an unparsable mantissa
asserts:
- equal:
path: result_garbage_suffixed
value: 0
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,21 @@ tests:
asserts:
- notEqual:
path: result_vpa
value: ""
value: ""
- it: should size the limits from a fractional min instead of collapsing them to zero
asserts:
# 0.5 cores * 1.5 = 750m, against requests of 0.5 cores. A limit below the requests
# is rejected by the API server, which is what the zero used to produce.
- equal:
path: result_fractional_limits_cpu
value: "750m"
- equal:
path: result_fractional_requests_cpu
value: "0.5"
# 0.5Gi * 1.5 = 805306368 bytes.
- equal:
path: result_fractional_limits_memory
value: "805306368"
- equal:
path: result_fractional_requests_memory
value: "0.5Gi"
Loading