helm-schema-k8s 0.0.4

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
//! Multi-version K8s resolution (explicit version list + auto-fallback
//! window) end-to-end tests using `MockFetcher` so no real network
//! calls happen.

use std::sync::Arc;

use color_eyre::eyre;
use helm_schema_core::{ResourceRef, YamlPath};
use helm_schema_k8s::{
    Chain, Diagnostic, DiagnosticSink, K8sSchemaProvider, K8sVersionChain,
    KubernetesJsonSchemaProvider,
    cache::{k8s_cache_path, not_found_marker_exists},
    default_source_id,
};
use test_util::prelude::sim_assert_eq;

/// Shared provider fixtures for K8s integration tests.
pub mod common;
use common::{MockFetcher, MockResponse};

fn tmp_dir(label: &str) -> eyre::Result<std::path::PathBuf> {
    let p = std::env::temp_dir().join(format!(
        "helm-schema.{label}.{}.{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_nanos())
            .unwrap_or(0)
    ));
    std::fs::create_dir_all(&p)?;
    Ok(p)
}

fn pdb_v1beta1_doc() -> String {
    r#"{"type":"object","x-kubernetes-group-version-kind":[{"group":"policy","version":"v1beta1","kind":"PodDisruptionBudget"}],"properties":{"spec":{"type":"object","properties":{"maxUnavailable":{"type":"integer"}}}}}"#.to_string()
}

#[test]
fn k8s_version_fallback_falls_back_to_older() -> eyre::Result<()> {
    let cache_dir = tmp_dir("k8s-fallback-falls-back")?;
    let mock = Arc::new(
        MockFetcher::new()
            .with(
                "https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/v1.35.0/poddisruptionbudget-policy-v1beta1.json",
                MockResponse::NotFound,
            )
            .with_body(
                "https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/v1.24.0/poddisruptionbudget-policy-v1beta1.json",
                pdb_v1beta1_doc().into_bytes(),
            ),
    );
    let diagnostics = DiagnosticSink::new();
    let provider = KubernetesJsonSchemaProvider::with_versions(K8sVersionChain::new(
        vec!["v1.35.0".to_string(), "v1.24.0".to_string()],
        None,
    ))
    .with_cache_dir(cache_dir)
    .with_allow_download(true)
    .with_fetcher(mock.clone())
    .with_diagnostic_sink(diagnostics.clone());

    let chain = Chain::new(vec![Box::new(provider)]).with_diagnostic_sink(diagnostics.clone());

    let resource = ResourceRef::concrete(
        "policy/v1beta1".to_string(),
        "PodDisruptionBudget".to_string(),
    );
    let schema = chain.schema_fragment_for_resource_path(&resource, &YamlPath(Vec::new()));
    assert!(schema.is_some(), "expected fallback to v1.24.0 to resolve");

    let diagnostics = diagnostics.snapshot();
    assert!(
        diagnostics.iter().any(|d| matches!(
            d,
            Diagnostic::ResolvedFromFallbackVersion {
                kind,
                api_version,
                primary_version,
                resolved_version,
            } if kind == "PodDisruptionBudget"
                && api_version == "policy/v1beta1"
                && primary_version == "v1.35.0"
                && resolved_version == "v1.24.0"
        )),
        "expected ResolvedFromFallbackVersion diagnostic; got {diagnostics:?}"
    );
    Ok(())
}

