我想用多行创建kubernetes配置图,例如这种yaml:

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

我应该如何在原型(prototype)的零件函数中编写它?
    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“等效”是|||构造(请参见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

关于kubernetes - 如何在ksonnet/jsonnet中写入多行值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50238470/

10-11 11:03