Skip to content

Commit

Permalink
one feature per group
Browse files Browse the repository at this point in the history
Instead of using one feature for each group/version combination, we now create one feature for each group. This reduces the number of features this crate exposes and requires fewer features to be added for consumers when using multiple kinds from a group.

Relates to #74

Signed-off-by: Sebastian Hoß <[email protected]>
  • Loading branch information
sebhoss committed Nov 21, 2023
1 parent cf16bf9 commit 3fae230
Show file tree
Hide file tree
Showing 1,515 changed files with 3,251 additions and 4,224 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/update-crds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ jobs:
run: ./code-generator/generate.sh
- name: Fix changed CRDs
run: |
for module in $(git diff --name-only origin/main -- ./kube-custom-resources-rs/src | grep --invert-match lib.rs | xargs --no-run-if-empty -I{} dirname {} | sort --unique | xargs --no-run-if-empty -I{} basename {}); do
if [ -f "./kube-custom-resources-rs/src/${module}/mod.rs" ]; then
./code-generator/fix-cargo-warnings.sh "${module}"
for feature in $(git diff --name-only origin/main -- ./kube-custom-resources-rs/src | grep --invert-match lib.rs | xargs --no-run-if-empty -I{} dirname {} | sort --unique | xargs --no-run-if-empty -I{} basename {}); do
if [ -f "./kube-custom-resources-rs/src/${feature}/mod.rs" ]; then
./code-generator/fix-cargo-warnings.sh "${feature}"
fi
done
- id: cpr
Expand Down
7 changes: 3 additions & 4 deletions .github/workflows/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ jobs:
run: cargo check --package code-generator --locked
- name: Check changed CRDs
run: |
for module in $(git diff --name-only origin/main -- ./kube-custom-resources-rs/src | grep --invert-match lib.rs | xargs --no-run-if-empty -I{} dirname {} | sort --unique | xargs --no-run-if-empty -I{} basename {}); do
if [ -f "./kube-custom-resources-rs/src/${module}/mod.rs" ]; then
echo "testing ${module}"
cargo check --lib --package kube-custom-resources-rs --features "${module}" --locked
for feature in $(git diff --name-only origin/main -- ./kube-custom-resources-rs/src | grep --invert-match lib.rs | xargs --no-run-if-empty -I{} dirname {} | sort --unique | xargs --no-run-if-empty -I{} basename {}); do
if [ -f "./kube-custom-resources-rs/src/${feature}/mod.rs" ]; then
./code-generator/test-custom-resources.sh "${feature}"
fi
done
env:
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Replace `<version>` with the latest available [release](https://crates.io/crates

### Features

Each group/version of a Kubernetes custom resource has a corresponding Cargo feature in this crate. The group/version of a custom resource can be seen in the `apiVersion` field of a resource, e.g.:
Each group of a Kubernetes custom resource has a corresponding Cargo feature in this crate. The group of a custom resource can be seen in the `apiVersion` field of a resource, e.g.:

```yaml
apiVersion: cert-manager.io/v1
Expand All @@ -29,14 +29,16 @@ metadata:
...
```
Since Cargo imposes certain rules on how features can be named, `.`, `-`, and `/` are all mapped to `_`. Therefore, the feature that contains the custom resource from the example above is called `cert_manager_io_v1` and can be enabled like this:
In the above example, `cert-manager.io` is the group and `v1` is the version. Since Cargo imposes certain rules on how features can be named, `.`, `-`, and `/` are all mapped to `_`. Therefore, the feature that contains the custom resource from the example above is called `cert_manager_io` and can be enabled like this:

```toml
[dependencies]
kube-custom-resources-rs = { version = "<version>", features = ["cert_manager_io_v1"] }
kube-custom-resources-rs = { version = "<version>", features = ["cert_manager_io"] }
```

Take a look at the [docs](https://docs.rs/kube-custom-resources-rs/latest/kube_custom_resources_rs/) to see all available features and the GVKs they contain.
Each version within a group has a corresponding module in that feature, e.g. there is a module called `v1` in the feature `cert_manager_io`.

Take a look at the [docs](https://docs.rs/kube-custom-resources-rs/latest/kube_custom_resources_rs/) to see all available features and the group/version/kinds they contain.

## Versioning

Expand Down
6 changes: 3 additions & 3 deletions code-generator/adjust-cargo-toml.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
sed -i '/\[features\]/,$d' ./kube-custom-resources-rs/Cargo.toml
echo '[features]' >>./kube-custom-resources-rs/Cargo.toml

find ./kube-custom-resources-rs/src -type f -name 'mod.rs' -print0 | LC_ALL=C sort --zero-terminated | while IFS= read -r -d '' file; do
module=$(basename "$(dirname "${file}")")
echo "${module} = []"
find ./kube-custom-resources-rs/src -maxdepth 2 -type f -name 'mod.rs' -print0 | LC_ALL=C sort --zero-terminated | while IFS= read -r -d '' file; do
feature=$(basename "$(dirname "${file}")")
echo "${feature} = []"
done >> ./kube-custom-resources-rs/Cargo.toml
14 changes: 8 additions & 6 deletions code-generator/create-custom-resources.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ for file in ./crd-catalog/**/*.yaml; do
crd=$(basename "${path}")
version=$(basename "$(dirname "${file}")")
group=$(basename "$(dirname "$(dirname "${file}")")")
rust_crd=$(echo "${crd}" | sed -e 's/\./_/g' -e 's/-/_/g')
rust_group=$(echo "${group}" | sed -e 's/\./_/g' -e 's/-/_/g')
module="${rust_group}_${version}"
resource_filename=$(echo "${crd}" | sed -e 's/\./_/g' -e 's/-/_/g')
cargo_group=$(echo "${group}" | sed -e 's/\./_/g' -e 's/-/_/g')
cargo_feature="${cargo_group}"
feature_directory="./kube-custom-resources-rs/src/${cargo_feature}"
version_directory="${feature_directory}/${version}"

mkdir --parents "./kube-custom-resources-rs/src/${module}"
mkdir --parents "${feature_directory}/${version}"

if [ -f "${args}" ]; then
if ! xargs --arg-file="${args}" --delimiter='\n' kopium --docs --filename="${file}" > "./kube-custom-resources-rs/src/${module}/${rust_crd}.rs"; then
if ! xargs --arg-file="${args}" --delimiter='\n' kopium --docs --filename="${file}" > "${version_directory}/${resource_filename}.rs"; then
echo " error in ${file}"
fi
else
if ! kopium --docs --filename="${file}" --derive=Default --derive=PartialEq > "./kube-custom-resources-rs/src/${module}/${rust_crd}.rs"; then
if ! kopium --docs --filename="${file}" --derive=Default --derive=PartialEq > "${version_directory}/${resource_filename}.rs"; then
echo " error in ${file}"
fi
fi
Expand Down
31 changes: 23 additions & 8 deletions code-generator/create-mod-rs-files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,30 @@
# SPDX-License-Identifier: 0BSD

### Generate mod.rs files
for mld in ./kube-custom-resources-rs/src/*; do
if [ "$(basename "${mld}")" == lib.rs ]; then
for fld in ./kube-custom-resources-rs/src/*; do
if [ "$(basename "${fld}")" == lib.rs ]; then
continue
fi
find "${mld}" -type f -name '*.rs' -not -name 'mod.rs' -print0 | LC_ALL=C sort --zero-terminated | while IFS= read -r -d '' file; do
crd=$(basename "${file%.*}")
echo "pub mod ${crd};"
done > "${mld}/mod.rs"
if [ ! -s "${mld}/mod.rs" ]; then
rm --force "${mld}/mod.rs"

for vld in "${fld}"/*; do
if [ "$(basename "${vld}")" == mod.rs ]; then
continue
fi

find "${vld}" -type f -name '*.rs' -not -name 'mod.rs' -print0 | LC_ALL=C sort --zero-terminated | while IFS= read -r -d '' file; do
crd=$(basename "${file%.*}")
echo "pub mod ${crd};"
done > "${vld}/mod.rs"
if [ ! -s "${vld}/mod.rs" ]; then
rm --force "${vld}/mod.rs"
fi
done

find "${fld}" -mindepth 2 -type f -name 'mod.rs' -print0 | LC_ALL=C sort --zero-terminated | while IFS= read -r -d '' file; do
version=$(basename "$(dirname "${file}")")
echo "pub mod ${version};"
done > "${fld}/mod.rs"
if [ ! -s "${fld}/mod.rs" ]; then
rm --force "${fld}/mod.rs"
fi
done
42 changes: 22 additions & 20 deletions code-generator/src/bin/lib_rs_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ fn main() -> Result<()> {
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:")?;
writeln!(buffer, "Every group has its own feature in this crate. The available features are as follows:")?;

let mut entries: HashMap<String, HashMap<String, Vec<CustomResourceDefinition>>> = HashMap::new();

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) => {
Expand All @@ -45,14 +46,16 @@ fn main() -> Result<()> {
let feature = group.replace(".", "_").replace("-", "_");

let resource_target = format!(
"{}/{}_{}/{}.rs",
"{}/{}/{}/{}.rs",
sources,
feature,
version,
crd.spec.names.plural.replace(".", "_").replace("-", "_")
);
if Path::new(&resource_target).exists() {
crds.entry(format!("{}/{}", group, version))
entries.entry(group.to_string())
.or_insert_with(HashMap::new)
.entry(version.to_string())
.or_insert_with(Vec::new)
.push(crd);
}
Expand All @@ -61,32 +64,31 @@ fn main() -> Result<()> {
}
}

for (api_version, kinds) in crds.iter().sorted_by_key(|x| x.0) {
let feature = api_version
for (group, versions) in entries.iter().sorted_by_key(|x| x.0) {
let feature = group
.replace(".", "_")
.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)?;
for (version, kinds) in versions.iter().sorted_by_key(|x| x.0) {
writeln!(buffer, "")?;
writeln!(buffer, "- apiVersion: `{}/{}`", group, version)?;
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
for (group, _) in entries.iter().sorted_by_key(|x| x.0) {
let feature = group
.replace(".", "_")
.replace("-", "_")
.replace("/", "_");

.replace("-", "_");
writeln!(buffer, "#[cfg(feature = \"{}\")]", feature)?;
writeln!(buffer, "pub mod {};", feature)?;
}
Expand Down
Loading

0 comments on commit 3fae230

Please sign in to comment.