helm-schema 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
use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};

use helm_schema_ast::extract_values_yaml_descriptions;
use serde_yaml::Value as YamlValue;
use tracing::instrument;
use vfs::VfsPath;

use super::paths::scope_values_path;
use super::types::ChartContext;
use crate::error::{CliError, EngineResult};

#[instrument(skip_all)]
pub fn build_composed_values_document(
    charts: &[ChartContext],
    include_subchart_values: bool,
) -> EngineResult<YamlValue> {
    let root = charts.first().ok_or(CliError::NoChartsDiscovered)?;

    let root_values_path = root.chart_dir.join("values.yaml")?;
    let mut doc = if root_values_path.is_file()? {
        serde_yaml::from_str::<YamlValue>(&root_values_path.read_to_string()?)?
    } else {
        YamlValue::Mapping(serde_yaml::Mapping::default())
    };

    if include_subchart_values {
        compose_subchart_values(charts, &mut doc)?;
    }

    Ok(doc)
}

/// The dependency charts' declared defaults, composed under their value
/// prefixes with each parent's effective `global` values propagated into
/// its direct children, MINUS every path the root chart's own values.yaml
/// declares. A key present here fills at the SUBCHART's coalesce stage even
/// when the parent-level document misses it, so schema generation reads
/// absence at such paths as the subchart default instead of nil.
/// Root-declared keys are excluded because their absence can only mean Helm
/// null-deletion, which poisons the key through every later merge stage —
/// the subchart default does NOT resurrect a deleted key.
#[instrument(skip_all)]
pub fn build_dependency_values_document(charts: &[ChartContext]) -> EngineResult<YamlValue> {
    let root = charts.first().ok_or(CliError::NoChartsDiscovered)?;
    let mut doc = YamlValue::Mapping(serde_yaml::Mapping::default());
    compose_subchart_values(charts, &mut doc)?;
    let root_values_path = root.chart_dir.join("values.yaml")?;
    if root_values_path.is_file()? {
        let parent = serde_yaml::from_str::<YamlValue>(&root_values_path.read_to_string()?)?;
        doc = subtract_declared_paths(&doc, &parent);
    }
    Ok(doc)
}

/// The dependency charts' declared defaults, composed under their value
/// prefixes, WITHOUT the parent-declared subtraction: what helm refills a
/// missing or null dependency values root with. `coalesceDeps` recreates
/// the root table and coalesces the subchart's own values into it, and the
/// parent's defaults for that root went with the deletion — so a key the
/// subchart declares comes back while a parent-only key stays gone.
#[instrument(skip_all)]
pub fn build_dependency_refill_values_document(charts: &[ChartContext]) -> EngineResult<YamlValue> {
    let mut doc = YamlValue::Mapping(serde_yaml::Mapping::default());
    compose_subchart_values(charts, &mut doc)?;
    Ok(doc)
}

pub(crate) struct DependencyGlobalOwnership {
    pub(crate) shadowed_input_paths: BTreeSet<String>,
}

pub(crate) fn build_dependency_global_ownership(
    charts: &[ChartContext],
) -> EngineResult<DependencyGlobalOwnership> {
    let mut declarations = Vec::new();
    for chart in charts {
        let values_path = chart.chart_dir.join("values.yaml")?;
        if !values_path.is_file()? {
            continue;
        }
        let defaults = serde_yaml::from_str::<YamlValue>(&values_path.read_to_string()?)?;
        let Some(global) = defaults
            .as_mapping()
            .and_then(|mapping| mapping.get(YamlValue::String("global".to_string())))
            .and_then(YamlValue::as_mapping)
        else {
            continue;
        };
        let mut relative_paths = Vec::new();
        collect_declared_default_paths(global, &mut Vec::new(), &mut relative_paths);
        declarations.push((chart.values_prefix.clone(), relative_paths));
    }

    let mut shadowed_input_paths = BTreeSet::new();
    for (owner_prefix, relative_paths) in declarations {
        for chart in charts.iter().filter(|chart| {
            chart.values_prefix.len() > owner_prefix.len()
                && chart.values_prefix.starts_with(&owner_prefix)
        }) {
            for relative_path in &relative_paths {
                shadowed_input_paths
                    .insert(scoped_global_path(&chart.values_prefix, relative_path));
            }
        }
    }

    Ok(DependencyGlobalOwnership {
        shadowed_input_paths,
    })
}

