1. Helm 依赖管理
在 Helm 中,一个 Chart 可能依赖于任何数量的其他 Chart。
虽然有一些团队需要手动管理依赖关系的优势,但声明依赖关系的首选方法是使用 Chart 内部管理文件。
- Helm v2 版本v通过 requirements.yaml 文件动态链接或引入 charts/ 目录并手动管理。
Helm v3 版本通过 Charts.yaml 中 dependencies 配置,同样管理 charts/ 目录。
配置使用方式 Chart.yaml 文件与 Chart 对象 。
依赖的管理命令 helm dependency
1.1. Chart.yaml 中支持的所有字段
dependencies:
- name: string
version: string
repository: string
condition: string
tags: []string
enabled: bool
import-values: []interface{}
alias: string
在 dependencies 中依赖说明
name
: 依赖 Chart 包的名称,需要和实际的 Chart 名称保持一致version
: 依赖 Chart 的固定版本或版本范围,在 lock 文件中只会明确一个固定的版本repository
: 获取 Chart 的仓库地址,在这个仓库中有 index.yaml 文件,并能够找到这个 Chartcondition
: 一个 boolean 类型的 yaml 路径(值),用来启用/禁用 chart (如:subchart1.enabled
)tags
: Tags 可以用来给 chart 分组,一起启用或禁用。enabled
: 用来确认是否需要启用加载该 Chart 的一个 bool 值import-values
: ImportValues 保存要导入的源值到父键的映射。每个项可以是一个字符串或一对子/父子列表项。alias
: 给 Chart 配置一个 alias 别名
1.2. 字段特别说明
1.2.1. version 版本
如果有可能的话,使用版本范围而不是某个固定的版本。建议的默认设置时使用补丁级别版本的匹配:
version: ~1.2.3
这样会匹配 1.2.3
以及该版本的任何补丁,也就是说,~1.2.3
相当于 >= 1.2.3, < 1.3.0
关于完整的版本匹配语法,请参照 语义版本文档。
预发布版本
上述版本约束不适用于预发布版本。比如 version: ~1.2.3
可以匹配 version: ~1.2.4
但不能匹配 version: ~1.2.3-1
。 预发布及补丁级别匹配如下:
version: ~1.2.3-0
1.2.2. repository 仓库 URL
如果可能的话,使用 https://
仓库 URL,而不是 http://
URL。
如果这个仓库已经被添加到仓库索引文件中,仓库名称可以作为 URL 的别名。使用 alias:
或 @
后跟仓库名称。
文件 URL( file://...
) 被认为是一种有固定部署管道组装的 chart 的 “特例”。
当使用 下载器插件时,URL会使用特定于插件的方案。 注意,chart 的用户需要安装一个支持该方案的插件来更新或构建依赖关系。
当 repository 字段为空时,Helm 无法对依赖项执行依赖管理操作。在这种场景下,Helm 假定依赖关系位于 charts 文件夹的子目录中,名称与依赖关系的name属性相同。
1.2.3. condition/tags 条件和标签
条件和标签可以被添加到任意 可选的 依赖中。
条件的首先格式是:
condition: somechart.enabled
somechart 是依赖的 chart 名称。
当多个子 chart(依赖)一起提供可选或可交换的特性时,这些 chart 应该共享相同的标签。
比如,如果 nginx 和 memcached 在 chart 中一起提供性能优化,且需要在使用该功能时同时存在,则都应该有如下的标签部分:
tags:
- webaccelerator
这允许用户使用一个标签打开和关闭该功能。
1.3. 示例
例如 Wordpress 的依赖配置:
dependencies:
- condition: memcached.enabled
name: memcached
repository: https://charts.bitnami.com/bitnami
version: 6.x.x
- condition: mariadb.enabled
name: mariadb
repository: https://charts.bitnami.com/bitnami
version: 10.x.x
- name: common
repository: https://charts.bitnami.com/bitnami
tags:
- bitnami-common
version: 1.x.x
2. 源码解析
源码路径: https://github.com/helm/helm/blob/v3.7.2/pkg/chart/dependency.go
代码内容:
/*
Copyright The Helm Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package chart
import "time"
// Dependency describes a chart upon which another chart depends.
//
// Dependencies can be used to express developer intent, or to capture the state
// of a chart.
type Dependency struct {
// Name is the name of the dependency.
//
// This must mach the name in the dependency's Chart.yaml.
Name string `json:"name"`
// Version is the version (range) of this chart.
//
// A lock file will always produce a single version, while a dependency
// may contain a semantic version range.
Version string `json:"version,omitempty"`
// The URL to the repository.
//
// Appending `index.yaml` to this string should result in a URL that can be
// used to fetch the repository index.
Repository string `json:"repository"`
// A yaml path that resolves to a boolean, used for enabling/disabling charts (e.g. subchart1.enabled )
Condition string `json:"condition,omitempty"`
// Tags can be used to group charts for enabling/disabling together
Tags []string `json:"tags,omitempty"`
// Enabled bool determines if chart should be loaded
Enabled bool `json:"enabled,omitempty"`
// ImportValues holds the mapping of source values to parent key to be imported. Each item can be a
// string or pair of child/parent sublist items.
ImportValues []interface{} `json:"import-values,omitempty"`
// Alias usable alias to be used for the chart
Alias string `json:"alias,omitempty"`
}
// Validate checks for common problems with the dependency datastructure in
// the chart. This check must be done at load time before the dependency's charts are
// loaded.
func (d *Dependency) Validate() error {
d.Name = sanitizeString(d.Name)
d.Version = sanitizeString(d.Version)
d.Repository = sanitizeString(d.Repository)
d.Condition = sanitizeString(d.Condition)
for i := range d.Tags {
d.Tags[i] = sanitizeString(d.Tags[i])
}
if d.Alias != "" && !aliasNameFormat.MatchString(d.Alias) {
return ValidationErrorf("dependency %q has disallowed characters in the alias", d.Name)
}
return nil
}
// Lock is a lock file for dependencies.
//
// It represents the state that the dependencies should be in.
type Lock struct {
// Generated is the date the lock file was last generated.
Generated time.Time `json:"generated"`
// Digest is a hash of the dependencies in Chart.yaml.
Digest string `json:"digest"`
// Dependencies is the list of dependencies that this lock file has locked.
Dependencies []*Dependency `json:"dependencies"`
}