use k8s_openapi::apimachinery::pkg::apis::meta::v1::OwnerReference;
use kube::core::ObjectMeta;
use std::collections::BTreeMap;
#[derive(Default)]
pub struct ObjectMetaBuilder {
name: Option<String>,
namespace: Option<String>,
labels: BTreeMap<String, String>,
annotations: BTreeMap<String, String>,
owner_references: Vec<OwnerReference>,
}
impl ObjectMetaBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn namespace(mut self, namespace: impl Into<String>) -> Self {
self.namespace = Some(namespace.into());
self
}
pub fn label(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.labels.insert(key.into(), value.into());
self
}
pub fn labels(
mut self,
pairs: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>,
) -> Self {
for (k, v) in pairs {
self.labels.insert(k.into(), v.into());
}
self
}
pub fn annotation(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.annotations.insert(key.into(), value.into());
self
}
pub fn owner_ref(mut self, owner_ref: OwnerReference) -> Self {
self.owner_references.push(owner_ref);
self
}
pub fn build(self) -> ObjectMeta {
ObjectMeta {
name: self.name,
namespace: self.namespace,
labels: if self.labels.is_empty() {
None
} else {
Some(self.labels)
},
annotations: if self.annotations.is_empty() {
None
} else {
Some(self.annotations)
},
owner_references: if self.owner_references.is_empty() {
None
} else {
Some(self.owner_references)
},
..Default::default()
}
}
}