fn collect_declared_default_paths(
    mapping: &serde_yaml::Mapping,
    prefix: &mut Vec<String>,
    paths: &mut Vec<Vec<String>>,
) {
    for (key, value) in mapping {
        let Some(key) = key.as_str() else {
            continue;
        };
        prefix.push(key.to_string());
        if let YamlValue::Mapping(mapping) = value {
            collect_declared_default_paths(mapping, prefix, paths);
        } else {
            paths.push(prefix.clone());
        }
        prefix.pop();
    }
}

fn scoped_global_path(chart_prefix: &[String], relative_path: &[String]) -> String {
    helm_schema_core::join_value_path(
        chart_prefix
            .iter()
            .cloned()
            .chain(std::iter::once("global".to_string()))
            .chain(relative_path.iter().cloned()),
    )
}

/// `composed` minus every path `parent` declares: keys both documents
/// carry recurse member-wise (a parent `falco-talon: {}` stub keeps the
/// subchart's keys underneath), keys only `composed` carries survive
/// whole, and parent-declared leaves are removed.
fn subtract_declared_paths(composed: &YamlValue, parent: &YamlValue) -> YamlValue {
    match (composed, parent) {
        (YamlValue::Mapping(composed_map), YamlValue::Mapping(parent_map)) => {
            let mut remaining = serde_yaml::Mapping::new();
            for (key, value) in composed_map {
                match parent_map.get(key) {
                    None => {
                        remaining.insert(key.clone(), value.clone());
                    }
                    Some(parent_value) => {
                        let child = subtract_declared_paths(value, parent_value);
                        if !matches!(child, YamlValue::Null) {
                            remaining.insert(key.clone(), child);
                        }
                    }
                }
            }
            if remaining.is_empty() {
                YamlValue::Null
            } else {
                YamlValue::Mapping(remaining)
            }
        }
        _ => YamlValue::Null,
    }
}

#[instrument(skip_all)]
pub fn build_composed_values_descriptions(
    charts: &[ChartContext],
    include_subchart_values: bool,
    values_files: &[PathBuf],
) -> EngineResult<BTreeMap<String, String>> {
    let root = charts.first().ok_or(CliError::NoChartsDiscovered)?;
    let mut descriptions = BTreeMap::new();

    add_values_file_descriptions(&root.chart_dir, &[], &mut descriptions)?;

    if include_subchart_values {
        for chart in charts {
            if chart.values_prefix.is_empty() {
                continue;
            }
            add_values_file_descriptions(
                &chart.chart_dir,
                &chart.values_prefix,
                &mut descriptions,
            )?;
        }
    }

    for path in values_files {
        add_layered_values_file_descriptions(path, &mut descriptions)?;
    }

    Ok(descriptions)
}

fn compose_subchart_values(charts: &[ChartContext], doc: &mut YamlValue) -> EngineResult<()> {
    let mut subcharts = charts
        .iter()
        .filter(|chart| !chart.values_prefix.is_empty())
        .collect::<Vec<_>>();
    subcharts.sort_by(|left, right| {
        left.values_prefix
            .len()
            .cmp(&right.values_prefix.len())
            .then_with(|| left.values_prefix.cmp(&right.values_prefix))
    });

    for chart in subcharts {
        let parent_prefix = chart
            .values_prefix
            .get(..chart.values_prefix.len().saturating_sub(1))
            .unwrap_or_default();
        let parent_global = value_at_path(doc, parent_prefix)
            .and_then(YamlValue::as_mapping)
            .and_then(|mapping| mapping.get(YamlValue::String("global".to_string())))
            .cloned();
        let target = ensure_mapping_path(doc, &chart.values_prefix);
        coalesce_global_values(target, parent_global.as_ref());

        let path = chart.chart_dir.join("values.yaml")?;
        if !path.is_file()? {
            continue;
        }
        let defaults: YamlValue = serde_yaml::from_str(&path.read_to_string()?)?;
        let dependency_keys = direct_dependency_keys(charts, &chart.values_prefix);
        coalesce_chart_values(target, defaults, &dependency_keys);
    }

    Ok(())
}

fn add_values_file_descriptions(
    chart_dir: &VfsPath,
    prefix: &[String],
    out: &mut BTreeMap<String, String>,
) -> EngineResult<()> {
    let values_path = chart_dir.join("values.yaml")?;
    if !values_path.is_file()? {
        return Ok(());
    }

    let descriptions = extract_values_yaml_descriptions(&values_path.read_to_string()?);

    for (path, description) in descriptions {
        let scoped_path = scope_values_path(&path, prefix);
        out.entry(scoped_path).or_insert(description);
    }

    Ok(())
}

