File: kubernetes.html

package info (click to toggle)
jsonnet 0.20.0%2Bds-3.1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 16,776 kB
  • sloc: cpp: 23,318; python: 1,788; javascript: 1,003; ansic: 885; sh: 745; makefile: 194; java: 140
file content (407 lines) | stat: -rw-r--r-- 12,956 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
---
layout: default
title: Kubernetes
---

<div class="hgroup">
  <div class="hgroup-inline">
    <div class="panel">
      <h1 id="kubernetes">Kubernetes</h1>
    </div>
    <div style="clear: both"></div>
  </div>
</div>

<div class="hgroup">
  <div class="hgroup-inline">
    <div class="panel">
      <p>
        <a href="https://kubernetes.io/">Kubernetes</a> is a cluster orchestration system for
        containerized workloads.  Out of the box, it is configured with YAML files, making it a
        natural fit for Jsonnet.
      </p>
    </div>
    <div style="clear: both"></div>
  </div>
</div>

<div class="hgroup">
  <div class="hgroup-inline">
    <div class="panel">
      <h2 id="integrating">Using Jsonnet With Kubernetes</h2>
    </div>
    <div style="clear: both"></div>
  </div>
</div>

<div class="hgroup">
  <div class="hgroup-inline">
    <div class="panel">
      <p>
        Jsonnet's output JSON can be consumed by <tt>kubecfg</tt> which
        treats it as if it were YAML.  This approach also works for other configuration formats that
        extend JSON, such as Javascript or HashiCorp's Configuration Language (<a
        href="https://github.com/hashicorp/hcl">HCL</a>).
      </p>
      <p>
        If we configure only a single Kubernetes object in each run of Jsonnet, we miss out on the
        ability to remove duplication not just within an object, but across sets of objects with
        commonalities.  Fortunately, there are three ways to configure sets of Kubernetes objects:
        Use YAML <a href="/learning/getting_started.html#stream">stream</a> output, <a
        href="/learning/getting_started.html#multi">multi-file</a> output, or a single kubectl
        list object.  This latter option is provided by Kubernetes without requiring any special
        support from Jsonnet.  It allows us to group several Kubernetes objects into a single
        object.  This is the most popular option because it does not require intermediate files, and
        other tools don't always reliably support YAML streams.
      </p>
        The Kubernetes community has been very active with Jsonnet, and the following resources may
        be helpful:
      </p>
      <ul>
        <li>
          A <a href="https://github.com/google/jsonnet/tree/master/case_studies/kubernetes">simple
          example</a> from the Jsonnet repo.
        </li>
        <li>
          <a href="https://github.com/kubecfg/kubecfg">Kubecfg</a> (whose name may sound familiar to
          current or ex-Googlers) is an unopiniated tool for evaluating Jsonnet and
          pushing the results to Kubernetes.  It comes with a <a
          href="https://github.com/bitnami-labs/kube-libsonnet">useful template library</a>.  See
          this <a href="https://engineering.bitnami.com/articles/an-example-of-real-kubernetes-bitnami.html">blog post</a>.
        </li>
        <li>
          <a href="https://tanka.dev">Tanka</a>, by <a href="https://grafana.com">Grafana Labs</a>
          is the spiritual successor of Ksonnet. It extends jsonnet with native functions
          to import Kustomize manifests and Helm charts, making it an allround tool to
          work with Kubernetes.
        </li>
        <li>
          <a href="https://github.com/jsonnet-libs/k8s">jsonnet-libs/k8s</a>
          generator produces over 30 Jsonnet Kubernetes libraries and counting, with most
          notably the <a href="https://jsonnet-libs.github.io/k8s-libsonnet/">k8s-libsonnet</a> library
          as the succesor of ksonnet-lib.
        </li>
        <li>
          <a href="https://github.com/deepmind/kapitan">Kapitan</a> by <a
          href="https://deepmind.com">Deepmind</a> is another tool for driving Kubernetes with
          Jsonnet and textual templating.
        </li>
        <li>
          <a href="https://www.box.com">Box</a> have <a
          href="https://blog.box.com/blog/kubernetes-box-microservices-maximum-velocity/">blogged</a>
          and <a href="https://youtu.be/QIDrdZlEQdw?t=10m35s">spoken about</a> how their internal
          Kubernetes-based infrastructure uses Jsonnet.
        </li>
        <li>
          <a href="https://databricks.com">Databricks</a> have <a
          href="https://databricks.com/blog/2017/06/26/declarative-infrastructure-jsonnet-templating-language.html">written
          about</a> how they manage infrastructure on Kubernetes using Jsonnet.  They also published
          a <a href="https://github.com/databricks/jsonnet-style-guide">style guide</a>, which may
          be informative.
        </li>
      </ul>
    </div>
    <div style="clear: both"></div>
  </div>
