在Helm模板中编码整数

问题描述 投票:1回答:1

我正在为Web服务开发一组Helm模板,该模板将整数ID作为其配置的一部分。该ID成为服务端点的一部分,被编码为Web安全的base64字符集:

0=A
1=B
2=C
...
26=a
...
63=_

在我的Helm模板中,我希望采用该整数ID并确定编码值,以便可以将其插入Nginx location块中。实际的编码逻辑类似于(伪代码):

func Encode(int i) {
  byte b = i << 2 # shift integer two bits
  string s = web_base64(b)
  char c = s[0] # return first char only
}

到目前为止,我在Helm中获得的距离最近的只是创建一个查找,例如$d := dict "0" "A" "1" "B" "2" "C" ...,然后使用{{ .Values.Id | toString | get $d }}

还有另一种方法吗?

kubernetes-helm sprig-template-functions
1个回答
0
投票

我终于想到了:

{{- with .Values.deployment.Id | int }}
  {{- if eq . 63 }}_
  {{- else if eq . 62 }}-
  {{- else if gt . 51 }}{{- sub . 52 | printf "%c" }}
  {{- else if gt . 25 }}{{- add . 71 | printf "%c" }}
  {{- else }}{{- add . 65 | printf "%c" }}
  {{- end }}
{{- end }}

意识到我可以通过printf进行有序转换是一个很大的时刻,只要.Id的值不为0,此方法就很好用。如果是,则跳过整个块。这似乎是对with关键字的限制。所以,我只剩下这个了:

{{- if eq (int .Values.deployment.Id) 63 }}_
{{- else if eq (int .Values.deployment.Id) 62 }}-
{{- else if gt (int .Values.deployment.Id) 51 }}{{- sub (int .Values.deployment.Id) 52 | printf "%c" }}
{{- else if gt (int .Values.deployment.Id) 25 }}{{- add (int .Values.deployment.Id) 71 | printf "%c" }}
{{- else }}{{- add (int .Values.deployment.Id) 65 | printf "%c" }}
{{- end }}

仍然有些丑陋,但是比庞大的查找要好。

© www.soinside.com 2019 - 2024. All rights reserved.