如何在Helm多子Chart中用对象数组配置依赖的condition字段
通过父Chart的condition字段控制子Chart安装的解决方案
问题背景
你希望通过父Chart的condition字段控制子Chart的安装,当前设计了如下values.schema.yaml:
{ "$schema": "https://json-schema.org/draft-07/schema#", "properties": { "global": { "type": "object", "properties": { "deploy": { "description": "Controls which subcharts (not) to deploy. Provide a separate values file to enable them.", "type": "array", "items": { "type": "object", "properties": { "name": { "description": "Name of the subchart", "type": "string" }, "enabled": { "description": "Whether to install the subchart or not", "type": "boolean" } } } } } } } }
对应的父Chart values.yaml 配置示例:
global: deploy: - name: "subchart01" enabled: false - name: "subchart02" enabled: true
但无法在Chart.yaml的condition字段中通过数组匹配子Chart名称来引用对应的enabled值:
dependencies: - name: subchart01 version: 0.1.0 repository: "file://charts/subchart01" condition: global.deploy. ??? .enabled
核心问题
Helm的condition字段仅支持固定YAML路径的布尔值引用,无法通过数组元素的name字段进行动态匹配取值,因此当前数组结构的global.deploy无法直接满足需求。
解决方案
方案一:调整为对象结构(推荐)
将global.deploy从数组改为对象,以子Chart名称作为对象的key,直接存储对应enabled状态,这样就能通过固定路径引用。
修改后的values.schema.yaml:
{ "$schema": "https://json-schema.org/draft-07/schema#", "properties": { "global": { "type": "object", "properties": { "deploy": { "description": "Controls which subcharts (not) to deploy. Provide a separate values file to enable them.", "type": "object", "additionalProperties": { "type": "object", "properties": { "enabled": { "description": "Whether to install the subchart or not", "type": "boolean" } } } } } } } }
对应的values.yaml配置:
global: deploy: subchart01: enabled: false subchart02: enabled: true
此时Chart.yaml的condition字段可直接写固定路径:
dependencies: - name: subchart01 version: 0.1.0 repository: "file://charts/subchart01" condition: global.deploy.subchart01.enabled - name: subchart02 version: 0.1.0 repository: "file://charts/subchart02" condition: global.deploy.subchart02.enabled
方案二:采用Helm常规模式(更简洁)
直接在父Chart的顶层values中为每个子Chart设置独立的enabled字段,这是Helm社区的常规做法,无需嵌套在global.deploy下:
修改后的values.schema.yaml:
{ "$schema": "https://json-schema.org/draft-07/schema#", "properties": { "subchart01": { "type": "object", "properties": { "enabled": { "description": "Whether to install subchart01", "type": "boolean", "default": false } } }, "subchart02": { "type": "object", "properties": { "enabled": { "description": "Whether to install subchart02", "type": "boolean", "default": false } } } } }
对应的values.yaml:
subchart01: enabled: false subchart02: enabled: true
Chart.yaml的condition配置更简洁:
dependencies: - name: subchart01 version: 0.1.0 repository: "file://charts/subchart01" condition: subchart01.enabled - name: subchart02 version: 0.1.0 repository: "file://charts/subchart02" condition: subchart02.enabled
内容的提问来源于stack exchange,提问作者BdN3504
相关产品推荐
相关产品推荐

