diff --git a/k8s-pb-codegen/Cargo.toml b/k8s-pb-codegen/Cargo.toml index 55195e0..e7d268d 100644 --- a/k8s-pb-codegen/Cargo.toml +++ b/k8s-pb-codegen/Cargo.toml @@ -8,17 +8,11 @@ license = "Apache-2.0" name = "pbcodegen" path = "pbcodegen.rs" -[lib] -name = "pbcodegen" -path = "src/lib.rs" - [dependencies] bytes = "1.0.1" prost = "0.8.0" prost-build = "0.8.0" prost-types = "0.8.0" -serde_json = "1.0.67" -serde = { version = "1.0.130", features = ["derive"] } log = "0.4.14" anyhow = "1.0.44" env_logger = "0.9.0" diff --git a/k8s-pb-codegen/out/api.apps.v1.rs b/k8s-pb-codegen/out/api.apps.v1.rs index d0e7388..0859778 100644 --- a/k8s-pb-codegen/out/api.apps.v1.rs +++ b/k8s-pb-codegen/out/api.apps.v1.rs @@ -737,8 +737,3 @@ pub struct StatefulSetUpdateStrategy { #[prost(message, optional, tag="2")] pub rolling_update: ::core::option::Option, } -// TODO generics for controllerrevisions apps/v1 -// TODO generics for daemonsets apps/v1 -// TODO generics for deployments apps/v1 -// TODO generics for replicasets apps/v1 -// TODO generics for statefulsets apps/v1 \ No newline at end of file diff --git a/k8s-pb-codegen/out/api.autoscaling.v1.rs b/k8s-pb-codegen/out/api.autoscaling.v1.rs index 56273f6..f6d3832 100644 --- a/k8s-pb-codegen/out/api.autoscaling.v1.rs +++ b/k8s-pb-codegen/out/api.autoscaling.v1.rs @@ -482,4 +482,3 @@ pub struct ScaleStatus { #[prost(string, optional, tag="2")] pub selector: ::core::option::Option<::prost::alloc::string::String>, } -// TODO generics for horizontalpodautoscalers autoscaling/v1 \ No newline at end of file diff --git a/k8s-pb-codegen/out/api.autoscaling.v2beta1.rs b/k8s-pb-codegen/out/api.autoscaling.v2beta1.rs index 5be4516..c0381c0 100644 --- a/k8s-pb-codegen/out/api.autoscaling.v2beta1.rs +++ b/k8s-pb-codegen/out/api.autoscaling.v2beta1.rs @@ -459,4 +459,3 @@ pub struct ResourceMetricStatus { #[prost(message, optional, tag="3")] pub current_average_value: ::core::option::Option, } -// TODO generics for horizontalpodautoscalers autoscaling/v2beta1 \ No newline at end of file diff --git a/k8s-pb-codegen/out/api.autoscaling.v2beta2.rs b/k8s-pb-codegen/out/api.autoscaling.v2beta2.rs index 4bc0e69..73fa247 100644 --- a/k8s-pb-codegen/out/api.autoscaling.v2beta2.rs +++ b/k8s-pb-codegen/out/api.autoscaling.v2beta2.rs @@ -483,4 +483,3 @@ pub struct ResourceMetricStatus { #[prost(message, optional, tag="2")] pub current: ::core::option::Option, } -// TODO generics for horizontalpodautoscalers autoscaling/v2beta2 \ No newline at end of file diff --git a/k8s-pb-codegen/out/api.batch.v1.rs b/k8s-pb-codegen/out/api.batch.v1.rs index 28a3a62..63bdf03 100644 --- a/k8s-pb-codegen/out/api.batch.v1.rs +++ b/k8s-pb-codegen/out/api.batch.v1.rs @@ -348,5 +348,3 @@ pub struct UncountedTerminatedPods { #[prost(string, repeated, tag="2")] pub failed: ::prost::alloc::vec::Vec<::prost::alloc::string::String>, } -// TODO generics for cronjobs batch/v1 -// TODO generics for jobs batch/v1 \ No newline at end of file diff --git a/k8s-pb-codegen/out/api.batch.v1beta1.rs b/k8s-pb-codegen/out/api.batch.v1beta1.rs index 55901bc..4bfa095 100644 --- a/k8s-pb-codegen/out/api.batch.v1beta1.rs +++ b/k8s-pb-codegen/out/api.batch.v1beta1.rs @@ -114,4 +114,3 @@ pub struct JobTemplateSpec { #[prost(message, optional, tag="2")] pub spec: ::core::option::Option, } -// TODO generics for cronjobs batch/v1beta1 \ No newline at end of file diff --git a/k8s-pb-codegen/out/api.policy.v1.rs b/k8s-pb-codegen/out/api.policy.v1.rs index f689e80..e091228 100644 --- a/k8s-pb-codegen/out/api.policy.v1.rs +++ b/k8s-pb-codegen/out/api.policy.v1.rs @@ -123,4 +123,3 @@ pub struct PodDisruptionBudgetStatus { #[prost(message, repeated, tag="7")] pub conditions: ::prost::alloc::vec::Vec, } -// TODO generics for poddisruptionbudgets policy/v1 \ No newline at end of file diff --git a/k8s-pb-codegen/out/api.policy.v1beta1.rs b/k8s-pb-codegen/out/api.policy.v1beta1.rs index d5957bd..3e5927f 100644 --- a/k8s-pb-codegen/out/api.policy.v1beta1.rs +++ b/k8s-pb-codegen/out/api.policy.v1beta1.rs @@ -415,5 +415,3 @@ pub struct SupplementalGroupsStrategyOptions { #[prost(message, repeated, tag="2")] pub ranges: ::prost::alloc::vec::Vec, } -// TODO generics for poddisruptionbudgets policy/v1beta1 -// TODO generics for podsecuritypolicies policy/v1beta1 \ No newline at end of file diff --git a/k8s-pb-codegen/pbcodegen.rs b/k8s-pb-codegen/pbcodegen.rs index 97c8ae2..dd8e3d3 100644 --- a/k8s-pb-codegen/pbcodegen.rs +++ b/k8s-pb-codegen/pbcodegen.rs @@ -1,14 +1,7 @@ -use prost::Message; -use prost_types::{FileDescriptorProto, FileDescriptorSet}; #[macro_use] extern crate log; use anyhow::{Context, Result}; -use pbcodegen::*; -use std::{ - fs, - io::Write, - path::{Path, PathBuf}, -}; +use std::{fs, path::Path}; fn read_file>(path: P) -> Result { Ok(fs::read_to_string(&path).with_context(|| format!("read {}", path.as_ref().display()))?) @@ -31,55 +24,6 @@ fn main() -> Result<()> { .out_dir("./out") .compile_protos(protos.as_slice(), &["protos/"])?; - info!("loading api-resources"); - let apif: String = read_file(root.join("openapi/api-resources.json"))?; - let apis: Vec = - serde_json::from_str(&apif).with_context(|| "parse api-resources.json".to_string())?; - - let buf = - std::fs::read(root.join("protos.fds")).with_context(|| "read protos.fds".to_string())?; - let fds = FileDescriptorSet::decode(&*buf).unwrap(); // pulls in proto::Message - - // NB: FDS fields: https://github.com/tokio-rs/prost/blob/32bc87cd0b7301f6af1a338e9afd7717d0f42ca9/prost-types/src/protobuf.rs#L1-L7 - // FDS usage: https://github.com/tokio-rs/prost/blob/32bc87cd0b7301f6af1a338e9afd7717d0f42ca9/prost-build/src/lib.rs#L765-L825 - for f in fds.file { - if let Some(pkg) = f.package { - let pkgpath = Path::new("./out").join(format!("{}.rs", pkg)); - // match the pkg name to a key in api-resources.json - info!("generating {}", pkg); - if let Some(apikey) = pkgname_to_api_key(&pkg) { - let mut codegen = vec![]; - // find the corresponding apiGroupVersion - if let Some(apigr) = apis.iter().find(|ag| ag.apiGroupVersion == apikey) { - for dp in f.message_type { - if let Some(name) = dp.name { - let children: Vec = - apigr.resources.iter().map(|gr| gr.kind.clone()).collect(); - info!(" - found {:?} in {}", children, apikey); - if name.ends_with("List") { - continue; - } - // find the inner resource with matching kind name - if let Some(api) = apigr.resources.iter().find(|gr| name == gr.kind) { - codegen.push(api.generics()); - } else { - debug!(" - no-generics for {}/{} (not in {})", pkg, name, apikey); - } - } - } - } else { - warn!("skipped: {} (didn't find {})", pkg, apikey); - } - let generics = codegen.join("\n"); - let mut file = fs::OpenOptions::new() - .write(true) - .append(true) - .open(&pkgpath)?; - file.write(generics.as_bytes())?; - } - } - } - // Generate code in `src/` by reading files in `OUT_DIR`? // Need to create `mod.rs` file for each level based on the filename, and write generated code in correct file. Ok(()) diff --git a/k8s-pb-codegen/src/lib.rs b/k8s-pb-codegen/src/lib.rs deleted file mode 100644 index 5fcf16b..0000000 --- a/k8s-pb-codegen/src/lib.rs +++ /dev/null @@ -1,65 +0,0 @@ -// codify structs in api-resource.json -// this is the root struct (we have a vector of them) -#[derive(serde::Deserialize)] -pub struct GenApiGroupResources { - pub apiGroupVersion: String, - pub resources: Vec, -} -// main resource struct -#[derive(serde::Deserialize)] -pub struct GenApiResource { - /// plural name - pub name: String, - #[serde(default)] - pub namespaced: bool, - pub subresource: bool, - /// apigroup/ver - pub apiGroupVersion: String, - /// apigroup - pub group: String, - /// ver - pub version: String, - pub kind: String, - /// expected module path :: delimited - pub rust: String, - /// allowed verbs - pub verbs: Vec, - pub scopedVerbs: ScopedVerbs, - /// vec of found root paths - /// - /// "/apis/apps/v1/controllerrevisions", - /// "/apis/apps/v1/namespaces/{namespace}/controllerrevisions", - /// "/apis/apps/v1/namespaces/{namespace}/controllerrevisions/{name}" - pub paths: Vec, -} -#[derive(serde::Deserialize)] -pub struct ScopedVerbs { - #[serde(default)] - pub all: Vec, - #[serde(default)] - pub namespaced: Vec, -} - -impl GenApiResource { - pub fn generics(&self) -> String { - // TODO: use codegen crate here ? - format!("// generics for {} {}", self.name, self.apiGroupVersion) - } -} - -pub fn pkgname_to_api_key(pkg: &str) -> Option { - // TODO: this function is dumb. we probably need to have a better key in the root object than apiGroupVersion - // otherwise we'd have to match up weird paths like api.storage.v1 -> storage.k8s.io/v1 - if let Some((catpth, ver)) = pkg.rsplit_once(".") { - if let Some((category, pth)) = catpth.split_once(".") { - match category { - "api" => Some(format!("{}/{}", pth, ver)), - _ => None, - } - } else { - None - } - } else { - None - } -}