【问题标题】:Helm Override common template valuesHelm Override 通用模板值
【发布时间】:2020-06-19 16:39:30
【问题描述】:

这里的第一天掌舵用户。试图了解如何为 k8s 资源构建通用模板。 假设我在单个图表中有 10 个 cron 作业,所有这些作业仅在参数和名称上有所不同。今天存在 10 个完整的作业清单,并且 95% 的清单内容是相等的。我想移动模板中的公共部分并创建 10 个清单,我将在其中为 args 和名称提供特定值。

所以我定义了模板_cron-job.yaml

    {{- define "common.cron-job"}}
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: {{ include "costing-report.name" . }}-bom-{{ .Values.env }}
  labels:
{{ include "costing-report.labels" . | indent 4 }}
spec:
  schedule: "{{ .Values.cronjob.scheduleBom }}"
  suspend: {{ .Values.cronjob.suspendBom }}
  {{- with .Values.cronjob.concurrencyPolicy }}
  concurrencyPolicy: {{ . }}
  {{- end }}
  {{- with .Values.cronjob.failedJobsHistoryLimit }}
  failedJobsHistoryLimit: {{ . }}
  {{- end }}
  {{- with .Values.cronjob.successfulJobsHistoryLimit }}
  successfulJobsHistoryLimit: {{ . }}
  {{- end }}
  jobTemplate:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "costing-report.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
      template:
        spec:
          containers:
            - name: {{ .Chart.Name }}
              image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
              imagePullPolicy: {{ .Values.image.pullPolicy }}
              args: ["--report=Bom","--email={{ .Values.configmap.service.email_bom }}"]
              env:
                - name: spring_profiles_active
                  value: "{{ .Values.env }}"
              envFrom:
                - configMapRef:
                    name: {{ include "costing-report.fullname" . }}
                - secretRef:
                    name: {{ .Values.secrets.properties }}
          restartPolicy: Never
          {{- with .Values.imagePullSecrets }}
          imagePullSecrets:
            {{- toYaml . | nindent 8 }}
          {{- end }}    
{{- end -}}

现在我需要创建覆盖名称和参数 job1.yaml 的作业清单

{{- template "common.cron-job" . -}}
??? override ???
name: {{ include "cost-report.name" . }}-job1-{{ .Values.env }}
jobTemplate:
spec:
  template:
    spec:
      containers:
        args: ["--report=Bom","--email={{ .Values.configmap.service.email_bom }}"]

有没有办法做到这一点?我在 helm 文档中没有找到这个。我确实找到了这个https://github.com/helm/charts/tree/master/incubator/common,但它并没有很好地工作并且给了我错误。

谢谢。

【问题讨论】:

  • 您使用提供的链接时遇到了什么错误?

标签: templates kubernetes charts overriding kubernetes-helm


【解决方案1】:

找到解决方案

选项 1 使用来自 helm github https://github.com/helm/charts/tree/master/incubator/common 的示例 基于 yaml 合并和值覆盖的解决方案。非常灵活,允许您定义通用模板并使用它们来组成最终的 k8s 清单。

选项 2 定义通用模板并使用所需值传递参数。 就我而言,它看起来像这样。

_common.cronjob.yaml

{{- define "common.cronjob" -}}
{{- $root := .root -}} 
{{- $name := .name -}} 
{{- $schedule := .schedule -}} 
{{- $suspend := .suspend -}} 
{{- $args := .args -}} 

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: {{ $name }}
  labels:
{{ include "costing-report.labels" $root | indent 4 }}
spec:
  schedule: {{ $schedule }}
  suspend: {{ $suspend }}
  {{- with $root.Values.cronjob.concurrencyPolicy }}
  concurrencyPolicy: {{ . }}
  {{- end }}
  {{- with $root.Values.cronjob.failedJobsHistoryLimit }}
  failedJobsHistoryLimit: {{ . }}
  {{- end }}
  {{- with $root.Values.cronjob.successfulJobsHistoryLimit }}
  successfulJobsHistoryLimit: {{ . }}
  {{- end }}
  jobTemplate:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "costing-report.name" $root }}
        app.kubernetes.io/instance: {{ $root.Release.Name }}
    spec:
      template:
        spec:
          containers:
            - name: {{ $root.Chart.Name }}
              image: "{{ $root.Values.image.repository }}:{{ $root.Values.image.tag }}"
              imagePullPolicy: {{ $root.Values.image.pullPolicy }}
              args: {{ $args }}
              env:
                - name: spring_profiles_active
                  value: "{{ $root.Values.env }}"
              envFrom:
                - configMapRef:
                    name: {{ include "costing-report.fullname" $root }}
                - secretRef:
                    name: {{ $root.Values.secrets.properties }}
          restartPolicy: Never
          {{- with $root.Values.imagePullSecrets }}
          imagePullSecrets:
            {{- toYaml . | nindent 8 }}
          {{- end }}
{{- end -}}

然后创建作业清单,定义要传递给通用模板的值

bom-cronjob.yaml

{{ $bucket := (printf "%s%s%s" "\"--bucket=ll-costing-report-" .Values.env "\"," )}}
{{ $email := (printf "%s%s%s" "\"--email=" .Values.configmap.service.email_bom "\"") }}
{{ $args := (list "\"--report=Bom\"," "\"--reportType=load\"," "\"--source=bamboorose\"," $bucket "\"--table=COSTING_BOM\"," "\"--ignoreLines=1\"," "\"--truncate=true\"," $email )}}
{{ $name := (printf "%s%s" "costing-report.name-bom-" .Values.env )}}
{{- template "common.cronjob" (dict "root" . "name" $name "schedule" .Values.cronjob.scheduleBom "suspend" .Values.cronjob.suspendBom "args" $args) -}}

最后一行可以解决问题。诀窍是您只能将单个参数传递给模板,在我的情况下,它是包含模板端所需的所有值的字典。您可以省略定义模板变量并立即使用 dict 值。请注意,我将根上下文(范围)作为“根”和前缀传递。在模板中使用“root”。

【讨论】:

    猜你喜欢
    • 2022-08-16
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 2019-08-27
    相关资源
    最近更新 更多