-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema_validations.rs
More file actions
98 lines (87 loc) · 3.86 KB
/
schema_validations.rs
File metadata and controls
98 lines (87 loc) · 3.86 KB
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
//! OpenAPI Schema Validation Example
//!
//! This example demonstrates how to use OpenAPI schema validation with the fake client.
//! Validation is most useful when working with dynamically-generated or loaded resources
//! (e.g., from YAML files), as Rust's type system already catches most structural errors
//! at compile time for statically-typed code.
//!
//! Run with: cargo run --example schema_validations --features validation
//! Run tests: cargo test --example schema_validations --features validation
#![cfg(feature = "validation")]
use k8s_openapi::api::core::v1::{Container, Pod, PodSpec, Service, ServicePort, ServiceSpec};
use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;
use kube::Api;
use kube_fake_client::ClientBuilder;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("=== OpenAPI Schema Validation Example ===\n");
// Create client with schema validation
let client = ClientBuilder::new()
.with_schema_validation_file("kubernetes/api/openapi/swagger.json")?
.with_validation_for("/v1/Pod")?
.with_validation_for("/v1/Service")?
.build()
.await?;
println!("Creating Pod...");
let pods: Api<Pod> = Api::namespaced(client.clone(), "default");
let pod = Pod {
metadata: ObjectMeta {
name: Some("example-pod".to_string()),
namespace: Some("default".to_string()),
..Default::default()
},
spec: Some(PodSpec {
containers: vec![Container {
name: "nginx".to_string(),
image: Some("nginx:latest".to_string()),
..Default::default()
}],
..Default::default()
}),
..Default::default()
};
pods.create(&Default::default(), &pod).await?;
println!("✓ Valid pod created successfully\n");
println!("Creating Service...");
let services: Api<Service> = Api::namespaced(client, "default");
let service = Service {
metadata: ObjectMeta {
name: Some("example-service".to_string()),
namespace: Some("default".to_string()),
..Default::default()
},
spec: Some(ServiceSpec {
ports: Some(vec![ServicePort {
port: 80,
..Default::default()
}]),
..Default::default()
}),
..Default::default()
};
services.create(&Default::default(), &service).await?;
println!("✓ Valid service created successfully\n");
println!("=== Validation Notes ===\n");
println!("The validator enforces OpenAPI schema validation including:");
println!(" ✓ Type checking (string, number, object, array, etc.)");
println!(" ✓ Required field validation");
println!(" ✓ Structural validation (object shapes, array items)");
println!();
println!("IMPORTANT: Kubernetes built-in resource schemas do NOT include");
println!("value constraint definitions (minLength, pattern, min/max, etc.).");
println!("They rely on admission controllers to enforce those at runtime.");
println!();
println!("This validator is useful for:");
println!(" • Validating dynamically loaded resources (e.g., from YAML)");
println!(" • Catching structural errors before submission");
println!(" • Type validation when using untyped JSON");
println!(" • Testing that resources match the expected schema structure");
println!();
println!("=== Key Features ===\n");
println!(" ✓ Simple API - just specify the swagger file path");
println!(" ✓ Selective validation - enable only resources you need");
println!(" ✓ Lazy schema compilation - schemas compiled on first validation");
println!(" ✓ Validation errors show exactly what's wrong");
println!(" ✓ Works with any OpenAPI file (Kubernetes built-in or CRDs)");
Ok(())
}