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
use super::*;
use indoc::indoc;

/// cilium `upgradeCompatibility`: a strict parser applied to
/// `default LITERAL .Values.path` only ever sees the raw value on the truthy
/// arm. Every Helm-empty input (`false`, `0`, `""`, `{}`, `[]`, `null`)
/// selects the literal fallback and renders, so the raw path must stay open
/// for the whole Helm-falsy set while truthy inputs keep the parser domain.
#[test]
fn default_literal_fallback_keeps_helm_empty_inputs_open_for_parsers() {
    let src = indoc! {r#"
        {{- if semverCompare ">=1.8" (default "1.8" .Values.upgradeCompatibility) }}
        apiVersion: v1
        kind: ConfigMap
        metadata:
          name: test
        {{- end }}
    "#};
    let schema = schema_for_values_yaml(parse_ir(src), None);

    for instance in [
        serde_json::json!({}),
        serde_json::json!({ "upgradeCompatibility": null }),
        serde_json::json!({ "upgradeCompatibility": false }),
        serde_json::json!({ "upgradeCompatibility": 0 }),
        serde_json::json!({ "upgradeCompatibility": "" }),
        serde_json::json!({ "upgradeCompatibility": {} }),
        serde_json::json!({ "upgradeCompatibility": [] }),
        serde_json::json!({ "upgradeCompatibility": "1.14" }),
        serde_json::json!({ "upgradeCompatibility": "v1.2.3-rc.1" }),
    ] {
        assert!(
            schema_accepts_instance(&schema, &instance),
            "a Helm-empty input selects the fallback and never reaches the parser: \
             instance={instance}; schema={schema}"
        );
    }
    for instance in [
        serde_json::json!({ "upgradeCompatibility": "garbage" }),
        serde_json::json!({ "upgradeCompatibility": { "a": 1 } }),
        serde_json::json!({ "upgradeCompatibility": [1] }),
        serde_json::json!({ "upgradeCompatibility": true }),
    ] {
        assert!(
            !schema_accepts_instance(&schema, &instance),
            "a truthy non-semver input survives selection and aborts semverCompare: \
             instance={instance}; schema={schema}"
        );
    }
}

/// cloudnative-pg `nameOverride`: the fullname helpers select
/// `default .Chart.Name .Values.nameOverride` before `trunc`/`contains`, so
/// the string contract binds only truthy raw values even when the consuming
/// template is guarded by an unrelated liveness switch.
#[test]
fn helper_default_fallback_keeps_helm_empty_inputs_open_for_string_consumers() {
    let helpers = indoc! {r#"
        {{- define "chart.name" -}}
        {{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
        {{- end }}
        {{- define "chart.fullname" -}}
        {{- $name := default .Chart.Name .Values.nameOverride }}
        {{- if contains $name .Release.Name }}
        {{- .Release.Name | trunc 63 | trimSuffix "-" }}
        {{- else }}
        {{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
        {{- end }}
        {{- end }}
    "#};
    let src = indoc! {r#"
        {{- if .Values.config.create }}
        apiVersion: v1
        kind: ConfigMap
        metadata:
          name: {{ include "chart.fullname" . }}
          labels:
            app: {{ include "chart.name" . }}
        {{- end }}
    "#};
    let schema = schema_for_values_yaml(
        parse_ir_with_helpers(src, helpers),
        Some(indoc! {r#"
            nameOverride: ""
            config:
              create: true
        "#}),
    );

    // The `config` host is navigated on every render, so every composed
    // instance carries it — its absence is the null-deleted state helm
    // aborts on, pinned separately.
    for instance in [
        serde_json::json!({ "config": { "create": true } }),
        serde_json::json!({ "nameOverride": null, "config": { "create": true } }),
        serde_json::json!({ "nameOverride": false, "config": { "create": false } }),
        serde_json::json!({ "nameOverride": "", "config": { "create": true } }),
        serde_json::json!({ "nameOverride": {}, "config": { "create": false } }),
        serde_json::json!({ "nameOverride": "custom-name", "config": { "create": true } }),
        serde_json::json!({ "nameOverride": false, "config": { "create": true } }),
        serde_json::json!({ "nameOverride": {}, "config": { "create": true } }),
        serde_json::json!({ "nameOverride": "custom", "config": { "create": true } }),
    ] {
        assert!(
            schema_accepts_instance(&schema, &instance),
            "Helm-empty overrides take the chart-name fallback and render: \
             instance={instance}; schema={schema}"
        );
    }
    assert!(
        !schema_accepts_instance(
            &schema,
            &serde_json::json!({ "nameOverride": { "a": 1 }, "config": { "create": true } })
        ),
        "a live truthy map survives selection and aborts trunc: {schema}"
    );
}

/// cloudnative-pg `namespaceOverride`: a raw value read only inside its
/// own truthy `if` arm is skipped entirely for every Helm-falsy input, so a
/// downstream string contract must not exclude those inputs.
#[test]
fn truthy_guarded_read_keeps_helm_falsy_inputs_open() {
    let helpers = indoc! {r#"
        {{- define "chart.namespace" -}}
        {{- if .Values.namespaceOverride -}}
        {{- .Values.namespaceOverride -}}
        {{- else -}}
        {{- .Release.Namespace -}}
        {{- end -}}
        {{- end -}}
    "#};
    let src = indoc! {r#"
        {{- if .Values.rbac.create }}
        apiVersion: v1
        kind: ServiceAccount
        metadata:
          name: test
          namespace: {{ include "chart.namespace" . }}
        {{- end }}
    "#};
    let schema = schema_for_values_yaml(
        parse_ir_with_helpers(src, helpers),
        Some(indoc! {r#"
            namespaceOverride: ""
            rbac:
              create: true
        "#}),
    );

    // Every instance carries the navigated `rbac` host: the chart reads
    // `.Values.rbac.create` on every render, so a coalesced document
    // without it is the null-deleted state helm aborts on.
    for instance in [
        serde_json::json!({ "namespaceOverride": false, "rbac": { "create": true } }),
        serde_json::json!({ "namespaceOverride": "", "rbac": { "create": true } }),
        serde_json::json!({ "namespaceOverride": {}, "rbac": { "create": true } }),
        serde_json::json!({ "namespaceOverride": "custom-ns", "rbac": { "create": true } }),
        serde_json::json!({ "namespaceOverride": false, "rbac": { "create": false } }),
        serde_json::json!({ "namespaceOverride": "ns", "rbac": { "create": true } }),
    ] {
        assert!(
            schema_accepts_instance(&schema, &instance),
            "falsy overrides skip the truthy arm and render the release namespace: \
             instance={instance}; schema={schema}"
        );
    }
}
/// cloudnative-pg, reduced: the fullname helper's `default`-selected
/// string contract stays truthy-scoped even when the consuming template is
/// gated by an unrelated liveness switch, so Helm-empty overrides render.
#[test]
fn liveness_gated_helper_keeps_helm_empty_fallback_inputs_open() {
    let helpers = indoc! {r#"
        {{- define "cloudnative-pg.name" -}}
        {{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
        {{- end }}
    "#};
    let src = indoc! {r#"
        {{- if .Values.config.create }}
        apiVersion: v1
        kind: ConfigMap
        metadata:
          name: config-name
        data:
          chartname: {{ include "cloudnative-pg.name" . }}
        {{- end }}
    "#};
    let schema = schema_for_values_yaml(
        parse_ir_with_helpers(src, helpers),
        Some(indoc! {r#"
            nameOverride: ""
            fullnameOverride: ""
            namespaceOverride: ""
            config:
              create: true
        "#}),
    );
    for instance in [
        serde_json::json!({ "nameOverride": false, "config": { "create": true } }),
        serde_json::json!({ "nameOverride": {}, "config": { "create": true } }),
    ] {
        assert!(
            schema_accepts_instance(&schema, &instance),
            "helm-empty stays open: instance={instance}; schema={schema}"
        );
    }
}

/// A `toString | trimSuffix "-jmx"` stage before a `semverCompare` binds
/// the semver domain through the exact stripped-affix preimage: the raw
/// tag may optionally wear the trimmed suffix, but a mid-string
/// occurrence no longer exempts the whole value (datadog's derived agent
/// tag).
#[test]
fn trim_suffix_projects_the_parser_domain_through_the_affix_preimage() {
    let helpers = indoc! {r#"
        {{- define "repro.agentVersion" -}}
        {{- $version := .Values.agents.tag | toString | trimSuffix "-jmx" -}}
        {{- if semverCompare "<7.67.0" $version -}}
        {{- fail "agent versions before 7.67.0 are not supported" -}}
        {{- end -}}
        {{- $version -}}
        {{- end -}}
    "#};
    let src = indoc! {r#"
        apiVersion: v1
        kind: ConfigMap
        metadata:
          name: cm
        data:
          version: {{ include "repro.agentVersion" . | quote }}
    "#};
    let values_yaml = indoc! {r#"
        agents:
          tag: "7.80.1"
    "#};
    let schema = schema_for_values_yaml(parse_ir_with_helpers(src, helpers), Some(values_yaml));
    for (tag, want, label) in [
        (serde_json::json!("7.80.1"), true, "a plain version parses"),
        (
            serde_json::json!("7.80.1-jmx"),
            true,
            "the jmx-suffixed version is trimmed before parsing",
        ),
        (
            serde_json::json!("junk"),
            false,
            "a non-version aborts the parser",
        ),
        (
            serde_json::json!("junk-jmx"),
            false,
            "trimming the suffix still leaves a non-version",
        ),
        (
            serde_json::json!("v-jmx-7.80.1"),
            false,
            "a mid-string token no longer exempts the raw value",
        ),
    ] {
        let instance = serde_json::json!({ "agents": { "tag": tag } });
        assert!(
            schema_accepts_instance(&schema, &instance) == want,
            "trim-suffix parser preimage ({label}): instance={instance}; schema={schema}"
        );
    }
}

/// datadog's otel gateway image helper replaces a FALSY tag with the
/// agent-version fallback (another source path) before its `semverCompare`
/// checks, so only truthy raw values reach the parser: an empty or null
/// tag renders through the fallback while a truthy non-version aborts.
#[test]
fn falsy_reassignment_to_another_source_scopes_the_parser_to_truthy_values() {
    let helpers = indoc! {r#"
        {{- define "repro.agentVersion" -}}
        {{- $version := .Values.agents.tag | toString | trimSuffix "-jmx" -}}
        {{- if eq $version "latest" -}}
        {{- $version = "7.80.1" -}}
        {{- end -}}
        {{- $version -}}
        {{- end -}}

        {{- define "repro.gatewayImage" -}}
          {{- $imageTag := .Values.gw.tag -}}
          {{- if not $imageTag -}}
            {{- $imageTag = include "repro.agentVersion" . -}}
          {{- end -}}
          {{- $imageTag = $imageTag | toString -}}
          {{- if semverCompare "<7.67.0" $imageTag -}}
            {{- fail "agent versions before 7.67.0 are not supported" -}}
          {{- end -}}
          {{- $imageTag -}}
        {{- end -}}
    "#};
    let src = indoc! {r#"
        {{- if .Values.gw.enabled }}
        apiVersion: apps/v1
        kind: Deployment
        metadata:
          name: gw
        spec:
          template:
            spec:
              containers:
                - name: gw
                  image: "agent:{{ include "repro.gatewayImage" . }}"
        {{- end }}
    "#};
    let values_yaml = indoc! {r#"
        gw:
          enabled: false
          tag: ""
        agents:
          tag: "7.80.1"
    "#};
    let ir = parse_ir_with_helpers(src, helpers);
    let schema = schema_for_values_yaml(ir, Some(values_yaml));
    for (tag, want) in [
        (serde_json::json!(""), true),
        (serde_json::json!(null), true),
        (serde_json::json!("7.70.0"), true),
        (serde_json::json!("junk"), false),
    ] {
        let instance = serde_json::json!({
            "gw": { "enabled": true, "tag": tag },
            "agents": { "tag": "7.80.1" },
        });
        assert!(
            schema_accepts_instance(&schema, &instance) == want,
            "falsy-reassignment parser scoping: instance={instance}; schema={schema}"
        );
    }
}

/// cilium's hubble-ui tag gate strips a digest suffix and a leading `v`
/// before comparing (`regexReplaceAll "@.*$" TAG "" | trimPrefix "v" |
/// semverCompare "<0.9.0"` → fail). Both reject lanes compose through the
/// ordered chain exactly: an unparsable core aborts `semverCompare`
/// itself, a parseable version below the bound hits the `fail`, and valid
/// versions — optionally v-prefixed, optionally digest-suffixed — render,
/// as does the `ne "latest"`-excluded sentinel.
#[test]
fn digest_strip_and_v_trim_compose_the_semver_bound_exactly() {
    let src = indoc! {r#"
        {{- if ne .Values.tag "latest" }}
        {{- if regexReplaceAll "@.*$" .Values.tag "" | trimPrefix "v" | semverCompare "<0.9.0" }}
        {{- fail "tag must be >= v0.9.0" }}
        {{- end }}
        {{- end }}
        apiVersion: v1
        kind: ConfigMap
        metadata:
          name: cm
        data:
          tag: {{ .Values.tag | quote }}
    "#};
    let values_yaml = indoc! {r#"
        tag: "latest"
    "#};
    let schema = schema_for_values_yaml(parse_ir(src), Some(values_yaml));
    for (tag, want, label) in [
        (
            serde_json::json!("v0.13.5"),
            true,
            "a valid version renders",
        ),
        (
            serde_json::json!("v0.13.5@sha256:abc"),
            true,
            "the digest suffix is stripped before parsing",
        ),
        (serde_json::json!("0.9.0"), true, "the bound itself passes"),
        (
            serde_json::json!("latest"),
            true,
            "the ne-excluded sentinel never reaches the gate",
        ),
        (
            serde_json::json!("garbage"),
            false,
            "an unparsable core aborts semverCompare",
        ),
        (
            serde_json::json!("v0.1.0"),
            false,
            "a version below the bound hits the fail",
        ),
        (
            serde_json::json!("0.1.0@sha256:abc"),
            false,
            "the bound applies to the digest-stripped core",
        ),
        (
            serde_json::json!("vv0.8.0"),
            false,
            "one trimmed v still leaves a below-bound version",
        ),
    ] {
        let instance = serde_json::json!({ "tag": tag });
        assert!(
            schema_accepts_instance(&schema, &instance) == want,
            "digest-strip/v-trim semver bound ({label}): instance={instance}; schema={schema}"
        );
    }
}