# Helm内置对象和摸版语言

### Helm 与 k8s 集群兼容版本

Helm 版本 支持的 Kubernetes 版本 3.7.x 1.22.x - 1.19.x 3.6.x 1.21.x - 1.18.x 3.5.x 1.20.x - 1.17.x 3.4.x 1.19.x - 1.16.x 3.3.x 1.18.x - 1.15.x 3.2.x 1.18.x - 1.15.x 3.1.x 1.17.x - 1.14.x 3.0.x 1.16.x - 1.13.x 2.16.x 1.16.x - 1.15.x 2.15.x 1.15.x - 1.14.x 2.14.x 1.14.x - 1.13.x 2.13.x 1.13.x - 1.12.x 2.12.x 1.12.x - 1.11.x 2.11.x 1.11.x - 1.10.x 2.10.x 1.10.x - 1.9.x 2.9.x 1.10.x - 1.9.x 2.8.x 1.9.x - 1.8.x 2.7.x 1.8.x - 1.7.x 2.6.x 1.7.x - 1.6.x 2.5.x 1.6.x - 1.5.x 2.4.x 1.6.x - 1.5.x 2.3.x 1.5.x - 1.4.x 2.2.x 1.5.x - 1.4.x 2.1.x 1.5.x - 1.4.x 2.0.x 1.4.x - 1.3.x

### 标准标签

名称 状态 描述 app.kubernetes.io/name 推荐 app名称，反应整个app。{{ template "name" . }}经常用于此。很多Kubernetes清单会使用这个，但不是Helm指定的。 helm.sh/chart 推荐 chart的名称和版本： {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "\_" }}。 app.kubernetes.io/managed-by 推荐 此值应始终设置为 {{ .Release.Service }}。 用来查找被Helm管理的所有内容。 app.kubernetes.io/instance 推荐 这个应该是{{ .Release.Name }}。 有助于在同一应用程序中区分不同的实例。 app.kubernetes.io/version 可选 app的版本，且被设置为 {{ .Chart.AppVersion }}. app.kubernetes.io/component 可选 这是通用标签，用于标记块在应用程序中可能扮演的不同角色。比如 app.kubernetes.io/component: frontend。 app.kubernetes.io/part-of 可选 当多个chart或块用于构建一个应用程序时。比如，应用软件和数据库生成一个网站。这可以设置为受支持的顶级应用程序。 app.kubernetes.io/created-by 可选 创建者

### 内置对象

Release.Name： release名称 Release.Namespace： 版本中包含的命名空间(如果manifest没有覆盖的话) Release.IsUpgrade： 如果当前操作是升级或回滚的话，需要将该值设置为true Release.IsInstall： 如果当前操作是安装的话，需要将该值设置为true Release.Revision： 此次修订的版本号。安装时是1，每次升级或回滚都会自增 Release.Service： 该service用来渲染当前模板。Helm里一般是Helm

Files.Get 通过文件名获取文件的方法。 （.Files.Getconfig.ini） Files.GetBytes 用字节数组代替字符串获取文件内容的方法。 对图片之类的文件很有用 Files.Glob 用给定的shell glob模式匹配文件名返回文件列表的方法 Files.Lines 逐行读取文件内容的方法。迭代文件中每一行时很有用 Files.AsSecrets 使用Base 64编码字符串返回文件体的方法 Files.AsConfig 使用YAML格式返回文件体的方法

Capabilities.APIVersions 是一个版本集合 Capabilities.APIVersions.Has $version 说明集群中的版本 (e.g., batch/v1) 或是资源 (e.g., apps/v1/Deployment) 是否可用 Capabilities.KubeVersion 和 Capabilities.KubeVersion.Version 是Kubernetes的版本号 Capabilities.KubeVersion.Major Kubernetes的主版本 Capabilities.KubeVersion.Minor Kubernetes的次版本 Capabilities.HelmVersion 包含Helm版本详细信息的对象，和 helm version 的输出一致 Capabilities.HelmVersion.Version 是当前Helm版本的语义格式 Capabilities.HelmVersion.GitCommit Helm的git sha1值 Capabilities.HelmVersion.GitTreeState 是Helm git树的状态 Capabilities.HelmVersion.GoVersion 是使用的Go编译器版本

