Skip to content

Commit

Permalink
put namespace inside the tri-state
Browse files Browse the repository at this point in the history
  • Loading branch information
clux committed Mar 28, 2020
1 parent 4d2d1c1 commit fb3224d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
9 changes: 7 additions & 2 deletions kube/src/api/crds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,18 @@ impl CrBuilder {
/// Make Resource useable on CRDs without k8s_openapi
impl From<CustomResource> for Resource {
fn from(c: CustomResource) -> Self {
use crate::api::resource::ResourceScope;
let scope = if let Some(ns) = c.namespace {
ResourceScope::Namespace(ns)
} else {
ResourceScope::All
};
Self {
api_version: c.api_version,
kind: c.kind,
group: c.group,
version: c.version,
namespace: c.namespace, // yeah, needs to be merged into scope
scope: crate::api::resource::ResourceScope::Namespace, // otherwise can clash with ::All
scope,
}
}
}
Expand Down
12 changes: 3 additions & 9 deletions kube/src/api/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,13 @@ pub struct Resource {
/// The version of the resource.
pub version: String,

/// The namespace if the resource resides (if namespaced)
pub namespace: Option<String>,

pub scope: ResourceScope,
}

#[derive(Clone, Debug, PartialEq)]
pub enum ResourceScope {
Cluster,
Namespace, // could maybe put the resource.namespace string in here
Namespace(String),
All,
}

Expand Down Expand Up @@ -88,7 +85,6 @@ impl Resource {
kind: K::KIND.to_string(),
group: K::GROUP.to_string(),
version: K::VERSION.to_string(),
namespace: None,
scope: ResourceScope::Cluster,
}
}
Expand All @@ -103,7 +99,6 @@ impl Resource {
kind: K::KIND.to_string(),
group: K::GROUP.to_string(),
version: K::VERSION.to_string(),
namespace: None,
scope: ResourceScope::All
}
}
Expand All @@ -115,8 +110,7 @@ impl Resource {
kind: K::KIND.to_string(),
group: K::GROUP.to_string(),
version: K::VERSION.to_string(),
namespace: Some(ns.to_string()),
scope: ResourceScope::Namespace,
scope: ResourceScope::Namespace(ns.to_string()),
}
}
}
Expand All @@ -125,7 +119,7 @@ impl Resource {

impl Resource {
pub(crate) fn make_url(&self) -> String {
let n = if let Some(ns) = &self.namespace {
let n = if let ResourceScope::Namespace(ns) = &self.scope {
format!("namespaces/{}/", ns)
} else {
"".into()
Expand Down
8 changes: 7 additions & 1 deletion kube/src/runtime/reflector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,15 @@ where
/// If you are using a non-namespaced resources with name clashes,
/// Try `Reflector::get_within` instead.
pub fn get(&self, name: &str) -> Result<Option<K>> {
use crate::api::resource::ResourceScope;
let namespace = match &self.resource.scope {
ResourceScope::Namespace(ns) => Some(ns.to_owned()),
_ => None,
};
let id = ObjectId {
name: name.into(),
namespace: self.resource.namespace.clone(),
// TODO: impl From<Resource> for ObjectId
namespace,
};

futures::executor::block_on(async { Ok(self.state.lock().await.data.get(&id).map(Clone::clone)) })
Expand Down

0 comments on commit fb3224d

Please sign in to comment.