helm-schema-gen 0.0.7

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
pub mod cases;

use color_eyre::eyre::{self, OptionExt as _, WrapErr as _};
use helm_schema_ast::DefineIndex;
use helm_schema_core::{ResourceSchemaOracle, YamlPath};
use helm_schema_gen::{PreparedValuesDocuments, ValuesSchemaInput, generate_values_schema};
use helm_schema_ir::{ContractIr, ResourceRef};
use helm_schema_k8s::{
    Chain, CrdsCatalogSchemaProvider, K8sSchemaProvider, KubernetesJsonSchemaProvider,
};
use serde::Deserialize;
use serde_json::Value;
use std::path::Path;
use std::process::Command;
use test_util::prelude::sim_assert_eq;

pub fn build_define_index(
    spec: test_util::DefineSourceSpec<'_>,
    _helper_parse_mode: HelperParseMode,
) -> eyre::Result<DefineIndex> {
    let mut idx = DefineIndex::new();
    for source in spec.load()? {
        idx.add_file_source(&source.path, &source.source);
    }
    Ok(idx)
}

#[derive(Clone, Copy)]
pub enum ProviderKind<'a> {
    K8s(&'a str),
    CrdK8s(&'a str),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum HelperParseMode {
    Lenient,
    Strict,
}

#[derive(Clone, Copy)]
pub struct SchemaCorpusCase<'a> {
    pub template_path: &'a str,
    pub values_path: &'a str,
    pub fixture_values_yaml: Option<&'a str>,
    pub expected_fixture: &'a str,
    pub define_sources: test_util::DefineSourceSpec<'a>,
    pub provider: ProviderKind<'a>,
    pub helper_parse_mode: HelperParseMode,
    pub dump_stem: &'a str,
}

#[derive(Clone, Copy)]
pub struct HelmRenderCase<'a> {
    pub name: &'a str,
    pub chart_path: &'a str,
    pub show_only: Option<&'a str>,
    pub extra_args: &'a [&'a str],
}

#[derive(Clone, Copy)]
pub enum RenderedSchemaProviderKind<'a> {
    K8s(&'a str),
    CrdCatalog,
}

#[derive(Clone, Copy)]
pub struct RenderedManifestValidationCase<'a> {
    pub render: HelmRenderCase<'a>,
    pub provider: RenderedSchemaProviderKind<'a>,
}

#[derive(Clone, Copy)]
pub struct SchemaExpectation<'a> {
    pub instance: &'a str,
    pub accepted: bool,
    pub message: &'a str,
}

#[derive(Clone, Copy)]
pub struct SchemaBehaviorCase<'a> {
    pub schema_case: SchemaCorpusCase<'a>,
    pub expectations: &'a [SchemaExpectation<'a>],
}

/// K8s provider reading only the vendored bundle.
///
/// Provider availability is a deterministic test INPUT: the bundle pins which
/// upstream schemas a test can see. Reaching the ambient user cache with
/// downloads enabled made results depend on cache warmth and on
/// `raw.githubusercontent.com` being reachable — the exact failure mode the
/// bundle exists to remove. Every provider a test builds must come from here.
pub fn bundled_k8s_provider(version: &str) -> KubernetesJsonSchemaProvider {
    KubernetesJsonSchemaProvider::new(version.to_string())
        .with_cache_dir(
            test_util::workspace_testdata().join("provider-bundle/kubernetes-json-schema-cache"),
        )
        .with_allow_download(false)
}

/// CRD catalog provider reading only the vendored bundle. See
/// [`bundled_k8s_provider`] for why downloads stay off.
pub fn bundled_crd_provider() -> CrdsCatalogSchemaProvider {
    CrdsCatalogSchemaProvider::new()
        .with_cache_dir(test_util::workspace_testdata().join("provider-bundle/crds-catalog-cache"))
        .with_allow_download(false)
}

/// Production-like K8s provider path for chart-level generator tests.
///
/// These tests are meant to approximate what end users run through the CLI,
/// so they use the chain layer plus apiVersion inference instead of the older
/// single-provider shortcut.
pub fn production_k8s_chain(version: &str) -> Chain {
    let k8s_provider = bundled_k8s_provider(version).with_api_version_guess(true);
    Chain::new(vec![Box::new(k8s_provider)]).with_inference_enabled(true)
}

/// Production-like CRD + K8s provider path for chart-level generator tests.
///
/// This keeps real-world CRD-consuming chart tests on the same resolution path
/// as the CLI while leaving lower-layer provider-specific tests free to pin a
/// single provider when that is the actual subject under test.
pub fn production_crd_k8s_chain(version: &str) -> Chain {
    let crds = bundled_crd_provider();
    let k8s_provider = bundled_k8s_provider(version).with_api_version_guess(true);
    Chain::new(vec![Box::new(crds), Box::new(k8s_provider)]).with_inference_enabled(true)
}

