Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 77 additions & 59 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ members = [
"interface-manager",
"k8s-intf",
"k8s-less",
"k8s-sample-crds",
"k8s-sample-derive",
"left-right-tlcache",
"mgmt",
"nat",
Expand Down Expand Up @@ -64,6 +66,8 @@ init = { path = "./init", package = "dataplane-init", features = [] }
interface-manager = { path = "./interface-manager", package = "dataplane-interface-manager", features = [] }
k8s-intf = { path = "./k8s-intf", package = "dataplane-k8s-intf", features = [] }
k8s-less = { path = "./k8s-less", package = "dataplane-k8s-less", features = [] }
k8s-sample-crds = { path = "./k8s-sample-crds", package = "dataplane-k8s-sample-crds", features = [] }
k8s-sample-derive = { path = "./k8s-sample-derive", package = "dataplane-k8s-sample-derive", features = [] }
left-right-tlcache = { path = "./left-right-tlcache", package = "dataplane-left-right-tlcache", features = [] }
lpm = { path = "./lpm", package = "dataplane-lpm", features = [] }
mgmt = { path = "./mgmt", package = "dataplane-mgmt", features = [] }
Expand Down
2 changes: 2 additions & 0 deletions k8s-intf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ lpm = { workspace = true, optional = true }
net = { workspace = true, optional = true, features = ["bolero"] }
tracectl = { workspace = true }
tracing = { workspace = true }
k8s-sample-derive = { workspace = true }

# external
bolero = { workspace = true, optional = true }
Expand All @@ -40,3 +41,4 @@ net = { workspace = true, features = ["bolero", "test_buffer"] }
[build-dependencies]
dotenvy = { workspace = true, features = [] }
ureq = { workspace = true, features = ["rustls", "gzip"] }

4 changes: 3 additions & 1 deletion k8s-intf/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ fn fetch_crd(url: &str) -> String {
const LICENSE_PREAMBLE: &str = "// SPDX-License-Identifier: Apache-2.0
// Copyright Open Network Fabric Authors

use k8s_sample_derive::Sample;
use crate::samplegen::sample::Sample;
";

/// Fixup the types in the generated Rust code
Expand Down Expand Up @@ -75,7 +77,7 @@ fn fixup_types(raw: String) -> String {
fn generate_rust_for_crd(crd_content: &str) -> String {
// Run kopium with stdin input
let mut child = std::process::Command::new("kopium")
.args(["-D", "PartialEq", "-Af", "-"])
.args(["-D", "PartialEq", "-D", "Sample", "-Af", "-"])
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
Expand Down
55 changes: 29 additions & 26 deletions k8s-intf/src/generated/gateway_agent_crd.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions k8s-intf/src/generated/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
#[allow(clippy::all, clippy::pedantic)]
#[rustfmt::skip]
pub mod gateway_agent_crd;

pub use k8s_sample_derive::Sample;
1 change: 1 addition & 0 deletions k8s-intf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
pub mod bolero;
pub mod client;
pub mod generated;
pub mod samplegen;
pub mod utils;

pub mod gateway_agent_crd {
Expand Down
60 changes: 60 additions & 0 deletions k8s-intf/src/samplegen/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Open Network Fabric Authors

pub mod sample;

use crate::gateway_agent_crd::GatewayAgentSpec;
use crate::samplegen::sample::Sample;

use serde_json::to_string_pretty;
use std::fs;
use std::path::PathBuf;

/// Generate sample json and yaml files in the indicated path
///
/// # Errors
/// On error, this function returns a string describing what went wrong.
pub fn generate_samples(path: &str, filename: &str) -> Result<(), String> {
let crd = GatewayAgentSpec::sample();
let json = to_string_pretty(&crd)
.map_err(|e| format!("Failed to serialize CRD as pretty-JSON: {e}"))?;
let yaml = serde_yaml_ng::to_string(&crd)
.map_err(|e| format!("Failed to serialize CRD as YAML: {e}"))?;

let output_dir = PathBuf::from(path);
fs::create_dir_all(&output_dir)
.map_err(|e| format!("Failed to create output directory: {e}"))?;

let mut jsonfile = PathBuf::from(path).join(filename);
jsonfile.set_extension("json");
fs::write(&jsonfile, json).map_err(|e| format!("Failed to write sample JSON file: {e}"))?;

let mut yamlfile = PathBuf::from(path).join(filename);
yamlfile.set_extension("yaml");
fs::write(&yamlfile, yaml).map_err(|e| format!("Failed to write sample YAML file: {e}"))?;

Ok(())
}

#[cfg(test)]
mod test {
use crate::gateway_agent_crd::GatewayAgentSpec;
use crate::samplegen::sample::Sample;
use serde_json::to_string_pretty;

// This basically tests that sample can be built
#[test]
fn test_samples() {
let crd = GatewayAgentSpec::sample();
println!("\n CRD in Rust:\n");
println!("{crd:#?}");

let json = to_string_pretty(&crd).unwrap();
println!("\n CRD in JSON:\n");
println!("{json}");

let yaml = serde_yaml_ng::to_string(&crd).unwrap();
println!("\n CRD in YAML:\n");
println!("{yaml}");
}
}
Loading
Loading