#[test]
fn k8s_version_fallback_offline_returns_none() -> eyre::Result<()> {
    let cache_dir = tmp_dir("k8s-fallback-offline")?;
    let mock = Arc::new(MockFetcher::new().with_default(MockResponse::NetworkDisabled));
    let diagnostics = DiagnosticSink::new();
    let provider = KubernetesJsonSchemaProvider::with_versions(K8sVersionChain::new(
        vec!["v1.35.0".to_string(), "v1.24.0".to_string()],
        None,
    ))
    .with_cache_dir(cache_dir)
    .with_allow_download(true)
    .with_fetcher(mock.clone())
    .with_diagnostic_sink(diagnostics.clone());

    let chain = Chain::new(vec![Box::new(provider)]).with_diagnostic_sink(diagnostics.clone());

    let resource = ResourceRef::concrete(
        "policy/v1beta1".to_string(),
        "PodDisruptionBudget".to_string(),
    );
    let schema = chain.schema_fragment_for_resource_path(&resource, &YamlPath(Vec::new()));
    assert!(schema.is_none(), "offline → no schema");
    let diagnostics = diagnostics.snapshot();
    assert!(
        diagnostics.iter().any(|d| matches!(
            d,
            Diagnostic::MissingSchema { kind, k8s_versions_tried, .. }
                if kind == "PodDisruptionBudget"
                    && k8s_versions_tried.contains(&"v1.35.0".to_string())
                    && k8s_versions_tried.contains(&"v1.24.0".to_string())
        )),
        "expected MissingSchema with both versions; got {diagnostics:?}"
    );
    Ok(())
}

#[test]
fn has_resource_does_not_speculatively_download() -> eyre::Result<()> {
    let cache_dir = tmp_dir("k8s-has-resource-no-download")?;
    let mock = Arc::new(MockFetcher::new());
    let provider = KubernetesJsonSchemaProvider::with_versions(K8sVersionChain::new(
        vec![
            "v1.35.0".to_string(),
            "v1.34.0".to_string(),
            "v1.33.0".to_string(),
            "v1.32.0".to_string(),
            "v1.31.0".to_string(),
        ],
        None,
    ))
    .with_cache_dir(cache_dir)
    .with_allow_download(true)
    .with_fetcher(mock.clone());

    let resource = ResourceRef::concrete(
        "policy/v1beta1".to_string(),
        "PodDisruptionBudget".to_string(),
    );
    let owns = provider.has_resource(&resource);
    assert!(!owns, "empty cache + no downloads → has_resource=false");
    sim_assert_eq!(
        have: mock.total_calls(),
        want: 0,
        "has_resource must not trigger any fetches"
    );
    Ok(())
}

#[test]
fn explicit_k8s_version_order_preserved() {
    // No sort: the chain order returned by `K8sVersionChain::ordered`
    // must match the order the user typed.
    let chain = K8sVersionChain::new(vec!["v1.24.0".to_string(), "v1.35.0".to_string()], None);
    sim_assert_eq!(have: chain.ordered(), want: vec!["v1.24.0", "v1.35.0"]);
}

#[test]
fn k8s_version_fallback_chain_derivation() {
    // `auto` with default window expands the single explicit version
    // into a 6-element descending chain.
    let chain = K8sVersionChain::new(vec!["v1.35.0".to_string()], Some(5));
    sim_assert_eq!(
        have: chain.ordered(),
        want: vec![
            "v1.35.0", "v1.34.0", "v1.33.0", "v1.32.0", "v1.31.0", "v1.30.0",
        ]
    );
}