</div>

<div class="hgroup">
  <div class="hgroup-inline">
    <div class="panel">
      <h2 id="syntax">Conversion Tool</h2>
    </div>
    <div style="clear: both"></div>
  </div>
</div>

<div class="hgroup">
  <div class="hgroup-inline">
    <div class="panel">
      <p>
        As a convenience, the following tool allows you to convert YAML Kubernetes examples into
        basic Jsonnet files.  It simply converts the YAML to JSON and then runs the Jsonnet
        formatter on that JSON to make it look a bit cleaner.  Sorry, the original file's comments
        are lost during the YAML parsing.  This tool just helps you get started.  You will want to
        further clean up the code to remove duplication.
      </p>
    </div>
    <div style="clear: both"></div>
  </div>
</div>

<div class="inverse hgroup">
  <div class=hgroup-inline>
    <div class="tab-window-input" id="kube-convert-input">
      <div class="tab-header">
      </div>
      <textarea id=kube-yaml>
        kind: ReplicationController
        apiVersion: v1
        metadata:
          name: spark-master-controller
        spec:
          replicas: 1
          selector:
            component: spark-master
          template:
            metadata:
              labels:
                component: spark-master
            spec:
              containers:
                - name: spark-master
                  image: k8s.gcr.io/spark:1.5.2_v1
                  command: ["/start-master"]
                  ports:
                    - containerPort: 7077
                    - containerPort: 8080
                  resources:
                    requests:
                      cpu: 100m
      </textarea>
    </div>
    <div class="bigarrow">➡</div>
    <div class="tab-window-output" id="kube-convert-output">
      <div class="tab-header">
        <div class=selected onclick="tab_click(this, 'kube-convert-output')">output.jsonnet</div>
      </div>
      <textarea readonly class="selected code-json" id="kube-convert-output">
        {
          kind: 'ReplicationController',
          apiVersion: 'v1',
          metadata: {
            name: 'spark-master-controller',
          },
          spec: {
            replicas: 1,
            selector: {
              component: 'spark-master',
            },
            template: {
              metadata: {
                labels: {
                  component: 'spark-master',
                },
              },
              spec: {
                containers: [
                  {
                    name: 'spark-master',
                    image: 'k8s.gcr.io/spark:1.5.2_v1',
                    command: [
                      '/start-master',
                    ],
                    ports: [
                      {
                        containerPort: 7077,
                      },
                      {
                        containerPort: 8080,
                      },
                    ],
                    resources: {
                      requests: {
                        cpu: '100m',
                      },
                    },
                  },
                ],
              },
            },
          },
        }
      </textarea>
    </div>
    <script>
      yaml_conv_demo(
        'kube-convert-input',
        'kube-yaml',
        'kube.yaml',
        'kube-convert-output',
      );
    </script>
    <div style="clear: both"></div>
  </div>
</div>

<div class="hgroup">
  <div class="hgroup-inline">
    <div class="panel">
      <h2 id=collections>Kubernetes Collections</h2>
    </div>
    <div style="clear: both"></div>
  </div>
</div>

