From fb3224d42e7dd9d61a8fb7b9c4570b15cd9de4aa Mon Sep 17 00:00:00 2001 From: clux Date: Sat, 28 Mar 2020 12:13:05 +0000 Subject: [PATCH] put namespace inside the tri-state --- kube/src/api/crds.rs | 9 +++++++-- kube/src/api/resource.rs | 12 +++--------- kube/src/runtime/reflector.rs | 8 +++++++- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/kube/src/api/crds.rs b/kube/src/api/crds.rs index 926290478..2f6ae6fcc 100644 --- a/kube/src/api/crds.rs +++ b/kube/src/api/crds.rs @@ -110,13 +110,18 @@ impl CrBuilder { /// Make Resource useable on CRDs without k8s_openapi impl From 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, } } } diff --git a/kube/src/api/resource.rs b/kube/src/api/resource.rs index a37a7227f..04b867c86 100644 --- a/kube/src/api/resource.rs +++ b/kube/src/api/resource.rs @@ -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, - pub scope: ResourceScope, } #[derive(Clone, Debug, PartialEq)] pub enum ResourceScope { Cluster, - Namespace, // could maybe put the resource.namespace string in here + Namespace(String), All, } @@ -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, } } @@ -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 } } @@ -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()), } } } @@ -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() diff --git a/kube/src/runtime/reflector.rs b/kube/src/runtime/reflector.rs index 3575465a4..7ac3b4501 100644 --- a/kube/src/runtime/reflector.rs +++ b/kube/src/runtime/reflector.rs @@ -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> { + 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 for ObjectId + namespace, }; futures::executor::block_on(async { Ok(self.state.lock().await.data.get(&id).map(Clone::clone)) })