#[test]
fn resolved_from_fallback_version_payload_fields() -> eyre::Result<()> {
    let cache_dir = tmp_dir("k8s-fallback-payload-fields")?;
    let mock = Arc::new(
        MockFetcher::new()
            .with(
                "https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/v1.35.0/poddisruptionbudget-policy-v1beta1.json",
                MockResponse::NotFound,
            )
            .with_body(
                "https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/v1.24.0/poddisruptionbudget-policy-v1beta1.json",
                pdb_v1beta1_doc().into_bytes(),
            ),
    );
    let diagnostics = DiagnosticSink::new();
    let provider = KubernetesJsonSchemaProvider::with_versions(K8sVersionChain::new(
        vec!["v1.35.0".to_string(), "v1.24.0".to_string()],
        None,
    ))
    .with_cache_dir(cache_dir)
    .with_allow_download(true)
    .with_fetcher(mock)
    .with_diagnostic_sink(diagnostics.clone());

    let chain = Chain::new(vec![Box::new(provider)]).with_diagnostic_sink(diagnostics.clone());
    let _ = chain.schema_fragment_for_resource_path(
        &ResourceRef::concrete(
            "policy/v1beta1".to_string(),
            "PodDisruptionBudget".to_string(),
        ),
        &YamlPath(Vec::new()),
    );

    let snapshot = diagnostics.snapshot();
    let payload = snapshot
        .iter()
        .find_map(|d| match d {
            Diagnostic::ResolvedFromFallbackVersion {
                kind,
                api_version,
                primary_version,
                resolved_version,
            } => Some((
                kind.clone(),
                api_version.clone(),
                primary_version.clone(),
                resolved_version.clone(),
            )),
            _ => None,
        })
        .expect("ResolvedFromFallbackVersion must be present");

    sim_assert_eq!(have: payload.0, want: "PodDisruptionBudget");
    sim_assert_eq!(have: payload.1, want: "policy/v1beta1");
    sim_assert_eq!(have: payload.2, want: "v1.35.0");
    sim_assert_eq!(have: payload.3, want: "v1.24.0");
    Ok(())
}

#[test]
fn k8s_negative_cache_per_source_and_version() -> eyre::Result<()> {
    let cache_dir = tmp_dir("k8s-negcache-per-source")?;
    let mirror_url = "https://example.com/mirror";
    let default_url = "https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/v1.35.0/poddisruptionbudget-policy-v1beta1.json";
    let mirror_resource_url =
        format!("{mirror_url}/v1.35.0/poddisruptionbudget-policy-v1beta1.json");
    let mock = Arc::new(
        MockFetcher::new()
            .with(default_url, MockResponse::NotFound)
            .with_body(mirror_resource_url.clone(), pdb_v1beta1_doc().into_bytes()),
    );
    let provider = KubernetesJsonSchemaProvider::with_versions(K8sVersionChain::new(
        vec!["v1.35.0".to_string()],
        None,
    ))
    .with_cache_dir(cache_dir)
    .with_mirrors(vec![mirror_url.to_string()])
    .with_allow_download(true)
    .with_fetcher(mock.clone());

    let resource = ResourceRef::concrete(
        "policy/v1beta1".to_string(),
        "PodDisruptionBudget".to_string(),
    );

    let _ = provider.lookup(&resource, &YamlPath(Vec::new()));
    let _ = provider.lookup(&resource, &YamlPath(Vec::new()));

    sim_assert_eq!(
        have: mock.calls_for(default_url),
        want: 1,
        "default source negative cache prevents retry"
    );
    // Mirror returned 200 on first call; on second call the in-memory
    // cache or on-disk cache short-circuits. Either way, no MORE than
    // one fetch should hit the mirror per lookup (it's read once,
    // cached, reused).
    assert!(
        mock.calls_for(&mirror_resource_url) <= 2,
        "mirror should be fetched at most once per file (first to populate)"
    );
    Ok(())
}

#[test]
fn negative_cache_avoids_retry() -> eyre::Result<()> {
    let cache_dir = tmp_dir("k8s-negative-cache")?;
    let mock = Arc::new(MockFetcher::new().with_default(MockResponse::NotFound));
    let provider = KubernetesJsonSchemaProvider::with_versions(K8sVersionChain::new(
        vec!["v1.35.0".to_string()],
        None,
    ))
    .with_cache_dir(cache_dir)
    .with_allow_download(true)
    .with_fetcher(mock.clone());

    let resource = ResourceRef::concrete(
        "policy/v1beta1".to_string(),
        "PodDisruptionBudget".to_string(),
    );

    let _ = provider.lookup(&resource, &YamlPath(Vec::new()));
    let _ = provider.lookup(&resource, &YamlPath(Vec::new()));
    let _ = provider.lookup(&resource, &YamlPath(Vec::new()));

    // Multiple call sites attempt several filename candidates per
    // resource; what matters is that no URL is fetched twice.
    let counts_unique: std::collections::HashMap<usize, usize> = std::collections::HashMap::new();
    let _ = counts_unique;
    // Spot-check: the canonical filename is fetched at most once.
    let url = "https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/v1.35.0/poddisruptionbudget-policy-v1beta1.json";
    sim_assert_eq!(
        have: mock.calls_for(url),
        want: 1,
        "negative cache must prevent repeat fetches for the same URL"
    );
    Ok(())
}

