如何在 ksonnet/jsonnet 中写入多行值

how to write multi-line value in ksonnet/jsonnet

我想创建一个多行的kubernetes config map,比如这种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 中生成多行值的任何想法?

yaml|jsonnet "equivalent" 是 ||| 构造 (参见 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