Skip to content

Commit

Permalink
generate docs in lib.rs
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Hoß <[email protected]>
  • Loading branch information
sebhoss committed Nov 5, 2023
1 parent 9371b33 commit ae3c352
Show file tree
Hide file tree
Showing 8 changed files with 2,634 additions and 12 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/update-crds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ jobs:
run: cargo run --package code-generator --bin dep5_generator
- name: Fixup YAML
run: find ./crd-catalog -name 'fixup.sh' -type f -exec {} \;
- name: Generate Code
- name: Generate Resources
run: ./code-generator/generate.sh
- name: Generate lib.rs
run: cargo run --package code-generator --bin lib_rs_generator
- id: cpr
name: Create Pull Request
uses: peter-evans/create-pull-request@v5
Expand Down
17 changes: 17 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ SPDX-License-Identifier: 0BSD

This repository contains [kube-rs](https://kube.rs/) compatible bindings for Kubernetes [custom resources](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/). Each binding is generated with [kopium](https://github.com/kube-rs/kopium) and updated weekly.

Feel free to add your own CRD to the [catalog](code-generator/src/catalog.rs)!
Feel free to add your own CRD to the [catalog](https://github.com/metio/kube-custom-resources-rs/blob/main/code-generator/src/catalog.rs)!

## Installation

Expand Down
2 changes: 2 additions & 0 deletions code-generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ publish = false
reqwest = { version = "0.11.22", default-features = false, features = ["blocking", "rustls-tls"] }
k8s-openapi = { version = "0.20.0", features = ["latest"] }
serde_yaml = { version = "0.9.27" }
glob = { version = "0.3.1" }
itertools = { version = "0.11.0" }
5 changes: 1 addition & 4 deletions code-generator/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -260,17 +260,14 @@ for file in $(find ./crd-catalog -name '*.yaml' -type f | LC_ALL=C sort --genera
done


### Adjust Cargo.toml and src/lib.rs
### Adjust Cargo.toml
sed -i '/\[features\]/,$d' ./kube-custom-resources-rs/Cargo.toml
echo '[features]' >>./kube-custom-resources-rs/Cargo.toml
rm --force ./kube-custom-resources-rs/src/lib.rs

for mld in $(find ./kube-custom-resources-rs/src -type d | LC_ALL=C sort --general-numeric-sort); do
module=$(basename "${mld}")

if [ -f "${mld}/mod.rs" ]; then
echo "#[cfg(feature = \"${module}\")]" >> ./kube-custom-resources-rs/src/lib.rs
echo "pub mod ${module};" >> ./kube-custom-resources-rs/src/lib.rs
echo "${module} = []" >>./kube-custom-resources-rs/Cargo.toml
fi
done
Expand Down
95 changes: 95 additions & 0 deletions code-generator/src/bin/lib_rs_generator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// SPDX-FileCopyrightText: The kube-custom-resources-rs Authors
// SPDX-License-Identifier: 0BSD

use std::collections::HashMap;
use std::fs;
use std::fs::OpenOptions;
use std::io::BufWriter;
use std::io::{Result, Write};
use std::path::Path;

use glob::glob;
use itertools::Itertools;
use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::CustomResourceDefinition;

fn main() -> Result<()> {
let root = concat!(env!("CARGO_MANIFEST_DIR"), "/..");
let crd_catalog = format!("{}/crd-catalog", root);
let sources = format!("{}/kube-custom-resources-rs/src", root);
let lib_rs_file = format!("{}/lib.rs", sources);
let file = OpenOptions::new()
.write(true)
.truncate(true)
.open(lib_rs_file)
.expect("unable to open file");
let mut buffer = BufWriter::new(file);

writeln!(buffer, "/*!")?;
writeln!(buffer, "This crate contains [kube-rs](https://kube.rs/) compatible bindings for Kubernetes [custom resources](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/). Each binding is generated with [kopium](https://github.com/kube-rs/kopium) and updated weekly.")?;
writeln!(buffer, "")?;
writeln!(buffer, "# Available Features")?;
writeln!(buffer, "")?;
writeln!(buffer, "Every group/version combination is its own feature in this crate. The available features are as follows:")?;

let yaml_files = format!("{}/**/*.yaml", crd_catalog);
let mut crds: HashMap<String, Vec<CustomResourceDefinition>> = HashMap::new();
for entry in glob(&yaml_files).expect("Failed to read glob pattern") {
match entry {
Ok(path) => {
let content = fs::read_to_string(path).expect("should be able to read file");
let crd = serde_yaml::from_str::<CustomResourceDefinition>(&content)
.expect("should be able to parse YAML");

let group = &crd.spec.group;
let version = &crd.spec.versions[0].name;
let feature = group.replace(".", "_").replace("-", "_");

let resource_target = format!(
"{}/{}_{}/{}.rs",
sources,
feature,
version,
crd.spec.names.plural.replace(".", "_").replace("-", "_")
);
if Path::new(&resource_target).exists() {
crds.entry(format!("{}/{}", group, version))
.or_insert_with(Vec::new)
.push(crd);
}
}
Err(e) => println!("{:?}", e),
}
}

for (api_version, kinds) in crds.iter().sorted_by_key(|x| x.0) {
let feature = api_version
.replace(".", "_")
.replace("-", "_")
.replace("/", "_");

writeln!(buffer, "")?;
writeln!(buffer, "## {}", feature)?;
writeln!(buffer, "")?;
writeln!(buffer, "apiVersion: `{}`", api_version)?;
writeln!(buffer, "")?;
writeln!(buffer, "kinds:")?;

for crd in kinds {
writeln!(buffer, "- `{}`", crd.spec.names.kind)?;
}
}
writeln!(buffer, " */")?;
writeln!(buffer, "")?;

for (api_version, _) in crds.iter().sorted_by_key(|x| x.0) {
let feature = api_version
.replace(".", "_")
.replace("-", "_")
.replace("/", "_");

writeln!(buffer, "#[cfg(feature = \"{}\")]", feature)?;
writeln!(buffer, "pub mod {};", feature)?;
}

Ok(())
}
4 changes: 0 additions & 4 deletions kube-custom-resources-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ homepage = "https://github.com/metio/kube-custom-resources-rs/"
repository = "https://github.com/metio/kube-custom-resources-rs"
readme = "../README.md"

[package.metadata.docs.rs]
# see https://docs.rs/about/metadata
all-features = true

[dependencies]
kube = { version = "0.87.1", features = ["derive"] }
k8s-openapi = { version = "0.20.0", features = ["latest"] }
Expand Down
Loading

0 comments on commit ae3c352

Please sign in to comment.