#[test]
fn persisted_not_found_marker_avoids_cross_process_retry() -> eyre::Result<()> {
    let cache_dir = tmp_dir("k8s-persistent-negative-cache")?;
    let url = "https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/v1.35.0/poddisruptionbudget-policy-v1beta1.json";
    let first_fetcher = Arc::new(MockFetcher::new().with_default(MockResponse::NotFound));
    let first_provider = KubernetesJsonSchemaProvider::with_versions(K8sVersionChain::new(
        vec!["v1.35.0".to_string()],
        None,
    ))
    .with_cache_dir(cache_dir.clone())
    .with_allow_download(true)
    .with_fetcher(first_fetcher.clone());

    let resource = ResourceRef::concrete(
        "policy/v1beta1".to_string(),
        "PodDisruptionBudget".to_string(),
    );

    let _ = first_provider.lookup(&resource, &YamlPath(Vec::new()));
    sim_assert_eq!(
        have: first_fetcher.calls_for(url),
        want: 1,
        "first lookup should fetch once and persist the authoritative 404"
    );

    let second_fetcher = Arc::new(MockFetcher::new().with_default(MockResponse::NotFound));
    let second_provider = KubernetesJsonSchemaProvider::with_versions(K8sVersionChain::new(
        vec!["v1.35.0".to_string()],
        None,
    ))
    .with_cache_dir(cache_dir)
    .with_allow_download(true)
    .with_fetcher(second_fetcher.clone());

    let _ = second_provider.lookup(&resource, &YamlPath(Vec::new()));
    sim_assert_eq!(
        have: second_fetcher.total_calls(),
        want: 0,
        "persisted not-found marker should avoid a cross-process retry"
    );
    Ok(())
}

#[test]
fn no_cache_bypasses_not_found_marker_and_refreshes_positive_schema() -> eyre::Result<()> {
    let cache_dir = tmp_dir("k8s-no-cache-refreshes-negative")?;
    let url = "https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/v1.35.0/poddisruptionbudget-policy-v1beta1.json";
    let stale_fetcher = Arc::new(MockFetcher::new().with_default(MockResponse::NotFound));
    let stale_provider = KubernetesJsonSchemaProvider::with_versions(K8sVersionChain::new(
        vec!["v1.35.0".to_string()],
        None,
    ))
    .with_cache_dir(cache_dir.clone())
    .with_allow_download(true)
    .with_fetcher(stale_fetcher.clone());

    let resource = ResourceRef::concrete(
        "policy/v1beta1".to_string(),
        "PodDisruptionBudget".to_string(),
    );
    let schema_path = k8s_cache_path(
        &cache_dir,
        default_source_id(),
        "v1.35.0",
        "poddisruptionbudget-policy-v1beta1.json",
    );

    let _ = stale_provider.lookup(&resource, &YamlPath(Vec::new()));
    sim_assert_eq!(
        have: stale_fetcher.calls_for(url),
        want: 1,
        "setup should persist an initial authoritative 404"
    );
    assert!(
        not_found_marker_exists(&schema_path),
        "setup should write a persistent not-found marker"
    );

    let refresh_fetcher =
        Arc::new(MockFetcher::new().with_body(url, pdb_v1beta1_doc().into_bytes()));
    let refresh_provider = KubernetesJsonSchemaProvider::with_versions(K8sVersionChain::new(
        vec!["v1.35.0".to_string()],
        None,
    ))
    .with_cache_dir(cache_dir.clone())
    .with_allow_download(true)
    .with_use_cache(false)
    .with_fetcher(refresh_fetcher.clone());

    let refreshed = refresh_provider
        .lookup(&resource, &YamlPath(Vec::new()))
        .into_schema_fragment();
    assert!(
        refreshed.is_some(),
        "--no-cache should bypass the stale not-found marker"
    );
    sim_assert_eq!(
        have: refresh_fetcher.calls_for(url),
        want: 1,
        "--no-cache should make a fresh upstream request"
    );
    assert!(
        !not_found_marker_exists(&schema_path),
        "successful refresh should remove the stale not-found marker"
    );

    let offline_fetcher = Arc::new(MockFetcher::new().with_default(MockResponse::NetworkDisabled));
    let offline_provider = KubernetesJsonSchemaProvider::with_versions(K8sVersionChain::new(
        vec!["v1.35.0".to_string()],
        None,
    ))
    .with_cache_dir(cache_dir)
    .with_allow_download(false)
    .with_fetcher(offline_fetcher);
    assert!(
        offline_provider
            .lookup(&resource, &YamlPath(Vec::new()))
            .into_schema_fragment()
            .is_some(),
        "fresh positive schema should be cached for later normal runs"
    );
    Ok(())
}

