Kubernetes openapi unmangler.
Generates rust structs from customresourcedefinitions
in your kubernetes cluster following the spec/status model, by using their embedded openapi schema.
⚠️ WARNING: not feature complete ⚠️
Requirements:
customresourcedefinition
with schemakube-derive
to provide api integration with kube
description
values in schemamycrd.group.io
or from file via -f crd.yaml
Grab a prebuilt musl/darwin binary from the latest release, or install from crates.io:
sh
cargo install kopium
If you have a crd installed in your cluster, pass a crd name accessible from your KUBECONFIG
:
sh
kopium prometheusrules.monitoring.coreos.com -A > prometheusrule.rs
Or pass a file/stdin via -f
:
sh
curl -sSL https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/main/example/prometheus-operator-crd/monitoring.coreos.com_prometheusrules.yaml \
| kopium -Af - > prometheusrule.rs
```rust use kube::CustomResource; use schemars::JsonSchema; use serde::{Serialize, Deserialize}; use std::collections::BTreeMap; use k8s_openapi::apimachinery::pkg::util::intstr::IntOrString;
/// Specification of desired alerting rule definitions for Prometheus.
pub struct PrometheusRuleSpec {
/// Content of Prometheus rule file
#[serde(default, skipserializingif = "Option::is_none")]
pub groups: Option
/// RuleGroup is a list of sequentially evaluated recording and alerting rules.
pub struct PrometheusRuleGroups {
#[serde(default, skipserializingif = "Option::isnone")]
pub interval: Option
/// Rule describes an alerting or recording rule
pub struct PrometheusRuleGroupsRules {
#[serde(default, skipserializingif = "Option::isnone")]
pub alert: Option
Simply add the generated file (e.g. output from above in prometheusrule.rs
) to your library, and import (at least) the special root type:
```rust use prometheusrule::PrometheusRule; use kube::{Api, Client, ResourceExt};
async fn main() -> Result<(), Box
Autocompletion for most shells available via kopium completions
:
sh
source <(kopium completions bash)
Unit tests and running kopium
from a file do not require a cluster and can be run with:
sh
cargo test --lib
cargo run --bin kopium -- -f mycrd.yaml -A
Full integration tests use your current cluster to try to read a CRD and a gen
object (instance of the CRD type) and parse it into the generated type:
sh
cargo run --bin kopium -- -f prometheusrules.monitoring.coreos.com > tests/gen.rs
echo "pub type CR = PrometheusRule;" >> tests/gen.rs
kubectl apply -f tests/pr.yaml # needs to contain a CR with name "gen"
cargo test --test runner -- --nocapture
test shortcuts available via just
in the justfile
and run pre-merge.
Apache 2.0 licensed. See LICENSE for details.