[cozy-lib] Add a hexToInt helper

Simplifies parsing sha256 digits to decimals for the VPC peering
feature.

Signed-off-by: Timofei Larkin <lllamnyp@gmail.com>
This commit is contained in:
Timofei Larkin 2026-03-19 07:29:47 +03:00
parent 2c0a9fb2cc
commit 55f6882387
2 changed files with 5 additions and 7 deletions

View file

@ -16,7 +16,6 @@ spec:
namespaces:
- {{ .Release.Namespace }}
{{- if .Values.peers }}
{{- $hexLookup := dict "0" 0 "1" 1 "2" 2 "3" 3 "4" 4 "5" 5 "6" 6 "7" 7 "8" 8 "9" 9 "a" 10 "b" 11 "c" 12 "d" 13 "e" 14 "f" 15 }}
vpcPeerings:
{{- range .Values.peers }}
{{- $remoteRelease := printf "virtualprivatecloud-%s" .vpcName }}
@ -24,12 +23,8 @@ spec:
{{- $sorted := list $vpcId $remoteVpcId | sortAlpha }}
{{- $pairKey := join "/" $sorted }}
{{- $pairHash := sha256sum $pairKey }}
{{- $h0 := int (get $hexLookup (substr 0 1 $pairHash)) }}
{{- $h1 := int (get $hexLookup (substr 1 2 $pairHash)) }}
{{- $h2 := int (get $hexLookup (substr 2 3 $pairHash)) }}
{{- $h3 := int (get $hexLookup (substr 3 4 $pairHash)) }}
{{- $byte0 := add (mul $h0 16) $h1 }}
{{- $byte1 := add (mul $h2 16) $h3 }}
{{- $byte0 := int (include "cozy-lib.strings.hexToInt" (substr 0 2 $pairHash)) }}
{{- $byte1 := int (include "cozy-lib.strings.hexToInt" (substr 2 4 $pairHash)) }}
{{- $oct3 := int (add (mod $byte0 254) 1) }}
{{- $base4 := int (mul (mod $byte1 64) 4) }}
{{- if eq $vpcId (index $sorted 0) }}

View file

@ -0,0 +1,3 @@
{{- define "cozy-lib.strings.hexToInt" }}
{{- printf "num: 0x%s" . | fromYaml | dig "num" 0 }}
{{- end }}