Skip to content

Commit

Permalink
Allow to create non-controller owner reference for resource (#1475)
Browse files Browse the repository at this point in the history
Signed-off-by: Danil Grigorev <[email protected]>
  • Loading branch information
Danil-Grigorev authored Apr 29, 2024
1 parent b83bd1b commit 72eef44
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion kube-core/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,60 @@ pub trait Resource {
///
/// Note: this returns an `Option`, but for objects populated from the apiserver,
/// this Option can be safely unwrapped.
///
/// ```
/// use k8s_openapi::api::core::v1::ConfigMap;
/// use k8s_openapi::api::core::v1::Pod;
/// use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;
/// use kube_core::Resource;
///
/// let p = Pod::default();
/// let controller_ref = p.controller_owner_ref(&());
/// let cm = ConfigMap {
/// metadata: ObjectMeta {
/// name: Some("pod-configmap".to_string()),
/// owner_references: Some(controller_ref.into_iter().collect()),
/// ..ObjectMeta::default()
/// },
/// ..Default::default()
/// };
/// ```
fn controller_owner_ref(&self, dt: &Self::DynamicType) -> Option<OwnerReference> {
Some(OwnerReference {
controller: Some(true),
..self.owner_ref(dt)?
})
}

/// Generates an owner reference pointing to this resource
///
/// Note: this returns an `Option`, but for objects populated from the apiserver,
/// this Option can be safely unwrapped.
///
/// ```
/// use k8s_openapi::api::core::v1::ConfigMap;
/// use k8s_openapi::api::core::v1::Pod;
/// use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;
/// use kube_core::Resource;
///
/// let p = Pod::default();
/// let owner_ref = p.owner_ref(&());
/// let cm = ConfigMap {
/// metadata: ObjectMeta {
/// name: Some("pod-configmap".to_string()),
/// owner_references: Some(owner_ref.into_iter().collect()),
/// ..ObjectMeta::default()
/// },
/// ..Default::default()
/// };
/// ```
fn owner_ref(&self, dt: &Self::DynamicType) -> Option<OwnerReference> {
let meta = self.meta();
Some(OwnerReference {
api_version: Self::api_version(dt).to_string(),
kind: Self::kind(dt).to_string(),
name: meta.name.clone()?,
uid: meta.uid.clone()?,
controller: Some(true),
..OwnerReference::default()
})
}
Expand Down

0 comments on commit 72eef44

Please sign in to comment.