Helm 在values配置存在时 default函数 依然生效 的原因及解决

问题

在一些特殊的场景,对于pod的更新有时会通过helm upgrade 将replicas从1->0->1变化,但是当replicas 模板设置了default函数时,replicas会出现非预期的变化。

如下 helm 模板中

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "example.fullname" . }}
  labels:
    {{- include "example.labels" . | nindent 4 }}
spec:
  replicas: {{ default 1 .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "example.selectorLabels" . | nindent 6 }}
  template:

将values.yaml的replicaCount设置为0时,并不会将replicas置为0,而是使用默认值1

helm template -s templates/deployment.yaml .
....
spec:
  replicas: 1

原因

default函数用于设置空值时的默认值,上述错误的原因源于 default 函数对 空值的定义
空值取决于以下类型:

  • 整型: 0
  • 字符串: “”
  • 列表: []
  • 字典: {}
  • 布尔: false
  • 以及所有的nil (或 null)

因此当values.yaml中将replicaCount为0,整型为0时等于空值,即使用default函数设置的值(1)

解决

使用 kindIs 函数

spec:
  {{- if kindIs "invalid" .Values.replicaCount}}
  replicas: 1
  {{- else }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "example.selectorLabels" . | nindent 6 }}

效果展示

$ helm template -s templates/deployment_kindis.yaml .
...
spec:
  replicas: 0
  selector:

kindIs "invalid"可以用于判断是否为 nil(values.yaml未设置)

PS: 此处使用replicas作为示例 略有牵强,请见谅

reference

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值