From 825aabd681c9f9c57963ef91a2cb51fe7f6908e0 Mon Sep 17 00:00:00 2001 From: suselz Date: Mon, 21 Sep 2026 14:13:05 +0500 Subject: [PATCH 1/2] fix(resources-management): stop aborting the rendering on a valid quantity 1.72.23 gave the CPU helper a `fail` branch it never had and narrowed both helpers to two regexes. Anything outside them now aborts the rendering of the whole chart, and the Kubernetes quantity grammar is wider than those regexes: cpu "+1" FAIL, was 1000 -- rendered correctly before cpu "-1" FAIL, was -1000 cpu "1." FAIL, was 0 cpu "100u" FAIL, was 0 cpu "100n" FAIL, was 0 cpu "1k" FAIL, was 0 mem "+1Gi" FAIL, was 1073741824 mem "abcGi" FAIL, was 0 A CRD that validates a quantity with the standard pattern accepts every one of them, and there is no canonicalization on the way to the template, so the value arrives verbatim. `+1` is the sharp case: it produced the right number before and takes the release down now. Both helpers now parse the grammar they are given: an optional sign, a decimal mantissa with either side of the point optional, a decimal exponent, and the n, u, m, k, M, G, T, P, E and Ki...Ei suffixes. Exact integer arithmetic still covers an integer mantissa, so no value changes its result. Nothing that used to produce a number aborts any more. An unparsable CPU value yields 0 as it did before 1.72.23, and a memory value with a known suffix and an unparsable mantissa does the same. The `fail` of the memory helper survives only where it already was, on a value carrying no suffix and no digits. The tests gained a case per quantity form, and `helm_lib_resources_management_original_pod_resources` gained the assertion that was missing when the zero was introduced: a fractional `vpa.cpu.min` with a `limitRatio` has to size the limits, not collapse them below the requests. Signed-off-by: suselz --- charts/helm_lib/Chart.yaml | 2 +- charts/helm_lib/README.md | 10 +-- .../templates/_resources_management.tpl | 72 ++++++++++++++----- ...es_management_cpu_units_to_millicores.yaml | 16 +++++ ...rces_management_memory_units_to_bytes.yaml | 6 ++ ...ces_management_original_pod_resources.yaml | 8 ++- ...nagement_cpu_units_to_millicores_test.yaml | 45 ++++++------ ...management_memory_units_to_bytes_test.yaml | 41 +++++------ ...anagement_original_pod_resources_test.yaml | 19 ++++- 9 files changed, 150 insertions(+), 69 deletions(-) diff --git a/charts/helm_lib/Chart.yaml b/charts/helm_lib/Chart.yaml index 53879cb..a877c2d 100644 --- a/charts/helm_lib/Chart.yaml +++ b/charts/helm_lib/Chart.yaml @@ -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." diff --git a/charts/helm_lib/README.md b/charts/helm_lib/README.md index 5b593ef..fe15dfc 100644 --- a/charts/helm_lib/README.md +++ b/charts/helm_lib/README.md @@ -1879,8 +1879,9 @@ 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. #### Usage @@ -1891,8 +1892,9 @@ 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 + 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. #### Usage diff --git a/charts/helm_lib/templates/_resources_management.tpl b/charts/helm_lib/templates/_resources_management.tpl index 23202e6..ad066d0 100644 --- a/charts/helm_lib/templates/_resources_management.tpl +++ b/charts/helm_lib/templates/_resources_management.tpl @@ -94,34 +94,68 @@ updatePolicy: {{- /* Usage: {{ include "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. */ -}} {{- 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 -}} + {{- $mantissa | float64 | mulf $cores | mulf 1000.0 | ceil | int64 -}} + {{- else -}} + {{- 0 -}} + {{- end -}} {{- end }} {{- end }} {{- /* Usage: {{ include "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 */ -}} +{{- /* 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. */ -}} {{- 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 -}} @@ -146,11 +180,15 @@ updatePolicy: {{- $mantissa = trimSuffix "P" $units -}}{{- $factor = 1000000000000000 -}} {{- else if hasSuffix "E" $units -}} {{- $mantissa = trimSuffix "E" $units -}}{{- $factor = 1000000000000000000 -}} + {{- else -}} + {{- $suffixed = false -}} {{- end -}} - {{- if regexMatch "^[0-9]+$" $mantissa -}} + {{- if regexMatch "^[+-]?[0-9]+$" $mantissa -}} {{- $mantissa | atoi | mul $factor -}} - {{- else if regexMatch "^[0-9]*[.]?[0-9]+([eE][+-]?[0-9]+)?$" $mantissa -}} + {{- else if regexMatch "^[+-]?(([0-9]+([.][0-9]*)?)|([.][0-9]+))([eE][+-]?[0-9]+)?$" $mantissa -}} {{- $mantissa | float64 | mulf (float64 $factor) | ceil | int64 -}} + {{- else if $suffixed -}} + {{- 0 -}} {{- else -}} {{- cat "ERROR: unknown memory format:" $units | fail -}} {{- end }} diff --git a/tests/templates/helm_lib_resources_management_cpu_units_to_millicores.yaml b/tests/templates/helm_lib_resources_management_cpu_units_to_millicores.yaml index e024f1e..bda83f7 100644 --- a/tests/templates/helm_lib_resources_management_cpu_units_to_millicores.yaml +++ b/tests/templates/helm_lib_resources_management_cpu_units_to_millicores.yaml @@ -12,3 +12,19 @@ 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 }} diff --git a/tests/templates/helm_lib_resources_management_memory_units_to_bytes.yaml b/tests/templates/helm_lib_resources_management_memory_units_to_bytes.yaml index 04e61e6..9d09fb7 100644 --- a/tests/templates/helm_lib_resources_management_memory_units_to_bytes.yaml +++ b/tests/templates/helm_lib_resources_management_memory_units_to_bytes.yaml @@ -14,3 +14,9 @@ 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 }} diff --git a/tests/templates/helm_lib_resources_management_original_pod_resources.yaml b/tests/templates/helm_lib_resources_management_original_pod_resources.yaml index 98511b8..a5acf6c 100644 --- a/tests/templates/helm_lib_resources_management_original_pod_resources.yaml +++ b/tests/templates/helm_lib_resources_management_original_pod_resources.yaml @@ -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 }} \ No newline at end of file +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 }} diff --git a/tests/tests/helm_lib_resources_management_cpu_units_to_millicores_test.yaml b/tests/tests/helm_lib_resources_management_cpu_units_to_millicores_test.yaml index 767a49a..ecaa227 100644 --- a/tests/tests/helm_lib_resources_management_cpu_units_to_millicores_test.yaml +++ b/tests/tests/helm_lib_resources_management_cpu_units_to_millicores_test.yaml @@ -4,30 +4,33 @@ templates: tests: - it: should convert CPU units to millicores asserts: - - equal: - path: result_100m - value: 100 - - equal: - path: result_2 - value: 2000 + - equal: {path: result_100m, value: 100} + - equal: {path: result_2, value: 2000} - it: should convert fractional CPU units to millicores asserts: - - equal: - path: result_0_5 - value: 500 - - equal: - path: result_1_5 - value: 1500 - - equal: - path: result_dot_25 - value: 250 - - equal: - path: result_number_0_5 - value: 500 + - equal: {path: result_0_5, value: 500} + - equal: {path: result_1_5, value: 1500} + - equal: {path: result_dot_25, value: 250} + - equal: {path: result_number_0_5, value: 500} + - equal: {path: result_1_dot, value: 1000} + + - it: should keep the sign + asserts: + - equal: {path: result_plus_1, value: 1000} + - equal: {path: result_minus_1, value: -1000} + + - 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} - it: should round a fractional millicore value up asserts: - - equal: - path: result_0_5m - value: 1 + - equal: {path: result_0_5m, value: 1} + + - it: should yield zero instead of aborting the rendering + asserts: + - equal: {path: result_garbage, value: 0} diff --git a/tests/tests/helm_lib_resources_management_memory_units_to_bytes_test.yaml b/tests/tests/helm_lib_resources_management_memory_units_to_bytes_test.yaml index 5eb62c9..3cd0d2e 100644 --- a/tests/tests/helm_lib_resources_management_memory_units_to_bytes_test.yaml +++ b/tests/tests/helm_lib_resources_management_memory_units_to_bytes_test.yaml @@ -4,33 +4,26 @@ templates: tests: - it: should convert memory units to bytes asserts: - - equal: - path: result_100Mi - value: 104857600 - - equal: - path: result_2Gi - value: 2147483648 - - equal: - path: result_512 - value: 512 - - equal: - path: result_100M - value: 100000000 + - equal: {path: result_100Mi, value: 104857600} + - equal: {path: result_2Gi, value: 2147483648} + - equal: {path: result_512, value: 512} + - equal: {path: result_100M, value: 100000000} - it: should convert fractional memory units to bytes asserts: - - equal: - path: result_0_5Gi - value: 536870912 - - equal: - path: result_1_5Gi - value: 1610612736 - - equal: - path: result_0_25Mi - value: 262144 + - equal: {path: result_0_5Gi, value: 536870912} + - equal: {path: result_1_5Gi, value: 1610612736} + - equal: {path: result_0_25Mi, value: 262144} - it: should round a fractional byte value up asserts: - - equal: - path: result_0_5 - value: 1 + - equal: {path: result_0_5, value: 1} + + - 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} diff --git a/tests/tests/helm_lib_resources_management_original_pod_resources_test.yaml b/tests/tests/helm_lib_resources_management_original_pod_resources_test.yaml index 7b0d972..7d0ddec 100644 --- a/tests/tests/helm_lib_resources_management_original_pod_resources_test.yaml +++ b/tests/tests/helm_lib_resources_management_original_pod_resources_test.yaml @@ -11,4 +11,21 @@ tests: asserts: - notEqual: path: result_vpa - value: "" \ No newline at end of file + 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" From 9895bf56212ab32a960893b9b8d4de25aa631528 Mon Sep 17 00:00:00 2001 From: suselz Date: Mon, 21 Sep 2026 15:48:01 +0500 Subject: [PATCH 2/2] fix(resources-management): cover the sub-byte suffixes and guard the int64 conversion Review of the previous commit found two cases it missed and one claim it got wrong. The memory helper never gained the n, u and m branches its CPU sibling did, so `100m`, `100n` and `100u` fell through to the `fail`. All three are valid Kubernetes quantities, and the commit message claimed the `fail` was reachable only by "a value carrying no suffix and no digits", which `100m` plainly contradicts. The branches are there now and the claim holds. Putting the k...E and Ki...Ei suffixes on the CPU path made a result of 10^18 cores expressible, and in millicores it no longer fits an int64. `1E`, `1Ei` and `10P` came back saturated at the int64 maximum, or wrapped negative on another toolchain; either way the caller got a limit the API server rejects where the value used to be 0. Both helpers now check the range before the conversion and return 0 outside it, which is what these inputs did before the suffixes were added. The doc comments state what the helpers promise: they convert, they do not validate. A negative quantity comes back negative, an out-of-range one comes back as 0, and rejecting either belongs to the caller. The test that asserts the sign says so too, rather than reading as an endorsement of a negative limit. The assertions that were reflowed into flow style are back in the block style of the file, so the diff carries additions only. Signed-off-by: suselz --- charts/helm_lib/README.md | 8 +- .../templates/_resources_management.tpl | 30 +++++-- ...es_management_cpu_units_to_millicores.yaml | 8 ++ ...rces_management_memory_units_to_bytes.yaml | 8 ++ ...nagement_cpu_units_to_millicores_test.yaml | 84 +++++++++++++++---- ...management_memory_units_to_bytes_test.yaml | 65 +++++++++++--- 6 files changed, 168 insertions(+), 35 deletions(-) diff --git a/charts/helm_lib/README.md b/charts/helm_lib/README.md index fe15dfc..28550da 100644 --- a/charts/helm_lib/README.md +++ b/charts/helm_lib/README.md @@ -1882,6 +1882,8 @@ list: 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 @@ -1893,8 +1895,10 @@ list: helper for converting memory units to bytes Accepts any Kubernetes memory quantity: an optional sign, a decimal mantissa, and any of the - 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. + 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 diff --git a/charts/helm_lib/templates/_resources_management.tpl b/charts/helm_lib/templates/_resources_management.tpl index ad066d0..d9fb998 100644 --- a/charts/helm_lib/templates/_resources_management.tpl +++ b/charts/helm_lib/templates/_resources_management.tpl @@ -97,6 +97,8 @@ updatePolicy: {{- /* 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 -}} {{- if regexMatch "^[+-]?[0-9]+m$" $units -}} @@ -138,7 +140,12 @@ updatePolicy: {{- $mantissa = trimSuffix "E" $units -}}{{- $cores = 1000000000000000000.0 -}} {{- end -}} {{- if regexMatch "^[+-]?(([0-9]+([.][0-9]*)?)|([.][0-9]+))([eE][+-]?[0-9]+)?$" $mantissa -}} - {{- $mantissa | float64 | mulf $cores | mulf 1000.0 | ceil | int64 -}} + {{- $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 -}} @@ -149,8 +156,10 @@ updatePolicy: {{- /* Usage: {{ include "helm_lib_resources_management_memory_units_to_bytes" }} */ -}} {{- /* helper for converting memory units to bytes */ -}} {{- /* Accepts any Kubernetes memory quantity: an optional sign, a decimal mantissa, and any of the */ -}} -{{- /* 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. */ -}} +{{- /* 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 -}} @@ -168,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 -}} @@ -183,10 +198,15 @@ updatePolicy: {{- else -}} {{- $suffixed = false -}} {{- 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]*)?)|([.][0-9]+))([eE][+-]?[0-9]+)?$" $mantissa -}} - {{- $mantissa | float64 | mulf (float64 $factor) | ceil | int64 -}} + {{- $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 -}} diff --git a/tests/templates/helm_lib_resources_management_cpu_units_to_millicores.yaml b/tests/templates/helm_lib_resources_management_cpu_units_to_millicores.yaml index bda83f7..75a9265 100644 --- a/tests/templates/helm_lib_resources_management_cpu_units_to_millicores.yaml +++ b/tests/templates/helm_lib_resources_management_cpu_units_to_millicores.yaml @@ -28,3 +28,11 @@ result_1k: {{ $result }} 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 }} diff --git a/tests/templates/helm_lib_resources_management_memory_units_to_bytes.yaml b/tests/templates/helm_lib_resources_management_memory_units_to_bytes.yaml index 9d09fb7..0f04638 100644 --- a/tests/templates/helm_lib_resources_management_memory_units_to_bytes.yaml +++ b/tests/templates/helm_lib_resources_management_memory_units_to_bytes.yaml @@ -20,3 +20,11 @@ result_plus_1Gi: {{ $result }} 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 }} diff --git a/tests/tests/helm_lib_resources_management_cpu_units_to_millicores_test.yaml b/tests/tests/helm_lib_resources_management_cpu_units_to_millicores_test.yaml index ecaa227..868d9a5 100644 --- a/tests/tests/helm_lib_resources_management_cpu_units_to_millicores_test.yaml +++ b/tests/tests/helm_lib_resources_management_cpu_units_to_millicores_test.yaml @@ -4,33 +4,83 @@ templates: tests: - it: should convert CPU units to millicores asserts: - - equal: {path: result_100m, value: 100} - - equal: {path: result_2, value: 2000} + - equal: + path: result_100m + value: 100 + - equal: + path: result_2 + value: 2000 - it: should convert fractional CPU units to millicores asserts: - - equal: {path: result_0_5, value: 500} - - equal: {path: result_1_5, value: 1500} - - equal: {path: result_dot_25, value: 250} - - equal: {path: result_number_0_5, value: 500} - - equal: {path: result_1_dot, value: 1000} + - equal: + path: result_0_5 + value: 500 + - equal: + path: result_1_5 + value: 1500 + - equal: + path: result_dot_25 + value: 250 + - equal: + path: result_number_0_5 + value: 500 + - equal: + path: result_1_dot + value: 1000 - - it: should keep the sign + - it: should round a fractional millicore value up asserts: - - equal: {path: result_plus_1, value: 1000} - - equal: {path: result_minus_1, value: -1000} + - 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_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 - - it: should round a fractional millicore value up + # 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_0_5m, value: 1} + - 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} + - 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 diff --git a/tests/tests/helm_lib_resources_management_memory_units_to_bytes_test.yaml b/tests/tests/helm_lib_resources_management_memory_units_to_bytes_test.yaml index 3cd0d2e..f4dfc07 100644 --- a/tests/tests/helm_lib_resources_management_memory_units_to_bytes_test.yaml +++ b/tests/tests/helm_lib_resources_management_memory_units_to_bytes_test.yaml @@ -4,26 +4,69 @@ templates: tests: - it: should convert memory units to bytes asserts: - - equal: {path: result_100Mi, value: 104857600} - - equal: {path: result_2Gi, value: 2147483648} - - equal: {path: result_512, value: 512} - - equal: {path: result_100M, value: 100000000} + - equal: + path: result_100Mi + value: 104857600 + - equal: + path: result_2Gi + value: 2147483648 + - equal: + path: result_512 + value: 512 + - equal: + path: result_100M + value: 100000000 - it: should convert fractional memory units to bytes asserts: - - equal: {path: result_0_5Gi, value: 536870912} - - equal: {path: result_1_5Gi, value: 1610612736} - - equal: {path: result_0_25Mi, value: 262144} + - equal: + path: result_0_5Gi + value: 536870912 + - equal: + path: result_1_5Gi + value: 1610612736 + - equal: + path: result_0_25Mi + value: 262144 - it: should round a fractional byte value up asserts: - - equal: {path: result_0_5, value: 1} + - 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} + - 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} + - equal: + path: result_garbage_suffixed + value: 0