Skip to content

Commit

Permalink
Add more information to Resource
Browse files Browse the repository at this point in the history
  • Loading branch information
MikailBag committed Apr 7, 2021
1 parent a1a50cc commit 83a6106
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 16 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ members = [
# internal
"tests",
"examples",
]
]

[patch.crates-io.k8s-openapi]
git = "https://github.com/mikailbag/k8s-openapi"
branch = "expose-resource-name"
8 changes: 8 additions & 0 deletions kube-derive/src/custom_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStrea
#plural.into()
}

fn scope(_: &()) -> kube::api::Scope {
if #namespaced {
kube::api::Scope::Namespaced
} else {
kube::api::Scope::Cluster
}
}

fn meta(&self) -> &kube::api::ObjectMeta {
&self.metadata
}
Expand Down
21 changes: 20 additions & 1 deletion kube/src/api/dynamic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
api::{metadata::TypeMeta, Resource},
api::{metadata::TypeMeta, Resource, Scope},
Error, Result,
};
use k8s_openapi::apimachinery::pkg::apis::meta::v1::{APIResource, ObjectMeta};
Expand All @@ -20,6 +20,8 @@ pub struct GroupVersionKind {
api_version: String,
/// Optional plural/resource
plural: Option<String>,
/// Scope (Unknown by default)
scope: Scope,
}

impl GroupVersionKind {
Expand Down Expand Up @@ -57,12 +59,18 @@ impl GroupVersionKind {
format!("{}/{}", group, version)
};
let plural = Some(ar.name.clone());
let scope = if ar.namespaced {
Scope::Namespaced
} else {
Scope::Cluster
};
Self {
group,
version,
kind,
api_version,
plural,
scope
}
}

Expand Down Expand Up @@ -94,6 +102,7 @@ impl GroupVersionKind {
kind,
api_version,
plural: None,
scope: Scope::Unknown,
})
}

Expand All @@ -102,6 +111,12 @@ impl GroupVersionKind {
self.plural = Some(plural.to_string());
self
}

/// Set explicit scope (instead of default Unknown)
pub fn scope(mut self, scope: Scope) -> Self {
self.scope = scope;
self
}
}

/// Represents a type-erased object resource.
Expand Down Expand Up @@ -247,6 +262,10 @@ impl Resource for DynamicObject {
}
}

fn scope(_dt: &GroupVersionKind) -> Scope {
Scope::Unknown
}

fn meta(&self) -> &ObjectMeta {
&self.metadata
}
Expand Down
28 changes: 28 additions & 0 deletions kube/src/api/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use std::{borrow::Cow, collections::BTreeMap};

/// Scope of the resource
#[derive(Serialize, Deserialize, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
pub enum Scope {
/// Resource is cluster-scoped, i.e. not bound
/// to any particular namespace
Cluster,
/// Resource is namespaced: it is owned by the namespace
/// specified in the `.metadata.namespace` field.
Namespaced,
/// Unknown scope
// Future work: delete this variant
Unknown,
}

/// An accessor trait for a kubernetes Resource.
///
/// This is for a subset of Kubernetes type that do not end in `List`.
Expand Down Expand Up @@ -32,6 +46,8 @@ pub trait Resource {
fn group(dt: &Self::DynamicType) -> Cow<'_, str>;
/// Returns version of this object
fn version(dt: &Self::DynamicType) -> Cow<'_, str>;
/// Returns resource scope
fn scope(dt: &Self::DynamicType) -> Scope;
/// Returns apiVersion of this object
fn api_version(dt: &Self::DynamicType) -> Cow<'_, str> {
let group = Self::group(dt);
Expand Down Expand Up @@ -194,6 +210,18 @@ where
K::API_VERSION.into()
}

fn plural(_: &()) -> Cow<'_, str> {
K::PLURAL_NAME.into()
}

fn scope(_: &()) -> Scope {
if K::NAMESPACED {
Scope::Namespaced
} else {
Scope::Cluster
}
}

fn meta(&self) -> &ObjectMeta {
self.metadata()
}
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 @@ -34,6 +34,6 @@ pub(crate) mod object;
pub use self::object::{Object, ObjectList, WatchEvent};

mod metadata;
pub use self::metadata::{ListMeta, ObjectMeta, Resource, ResourceExt, TypeMeta};
pub use self::metadata::{ListMeta, ObjectMeta, Resource, ResourceExt, Scope, TypeMeta};

#[cfg(feature = "admission")] pub mod admission;
26 changes: 13 additions & 13 deletions kube/src/api/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tracing::instrument;

use crate::{
api::{
DeleteParams, ListParams, ObjectList, Patch, PatchParams, PostParams, Request, Resource, WatchEvent,
DeleteParams, ListParams, ObjectList, Patch, PatchParams, PostParams, Request, Resource, WatchEvent, Scope
},
client::{Client, Status},
Result,
Expand Down Expand Up @@ -38,22 +38,15 @@ where
{
/// Cluster level resources, or resources viewed across all namespaces
pub fn all(client: Client) -> Self {
let url = K::url_path(&Default::default(), None);
Self {
client,
request: Request::new(url),
phantom: iter::empty(),
}
Self::all_with(client, &Default::default())
}

/// Namespaced resource within a given namespace
///
/// # Panics
/// This function panics if the resource is clister-scoped.
pub fn namespaced(client: Client, ns: &str) -> Self {
let url = K::url_path(&Default::default(), Some(ns));
Self {
client,
request: Request::new(url),
phantom: iter::empty(),
}
Self::namespaced_with(client, ns, &Default::default())
}
}

Expand All @@ -76,7 +69,13 @@ impl<K: Resource> Api<K> {
/// Namespaced resource within a given namespace
///
/// This function accepts `K::DynamicType` so it can be used with dynamic resources.
///
/// # Panics
/// This function panics if the resource is cluster-scoped.
pub fn namespaced_with(client: Client, ns: &str, dyntype: &K::DynamicType) -> Self {
if let Scope::Cluster = K::scope(dyntype) {
panic!("Namespaced Api created for the cluster-scoped resource");
}
let url = K::url_path(dyntype, Some(ns));
Self {
client,
Expand Down Expand Up @@ -382,3 +381,4 @@ impl<K> From<Api<K>> for Client {
api.client
}
}

0 comments on commit 83a6106

Please sign in to comment.