Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite dynamic api example #466

Merged
merged 3 commits into from
Mar 19, 2021
Merged
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
69 changes: 50 additions & 19 deletions examples/dynamic_api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#[macro_use] extern crate log;
use kube::{api::DynamicResource, Client};
//! In this example we will implement something similar
//! to `kubectl get all --all-namespaces`.

use k8s_openapi::apimachinery::pkg::apis::meta::v1::APIResourceList;
use kube::{
api::{DynamicObject, DynamicResource, Meta},
Client,
};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
Expand All @@ -8,7 +14,9 @@ async fn main() -> anyhow::Result<()> {
let client = Client::try_default().await?;

let v = client.apiserver_version().await?;
info!("api version: {:?}", v);
println!("api version: {:?}", v);

let ns_filter = std::env::var("NAMESPACE").ok();

// The following loops turn the /api or /apis listers into kube::api::Resource
// objects which can be used to make dynamic api calls.
Expand All @@ -18,31 +26,54 @@ async fn main() -> anyhow::Result<()> {
// loop over all api groups (except core v1)
let apigroups = client.list_api_groups().await?;
for g in apigroups.groups {
info!("group: {}", g.name);
debug!("group: {:?}", g);
let ver = g
.preferred_version
.as_ref()
.or_else(|| g.versions.first())
.expect("preferred or versions exists");
info!("polling: {} at {:?}", g.name, ver);
let apis = client.list_api_group_resources(&ver.group_version).await?;
dbg!(&apis);
for ar in apis.resources {
let r = DynamicResource::from_api_resource(&ar, &apis.group_version).into_resource();
dbg!(r);
}
dump_group(&client, &ver.group_version, apis, ns_filter.as_deref()).await?;
}
// core/v1 has a legacy endpoint
let coreapis = client.list_core_api_versions().await?;
for corever in coreapis.versions {
dbg!(&corever);
let apis = client.list_core_api_resources(&corever).await?;
debug!("Got {:?}", apis);
for cr in apis.resources {
dbg!(&cr);
let r = DynamicResource::from_api_resource(&cr, &apis.group_version).into_resource();
dbg!(r);

assert_eq!(coreapis.versions.len(), 1);
let corev1 = client.list_core_api_resources(&coreapis.versions[0]).await?;
dump_group(&client, &coreapis.versions[0], corev1, ns_filter.as_deref()).await?;

Ok(())
}

async fn dump_group(
client: &Client,
group_version: &str,
apis: APIResourceList,
ns_filter: Option<&str>,
) -> anyhow::Result<()> {
println!("{}", group_version);
for ar in apis.resources {
if !ar.verbs.contains(&"list".to_string()) {
continue;
}
if group_version.starts_with("discovery.k8s.io/") && ar.kind == "EndpointSlice"
|| group_version.starts_with("metrics.k8s.io/")
{
eprintln!("\tFIXME: skipping kind which would be otherwise incorrectly pluralized");
continue;
}
clux marked this conversation as resolved.
Show resolved Hide resolved
println!("\t{}", ar.kind);
let mut resource = DynamicResource::from_api_resource(&ar, &apis.group_version);
if ar.namespaced {
if let Some(ns) = ns_filter {
resource = resource.within(ns);
}
}
let api = resource.into_api::<DynamicObject>(client.clone());
let list = api.list(&Default::default()).await?;
for item in list.items {
let name = item.name();
let ns = item.namespace().map(|s| s + "/").unwrap_or_default();
println!("\t\t{}{}", ns, name);
}
}

Expand Down