/// Recursively remove `"additionalProperties": false` from a JSON schema.
///
/// Our generated schemas are per-template and use `additionalProperties: false`
/// to flag unknown keys. However, the chart's `values.yaml` contains values for
/// ALL templates, so a per-template schema will reject keys it doesn't cover.
/// Relaxing the schema lets us validate that the types/structure of the values
/// we *do* cover are correct without false positives from unrelated keys.
pub fn relax_schema(schema: &Value) -> Value {
    match schema {
        Value::Object(map) => {
            let mut out = serde_json::Map::new();
            for (k, v) in map {
                if k == "additionalProperties" && *v == Value::Bool(false) {
                    continue;
                }
                out.insert(k.clone(), relax_schema(v));
            }
            Value::Object(out)
        }
        Value::Array(arr) => Value::Array(arr.iter().map(relax_schema).collect()),
        other => other.clone(),
    }
}

/// Parse a `values.yaml` string into a [`serde_json::Value`].
///
/// Returns the top-level mapping as a JSON object.
pub fn values_yaml_to_json(values_yaml: &str) -> eyre::Result<Value> {
    serde_yaml::from_str(values_yaml).wrap_err("parse values.yaml as JSON")
}

pub fn generate_schema_with_values_yaml(
    contract: ContractIr,
    provider: &dyn ResourceSchemaOracle,
    values_yaml: Option<&str>,
) -> Value {
    let schema_signals = contract.finalize().into_schema_signals();
    let composed = values_yaml
        .and_then(|source| serde_yaml::from_str(source).ok())
        .unwrap_or(serde_yaml::Value::Null);
    let documents =
        PreparedValuesDocuments::new(composed, serde_yaml::Value::Null, serde_yaml::Value::Null);
    generate_values_schema(
        ValuesSchemaInput::new(&schema_signals, provider).with_values_documents(&documents),
    )
}

pub fn render_schema_case(case: &SchemaCorpusCase<'_>) -> eyre::Result<Value> {
    if let Some(values_yaml) = case.fixture_values_yaml {
        render_schema_case_with_values(case, values_yaml)
    } else {
        let values_yaml = test_util::read_testdata(case.values_path)?;
        render_schema_case_with_values(case, &values_yaml)
    }
}

pub fn render_schema_case_with_values(
    case: &SchemaCorpusCase<'_>,
    values_yaml: &str,
) -> eyre::Result<Value> {
    let src = test_util::read_testdata(case.template_path)?;
    let idx = build_define_index(case.define_sources, case.helper_parse_mode)?;
    let ir = helm_schema_ir::SymbolicIrContext::new(&idx).generate_contract_ir(&src);
    let provider = match case.provider {
        ProviderKind::K8s(version) => production_k8s_chain(version),
        ProviderKind::CrdK8s(version) => production_crd_k8s_chain(version),
    };
    let schema = generate_schema_with_values_yaml(ir, &provider, Some(values_yaml));

    if std::env::var("SCHEMA_DUMP").is_ok() {
        eprintln!("{}", serde_json::to_string_pretty(&schema)?);
        let path = std::env::temp_dir().join(format!("helm-schema.{}.schema.json", case.dump_stem));
        std::fs::write(&path, serde_json::to_vec_pretty(&schema)?)
            .wrap_err_with(|| format!("write schema dump to {}", path.display()))?;
    }

    Ok(schema)
}

pub fn assert_schema_fixture(case: &SchemaCorpusCase<'_>) -> eyre::Result<()> {
    let actual = render_schema_case(case)?;
    if std::env::var("SCHEMA_DUMP").is_ok() {
        return Ok(());
    }
    let expected: Value = serde_json::from_str(case.expected_fixture)
        .wrap_err_with(|| format!("parse expected schema fixture for {}", case.dump_stem))?;
    sim_assert_eq!(
        have: actual,
        want: expected,
        "schema fixture mismatch for {}",
        case.dump_stem,
    );
    Ok(())
}

pub fn assert_values_yaml_validates(case: &SchemaCorpusCase<'_>) -> eyre::Result<()> {
    let values_yaml = test_util::read_testdata(case.values_path)?;
    let schema = render_schema_case_with_values(case, &values_yaml)?;
    let errors = validate_values_yaml(&values_yaml, &schema)?;
    color_eyre::eyre::ensure!(
        errors.is_empty(),
        "values.yaml failed schema validation with {} error(s):\n{}",
        errors.len(),
        errors.join("\n")
    );
    Ok(())
}