#[test]
fn no_cache_authoritative_not_found_clears_stale_positive_schema() -> eyre::Result<()> {
    let cache_dir = tmp_dir("k8s-no-cache-clears-positive")?;
    let url = "https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/v1.35.0/poddisruptionbudget-policy-v1beta1.json";
    let resource = ResourceRef::concrete(
        "policy/v1beta1".to_string(),
        "PodDisruptionBudget".to_string(),
    );

    let positive_fetcher =
        Arc::new(MockFetcher::new().with_body(url, pdb_v1beta1_doc().into_bytes()));
    let positive_provider = KubernetesJsonSchemaProvider::with_versions(K8sVersionChain::new(
        vec!["v1.35.0".to_string()],
        None,
    ))
    .with_cache_dir(cache_dir.clone())
    .with_allow_download(true)
    .with_fetcher(positive_fetcher);
    assert!(
        positive_provider
            .lookup(&resource, &YamlPath(Vec::new()))
            .into_schema_fragment()
            .is_some(),
        "setup should write a positive cache entry"
    );

    let stale_fetcher = Arc::new(MockFetcher::new().with_default(MockResponse::NotFound));
    let stale_provider = KubernetesJsonSchemaProvider::with_versions(K8sVersionChain::new(
        vec!["v1.35.0".to_string()],
        None,
    ))
    .with_cache_dir(cache_dir.clone())
    .with_allow_download(true)
    .with_use_cache(false)
    .with_fetcher(stale_fetcher.clone());
    assert!(
        stale_provider
            .lookup(&resource, &YamlPath(Vec::new()))
            .into_schema_fragment()
            .is_none(),
        "--no-cache should accept the fresh authoritative 404"
    );
    sim_assert_eq!(
        have: stale_fetcher.calls_for(url),
        want: 1,
        "--no-cache should re-check upstream once"
    );

    let offline_fetcher = Arc::new(MockFetcher::new().with_default(MockResponse::NetworkDisabled));
    let offline_provider = KubernetesJsonSchemaProvider::with_versions(K8sVersionChain::new(
        vec!["v1.35.0".to_string()],
        None,
    ))
    .with_cache_dir(cache_dir)
    .with_allow_download(false)
    .with_fetcher(offline_fetcher);
    assert!(
        offline_provider
            .lookup(&resource, &YamlPath(Vec::new()))
            .into_schema_fragment()
            .is_none(),
        "stale positive cache entry should not survive the authoritative 404"
    );
    Ok(())
}