是否可以从掌舵子图表模板中提取样板YAML?

时间:2019-11-01 20:37:30

标签: kubernetes yaml kubernetes-helm

是否可以将样板YAML从子图表模板移动到父 _helpers.tpl values.yaml

头盔项目

    MyHelmApp
    │
    ├── Chart.yaml
    ├── values.yaml
    ├── templates
    │   ├── _helpers.tpl
    │   ├── configmap.yaml
    │   └── app-ingress-rules.yaml
    │
    └── charts
        │
        ├── app1
        │   ├── Chart.yaml
        │   ├── templates
        │   │   ├── _helpers.tpl
        │   │   ├── deployment.yaml
        │   │   └── service.yaml
        │   └── values.yaml
        ├── app2
        │   ├── Chart.yaml
        │   ├── templates
        │   │   ├── _helpers.tpl
        │   │   ├── deployment.yaml
        │   │   └── service.yaml
        │   └── values.yaml
        └── app3
            ├── Chart.yaml
            ├── templates
            │   ├── _helpers.tpl
            │   ├── deployment.yaml
            │   └── service.yaml
            └── values.yaml

详细说明:我有3个应用子图表,每个子模板中都有样板YAML。这些模板中的值在父图表的values.yaml中定义,并且变量路径相同。例如:

app1,app2和app3部署。yaml全部包含...

          livenessProbe:
            httpGet:
              path: {{ .Values.health.livenessProbe.path }}
              port: {{ .Values.health.livenessProbe.port }}
            initialDelaySeconds: {{ .Values.health.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.health.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.health.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.health.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.health.readinessProbe.failureThreshold }}

          readinessProbe:
            tcpSocket:
              port: {{ .Values.health.readinessProbe.port }}
            initialDelaySeconds: {{ .Values.health.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.health.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.health.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.health.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.health.readinessProbe.failureThreshold }}

values.yaml(在父图表中)

app1:
  health:
    livenessProbe:
      path: /system_health
      port: 8080
      initialDelaySeconds: 15
      periodSeconds: 20
      timeoutSeconds: 5
      successThreshold: 1
      failureThreshold: 3
    readinessProbe:
      port: 8080
      initialDelaySeconds: 5
      periodSeconds: 10
      timeoutSeconds: 5
      successThreshold: 1
      failureThreshold: 3

app2:
  health:
    livenessProbe:
      path: /system_health
      port: 8080
      initialDelaySeconds: 15
      periodSeconds: 20
      timeoutSeconds: 5
      successThreshold: 1
      failureThreshold: 3
    readinessProbe:
      port: 8080
      initialDelaySeconds: 5
      periodSeconds: 10
      timeoutSeconds: 5
      successThreshold: 1
      failureThreshold: 3

(etc)

回到问题:我想在子图表模板中获取相同的内容,并将其移至一个集中的位置,例如父图表的_helpers.tpl或values.yaml;这可能吗?您能否提供一个示例说明如何执行此操作?

1 个答案:

答案 0 :(得分:1)

Helm在具有共享模板名称空间的单个执行环境中,将所有父级和依赖项图一起给出的所有YAML呈现出来。从理论上讲,app1图表可以依赖于父级_helpers.tpl中定义的模板,并且在您显示的特定布局中它可以工作。

由于此环境设置,还可以编写一个“图表”,该图表实际上并不产生其自己的任何YAML,而仅包含模板。头盔3将包括“图书馆图表”作为特定概念。更好的布局仍然是拥有一个包含您共享模板的库,并对其进行引用。

MyHelmApp
\-- charts
  +-- app1
  | +-- Chart.yaml
  | \-- templates/...
  \-- common
    +-- Chart.yaml
    \-- templates
      \-- _helpers.tpl
          (but no *.yaml)

现在MyHelmApp依赖于app1app2app3,并且每个依赖于common。这样一来,您就可以独立于兄弟姐妹而独立安装它们。

Helm无法将YAML片段“推入”其他位置的对象中(Kustomize是相对较新的Kubernetes的一部分,可以做到这一点)。每个对象必须自己声明可能允许的任何自定义。因此,在每个图表中,您都必须声明

spec:
{{ include "common.probes" . | indent 2 }}

公共图表仅定义模板,并且在调用模板时,它们将使用本地化到子图表的.Values版本。

{{- define "common.probes" -}}
livenessProbe:
  httpGet:
    path: {{ .Values.health.livenessProbe.path }}
    et: cetera
{{ end -}}