Helm 模板浮点运算

Helm template float arithmetic

$ helm version
version.BuildInfo{Version:"v3.3.0", GitCommit:"8a4aeec08d67a7b84472007529e8097ec3742105", GitTreeState:"dirty", GoVersion:"go1.14.6"}

所以我有我的模板:

  minAvailable: {{ mul .Values.autoscaling.minReplicas 0.75 }}

values.yaml:

autoscaling:
  minReplicas: 3

我本来希望得到 2.25 的渲染输出,但我得到了 0(3 * 0 因为 0.75 被压低了...)

我试过

  minAvailable: {{ mul (float .Values.autoscaling.minReplicas) 0.75 }}

最终我会floor返回一个整数的值...

  minAvailable: {{ floor ( mul .Values.autoscaling.minReplicas 0.75 ) }}

但我只是不明白为什么我似乎不能做简单的浮点运算


我尝试过的其他东西

  minAvailable: {{ float64 .Values.autoscaling.minReplicas }} 
  minAvailable: {{ float64 .Values.autoscaling.minReplicas | toString }} 

不生成浮点数....

我什至在 values.yaml

中尝试过这样做
autoscaling:
  minReplicas: 3.0

Pod 中断预算实际上占百分比...

也可以

  minAvailable: "66%" # 2/3

  minAvailable: "75%" # 3/4

来自文档:

if you have 7 Pods and you set minAvailable to "50%", it's not immediately obvious whether that means 3 Pods or 4 Pods must be available. Kubernetes rounds up to the nearest integer, so in this case, 4 Pods must be available.

所以本质上,366%1.98,因此将四舍五入为 2

这些算术函数不是页面顶部 text/template language. They come from a package of useful extensions called Sprig that Helm includes. In particular, the documentation for its Math Functions 状态的核心 Go 的一部分

All math functions operate on int64 values unless specified otherwise.

您可以计算整数 x * 3 / 4,而不是尝试计算 floating-point x * 0.75。将其分解为 (x * 3) / 4,您可以将其作为相当精确的整数算法来执行:

minAvailable: {{ div (mul .Values.autoscaling.minReplicas 3) 4 }}

Helm 及其模板支持默认的 Go text/template functions and the function provided by the Sprig extension. Since Sprig version 3.2 it also supports Float Math Functions,例如 addfsubfmulfdivf 等。在您的情况下,您会刚需:

  minAvailable: {{ mulf .Values.autoscaling.minReplicas 0.75 }}