Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
rename TypeInfo to Inspect
more accurately explains what the trait is doing

Signed-off-by: clux <[email protected]>
  • Loading branch information
clux committed Dec 13, 2022
commit e58a6a76cfd3c69376bf3233677988db28e6e698
2 changes: 1 addition & 1 deletion kube-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub mod gvk;
pub use gvk::{GroupVersion, GroupVersionKind, GroupVersionResource};

pub mod metadata;
pub use metadata::{ListMeta, ObjectMeta, TypeInfo, TypeMeta};
pub use metadata::{Inspect, ListMeta, ObjectMeta, TypeMeta};

pub mod object;
pub use object::{NotUsed, Object, ObjectList};
Expand Down
25 changes: 11 additions & 14 deletions kube-core/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ pub struct TypeMeta {
pub kind: String,
}

/// A runtime accessor trait for TypeMeta
/// A runtime accessor trait for `TypeMeta`
///
/// This trait can be thought of as a limited variant of the `Resource` trait that reads from runtime properties.
/// It cannot retrieve the plural, nor the scope of a resource and requires an `ApiResource` for this instead.
///
/// For static types is generally leans on the static information, but for dynamic types, it inspects the object.
pub trait TypeInfo {
/// This trait is a runtime subset of the `Resource` trait that can read the object directly.
/// It **cannot** retrieve the plural, **nor** the scope of a resource (which requires an `ApiResource`).
pub trait Inspect {
/// Get the `TypeMeta` of an object
///
/// This is a safe `TypeMeta` getter for all object types
/// While it is generally safe to unwrap this option, do note that a few endpoints can elide it.
fn types(&self) -> Option<TypeMeta>;

/// Get the `TypeMeta` of an object that is guaranteed to have it
Expand All @@ -42,9 +41,8 @@ pub trait TypeInfo {
fn meta_mut(&mut self) -> &mut ObjectMeta;
}


// static types always have type info
impl<K> TypeInfo for K
// lean on static info on k8s_openapi generated types (safer than runtime lookups)
impl<K> Inspect for K
where
K: k8s_openapi::Resource,
K: k8s_openapi::Metadata<Ty = ObjectMeta>,
Expand Down Expand Up @@ -77,8 +75,8 @@ where
}
}

// dynamic types generally have typeinfo, but certain api endpoints can omit it
impl<P, U> TypeInfo for Object<P, U>
// always lookup from object in dynamic cases
impl<P, U> Inspect for Object<P, U>
where
P: Clone,
U: Clone,
Expand Down Expand Up @@ -108,7 +106,8 @@ where
}
}

