Skip to content
gqlxj1987's Blog
Go back

Why Fuck Templating Yaml

Edit page

原文链接

At some point, we decided it was okay for us to template yaml. When did this happen? How is this acceptable?

It’s incredibly likely that the reason you have multiple configuration files is because the $thing that uses that config is slightly different from its companions. Examples of this include:

Helm Charts

Optional values just make things ugly in templating languages, and you can’t just leave the value blank, so you have to resort to ugly loops and conditionals that are probably going to bite you later.

{{- with .Values.podAnnotations  }}
      annotations:
{{ toYaml . | indent 8  }}
{{- end  }}
something: nothing
  hello: goodbye

这段yaml可能不是valid

yaml部分,关于indent部分,很让人get confused

JSON, Jsonnet & YAML

YAML is a superset of JSON and converting between the two is trivial. Many applications and programming languages will parse JSON and YAML natively, and many can convert between the two very simple

采用json的方式,去定义yaml部分

local annotations = {
  'nginx.ingress.kubernetes.io/app-root': '/',
  'nginx.ingress.kubernetes.io/enable-cors': true,
};

{
  metadata: {
    annotations: annotations,
  },
} + { // this adds another JSON object
  metadata+: { // I'm using the + operator, so we'll append to the existing metadata
    annotations+: { // same as above
      something: 'nothing',
    },
  },
}

结果:

{
   "metadata": {
      "annotations": {
         "nginx.ingress.kubernetes.io/app-root": "/",
         "nginx.ingress.kubernetes.io/enable-cors": true,
         "something": "nothing"
      }
   }
}

Edit page
Share this post on:

Previous Post
Vitess
Next Post
ForkJoinPoll