fn drop_nulls(v: &Value) -> Value {
    match v {
        Value::Null => Value::Null,
        Value::Bool(_) | Value::Number(_) | Value::String(_) => v.clone(),
        Value::Array(arr) => Value::Array(
            arr.iter()
                .filter(|x| !x.is_null())
                .map(drop_nulls)
                .collect(),
        ),
        Value::Object(map) => {
            let mut out = serde_json::Map::new();
            for (k, v) in map {
                if v.is_null() {
                    continue;
                }
                out.insert(k.clone(), drop_nulls(v));
            }
            Value::Object(out)
        }
    }
}

/// Validate a JSON value against a JSON schema.
///
/// Returns a list of human-readable validation error strings.
/// An empty list means validation passed.
pub fn validate_json_against_schema(instance: &Value, schema: &Value) -> Vec<String> {
    let Ok(validator) = jsonschema::validator_for(schema) else {
        return vec!["failed to compile JSON schema".to_string()];
    };
    validator
        .iter_errors(instance)
        .map(|e| format!("{path}: {msg}", path = e.instance_path(), msg = e))
        .collect()
}

pub fn schema_accepts_instance(schema: &Value, instance: &Value) -> bool {
    validate_json_against_schema(instance, schema).is_empty()
}

/// Validate a `values.yaml` string against a generated JSON schema.
///
/// The schema is first relaxed (removing `additionalProperties: false`) so that
/// values for other templates don't cause false positives. Returns a list of
/// validation errors (empty = pass).
pub fn validate_values_yaml(values_yaml: &str, schema: &Value) -> eyre::Result<Vec<String>> {
    let json_values = drop_nulls(&values_yaml_to_json(values_yaml)?);
    let relaxed = relax_schema(schema);
    Ok(validate_json_against_schema(&json_values, &relaxed))
}

pub fn helm_template_render_with_args(
    chart_dir: &Path,
    show_only: Option<&str>,
    extra_args: &[&str],
) -> eyre::Result<String> {
    let mut cmd = Command::new("helm");
    cmd.arg("template").arg("test-release").arg(chart_dir);

    if let Some(template) = show_only {
        cmd.arg("--show-only").arg(template);
    }
    for arg in extra_args {
        cmd.arg(arg);
    }

    let output = cmd.output().wrap_err("run helm template")?;

    if output.status.success() {
        String::from_utf8(output.stdout).wrap_err("decode helm output as UTF-8")
    } else {
        let stderr = String::from_utf8_lossy(&output.stderr);
        // helm template prints errors to stdout sometimes
        let stdout = String::from_utf8_lossy(&output.stdout);
        Err(eyre::eyre!(
            "helm template failed:\nstderr: {stderr}\nstdout: {stdout}"
        ))
    }
}

pub fn render_helm_case(case: &HelmRenderCase<'_>) -> eyre::Result<String> {
    let chart_dir = test_util::workspace_testdata().join(case.chart_path);
    helm_template_render_with_args(&chart_dir, case.show_only, case.extra_args)
}

pub fn assert_helm_render_case(case: &HelmRenderCase<'_>) -> eyre::Result<()> {
    let rendered = render_helm_case(case)
        .wrap_err_with(|| format!("render Helm corpus case {}", case.name))?;
    color_eyre::eyre::ensure!(
        !rendered.is_empty(),
        "helm render produced empty YAML for {}",
        case.name
    );
    Ok(())
}

pub fn assert_schema_behavior_case(case: &SchemaBehaviorCase<'_>) -> eyre::Result<()> {
    let schema = render_schema_case(&case.schema_case)?;
    // Expectations are sparse OVERRIDES over the chart's declared defaults:
    // helm validates the coalesced document, so a template that navigates
    // `.Values.x.y` aborts on a document missing `x` — the state a user's
    // `null` deletion produces, which is pinned separately.
    let defaults = match case.schema_case.fixture_values_yaml {
        Some(values_yaml) => values_yaml.to_string(),
        None => test_util::read_testdata(case.schema_case.values_path)?,
    };
    let defaults: Value = serde_yaml::from_str::<serde_yaml::Value>(&defaults)
        .ok()
        .and_then(|doc| serde_json::to_value(doc).ok())
        .unwrap_or_else(|| serde_json::json!({}));
    for expectation in case.expectations {
        let overrides: Value = serde_json::from_str(expectation.instance)
            .wrap_err_with(|| format!("parse behavior JSON: {}", expectation.message))?;
        let mut instance = defaults.clone();
        merge_composed_override(&mut instance, overrides);
        let accepted = schema_accepts_instance(&schema, &instance);
        sim_assert_eq!(
            have: accepted,
            want: expectation.accepted,
            "{}: {}. schema={schema}",
            case.schema_case.dump_stem, expectation.message
        );
    }
    Ok(())
}

