Skip to content
Merged
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
5 changes: 4 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ codegen-fds:
codegen: codegen-fds
#!/usr/bin/env bash
set -exuo pipefail
rm -rf k8s-pb/src && mkdir k8s-pb/src
# src/lib.rs must exist to `cargo run`
touch k8s-pb/src/lib.rs
cd k8s-pb-codegen
rm -rf out/ && mkdir out
rm -rf tmp/ && mkdir tmp
cargo run
1 change: 1 addition & 0 deletions k8s-pb-codegen/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
protos.fds
tmp/
48 changes: 44 additions & 4 deletions k8s-pb-codegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ use prost_types::FileDescriptorSet;
#[macro_use]
extern crate log;
use anyhow::{Context, Result};
use std::{collections::HashMap, fs, path::Path};
use std::{
collections::{BTreeSet, HashMap},
fs,
path::Path,
};

use k8s_pb_codegen::Resource;

Expand All @@ -22,10 +26,12 @@ fn main() -> Result<()> {
debug!("protos: {:?}", protos);

info!("building protos");
let target_dir = root.join("../k8s-pb/src");
let tmp_dir = root.join("./tmp");
prost_build::Config::new()
// should probably switch to this
//.btree_map(&["."])
.out_dir("./out")
.out_dir(&tmp_dir)
.compile_protos(protos.as_slice(), &["protos/"])?;

info!("loading json");
Expand All @@ -37,6 +43,10 @@ fn main() -> Result<()> {
std::fs::read(root.join("protos.fds")).with_context(|| "read protos.fds".to_string())?;
let fds = FileDescriptorSet::decode(&*buf).unwrap(); // pulls in proto::Message

// Map of module path to the names of child modules.
let mut module_tree: HashMap<String, BTreeSet<String>> = HashMap::new();
// Generated packages
let mut pkgs: Vec<String> = Vec::new();
// 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 {
Expand All @@ -50,10 +60,40 @@ fn main() -> Result<()> {
}
}
}

let mut parts = pkg.split('.').collect::<Vec<_>>();
while !parts.is_empty() {
let module = parts.pop().unwrap();
let parent = parts.join("/");
module_tree
.entry(parent)
.or_insert_with(BTreeSet::new)
.insert(module.to_owned());
}
pkgs.push(pkg);
}
}

// 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.
for (k, mods) in module_tree {
let dst = target_dir.join(if k.is_empty() {
"lib.rs".to_owned()
} else {
format!("{}/mod.rs", k)
});
std::fs::create_dir_all(dst.parent().unwrap())?;
let lines = mods
.into_iter()
.map(|m| format!("pub mod {};", m))
.collect::<Vec<_>>();
std::fs::write(dst, lines.join("\n") + "\n")?;
}

for pkg in pkgs {
let src = tmp_dir.join(format!("{}.rs", &pkg));
let dst = target_dir.join(format!("{}/mod.rs", pkg.replace('.', "/")));
std::fs::create_dir_all(dst.parent().unwrap())?;
std::fs::rename(src, dst)?;
}

