kubernetesjsonnetksonnet

how to write multi-line value in ksonnet/jsonnet


I want to create a kubernetes config map with multi-lines, such as this kind of yaml:

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

How should I write it in a part function in my 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
"

but it generates yaml like this: (ks show default):

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

I want to mount this config map and run it as script, but I doubt this output can work. Any idea on how to generate multi-line value in ksonnet/jsonnet?


Solution

  • The jsonnet "equivalent" of yaml's | is the ||| construct (see https://jsonnet.org/ref/spec.html), applied your example:

    $ 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