How do I use json variables in a yaml file (Helm)

二次信任 提交于 2019-12-01 03:08:37

问题


I have a HELM values file which looks like so:

service:
  environment: dev
  spring_application_json: >-
    {
      "spring" : {
        "boot" : {
          "admin" : {
            "client" : {
              "enabled" : "false",
              "url" : "http://website1",
              "instance" : {
                "service-base-url" : "http://website2",
                "management-base-url" : "http://website3"
              }
            }
          }
        }
      }
    }

And a corresponding template file which grabs this value and inserts it as an environment variable to a container.

spec:
  replicas: {{ .Values.replicaCount }}
  template:
    spec:
      imagePullSecrets:
        - name: {{ .Values.image.pullSecret }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
            - name: ENVIRONMENT
              value: "{{ .Values.service.environment }}"
            - name: SPRING_APPLICATION_JSON
              value: "{{ .Values.service.spring_application_json }}"

However when I run the helm install I get the following error:

Error: YAML parse error on deployment.yaml: error converting YAML to JSON: yaml: line 40: did not find expected key

Which points to the line:

value: "{{ .Values.service.spring_application_json }}"

I believe its a problem with the way I'm trying to parse in a json string as a multiline environment variable? The ENVIRONMENT 'dev' variable works perfectly and this same YAML also works perfectly with docker-compose.


回答1:


There's an example a bit like this in the docs for spring cloud dataflow but the format in their documentation has the quotes escaped.

I was able to recreate the error and get past it by changing the values file entry to:

service:
  spring_application_json:
    {
      "spring" : {
        "boot" : {
          "admin" : {
            "client" : {
              "enabled" : "false",
              "url" : "http://website1",
              "instance" : {
                "service-base-url" : "http://website2",
                "management-base-url" : "http://website3"
              }
            }
          }
        }
      }
    }

And the deployment entry to:

    - name: SPRING_APPLICATION_JSON
      value: {{ .Values.service.spring_application_json | toJson | quote }}

Notice no quotes around this part as that is handled anyway.



来源:https://stackoverflow.com/questions/52930405/how-do-i-use-json-variables-in-a-yaml-file-helm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!