pub fn parse_yaml_documents(yaml: &str) -> eyre::Result<Vec<Value>> {
    let mut out = Vec::new();
    for doc in serde_yaml::Deserializer::from_str(yaml) {
        let value = Value::deserialize(doc).wrap_err("parse rendered YAML document as JSON")?;
        if value.is_null() {
            continue;
        }
        out.push(value);
    }
    Ok(out)
}

pub fn assert_rendered_manifest_validation_case(
    case: &RenderedManifestValidationCase<'_>,
) -> eyre::Result<()> {
    let rendered_yaml = render_helm_case(&case.render)
        .wrap_err_with(|| format!("render Helm corpus case {}", case.render.name))?;
    let docs = parse_yaml_documents(&rendered_yaml)?;
    color_eyre::eyre::ensure!(
        !docs.is_empty(),
        "rendered YAML contained no documents for {}",
        case.render.name
    );

    for doc in docs {
        let api_version = doc
            .get("apiVersion")
            .and_then(|value| value.as_str())
            .ok_or_eyre("rendered manifest missing apiVersion")?;
        let kind = doc
            .get("kind")
            .and_then(|value| value.as_str())
            .ok_or_eyre("rendered manifest missing kind")?;
        let resource = ResourceRef::concrete(api_version.to_string(), kind.to_string());
        let schema = materialized_schema_for_rendered_resource(case.provider, &resource)
            .ok_or_eyre(format!("load schema for rendered {api_version}/{kind}"))?;
        let errors = validate_json_against_schema(&doc, &schema);
        color_eyre::eyre::ensure!(
            errors.is_empty(),
            "rendered {api_version}/{kind} for {} failed schema validation with {} error(s):\n{}",
            case.render.name,
            errors.len(),
            errors.join("\n")
        );
    }
    Ok(())
}

fn materialized_schema_for_rendered_resource(
    provider: RenderedSchemaProviderKind<'_>,
    resource: &ResourceRef,
) -> Option<Value> {
    let schema = match provider {
        RenderedSchemaProviderKind::K8s(version) => {
            materialize_provider_root_schema(&bundled_k8s_provider(version), resource)?
        }
        RenderedSchemaProviderKind::CrdCatalog => {
            materialize_provider_root_schema(&bundled_crd_provider(), resource)?
        }
    };

    Some(match schema {
        Value::Object(mut object) => {
            let _ = object.remove("$schema");
            Value::Object(object)
        }
        other => other,
    })
}

fn materialize_provider_root_schema(
    provider: &impl K8sSchemaProvider,
    resource: &ResourceRef,
) -> Option<Value> {
    provider
        .lookup(resource, &YamlPath(Vec::new()))
        .into_schema_fragment()
        .map(helm_schema_core::ProviderSchemaFragment::into_schema)
}

/// Merge `overrides` into `base` the way helm coalesces user values: map
/// keys merge member-wise and a null override DELETES its key.
pub fn merge_composed_override(base: &mut Value, overrides: Value) {
    let (Some(base), Value::Object(overrides)) = (base.as_object_mut(), overrides) else {
        return;
    };
    for (key, value) in overrides {
        if value.is_null() {
            base.remove(&key);
        } else if base.get(&key).is_some_and(Value::is_object) && value.is_object() {
            if let Some(existing) = base.get_mut(&key) {
                merge_composed_override(existing, value);
            }
        } else {
            base.insert(key, value);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use test_util::prelude::sim_assert_eq;

    #[test]
    fn relax_removes_additional_properties_false() {
        let schema = serde_json::json!({
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "foo": {
                    "type": "object",
                    "additionalProperties": false,
                    "properties": {
                        "bar": { "type": "string" }
                    }
                }
            }
        });
        let relaxed = relax_schema(&schema);
        sim_assert_eq!(
            have: relaxed,
            want: serde_json::json!({
                "type": "object",
                "properties": {
                    "foo": {
                        "type": "object",
                        "properties": {
                            "bar": { "type": "string" }
                        }
                    }
                }
            })
        );
    }

    #[test]
    fn relax_keeps_additional_properties_object() {
        let schema = serde_json::json!({
            "type": "object",
            "additionalProperties": { "type": "string" }
        });
        let relaxed = relax_schema(&schema);
        sim_assert_eq!(have: relaxed, want: schema);
    }
}