fn add_layered_values_file_descriptions(
    values_path: &Path,
    out: &mut BTreeMap<String, String>,
) -> EngineResult<()> {
    let source = std::fs::read_to_string(values_path)?;
    let descriptions = extract_values_yaml_descriptions(&source);

    for (path, description) in descriptions {
        out.insert(path, description);
    }

    Ok(())
}

fn value_at_path<'a>(root: &'a YamlValue, path: &[String]) -> Option<&'a YamlValue> {
    let mut current = root;
    for segment in path {
        current = current
            .as_mapping()?
            .get(YamlValue::String(segment.clone()))?;
    }
    Some(current)
}

fn direct_dependency_keys(
    charts: &[ChartContext],
    parent_prefix: &[String],
) -> std::collections::BTreeSet<String> {
    charts
        .iter()
        .filter_map(|chart| {
            (chart.values_prefix.len() == parent_prefix.len() + 1
                && chart.values_prefix.starts_with(parent_prefix))
            .then(|| chart.values_prefix.last().cloned())
            .flatten()
        })
        .collect()
}

fn coalesce_global_values(destination_chart: &mut YamlValue, source_global: Option<&YamlValue>) {
    let YamlValue::Mapping(destination) = destination_chart else {
        return;
    };
    let global_key = YamlValue::String("global".to_string());
    let mut destination_global = match destination.get(&global_key) {
        None => serde_yaml::Mapping::new(),
        Some(YamlValue::Mapping(mapping)) => mapping.clone(),
        Some(_) => return,
    };
    let source_global = match source_global {
        None => serde_yaml::Mapping::new(),
        Some(YamlValue::Mapping(mapping)) => mapping.clone(),
        Some(_) => return,
    };

    for (key, source_value) in source_global {
        if let YamlValue::Mapping(source_mapping) = source_value {
            match destination_global.get(&key) {
                None => {
                    destination_global.insert(key, YamlValue::Mapping(source_mapping));
                }
                Some(YamlValue::Mapping(destination_mapping)) => {
                    let mut merged = source_mapping;
                    merge_mapping_existing_prefers_left(
                        &mut merged,
                        destination_mapping.clone(),
                        true,
                    );
                    destination_global.insert(key, YamlValue::Mapping(merged));
                }
                Some(_) => {}
            }
        } else if !destination_global
            .get(&key)
            .is_some_and(YamlValue::is_mapping)
        {
            destination_global.insert(key, source_value);
        }
    }

    destination.insert(global_key, YamlValue::Mapping(destination_global));
}

fn coalesce_chart_values(
    destination: &mut YamlValue,
    defaults: YamlValue,
    dependency_keys: &std::collections::BTreeSet<String>,
) {
    let (YamlValue::Mapping(destination), YamlValue::Mapping(defaults)) = (destination, defaults)
    else {
        return;
    };

    for (key, default) in defaults {
        let preserve_nulls = key
            .as_str()
            .is_some_and(|key| dependency_keys.contains(key));
        merge_value_existing_prefers_left(destination, key, default, preserve_nulls);
    }
}

fn ensure_mapping_path<'a>(root: &'a mut YamlValue, path: &[String]) -> &'a mut YamlValue {
    let mut current = root;

    for segment in path {
        if !matches!(current, YamlValue::Mapping(_)) {
            *current = YamlValue::Mapping(serde_yaml::Mapping::default());
        }

        let YamlValue::Mapping(mapping) = current else {
            return current;
        };

        let key = YamlValue::String(segment.clone());
        current = mapping
            .entry(key)
            .or_insert_with(|| YamlValue::Mapping(serde_yaml::Mapping::default()));
    }

    current
}

fn merge_mapping_existing_prefers_left(
    target: &mut serde_yaml::Mapping,
    incoming: serde_yaml::Mapping,
    preserve_nulls: bool,
) {
    for (key, value) in incoming {
        merge_value_existing_prefers_left(target, key, value, preserve_nulls);
    }
}

fn merge_value_existing_prefers_left(
    target: &mut serde_yaml::Mapping,
    key: YamlValue,
    incoming: YamlValue,
    preserve_nulls: bool,
) {
    let Some(existing) = target.get(&key).cloned() else {
        target.insert(key, incoming);
        return;
    };
    if existing.is_null() && !preserve_nulls && !incoming.is_null() {
        target.remove(&key);
        return;
    }
    if let (YamlValue::Mapping(mut existing), YamlValue::Mapping(incoming)) = (existing, incoming) {
        merge_mapping_existing_prefers_left(&mut existing, incoming, preserve_nulls);
        target.insert(key, YamlValue::Mapping(existing));
    }
}

#[cfg(test)]
#[path = "tests/values.rs"]
mod tests;