Helm中如何传递value
source link: https://www.purewhite.io/2018/01/17/helm-provide-value/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Helm 中如何传递 value
Helm 是 kubernetes 的官方包管理工具。根据官网上的描述 Helm is the best way to find, share, and use software built for Kubernetes.
可以看出 helm 在 kubernetes 社区中的定位。
这篇文章并不是 helm 的入门文章,而是着重于 helm 中的 chart 之间如何传递 value。希望进行 helm 入门的同学可以参考官方文档。
在 helm 的使用过程中,经常会出现两种需求:
- 在父 chart 中读取子 chart 的某些 value 的值
- 在父 chart 中修改子 chart 的某些 value 的值
helm 对于这两种场景提供了比较完备的支持,下面我们来具体讲一下解决方案。
在父 chart 中读取子 chart 的值
helm 提供了两种方法来应对这种情况:
使用 export 格式
如果说一个 child 的 chart 在 values 的 root 下有一个叫做 export
的 key,那么它的 parent chart 就可以直接在 requirements 里面通过指定需要 import 的 key 来将值 import 到自身的 values 里面,例子如下:
# parent's requirements.yaml file
...
import-values:
- data
# child's values.yaml file
...
exports:
data:
myint: 99
helm 会发现,我们指定了要 import data
这个 key,所以就去 child 的 values.yaml 里面寻找,发现了这个 key 有被 export,于是就 import 了它的内容。
这时候的 parent 的 values 如下:
# parent's values file
...
myint: 99
需要注意,在 parent 的 values 中 data
这个 key 不会被 import 进来,只会 import data
的内容。如果希望把这个 key 也一起 import 进来,可以使用下面说的方法。
使用 child/parent 格式
如果我们想要获得一些不在 exports 这个 key 下面的值,我们就必须指定在 child 中要 import 的路径,以及在 parent 中的对应路径,如下:
# parent's requirements.yaml file
dependencies:
- name: subchart1
repository: http://localhost:10191
version: 0.1.0
...
import-values:
- child: default.data
parent: myimports
根据如上的这个 requirements 文件,helm 将会在 child 的 chart 中寻找 default.data 的值,并导入到 parent 中的 myimports 这个路径下。
假设 parent 和 child 初始的 values 如下:
# parent's values.yaml file
myimports:
myint: 0
mybool: false
mystring: "helm rocks!"
# subchart1's values.yaml file
default:
data:
myint: 999
mybool: true
那么导入之后,真正渲染出来的 parent 的 values 的值为:
# parent's final values
myimports:
myint: 999
mybool: true
mystring: "helm rocks!"
可以看出来,parent 中的 values 把 myint 和 mybool 从 subchart1 里面 import 了进来。
在父 chart 中修改子 chart 的值
想要再父 chart 中修改子 chart 的值比较容易,假设子 chart 的名字是 mychartabc
,那么我们可以很简单地在父 chart 的 values 中通过以下方式进行修改:
# in parent's values.yaml
...
mychartabc:
key: value
这样就可以修改子 chart 的值了。
https://docs.helm.sh/developing_charts/#importing-child-values-via-requirements-yaml
https://docs.helm.sh/chart_template_guide/#overriding-values-from-a-parent-chart
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK