Skip to content

Commit

Permalink
finish rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
MikailBag committed Apr 23, 2021
1 parent 32b6083 commit 62f4d0a
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 177 deletions.
33 changes: 13 additions & 20 deletions examples/dynamic_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
//! to `kubectl get all --all-namespaces`.
use kube::{
api::{Api, ApiResource, DynamicObject, Resource, ResourceExt},
client::{Client, Discovery},
api::{Api, DynamicObject, ResourceExt},
client::{Client, Discovery, Scope},
};
use log::{info, warn};
use log::info;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
Expand All @@ -22,30 +22,23 @@ async fn main() -> anyhow::Result<()> {

for group in discovery.groups() {
let ver = group.preferred_version_or_guess();
for gvk in group.resources_by_version(ver) {
let kind = DynamicObject::kind(&gvk);
let (_, raw_resource) = discovery
.resolve_group_version_kind(group.name(), ver, &kind)
.unwrap();
let api: Api<DynamicObject> = if raw_resource.namespaced {
for (api_res, extras) in group.resources_by_version(ver) {
if !extras.operations.list {
continue;
}
let api: Api<DynamicObject> = if extras.scope == Scope::Namespaced {
if let Some(ns) = &ns_filter {
Api::namespaced_with(client.clone(), ns, &gvk)
Api::namespaced_with(client.clone(), ns, &api_res)
} else {
Api::all_with(client.clone(), &gvk)
Api::all_with(client.clone(), &api_res)
}
} else {
Api::all_with(client.clone(), &gvk)
Api::all_with(client.clone(), &api_res)
};

info!("{}/{} : {}", group.name(), ver, kind);
info!("{}/{} : {}", group.name(), ver, api_res.kind);

let list = match api.list(&Default::default()).await {
Ok(l) => l,
Err(e) => {
warn!("Failed to list: {:#}", e);
continue;
}
};
let list = api.list(&Default::default()).await?;
for item in list.items {
let name = item.name();
let ns = item.metadata.namespace.map(|s| s + "/").unwrap_or_default();
Expand Down
112 changes: 1 addition & 111 deletions kube/src/api/dynamic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::api::{metadata::TypeMeta, GroupVersionKind, Resource};
use k8s_openapi::apimachinery::pkg::apis::meta::v1::{APIResource, APIResourceList, ObjectMeta};
use k8s_openapi::apimachinery::pkg::apis::meta::v1::{APIResource, ObjectMeta};
use std::borrow::Cow;

/// Contains information about Kubernetes API resources
Expand Down Expand Up @@ -181,116 +181,6 @@ impl Resource for DynamicObject {
}
}

/// Resource scope
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub enum Scope {
/// Objects are global
Cluster,
/// Each object lives in namespace.
Namespaced,
}

/// Operations that are supported on the resource
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct Operations {
/// Object can be created
pub create: bool,
/// Single object can be queried
pub get: bool,
/// Multiple objects can be queried
pub list: bool,
/// A watch can be started
pub watch: bool,
/// A single object can be deleted
pub delete: bool,
/// Multiple objects can be deleted
pub delete_collection: bool,
/// Object can be updated
pub update: bool,
/// Object can be patched
pub patch: bool,
/// All other verbs
pub other: Vec<String>,
}

impl Operations {
/// Returns empty `Operations`
pub fn empty() -> Self {
Operations {
create: false,
get: false,
list: false,
watch: false,
delete: false,
delete_collection: false,
update: false,
patch: false,
other: Vec::new(),
}
}
}
/// Contains additional, detailed information abount API resource
pub struct ApiResourceExtras {
/// Scope of the resource
pub scope: Scope,
/// Available subresources. Please note that returned ApiResources are not
/// standalone resources. Their name will be of form `subresource_name`,
/// not `resource_name/subresource_name`.
/// To work with subresources, use `Request` methods.
pub subresources: Vec<(ApiResource, ApiResourceExtras)>,
/// Supported operations on this resource
pub operations: Operations,
}

impl ApiResourceExtras {
/// Creates ApiResourceExtras from `meta::v1::APIResourceList` instance.
/// This function correctly sets all fields except `subresources`.
/// # Panics
/// Panics if list does not contain resource named `name`.
pub fn from_apiresourcelist(list: &APIResourceList, name: &str) -> Self {
let ar = list
.resources
.iter()
.find(|r| r.name == name)
.expect("resource not found in APIResourceList");
let scope = if ar.namespaced {
Scope::Namespaced
} else {
Scope::Cluster
};
let mut operations = Operations::empty();
for verb in &ar.verbs {
match verb.as_str() {
"create" => operations.create = true,
"get" => operations.get = true,
"list" => operations.list = true,
"watch" => operations.watch = true,
"delete" => operations.delete = true,
"deletecollection" => operations.delete_collection = true,
"update" => operations.update = true,
"patch" => operations.patch = true,
_ => operations.other.push(verb.clone()),
}
}
let mut subresources = Vec::new();
let subresource_name_prefix = format!("{}/", name);
for res in &list.resources {
if let Some(subresource_name) = res.name.strip_prefix(&subresource_name_prefix) {
let mut api_resource = ApiResource::from_apiresource(res, &list.group_version);
api_resource.plural = subresource_name.to_string();
let extra = ApiResourceExtras::from_apiresourcelist(list, &res.name);
subresources.push((api_resource, extra));
}
}

ApiResourceExtras {
scope,
subresources,
operations,
}
}
}

#[cfg(test)]
mod test {
use crate::{
Expand Down
2 changes: 1 addition & 1 deletion kube/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod gvk;
pub use gvk::{GroupVersionKind, GroupVersionResource};

mod dynamic;
pub use dynamic::{ApiResource, ApiResourceExtras, DynamicObject, Operations, Scope};
pub use dynamic::{ApiResource, DynamicObject};

#[cfg(feature = "ws")] mod remote_command;
#[cfg(feature = "ws")] pub use remote_command::AttachedProcess;
Expand Down
Loading

0 comments on commit 62f4d0a

Please sign in to comment.