helm-schema-gen 0.0.6

Generate an accurate JSON schema for any helm chart
Documentation
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
use super::*;

/// an `else` arm that EXECUTES a member access closes the unmatched
/// scalar domain — `typeIs "string"` dispatch with a structural complement
/// must reject values neither arm renders (external-dns provider shape).
#[test]
fn executing_else_member_access_closes_unmatched_scalar_domain() {
    let src = indoc! {r#"
        apiVersion: v1
        kind: ConfigMap
        metadata:
          name: test
        data:
          {{- if typeIs "string" .Values.provider }}
          provider: {{ .Values.provider }}
          {{- else }}
          provider: {{ .Values.provider.name }}
          {{- end }}
    "#};
    let values_yaml = "provider: aws\n";
    let schema = schema_for_values_yaml(parse_ir(src), Some(values_yaml));

    for instance in [
        serde_json::json!({ "provider": "aws" }),
        serde_json::json!({ "provider": { "name": "aws" } }),
    ] {
        assert!(
            schema_accepts_instance(&schema, &instance),
            "both dispatch arms render: instance={instance}; schema={schema}"
        );
    }
    assert!(
        !schema_accepts_instance(&schema, &serde_json::json!({ "provider": 7 })),
        "the else arm dereferences `.name`, so a non-string scalar fails \
         rendering and must be rejected: {schema}"
    );
}

/// a type-dispatch complement nested under outer enable guards must
/// scope its object requirement to the complement arm — the string arm
/// stays valid when the outer guards are ACTIVE (cilium SPIRE image shape).
#[test]
fn nested_type_dispatch_keeps_string_arm_under_active_outer_guards() {
    let src = indoc! {r#"
        {{- if .Values.monitoring.enabled }}
        apiVersion: v1
        kind: ConfigMap
        metadata:
          name: test
        data:
          {{- if typeIs "string" .Values.image }}
          image: {{ .Values.image }}
          {{- else }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          {{- end }}
        {{- end }}
    "#};
    let values_yaml = indoc! {"
        monitoring:
          enabled: false
        image:
          repository: repo
          tag: latest
    "};
    let schema = schema_for_values_yaml(parse_ir(src), Some(values_yaml));

    assert!(
        schema_accepts_instance(
            &schema,
            &serde_json::json!({ "monitoring": { "enabled": true }, "image": "repo:1.2" })
        ),
        "the string arm renders under active outer guards; the complement \
         arm's object shape must not leak over it: {schema}"
    );
    assert!(
        schema_accepts_instance(
            &schema,
            &serde_json::json!({
                "monitoring": { "enabled": true },
                "image": { "repository": "repo", "tag": "1.2" }
            })
        ),
        "the object arm stays valid under active outer guards: {schema}"
    );
}

/// `with` rebinds dot, and a `typeOf .` dispatch inside the body must
/// bind to the originating value path — the executing `else` places dot
/// structurally, closing the unmatched scalar domain (minio
/// extraContainers shape).
#[test]
fn with_rebound_dot_type_dispatch_binds_source_path() {
    let src = indoc! {r#"
        apiVersion: v1
        kind: Pod
        metadata:
          name: test
        spec:
          containers:
            {{- with .Values.extraContainers }}
            {{- if eq (typeOf .) "string" }}
            {{- tpl . $ | nindent 4 }}
            {{- else }}
            {{- toYaml . | nindent 4 }}
            {{- end }}
            {{- end }}
    "#};
    let values_yaml = "extraContainers: []\n";
    let schema = schema_for_values_yaml(parse_ir(src), Some(values_yaml));

    for instance in [
        serde_json::json!({ "extraContainers": "- name: extra" }),
        serde_json::json!({ "extraContainers": [{ "name": "extra" }] }),
    ] {
        assert!(
            schema_accepts_instance(&schema, &instance),
            "string and structured arms both render: instance={instance}; schema={schema}"
        );
    }
    assert!(
        !schema_accepts_instance(&schema, &serde_json::json!({ "extraContainers": 7 })),
        "a non-string scalar reaches the structural else placement and \
         renders invalid YAML; it must be rejected: {schema}"
    );
}

/// an UNDECLARED selector-style object observed through member reads
/// must stay open — reads prove keys exist, they do not bound the member
/// set (nats-account-server `credentials.secret` shape, where `name` and
/// `key` are read from different templates).
#[test]
fn partially_observed_selector_object_stays_open() {
    let src = indoc! {r"
        apiVersion: v1
        kind: Pod
        metadata:
          name: test
        spec:
          volumes:
          - name: creds
            secret:
              secretName: {{ .Values.credentials.secret.name }}
    "};
    let schema = schema_for_values_yaml(parse_ir(src), None);

    assert!(
        schema_accepts_instance(
            &schema,
            &serde_json::json!({
                "credentials": { "secret": { "name": "secret-name", "key": "k" } }
            })
        ),
        "a member read must not close the selector object to observed keys: {schema}"
    );
}

/// a declared mapping the chart SERIALIZES (whole map or per-section)
/// is passthrough config — the declared default documents keys, it does not
/// bound them (grafana.ini / airflow config shape).
#[test]
fn serialized_declared_mapping_sections_stay_open() {
    let src = indoc! {r"
        apiVersion: v1
        kind: ConfigMap
        metadata:
          name: test
        data:
          config.yaml: |
            {{- toYaml .Values.config | nindent 4 }}
    "};
    let values_yaml = indoc! {"
        config:
          server:
            port: 80
    "};
    let schema = schema_for_values_yaml(parse_ir(src), Some(values_yaml));

    assert!(
        schema_accepts_instance(
            &schema,
            &serde_json::json!({
                "config": {
                    "server": { "port": 80, "root_url": "http://x" },
                    "smtp": { "enabled": true, "host": "mail" }
                }
            })
        ),
        "serialized passthrough config must accept keys beyond the declared \
         default shape: {schema}"
    );
}

/// guarded-read sibling: the serialized fact must survive a truthy
/// member read at the same path — the exact coredns `service.clusterIPs`
/// verification shape from the plan, applied to a declared mapping.
#[test]
fn guard_read_beside_serialized_render_keeps_mapping_open() {
    let src = indoc! {r"
        apiVersion: v1
        kind: ConfigMap
        metadata:
          name: test
        data:
          {{- if .Values.config.server }}
          port: {{ .Values.config.server.port }}
          {{- end }}
          config.yaml: |
            {{- toYaml .Values.config | nindent 4 }}
    "};
    let values_yaml = indoc! {"
        config:
          server:
            port: 80
    "};
    let schema = schema_for_values_yaml(parse_ir(src), Some(values_yaml));

    assert!(
        schema_accepts_instance(
            &schema,
            &serde_json::json!({
                "config": { "server": { "port": 80, "extra": 1 }, "another": {} }
            })
        ),
        "a truthy/member read beside the serialized render must not close \
         the mapping: {schema}"
    );
}

/// an undeclared, truthy-guarded, `toYaml`-serialized leaf renders
/// LISTS as well as maps — it must not be pinned to `object` (coredns
/// `service.clusterIPs` / nats-operator `tolerations` shape).
#[test]
fn serialized_truthy_guarded_leaf_admits_arrays() {
    let src = indoc! {r"
        apiVersion: example.com/v1
        kind: Widget
        metadata:
          name: test
        spec:
          {{- if .Values.service.clusterIPs }}
          clusterIPs:
          {{ toYaml .Values.service.clusterIPs | nindent 4 }}
          {{- end }}
    "};
    let values_yaml = indoc! {"
        service: {}
    "};
    let schema = schema_for_values_yaml(parse_ir(src), Some(values_yaml));

    assert!(
        schema_accepts_instance(
            &schema,
            &serde_json::json!({ "service": { "clusterIPs": ["10.96.0.10"] } })
        ),
        "a serialized guarded leaf renders arrays; the schema must not pin \
         it to object: {schema}"
    );
}

/// An incomplete member-host domain on an ancestor must not make its declared
/// descendants unconditional. The descendant's own guarded `join` use is the
/// exact shape contract and accepts both the declared list and the chart's
/// documented comma-separated string form (oauth2-proxy session storage).
#[test]
fn guarded_join_owns_descendant_beneath_preserved_member_host() {
    let src = indoc! {r#"
        apiVersion: v1
        kind: ConfigMap
        metadata:
          name: test
        data:
          {{- if eq (default "cookie" .Values.sessionStorage.type) "redis" }}
          {{- if eq (default "" .Values.sessionStorage.redis.clientType) "sentinel" }}
          urls: {{ join "," .Values.sessionStorage.redis.sentinel.connectionUrls | quote }}
          {{- end }}
          {{- end }}
    "#};
    let values_yaml = indoc! {"
        sessionStorage:
          type: cookie
          redis:
            clientType: standalone
            sentinel:
              connectionUrls: []
    "};
    let schema = schema_for_values_yaml(parse_ir(src), Some(values_yaml));

    for connection_urls in [
        serde_json::json!(["redis://one:26379", "redis://two:26379"]),
        serde_json::json!("redis://one:26379,redis://two:26379"),
    ] {
        let instance = serde_json::json!({
            "sessionStorage": {
                "type": "redis",
                "redis": {
                    "clientType": "sentinel",
                    "sentinel": { "connectionUrls": connection_urls }
                }
            }
        });
        assert!(
            schema_accepts_instance(&schema, &instance),
            "`join` owns the active descendant shape beneath its preserved \
             ancestor: instance={instance}; schema={schema}"
        );
    }
}

/// A value spliced into a CLI-flag string slot is formatted as text. The
/// empty-string declared default is intent, not an input-kind constraint.
#[test]
fn flag_splice_accepts_any_scalar_beyond_declared_string() {
    let src = indoc! {r"
        apiVersion: v1
        kind: Pod
        metadata:
          name: test
        spec:
          containers:
          - name: main
            args:
            - -v={{ .Values.klogLevel }}
    "};
    let values_yaml = "klogLevel: \"\"\n";
    let schema = schema_for_values_yaml(parse_ir(src), Some(values_yaml));

    for instance in [
        serde_json::json!({ "klogLevel": 8 }),
        serde_json::json!({ "klogLevel": "8" }),
        serde_json::json!({ "klogLevel": [8] }),
        serde_json::json!({ "klogLevel": { "level": 8 } }),
    ] {
        assert!(
            schema_accepts_instance(&schema, &instance),
            "flag splices format every input kind: instance={instance}; schema={schema}"
        );
    }
}

/// a declared BOOLEAN spliced into a flag slot accepts the string
/// form too (nack `readOnly` shape, `--read-only=true` renders either way).
#[test]
fn declared_boolean_flag_splice_accepts_string_form() {
    let src = indoc! {r"
        apiVersion: v1
        kind: Pod
        metadata:
          name: test
        spec:
          containers:
          - name: main
            args:
            - --read-only={{ .Values.readOnly }}
    "};
    let values_yaml = "readOnly: false\n";
    let schema = schema_for_values_yaml(parse_ir(src), Some(values_yaml));

    for instance in [
        serde_json::json!({ "readOnly": true }),
        serde_json::json!({ "readOnly": "true" }),
    ] {
        assert!(
            schema_accepts_instance(&schema, &instance),
            "a flag splice prints booleans and strings alike: instance={instance}; schema={schema}"
        );
    }
}

/// a declared boolean rendered inside a QUOTED string value accepts
/// the string form (nfs-subdir-external-provisioner `archiveOnDelete`
/// shape: `archiveOnDelete: "{{ .Values.storageClass.archiveOnDelete }}"`).
#[test]
fn quoted_string_slot_widen_declared_boolean_to_scalars() {
    let src = indoc! {r#"
        apiVersion: storage.k8s.io/v1
        kind: StorageClass
        metadata:
          name: test
        parameters:
          archiveOnDelete: "{{ .Values.storageClass.archiveOnDelete }}"
    "#};
    let values_yaml = indoc! {"
        storageClass:
          archiveOnDelete: false
    "};
    let schema = schema_for_values_yaml(parse_ir(src), Some(values_yaml));

    for instance in [
        serde_json::json!({ "storageClass": { "archiveOnDelete": "false" } }),
        serde_json::json!({ "storageClass": { "archiveOnDelete": false } }),
    ] {
        assert!(
            schema_accepts_instance(&schema, &instance),
            "a quoted scalar slot prints booleans and strings alike: instance={instance}; schema={schema}"
        );
    }
}

/// a declared-`{}` self-guarded fragment splices whatever the user
/// supplies — `toYaml` renders sequences as readily as maps, so the
/// empty-map placeholder union needs the array arm (nats-kafka
/// `additionalVolumes` shape).
#[test]
fn declared_empty_map_guarded_fragment_admits_arrays() {
    let src = indoc! {r"
        apiVersion: example.com/v1
        kind: Widget
        metadata:
          name: test
        spec:
          {{- if .Values.additionalVolumes }}
          volumes:
          {{- toYaml .Values.additionalVolumes | nindent 4 }}
          {{- end }}
    "};
    let values_yaml = "additionalVolumes: {}\n";
    let schema = schema_for_values_yaml(parse_ir(src), Some(values_yaml));

    for instance in [
        serde_json::json!({ "additionalVolumes": [{ "name": "v" }] }),
        serde_json::json!({ "additionalVolumes": { "v": { "hostPath": "/" } } }),
        serde_json::json!({ "additionalVolumes": {} }),
    ] {
        assert!(
            schema_accepts_instance(&schema, &instance),
            "toYaml fragments render lists and maps alike: instance={instance}; schema={schema}"
        );
    }
}

/// a value consumed only through `tpl` (here via a `with`-bound dot
/// inside an included helper) is a template STRING — the schema must keep
/// the string form valid (airflow `extraEnv` shape, declared `~`).
#[test]
fn with_dot_tpl_keeps_string_form_valid() {
    let helper_src = indoc! {r#"
        {{- define "test.env" }}
          {{- with .Values.extraEnv }}
            {{- tpl . $ | nindent 2 }}
          {{- end }}
        {{- end }}
    "#};
    let src = indoc! {r#"
        apiVersion: v1
        kind: ConfigMap
        metadata:
          name: test
        data:
          env: |
            {{- include "test.env" . | nindent 4 }}
    "#};
    let values_yaml = "extraEnv: ~\n";
    let schema = schema_for_values_yaml(parse_ir_with_helpers(src, helper_src), Some(values_yaml));

    for instance in [
        serde_json::json!({ "extraEnv": "- name: FOO\n  value: bar" }),
        serde_json::json!({ "extraEnv": null }),
    ] {
        assert!(
            schema_accepts_instance(&schema, &instance),
            "tpl consumes a template string; the string and declared-null \
             forms stay valid: instance={instance}; schema={schema}"
        );
    }
    assert!(
        !schema_accepts_instance(&schema, &serde_json::json!({ "extraEnv": { "a": 1 } })),
        "a truthy non-string reaches `tpl` and aborts rendering: {schema}"
    );
}

/// a values-declared OBJECT that only renders under its own truthy
/// guard accepts explicit `null` — helm null-deletion removes the key and
/// the falsy guard skips the branch (datadog `datadog.securityContext`
/// shape, declared `{runAsUser: 0}`).
#[test]
fn self_guarded_declared_object_accepts_explicit_null() {
    let src = indoc! {r"
        apiVersion: v1
        kind: Pod
        metadata:
          name: test
        spec:
          {{- if .Values.securityContext }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 4 }}
          {{- end }}
    "};
    let values_yaml = indoc! {"
        securityContext:
          runAsUser: 0
    "};
    let schema = schema_for_values_yaml(parse_ir(src), Some(values_yaml));

    for instance in [
        serde_json::json!({ "securityContext": null }),
        serde_json::json!({ "securityContext": { "runAsUser": 0 } }),
    ] {
        assert!(
            schema_accepts_instance(&schema, &instance),
            "helm null-deletion plus the falsy self-guard makes explicit \
             null render fine: instance={instance}; schema={schema}"
        );
    }
}

/// trivy half: literal-key `dig` evaluates structurally — sprig
/// type-asserts every step, so the subject carries a truthy⇒object
/// contract while the dug leaf may be any type.
#[test]
fn literal_key_dig_binds_intermediate_object_contract() {
    let src = indoc! {r#"
        apiVersion: v1
        kind: ConfigMap
        metadata:
          name: test
        data:
          value: {{ dig "section" "leaf" "fallback" .Values.cfg }}
    "#};
    let values_yaml = "cfg: {}\n";
    let schema = schema_for_values_yaml(parse_ir(src), Some(values_yaml));

    for instance in [
        serde_json::json!({ "cfg": { "section": { "leaf": "seven" } } }),
        serde_json::json!({ "cfg": {} }),
    ] {
        assert!(
            schema_accepts_instance(&schema, &instance),
            "dig walks maps and falls back on missing keys: instance={instance}; schema={schema}"
        );
    }
    assert!(
        !schema_accepts_instance(&schema, &serde_json::json!({ "cfg": "scalar" })),
        "sprig `dig` type-asserts its subject to a map; a truthy non-map \
         aborts rendering: {schema}"
    );
}

/// an `if (include …)` condition hole absorbs the called helper's
/// `kindIs` type-dispatch facts — the dispatched alternatives survive
/// beside the declared default shape (grafana hpa apiVersion shape).
#[test]
fn include_condition_absorbs_helper_type_dispatch_alternatives() {
    let helper_src = indoc! {r#"
        {{- define "test.enabled" -}}
        {{- if kindIs "map" .Values.autoscaling -}}
        {{- if .Values.autoscaling.enabled -}}
        true
        {{- end -}}
        {{- else if kindIs "string" .Values.autoscaling -}}
        {{- .Values.autoscaling -}}
        {{- end -}}
        {{- end -}}
    "#};
    let src = indoc! {r#"
        {{- if (include "test.enabled" .) }}
        apiVersion: v1
        kind: ConfigMap
        metadata:
          name: test
        data:
          enabled: "yes"
        {{- end }}
    "#};
    let values_yaml = indoc! {"
        autoscaling:
          enabled: false
    "};
    let schema = schema_for_values_yaml(parse_ir_with_helpers(src, helper_src), Some(values_yaml));

    for instance in [
        serde_json::json!({ "autoscaling": { "enabled": true } }),
        serde_json::json!({ "autoscaling": "on" }),
    ] {
        assert!(
            schema_accepts_instance(&schema, &instance),
            "the helper's kindIs dispatch proves both forms render: \
             instance={instance}; schema={schema}"
        );
    }
}

/// A helper that serializes a list beside a fixed list item requires the
/// source only while that serialization executes. `toYaml nil` emits
/// `null`, which cannot continue the helper's sequence (Fluent Bit's fixed
/// `DaemonSet` volumes and volume mounts).
#[test]
fn helper_sequence_continuation_requires_its_serialized_source() {
    let helpers = indoc! {r#"
        {{- define "pod" -}}
        volumeMounts:
          - name: config
            mountPath: /config
        {{- if eq .Values.kind "DaemonSet" }}
          {{- toYaml .Values.daemonSetVolumeMounts | nindent 2 }}
        {{- end }}
        {{- end -}}
    "#};
    let src = indoc! {r#"
        apiVersion: v1
        kind: Pod
        metadata:
          name: test
        spec:
          containers:
            - name: test
              image: test
              {{- include "pod" . | nindent 6 }}
    "#};
    let schema = schema_for_values_yaml(
        parse_ir_with_helpers(src, helpers),
        Some(indoc! {"
            kind: DaemonSet
            daemonSetVolumeMounts:
              - name: varlog
                mountPath: /var/log
        "}),
    );

    for (instance, want, label) in [
        (
            serde_json::json!({
                "kind": "DaemonSet",
                "daemonSetVolumeMounts": [{ "name": "varlog", "mountPath": "/var/log" }]
            }),
            true,
            "a live list continues the sequence",
        ),
        (
            serde_json::json!({ "kind": "DaemonSet" }),
            false,
            "a missing live source emits null beside the fixed item",
        ),
        (
            serde_json::json!({ "kind": "Deployment" }),
            true,
            "the inactive serialization leaves the source dormant",
        ),
    ] {
        assert!(
            schema_accepts_instance(&schema, &instance) == want,
            "helper serialization scope ({label}): instance={instance}; want={want}; schema={schema}"
        );
    }
}