kubernetes-helm - 如何覆盖 Helm Chart 中的表/映射
问题描述
我有values.yaml
一个
ingress:
enabled: false
volume:
hostPath:
path: /tmp
type: DirectoryOrCreate
我有一个overlay.yaml
改变values.yaml
.
ingress:
enabled: true
volume:
persistentVolumeClaim:
claimName: test
对于入口,它像我怀疑的那样工作,因为值enabled
将变为 true。但是,对于该卷,似乎表是相互添加而不是被覆盖的。例如,我会得到类似的东西:
volume:
persistentVolumeClaim:
claimName: test
hostPath:
path: /tmp
type: DirectoryOrCreate
我想在 values.yaml 中指定默认的卷类型及其配置(例如路径),但其他人可以自由地通过覆盖来更改它。但是,我现在“添加”了一个卷类型,而不是覆盖它。有没有办法做到这一点?
解决方案
null
是一个特定的有效 YAML 值(与 JSON 相同null
)。如果您将 Helm 值设置为null
,则 Gotext/template
逻辑会将其解组为 Go nil
,并且它将在if
语句和类似条件中显示为“false”。
volume:
persistentVolumeClaim:
claimName: test
hostPath: null
不过,我可能会在图表逻辑中避免这个问题。一种方法是使用单独的type
字段来说明您要查找的子字段:
# the chart's defaults in values.yaml
volume:
type: HostPath
hostPath: { ... }
# your `helm install -f` overrides
volume:
type: PersistentVolumeClaim
persistentVolumeClaim: { ... }
# (the default hostPath: will be present but unused)
第二种选择是使默认值“不存在”,或者完全禁用该功能,或者在图表代码中构建合理的默认值(如果它不存在)。
# values.yaml
# volume specifies where the application will keep persistent data.
# If unset, each replica will have independent data and this data will
# be lost on restart.
#
# volume:
#
# persistentVolumeClaim stores data in an existing PVC.
#
# persistentVolumeClaim:
# name: ???
# deep in templates/deployment.yaml
volumes:
{{- if .Values.volume.persistentVolumeClaim }}
- name: storage
persistentVolumeClaim:
claimName: {{ .Values.volume.persistentVolumeClaim.name }}
{{- else if .Values.volume.hostPath }}
- name: storage
hostPath:
path: {{ .Values.volume.hostPath.path }}
{{- end }}
{{-/* (just don't create a volume if not set) */}}
或者,总是提供某种存储,即使它不是那么有用:
volumes:
- name: storage
{{- if .Values.volume.persistentVolumeClaim }}
persistentVolumeClaim:
claimName: {{ .Values.volume.persistentVolumeClaim.name }}
{{- else if .Values.volume.hostPath }}
hostPath:
path: {{ .Values.volume.hostPath.path }}
{{- else }}
emptyDir: {}
{{- end }}
推荐阅读
- julia - 从匿名函数访问另一个函数的参数
- python - Python正则表达式用一个或多个单词验证名称?
- ruby-on-rails - 在相同的 2 个对象之间有 2 个不同的关联通常是不好的做法吗?
- algorithm - 算法 - 最小交换以调整矩阵
- python - 我正在使用 pygame 开发这款游戏,我正在尝试添加动画电源,但我遇到了麻烦
- ios - 在 Flutter VScode 中运行调试 ios 应用程序时出错
- python - 蟒蛇 | 我期待的情节没有出现
- c++ - CMake:OGREConfig.cmake
- python - 在 Chrome 中使用 Headless 模式效果不佳?
- mysql - 创建mysql表并加载数据