<div class="hgroup">
  <div class="hgroup-inline">
    <div class="panel">
      <p>
        In some places, the Kubernetes API objects use lists of named objects or (name, value) pairs
        rather than using objects to map names to values. This makes those objects hard to reference
        and extend in Jsonnet.  For example, the containers part of the Pod spec looks like this:
      </p>
      <pre>local PodSpec = {
  containers: [
    {
      name: 'foo',
      env: [
        { name: 'var1', value: 'somevalue' },
        { name: 'var2', value: 'somevalue' },
      ],
    },
    {
      name: 'bar',
      env: [
        { name: 'var2', value: 'somevalue' },
        { name: 'var3', value: 'somevalue' },
      ],
    },
  ],
};</pre>
      <p>
        If we wanted to override this to modify the <code>var3</code> environment variable of
        <code>bar</code>, we could use array indexes to reference the particular container and
        environment variable, but the resulting code is brittle and ugly:
      </p>
      <pre>PodSpec {
  containers: [
    super.containers[0],
    super.containers[1] {
      env: [
        super.env[0],
        super.env[1] { value: 'othervalue' },
      ],
    },
  ],
}</pre>
      <p>
        A better solution is to use an object to represent the mapping, converting to the Kubernetes
        representation post-hoc. We can now create a new Pod spec with the same override, specified
        much more easily and safely:
      </p>
    </div>
    <div style="clear: both"></div>
  </div>
</div>

<div class="inverse hgroup">
  <div class=hgroup-inline>
    <div class="tab-window-input" id="collections-input">
      <div class="tab-header">
      </div>
      <textarea id=pod-spec-jsonnet>
        local utils = import 'utils.libsonnet';

        local PodSpec = {
          containersObj:: {
            foo: {
              envObj:: {
                var1: 'somevalue',
                var2: 'somevalue',
              },
              env: utils.pairList(self.envObj),
            },
            bar: {
              envObj:: {
                var2: 'somevalue',
                var3: 'somevalue',
              },
              env: utils.pairList(self.envObj),
            },
          },
          containers:
            utils.namedObjectList(self.containersObj),
        };

        PodSpec {
          containersObj+: {
            bar+: { envObj+: { var3: 'othervalue' } }
          }
        }
      </textarea>
      <textarea id=utils-libsonnet>
        // utils.libsonnet
        {
          pairList(tab, kfield='name',
                   vfield='value'):: [
            { [kfield]: k, [vfield]: tab[k] }
            for k in std.objectFields(tab)
          ],

          namedObjectList(tab, name_field='name'):: [
            tab[name] + { [name_field]: name }
            for name in std.objectFields(tab)
          ],
        }
      </textarea>
    </div>
    <div class="bigarrow">➡</div>
    <div class="tab-window-output" id="collections-output">
      <div class="tab-header">
        <div class=selected onclick="tab_output_click(this, 'output-json')">
          output.json
        </div>
      </div>
      <textarea readonly class="selected code-json" id="output-json">
        {
          "containers": [
            {
              "env": [
                {
                  "name": "var2",
                  "value": "somevalue"
                },
                {
                  "name": "var3",
                  "value": "othervalue"
                }
              ],
              "name": "bar"
            },
            {
              "env": [
                {
                  "name": "var1",
                  "value": "somevalue"
                },
                {
                  "name": "var2",
                  "value": "somevalue"
                }
              ],
              "name": "foo"
            }
          ]
        }
      </textarea>
    </div>
    <script>
      demo(
        'collections-input',
        {
          'pod-spec-jsonnet': 'pod-spec.jsonnet',
          'utils-libsonnet': 'utils.libsonnet',
        },
        'pod-spec.jsonnet',
        'collections-output',
        false,
        false
      );
    </script>
    <div style="clear: both"></div>
  </div>
</div>

<div class="hgroup">
  <div class="hgroup-inline">
    <div class="panel">
      <p>
        Such a pattern also makes referencing easier. Now, you can use the following code to access
        the same environment variable:
      </p>
      <pre>PodSpec.containersObj.bar.envObj.var3</pre>
    </div>
    <div style="clear: both"></div>
  </div>
</div>