Template.Name: 当前模板的命名空间文件路径 (e.g. mychart/templates/mytemplate.yaml) Template.BasePath: 当前chart模板目录的路径 (e.g. mychart/templates)

### 示例： 调用 values 配置

```yaml
cat values.yaml
replicas: 1
imagePullSecrets: regcred
image:
  repository: registry.cn-beijing.aliyuncs.com/kouzf
  pullPolicy: Always
  ...

cat templates/deployment.yaml 
...
metadata:
    ...
      spec:
      containers:
      - name: {{ .Release.Name }}
        image: {{ .Values.image.repository }}/{{ .Release.Name }}:{{ .Values.image.tag }}
        imagePullPolicy: {{ .Values.image.pullPolicy }}

相应的service.yaml ingress.yaml 中的值都是统一配置在values.yaml 中通过Values 对象传递的。
```

### 示例： 配置 nginx.conf 调用

```yaml
apiVersion: v1
kind: ConfigMap
  metadata:
    name: {{ .Release.Name }}-configmap
    data:
      nginx.conf: {{.Files.Get "files/nginx.conf" | printf "%s" | indent 4}}

# 可以配制成一个通用模板文件，引入的nginx 配置文件通过Release.Name传入，这样实现了解耦。
# 默认的根目录为chart/，所以对应文件为chart/files/nginx.conf ,indent 4表示空格4个，"|"就是管道，与linux 中的管道意义相仿。
```

### 示例： Files.Glob 和 AsConfig 得使用

```yaml
# 这是一个configmap 作为 filebeat 得input 配置文件得示例

apiVersion: v1
kind: ConfigMap
metadata:
  name: configmap-filebeat
  namespace: asdf
data:
{{ (.Files.Glob "configmap/*").AsConfig | indent 2 }}

# 每个应用得日志输入配置成一个文件，然后读取这些文件把它们得配置存入 configmap 得 data 中，文件名字为key 内容为value。
```

### 模板中的流程控制

#### if / else

***

1. 一个布尔类型的假
2. 一个数字零
3. 一个空的字符串
4. 一个 nil(空或null)
5. 一个空的集合(map, slice, tuple, dict, array) 除了上面的这些情况外，其他所有的条件都为真。

```yaml
{{if PIPELINE}}
# Do something
{{else if OTHER PIPELINE}}
# Do something else
{{else}}
# Default case
{{end}}
```

#### with 指定范围

```yaml
语法:
{{ with 引用的对象 }}
这里可以使用 . (点)， 直接引用with指定的对象
{{ end }}
```

```yaml
# 例子: .Values.favorite是一个object类型
{{- with .Values.favorite }}
drink: {{ .drink | default "tea" | quote }}   #相当于.Values.favorite.drink
food: {{ .food | upper | quote }}
{{- end }}
```

#### range 循环

```yaml
语法1:
# 遍历map类型，用于遍历键值对象
# 变量key代表对象的属性名，val代表属性值
{{- range key,val := 键值对象 }}
{{ $key }}: {{ $val | quote }}
{{- end}}

语法2：
{{- range 数组 }}
{{ . | title | quote }} # . (点)，引用数组元素值。
{{- end }}

例子:
# values.yaml定义
# map类型
favorite:
  drink: coffee
  food: pizza
 
# 数组类型
pizzaToppings:
  - mushrooms
  - cheese
  - peppers
  - onions
 
# map类型遍历例子:
{{- range $key, $val := .Values.favorite }}
{{ $key }}: {{ $val | quote }}
{{- end}}
 
# 数组类型遍历例子:
{{- range .Values.pizzaToppings}}
{{ . | quote }}
{{- end}}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://close.gitbook.io/yun-wei-bi-ji/kubernetes/helm/helm-nei-zhi-dui-xiang-he-mo-ban-yu-yan.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