impl TypeInfo for DynamicObject {
// always lookup from object in dynamic cases
impl Inspect for DynamicObject {
fn types(&self) -> Option<TypeMeta> {
self.types.clone()
}
Expand All @@ -133,5 +132,3 @@ impl TypeInfo for DynamicObject {
&mut self.metadata
}
}

// NB: we can implement ResourceExt for things that impl TypeInfo but not Resource
2 changes: 1 addition & 1 deletion kube-core/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub trait Resource {
/// Return `TypeMeta` of a Resource where it can be statically determined
///
/// This is only possible on static types.
/// Dynamic types need to find these via discovery or through the `TypeInfo` trait.
/// Dynamic types need to find these via discovery or through the `Inspect` trait.
fn typemeta() -> Option<TypeMeta>;
}

Expand Down
2 changes: 1 addition & 1 deletion kube-derive/src/custom_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStrea

// 3. implement typeinfo trait
let impl_typeinfo = quote! {
impl #kube_core::TypeInfo for #rootident {
impl #kube_core::Inspect for #rootident {

fn types(&self) -> Option<#kube_core::TypeMeta> {
Some(#kube_core::TypeMeta {
Expand Down
18 changes: 9 additions & 9 deletions kube-runtime/src/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use futures::{
};
use kube_client::{
api::{Api, ListParams, Resource},
core::TypeInfo,
core::Inspect,
};
use pin_project::pin_project;
use serde::de::DeserializeOwned;
Expand Down Expand Up @@ -106,7 +106,7 @@ where
pub fn trigger_self<K, S>(stream: S) -> impl Stream<Item = Result<ReconcileRequest, S::Error>>
where
S: TryStream<Ok = K>,
K: TypeInfo,
K: Inspect,
{
trigger_with(stream, move |obj| {
Some(ReconcileRequest {
Expand All @@ -123,12 +123,12 @@ pub fn trigger_owners<KOwner, S>(
) -> impl Stream<Item = Result<ReconcileRequest, S::Error>>
where
S: TryStream,
S::Ok: Resource + TypeInfo,
S::Ok: Resource + Inspect,
KOwner: Resource,
KOwner::DynamicType: Clone,
{
trigger_with(stream, move |obj| {
let meta = TypeInfo::meta(&obj).clone();
let meta = Inspect::meta(&obj).clone();
let ns = meta.namespace;
let owner_type = owner_type.clone();
let child_ref = ObjectRef::from_obj(&obj);
Expand Down Expand Up @@ -221,7 +221,7 @@ pub fn applier<K, QueueStream, ReconcilerFut, Ctx>(
queue: QueueStream,
) -> impl Stream<Item = Result<(ObjectRef, Action), Error<ReconcilerFut::Error, QueueStream::Error>>>
where
K: Clone + Resource + TypeInfo + 'static,
K: Clone + Resource + Inspect + 'static,
K::DynamicType: Debug + Eq + Hash + Clone + Unpin,
ReconcilerFut: TryFuture<Ok = Action> + Unpin,
ReconcilerFut::Error: std::error::Error + 'static,
Expand Down Expand Up @@ -439,7 +439,7 @@ impl<ReconcilerErr> Future for RescheduleReconciliation<ReconcilerErr> {
/// ```
pub struct Controller<K>
where
K: Clone + Resource + TypeInfo + Debug + 'static,
K: Clone + Resource + Inspect + Debug + 'static,
K::DynamicType: Eq + Hash,
{
// NB: Need to Unpin for stream::select_all
Expand All @@ -459,7 +459,7 @@ where

impl<K> Controller<K>
where
K: Clone + Resource + TypeInfo + DeserializeOwned + Debug + Send + Sync + 'static,
K: Clone + Resource + Inspect + DeserializeOwned + Debug + Send + Sync + 'static,
K::DynamicType: Eq + Hash + Clone,
{
/// Create a Controller on a type `K`
Expand Down Expand Up @@ -541,7 +541,7 @@ where
///
/// [`OwnerReference`]: k8s_openapi::apimachinery::pkg::apis::meta::v1::OwnerReference
#[must_use]
pub fn owns<Child: Clone + Resource + TypeInfo + DeserializeOwned + Debug + Send + 'static>(
pub fn owns<Child: Clone + Resource + Inspect + DeserializeOwned + Debug + Send + 'static>(
mut self,
api: Api<Child>,
lp: ListParams,
Expand Down Expand Up @@ -618,7 +618,7 @@ where
/// [Operator-SDK]: https://sdk.operatorframework.io/docs/building-operators/ansible/reference/retroactively-owned-resources/
#[must_use]
pub fn watches<
Other: Clone + Resource + TypeInfo + DeserializeOwned + Debug + Send + 'static,
Other: Clone + Resource + Inspect + DeserializeOwned + Debug + Send + 'static,
I: 'static + IntoIterator<Item = ObjectRef>,
>(
mut self,
Expand Down
4 changes: 2 additions & 2 deletions kube-runtime/src/reflector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod store;
pub use self::object_ref::{Extra as ObjectRefExtra, ObjectRef};
use crate::watcher;
use futures::{Stream, TryStreamExt};
use kube_client::core::{Resource, TypeInfo};
use kube_client::core::{Inspect, Resource};
use std::hash::Hash;
pub use store::{store, Store};

Expand All @@ -18,7 +18,7 @@ pub use store::{store, Store};
/// the whole `Store` will be cleared whenever any of them emits a `Restarted` event.
pub fn reflector<K, W>(mut writer: store::Writer<K>, stream: W) -> impl Stream<Item = W::Item>
where
K: Resource + TypeInfo + Clone,
K: Resource + Inspect + Clone,
K::DynamicType: Eq + Hash + Clone,
W: Stream<Item = watcher::Result<watcher::Event<K>>>,
{
Expand Down
14 changes: 7 additions & 7 deletions kube-runtime/src/reflector/object_ref.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use derivative::Derivative;
use k8s_openapi::{api::core::v1::ObjectReference, apimachinery::pkg::apis::meta::v1::OwnerReference};
use kube_client::core::{ObjectMeta, Resource, TypeInfo, TypeMeta};
use kube_client::core::{Inspect, ObjectMeta, Resource, TypeMeta};
use std::fmt::{Debug, Display};
use thiserror::Error;

Expand All @@ -10,9 +10,9 @@ use thiserror::Error;
/// A dynamically typed reference to an object along with its namespace
///
/// Intended to be constructed from one of three sources:
/// 1. an object returned by the apiserver through the `TypeInfo` trait
/// 1. an object returned by the apiserver through the `Inspect` trait
/// 2. an `OwnerReference` found on an object returned by the apiserver
/// 3. a type implementing `TypeInfo` but with only a `name` pointing to the type
/// 3. a type implementing `Inspect` but with only a `name` pointing to the type
///
/// ```
/// use kube_client::core::Resource;
Expand Down Expand Up @@ -72,9 +72,9 @@ pub struct Extra {
}

impl ObjectRef {
/// Creates an `ObjectRef` from an object implementing `TypeInfo`
/// Creates an `ObjectRef` from an object implementing `Inspect`
#[must_use]
pub fn from_obj<K: TypeInfo>(obj: &K) -> Self {
pub fn from_obj<K: Inspect>(obj: &K) -> Self {
let meta = obj.meta();
Self {
name: meta.name.clone().unwrap_or_default(),
Expand Down Expand Up @@ -132,8 +132,8 @@ impl ObjectRef {
}

#[derive(Debug, Error)]
#[error("failed to find type information")]
/// Failed to parse group version
#[error("missing type information from ObjectRef")]
/// ObjectRef does not have TypeMeta
pub struct MissingTypeInfo;


Expand Down
8 changes: 4 additions & 4 deletions kube-runtime/src/reflector/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::ObjectRef;
use crate::watcher;
use ahash::AHashMap;
use derivative::Derivative;
use kube_client::{core::TypeInfo, Resource};
use kube_client::{core::Inspect, Resource};
use parking_lot::RwLock;
use std::{fmt::Debug, hash::Hash, sync::Arc};

Expand All @@ -18,7 +18,7 @@ pub struct Writer<K: 'static + Resource> {
store: Cache<K>,
}

impl<K: 'static + Resource + TypeInfo + Clone> Writer<K> {
impl<K: 'static + Resource + Inspect + Clone> Writer<K> {
/// Creates a new Writer with the specified dynamic type.
#[must_use]
pub fn new() -> Self {
Expand Down Expand Up @@ -74,7 +74,7 @@ pub struct Store<K: 'static + Resource> {
store: Cache<K>,
}

impl<K: 'static + Clone + Resource + TypeInfo> Store<K>
impl<K: 'static + Clone + Resource + Inspect> Store<K>
where
K::DynamicType: Eq + Hash + Clone,
{
Expand Down Expand Up @@ -120,7 +120,7 @@ where
#[must_use]
pub fn store<K>() -> (Store<K>, Writer<K>)
where
K: Resource + TypeInfo + Clone + 'static,
K: Resource + Inspect + Clone + 'static,
{
let w = Writer::<K>::default();
let r = w.as_reader();
Expand Down