在 helm 版本 2.14.1 的 helm 图表中包含系统用户名

Include system username in helm charts in helm version 2.14.1

我使用的是 helm 版本 2.14.1。我已经为一个应用程序创建了 helm 图表,用户将部署该应用程序以在 kubernetes 集群上测试他们的代码。我想为用户名值添加标签,这样我就可以按用户检索部署(按用户标签进行部署)。有没有办法在 helm 图表中包含系统用户名,就像我们在 Java 和 System.getProperty("user.name") 中所做的那样。我的 helm 模板是这样的:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "common.fullname" . }}--{{ Release.name }}
  labels:
    application: {{ include "common.name" . }}
    branch: "{{ Release.name }}"
    username: "{{ System.user.name }}"   # need to fetch the logged in user from system here
spec:
...

有没有标准的方法来实现这个,或者我是否可以允许用户在使用 helm installhelm template 命令时从命令行输入用户名?

编辑: 尽管 --set 可以为我的图表设置值,但我还需要在依赖项中设置相同的值。像这样:

values.yaml

username: "" 

dependency1:
 username: {{ .Values.username }} 

dependency2:
 username: {{ .Values.username }}

...

当然上面的实现是行不通的。我也需要在依赖项中引用设置值

这是一个基于评论的社区 Wiki 答案,为了提高可见度而发布。随意扩展它。

您可以使用带有 --set 选项的 helm template 命令:

--set stringArray              set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
--set-file stringArray         set values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2)
--set-string stringArray       set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)

--set 参数在将值传递到图表的其他方法中具有最高的优先级。这意味着默认值来自 values.yaml 可以被父图表的 values.yaml 覆盖,后者又可以被 user-supplied 值文件覆盖,后者又可以被覆盖通过 --set 个参数。

您可以在 official docs.

中查看更多详细信息和示例

我已经解决了这个问题。感谢@MichaelAlbers 和@WytrzymałyWiktor 的帮助。所以解决方法如下。

helm template path/to/chart --set global.username=username

然后在所有模板中将此值称为 {{ .Values.global.username }}。这也适用于任何依赖关系图。