Skip to content

Commit

Permalink
Add managed_fields accessors to ResourceExt (#898)
Browse files Browse the repository at this point in the history
Signed-off-by: clux <sszynrae@gmail.com>
  • Loading branch information
clux committed May 6, 2022
1 parent 2459b72 commit bb3ad20
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 16 additions & 1 deletion kube-core/src/resource.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
pub use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;
use k8s_openapi::{api::core::v1::ObjectReference, apimachinery::pkg::apis::meta::v1::OwnerReference};
use k8s_openapi::{
api::core::v1::ObjectReference,
apimachinery::pkg::apis::meta::v1::{ManagedFieldsEntry, OwnerReference},
};
use std::{borrow::Cow, collections::BTreeMap};

/// An accessor trait for a kubernetes Resource.
Expand Down Expand Up @@ -170,6 +173,10 @@ pub trait ResourceExt: Resource {
fn finalizers(&self) -> &[String];
/// Provides mutable access to the finalizers
fn finalizers_mut(&mut self) -> &mut Vec<String>;
/// Returns managed fields
fn managed_fields(&self) -> &[ManagedFieldsEntry];
/// Provides mutable access to managed fields
fn managed_fields_mut(&mut self) -> &mut Vec<ManagedFieldsEntry>;
}

// TODO: replace with ordinary static when BTreeMap::new() is no longer
Expand Down Expand Up @@ -225,4 +232,12 @@ impl<K: Resource> ResourceExt for K {
fn finalizers_mut(&mut self) -> &mut Vec<String> {
self.meta_mut().finalizers.get_or_insert_with(Vec::new)
}

fn managed_fields(&self) -> &[ManagedFieldsEntry] {
self.meta().managed_fields.as_deref().unwrap_or_default()
}

fn managed_fields_mut(&mut self) -> &mut Vec<ManagedFieldsEntry> {
self.meta_mut().managed_fields.get_or_insert_with(Vec::new)
}
}
2 changes: 2 additions & 0 deletions kube/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ mod test {
.entry("kube.rs".to_string())
.or_insert_with(|| "hello".to_string());
pod.finalizers_mut().push("kube-finalizer".to_string());
pod.managed_fields_mut().clear();
// NB: we are **not** pushing these back upstream - (Api::apply or Api::replace needed for it)
}
// check we can iterate over ObjectList normally - and check the mutations worked
Expand All @@ -347,6 +348,7 @@ mod test {
assert!(pod.labels().get("kube.rs").is_some());
assert!(pod.finalizers().contains(&"kube-finalizer".to_string()));
assert!(pod.spec().containers.is_empty());
assert!(pod.managed_fields().is_empty());
}
Ok(())
}
Expand Down

0 comments on commit bb3ad20

Please sign in to comment.