从Helm k8s的values.yaml中获取docker映像和标签作为键/值对

问题描述

我有一个Docker映像列表,我想将其作为环境变量传递给Deployment.yaml

values.yaml

contributions_list:
  - image: flogo-aws
    tag: 36
  - image: flogo-awsec2
    tag: 37
  - image: flogo-awskinesis
    tag: 18
  - image: flogo-chargify
    tag: 19

deployment.yaml

apiVersion: batch/v1
kind: Job
metadata:
  name: container-image-extractor
  namespace: local-tibco-tci
  labels:
    app.cloud.tibco.com/name: container-image-extractor
spec:
  backoffLimit: 0
  template:
    metadata:
      labels:
        app.cloud.tibco.com/name: container-image-extractor
    spec:
      nodeSelector:
        kubernetes.io/os: linux
      restartPolicy: Never
      containers:
      - name: container-image-extractor
        image: reldocker.tibco.com/stratosphere/container-image-extractor
        imagePullPolicy: IfNotPresent
        env:
        - name: SOURCE_DOCKER_IMAGE
          value: "<docker_image>:<docker_tag>"    # docker image from which contents to be copied
 

我的问题如下。

  1. 这是将docker映像和标签数组作为deployment.yaml的参数传递的正确方法
  2. 我将如何从values.yaml中替换Deployment.yaml中的,并应为每个docker映像和标签触发增量作业。

解决方法

这就是我要做的,为列表中的每个图像创建一个工作

{{- range .Values.contributions_list }}
apiVersion: batch/v1
kind: Job
metadata:
  name: "container-image-extractor-{{ .image }}-{{ .tag }}"
  namespace: local-tibco-tci
  labels:
    app.cloud.tibco.com/name: container-image-extractor
spec:
  backoffLimit: 0
  template:
    metadata:
      labels:
        app.cloud.tibco.com/name: container-image-extractor
    spec:
      nodeSelector:
        kubernetes.io/os: linux
      restartPolicy: Never
      containers:
      - name: container-image-extractor
        image: reldocker.tibco.com/stratosphere/container-image-extractor
        imagePullPolicy: IfNotPresent
        env:
        - name: SOURCE_DOCKER_IMAGE
          value: "{{ .image }}:{{ .tag }}"    # docker image from which contents to be copied
{{ end }}

如果使用此贡献列表之外的值(发行名称,env等),请不要忘记更改范围{{ $.Values.myjob.limits.cpu | quote }}$.很重要:)

编辑:如果您在循环的每次迭代中都没有更改名称,则每次都会覆盖配置。使用不同的名称,您将创建多个作业。

,

您需要按以下步骤修复deploy.yaml:

{{- range $contribution := .Values.contributions_list }}
apiVersion: batch/v1
kind: Job
metadata:
  name: container-image-extractor
  namespace: local-tibco-tci
  labels:
    app.cloud.tibco.com/name: container-image-extractor
spec:
  backoffLimit: 0
  template:
    metadata:
      labels:
        app.cloud.tibco.com/name: container-image-extractor
    spec:
      nodeSelector:
        kubernetes.io/os: linux
      restartPolicy: Never
      containers:
      - name: container-image-extractor
        image: reldocker.tibco.com/stratosphere/container-image-extractor
        imagePullPolicy: IfNotPresent
        env:
        - name: SOURCE_DOCKER_IMAGE
          value: "{{ $contribution.image }}:{{ $contribution.tag }}"
{{- end }}

如果您想了解头盔模板语法,可以查看this document

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...