如何在 yaml 文件中使用 json 变量(Helm) [英] How do I use json variables in a yaml file (Helm)

查看:193
本文介绍了如何在 yaml 文件中使用 json 变量(Helm)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的 HELM 值文件:

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 }}"

但是,当我运行 helm install 时,出现以下错误:

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

哪个指向线:

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

我认为我尝试将 json 字符串解析为多行环境变量的方式存在问题?ENVIRONMENT 'dev' 变量完美运行,同样的 YAML 也与 docker-compose 完美运行.

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.

推荐答案

spring cloud dataflow 但其文档中的格式已转义引号.

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"
              }
            }
          }
        }
      }
    }

以及部署入口:

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

注意这部分没有引号,因为无论如何都会处理.

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

这篇关于如何在 yaml 文件中使用 json 变量(Helm)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