Ok(())
}
1 change: 1 addition & 0 deletions k8s-pb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
prost = "0.8.0"
2 changes: 2 additions & 0 deletions k8s-pb/src/api/admission/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod v1;
pub mod v1beta1;
2 changes: 2 additions & 0 deletions k8s-pb/src/api/admissionregistration/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod v1;
pub mod v1beta1;
1 change: 1 addition & 0 deletions k8s-pb/src/api/apiserverinternal/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod v1alpha1;
3 changes: 3 additions & 0 deletions k8s-pb/src/api/apps/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod v1;
pub mod v1beta1;
pub mod v1beta2;
File renamed without changes.
2 changes: 2 additions & 0 deletions k8s-pb/src/api/authentication/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod v1;
pub mod v1beta1;
2 changes: 2 additions & 0 deletions k8s-pb/src/api/authorization/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod v1;
pub mod v1beta1;
3 changes: 3 additions & 0 deletions k8s-pb/src/api/autoscaling/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod v1;
pub mod v2beta1;
pub mod v2beta2;
2 changes: 2 additions & 0 deletions k8s-pb/src/api/batch/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod v1;
pub mod v1beta1;
File renamed without changes.
2 changes: 2 additions & 0 deletions k8s-pb/src/api/certificates/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod v1;
pub mod v1beta1;
2 changes: 2 additions & 0 deletions k8s-pb/src/api/coordination/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod v1;
pub mod v1beta1;
1 change: 1 addition & 0 deletions k8s-pb/src/api/core/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod v1;
File renamed without changes.
2 changes: 2 additions & 0 deletions k8s-pb/src/api/discovery/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod v1;
pub mod v1beta1;
2 changes: 2 additions & 0 deletions k8s-pb/src/api/events/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod v1;
pub mod v1beta1;
File renamed without changes.
1 change: 1 addition & 0 deletions k8s-pb/src/api/extensions/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod v1beta1;
2 changes: 2 additions & 0 deletions k8s-pb/src/api/flowcontrol/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod v1alpha1;
pub mod v1beta1;
1 change: 1 addition & 0 deletions k8s-pb/src/api/imagepolicy/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod v1alpha1;
22 changes: 22 additions & 0 deletions k8s-pb/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pub mod admission;
pub mod admissionregistration;
pub mod apiserverinternal;
pub mod apps;
pub mod authentication;
pub mod authorization;
pub mod autoscaling;
pub mod batch;
pub mod certificates;
pub mod coordination;
pub mod core;
pub mod discovery;
pub mod events;
pub mod extensions;
pub mod flowcontrol;
pub mod imagepolicy;
pub mod networking;
pub mod node;
pub mod policy;
pub mod rbac;
pub mod scheduling;
pub mod storage;
2 changes: 2 additions & 0 deletions k8s-pb/src/api/networking/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod v1;
pub mod v1beta1;
3 changes: 3 additions & 0 deletions k8s-pb/src/api/node/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod v1;
pub mod v1alpha1;
pub mod v1beta1;
File renamed without changes.
2 changes: 2 additions & 0 deletions k8s-pb/src/api/policy/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod v1;
pub mod v1beta1;
File renamed without changes.
3 changes: 3 additions & 0 deletions k8s-pb/src/api/rbac/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod v1;
pub mod v1alpha1;
pub mod v1beta1;
File renamed without changes.
3 changes: 3 additions & 0 deletions k8s-pb/src/api/scheduling/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod v1;
pub mod v1alpha1;
pub mod v1beta1;
3 changes: 3 additions & 0 deletions k8s-pb/src/api/storage/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod v1;
pub mod v1alpha1;
pub mod v1beta1;
File renamed without changes.
1 change: 1 addition & 0 deletions k8s-pb/src/apiextensions_apiserver/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod pkg;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod v1;
pub mod v1beta1;
1 change: 1 addition & 0 deletions k8s-pb/src/apiextensions_apiserver/pkg/apis/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod apiextensions;
1 change: 1 addition & 0 deletions k8s-pb/src/apiextensions_apiserver/pkg/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod apis;
1 change: 1 addition & 0 deletions k8s-pb/src/apimachinery/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod pkg;
1 change: 1 addition & 0 deletions k8s-pb/src/apimachinery/pkg/api/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod resource;
2 changes: 2 additions & 0 deletions k8s-pb/src/apimachinery/pkg/apis/meta/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod v1;
pub mod v1beta1;
2 changes: 2 additions & 0 deletions k8s-pb/src/apimachinery/pkg/apis/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod meta;
pub mod testapigroup;
1 change: 1 addition & 0 deletions k8s-pb/src/apimachinery/pkg/apis/testapigroup/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod v1;
4 changes: 4 additions & 0 deletions k8s-pb/src/apimachinery/pkg/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod api;
pub mod apis;
pub mod runtime;
pub mod util;
1 change: 1 addition & 0 deletions k8s-pb/src/apimachinery/pkg/util/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod intstr;
1 change: 1 addition & 0 deletions k8s-pb/src/kube_aggregator/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod pkg;
2 changes: 2 additions & 0 deletions k8s-pb/src/kube_aggregator/pkg/apis/apiregistration/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod v1;
pub mod v1beta1;
1 change: 1 addition & 0 deletions k8s-pb/src/kube_aggregator/pkg/apis/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod apiregistration;
1 change: 1 addition & 0 deletions k8s-pb/src/kube_aggregator/pkg/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod apis;
53 changes: 5 additions & 48 deletions k8s-pb/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,5 @@
pub mod api {
pub mod v1 {
pub mod core {
// include!(concat!(env!("OUT_DIR"), "/api.core.v1.rs"));
}
}
}

pub mod apimachinery {
pub mod pkg {
pub mod api {
pub mod resource {
// include!(concat!(
// env!("OUT_DIR"),
// "/apimachinery.pkg.api.resource.rs"
// ));
}
}

pub mod apis {
pub mod meta {
pub mod v1 {
// include!(concat!(
// env!("OUT_DIR"),
// "/apimachinery.pkg.apis.meta.v1.rs"
// ));
}
}
}

pub mod runtime {
// include!(concat!(env!("OUT_DIR"), "/apimachinery.pkg.runtime.rs"));

pub mod schema {
// include!(concat!(
// env!("OUT_DIR"),
// "/apimachinery.pkg.runtime.schema.rs"
// ));
}
}

pub mod util {
pub mod intstr {
// include!(concat!(env!("OUT_DIR"), "/apimachinery.pkg.util.intstr.rs"));
}
}
}
}
pub mod api;
pub mod apiextensions_apiserver;
pub mod apimachinery;
pub mod kube_aggregator;
pub mod metrics;
1 change: 1 addition & 0 deletions k8s-pb/src/metrics/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod pkg;
2 changes: 2 additions & 0 deletions k8s-pb/src/metrics/pkg/apis/custom_metrics/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod v1beta1;
pub mod v1beta2;
1 change: 1 addition & 0 deletions k8s-pb/src/metrics/pkg/apis/external_metrics/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod v1beta1;
2 changes: 2 additions & 0 deletions k8s-pb/src/metrics/pkg/apis/metrics/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod v1alpha1;
pub mod v1beta1;
3 changes: 3 additions & 0 deletions k8s-pb/src/metrics/pkg/apis/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod custom_metrics;
pub mod external_metrics;
pub mod metrics;
1 change: 1 addition & 0 deletions k8s-pb/src/metrics/pkg/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod apis;