31 个版本 (19 个重大更新)

0.20.1 2024年5月28日
0.19.0 2024年5月1日
0.17.2 2024年3月16日
0.16.2 2023年11月7日
0.2.1 2021年11月29日

#142 in 开发工具

Download history 273/week @ 2024-04-28 91/week @ 2024-05-05 35/week @ 2024-05-12 205/week @ 2024-05-19 210/week @ 2024-05-26 133/week @ 2024-06-02 172/week @ 2024-06-09 199/week @ 2024-06-16 302/week @ 2024-06-23 301/week @ 2024-06-30 169/week @ 2024-07-07 127/week @ 2024-07-14 213/week @ 2024-07-21 270/week @ 2024-07-28 162/week @ 2024-08-04 148/week @ 2024-08-11

805 每月下载量

Apache-2.0

140KB
2K SLoC

kopium

CI Crates.io dependency status

Kubernetes openapi unmangler.

根据规范/状态模型,使用嵌入的 openapi 架构从您的 Kubernetes 集群中的 customresourcedefinitions 生成 Rust 结构。

⚠️ 警告: 功能尚不完整 ⚠️

要求

功能

  • 即时查询:生成的类型使用 kube-derive 提供与 kube 的 api 集成
  • Rust 文档注释:可选地从模式中的 description 值中提取
  • 安全的转换:生成的类型使用 Rust 标准的 camelCase,偶尔使用 serde 重命名属性
  • 本地和 CI 中可用:可以通过 mycrd.group.io 或通过文件 -f crd.yaml 读取集群中的 crd

安装

直接获取预构建的 版本 / 通过 binstall,或者从 crates.io 安装

cargo install kopium # from src
cargo binstall kopium # from release

使用方法

如果您在集群中安装了 crd,请传递可从您的 KUBECONFIG 访问的 crd 名称

kopium prometheusrules.monitoring.coreos.com -A > prometheusrule.rs

或通过 -f 传递文件/标准输入

curl -sSL https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/main/example/prometheus-operator-crd/monitoring.coreos.com_prometheusrules.yaml \
    | kopium -Af - > prometheusrule.rs

输出

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.
#[derive(CustomResource, Serialize, Deserialize, Clone, Debug, JsonSchema)]
#[kube(group = "monitoring.coreos.com", version = "v1", kind = "PrometheusRule", plural = "prometheusrules")]
#[kube(namespaced)]
pub struct PrometheusRuleSpec {
    /// Content of Prometheus rule file
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub groups: Option<Vec<PrometheusRuleGroups>>,
}

/// RuleGroup is a list of sequentially evaluated recording and alerting rules.
#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
pub struct PrometheusRuleGroups {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub interval: Option<String>,
    pub name: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub partial_response_strategy: Option<String>,
    pub rules: Vec<PrometheusRuleGroupsRules>,
}

/// Rule describes an alerting or recording rule
#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
pub struct PrometheusRuleGroupsRules {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub alert: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub annotations: Option<BTreeMap<String, String>>,
    pub expr: IntOrString,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub r#for: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub labels: Option<BTreeMap<String, String>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub record: Option<String>,
}

与 kube 一起使用

只需将生成的文件(例如上述代码中的输出 prometheusrule.rs)添加到您的库中,并导入(至少)特殊根类型

use prometheusrule::PrometheusRule;
use kube::{Api, Client, ResourceExt};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::try_default().await?;
    let pr: Api<PrometheusRule> = Api::default_namespaced(client);
    for p in pr.list(&Default::default()).await? {
        println!("Found PrometheusRule {} in current namespace", p.name_any());
    }
    Ok(())
}

自动完成

大多数 shell 的自动完成功能通过 kopium completions 提供

source <(kopium completions bash)

测试

单元测试和从文件中运行 kopium 不需要集群,可以使用以下方式运行

cargo test --lib
cargo run --bin kopium -- -f mycrd.yaml -A

完整的集成测试使用您当前的集群尝试读取一个 CRD 和一个 gen 对象(CRD 类型的实例)并将其解析为生成的类型

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

通过 justjustfile 中提供的测试快捷方式并运行预合并。

现有绑定

某些绑定库将 kopium 输出发布到 crates.io 以便于使用,这可以避免在您的仓库中内联生成的文件(假设您不需要对输出进行自定义/修补)。

主要目录库是 metio/kube-custom-resources-rs,它通过功能(不要使用 all-features)选择 CRD。

特定组绑定库

请随意 编辑此文件 以添加更多。

许可

Apache 2.0 许可。有关详细信息,请参阅 LICENSE。

依赖关系

~57MB
~859K SLoC