Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project Neokubism #594

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 8 additions & 4 deletions examples/configmapgen_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,15 @@ async fn reconcile(generator: ConfigMapGenerator, ctx: Context<Data>) -> Result<
);
cm_api
.patch(
cm.metadata.name.as_ref().context(MissingObjectKey {
name: ".metadata.name",
})?,
&cm.metadata
.name
.as_ref()
.context(MissingObjectKey {
name: ".metadata.name",
})?
.clone(),
&PatchParams::apply("configmapgenerator.kube-rt.nullable.se"),
&Patch::Apply(&cm),
&Patch::Apply(cm),
)
.await
.context(ConfigMapCreationFailed)?;
Expand Down
15 changes: 6 additions & 9 deletions kube-core/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ use std::borrow::Cow;
/// and is generally produced from list/watch/delete collection queries on an [`Resource`](super::Resource).
///
/// This is almost equivalent to [`k8s_openapi::List<T>`](k8s_openapi::List), but iterable.
#[derive(Deserialize, Debug)]
pub struct ObjectList<T>
where
T: Clone,
{
#[derive(Clone, Deserialize, Debug)]
pub struct ObjectList<T> {
// NB: kind and apiVersion can be set here, but no need for it atm
/// ListMeta - only really used for its `resourceVersion`
///
Expand All @@ -32,7 +29,7 @@ where
pub items: Vec<T>,
}

impl<T: Clone> ObjectList<T> {
impl<T> ObjectList<T> {
/// `iter` returns an Iterator over the elements of this ObjectList
///
/// # Example
Expand Down Expand Up @@ -75,7 +72,7 @@ impl<T: Clone> ObjectList<T> {
}
}

impl<T: Clone> IntoIterator for ObjectList<T> {
impl<T> IntoIterator for ObjectList<T> {
type IntoIter = ::std::vec::IntoIter<Self::Item>;
type Item = T;

Expand All @@ -84,7 +81,7 @@ impl<T: Clone> IntoIterator for ObjectList<T> {
}
}

impl<'a, T: Clone> IntoIterator for &'a ObjectList<T> {
impl<'a, T> IntoIterator for &'a ObjectList<T> {
type IntoIter = ::std::slice::Iter<'a, T>;
type Item = &'a T;

Expand All @@ -93,7 +90,7 @@ impl<'a, T: Clone> IntoIterator for &'a ObjectList<T> {
}
}

impl<'a, T: Clone> IntoIterator for &'a mut ObjectList<T> {
impl<'a, T> IntoIterator for &'a mut ObjectList<T> {
type IntoIter = ::std::slice::IterMut<'a, T>;
type Item = &'a mut T;

Expand Down
11 changes: 6 additions & 5 deletions kube-core/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl PostParams {
/// ```
#[non_exhaustive]
#[derive(Debug)]
pub enum Patch<T: Serialize> {
pub enum Patch<T> {
/// [Server side apply](https://kubernetes.io/docs/reference/using-api/api-concepts/#server-side-apply)
///
/// Requires kubernetes >= 1.16
Expand All @@ -226,12 +226,13 @@ pub enum Patch<T: Serialize> {
Strategic(T),
}

impl<T: Serialize> Patch<T> {
impl<T> Patch<T> {
pub(crate) fn is_apply(&self) -> bool {
matches!(self, Patch::Apply(_))
}

pub(crate) fn content_type(&self) -> &'static str {
/// The MIME content type for the patch
pub fn content_type(&self) -> &'static str {
match &self {
Self::Apply(_) => "application/apply-patch+yaml",
#[cfg(feature = "jsonpatch")]
Expand All @@ -244,7 +245,8 @@ impl<T: Serialize> Patch<T> {
}

impl<T: Serialize> Patch<T> {
pub(crate) fn serialize(&self) -> Result<Vec<u8>> {
/// Try to serialize the patch object to JSON
pub fn serialize(&self) -> Result<Vec<u8>, serde_json::Error> {
match self {
Self::Apply(p) => serde_json::to_vec(p),
#[cfg(feature = "jsonpatch")]
Expand All @@ -253,7 +255,6 @@ impl<T: Serialize> Patch<T> {
Self::Strategic(p) => serde_json::to_vec(p),
Self::Merge(p) => serde_json::to_vec(p),
}
.map_err(Into::into)
}
}

Expand Down
2 changes: 1 addition & 1 deletion kube-core/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub trait Resource {
/// as type of this information.
///
/// See [`DynamicObject`](crate::dynamic::DynamicObject) for a valid implementation of non-k8s-openapi resources.
type DynamicType: Send + Sync + 'static;
type DynamicType: Clone + Send + Sync + 'static;

/// Returns kind of this object
fn kind(dt: &Self::DynamicType) -> Cow<'_, str>;
Expand Down
4 changes: 2 additions & 2 deletions kube-runtime/src/finalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ where
.context(CleanupFailed)?;
// Cleanup was successful, remove the finalizer so that deletion can continue
let finalizer_path = format!("/metadata/finalizers/{}", finalizer_i);
api.patch::<K>(
api.patch(
&name,
&PatchParams::default(),
&Patch::Json(json_patch::Patch(vec![
Expand Down Expand Up @@ -167,7 +167,7 @@ where
value: finalizer_name.into(),
})]
});
api.patch::<K>(
api.patch(
obj.meta().name.as_deref().context(UnnamedObject)?,
&PatchParams::default(),
&Patch::Json(patch),
Expand Down
2 changes: 2 additions & 0 deletions kube-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
// Triggered by many derive macros (kube-derive, derivative)
#![allow(clippy::default_trait_access)]
#![allow(clippy::type_repetition_in_bounds)]
// Triggered by tokio::test macros
#![allow(clippy::semicolon_if_nothing_returned)]

pub mod controller;
pub mod finalizer;
Expand Down
4 changes: 2 additions & 2 deletions kube-runtime/src/reflector/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ where
match event {
watcher::Event::Applied(obj) => {
self.store
.insert(ObjectRef::from_obj_with(&obj, self.dyntype.clone()), obj.clone());
.insert(ObjectRef::from_obj_with(obj, self.dyntype.clone()), obj.clone());
}
watcher::Event::Deleted(obj) => {
self.store
.remove(&ObjectRef::from_obj_with(&obj, self.dyntype.clone()));
.remove(&ObjectRef::from_obj_with(obj, self.dyntype.clone()));
}
watcher::Event::Restarted(new_objs) => {
let new_objs = new_objs
Expand Down
6 changes: 3 additions & 3 deletions kube-runtime/src/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ async fn step_trampolined<K: Resource + Clone + DeserializeOwned + Debug + Send
state: State<K>,
) -> (Option<Result<Event<K>>>, State<K>) {
match state {
State::Empty => match api.list(&list_params).await {
State::Empty => match api.list(list_params).await {
Ok(list) => (Some(Ok(Event::Restarted(list.items))), State::InitListed {
resource_version: list.metadata.resource_version.unwrap(),
}),
Err(err) => (Some(Err(err).context(InitialListFailed)), State::Empty),
},
State::InitListed { resource_version } => match api.watch(&list_params, &resource_version).await {
State::InitListed { resource_version } => match api.watch(list_params, &resource_version).await {
Ok(stream) => (None, State::Watching {
resource_version,
stream: stream.boxed(),
Expand Down Expand Up @@ -179,7 +179,7 @@ async fn step<K: Resource + Clone + DeserializeOwned + Debug + Send + 'static>(
mut state: State<K>,
) -> (Result<Event<K>>, State<K>) {
loop {
match step_trampolined(&api, &list_params, state).await {
match step_trampolined(api, list_params, state).await {
(Some(result), new_state) => return (result, new_state),
(None, new_state) => state = new_state,
}
Expand Down
2 changes: 2 additions & 0 deletions kube/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ tame-oauth = { version = "0.4.7", features = ["gcp"], optional = true }
pin-project = { version = "1.0.4", optional = true }
rand = { version = "0.8.3", optional = true }
tracing = { version = "0.1.25", features = ["log"], optional = true }
snafu = "0.6.10"
form_urlencoded = "1.0.1"

[dependencies.k8s-openapi]
version = "0.12.0"
Expand Down
Loading