5

我想用多行创建一个 kubernetes 配置映射,比如这种 yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: nifi-bootstrap
data:
  run.sh: |-
    echo "Waiting to run nslookup..."
    sleep 30

我应该如何将它写在我原型的部分函数中?

    parts:: {
        bootstrap(p):: {
            apiVersion: 'v1',
            kind: 'ConfigMap',
            metadata: {
                name: p.name + '-bootstrap',
                labels: {
                    app: p.app,
                    release: p.release,
                },
            },
            data: {
                'run.sh': "|-
line 1 
line 2
line 3
"

但它会像这样生成 yaml:(ks 显示默认值):

apiVersion: v1
data:
  run.sh: "|-\nline 1 \nline 2\nline 3\n"
kind: ConfigMap

我想挂载这个配置映射并将其作为脚本运行,但我怀疑这个输出是否可以工作。关于如何在 ksonnet/jsonnet 中生成多行值的任何想法?

4

1 回答 1

9

yamljsonnet “等效”是构造(参见https://jsonnet.org/ref/spec.html),应用了您的示例:||||

$ cat foo.jsonnet
{
  parts:: {
    bootstrap(p):: {
      apiVersion: "v1",
      kind: "ConfigMap",
      metadata: {
        name: p.name + "-bootstrap",
        labels: {
          app: p.app,
          release: p.release,
        },
      },
      data: {
        "run.sh": |||
          line 1
          line 2
          line 3
        |||,
      },
    },
  },
} {
  foo: self.parts.bootstrap({name: "foo", app: "bar", release: "v1"}),
}
$ jsonnet foo.jsonnet
{
   "foo": {
      "apiVersion": "v1",
      "data": {
         "run.sh": "line 1\nline 2\nline 3\n"
      },
      "kind": "ConfigMap",
      "metadata": {
         "labels": {
            "app": "bar",
            "release": "v1"
         },
         "name": "foo-bootstrap"
      }
   }
}
$ jsonnet foo.jsonnet|jq -r '.foo.data["run.sh"]'
line 1
line 2
line 3
于 2018-05-08T17:28:09.103 回答