use crate::{
discovery::ApiResource,
metadata::{ListMeta, ObjectMeta, TypeMeta},
resource::Resource,
};
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
#[derive(Deserialize, Debug)]
pub struct ObjectList<T>
where
T: Clone,
{
pub metadata: ListMeta,
#[serde(bound(deserialize = "Vec<T>: Deserialize<'de>"))]
pub items: Vec<T>,
}
impl<T: Clone> ObjectList<T> {
pub fn iter<'a>(&'a self) -> impl Iterator<Item = &T> + 'a {
self.items.iter()
}
pub fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = &mut T> + 'a {
self.items.iter_mut()
}
}
impl<T: Clone> IntoIterator for ObjectList<T> {
type IntoIter = ::std::vec::IntoIter<Self::Item>;
type Item = T;
fn into_iter(self) -> Self::IntoIter {
self.items.into_iter()
}
}
impl<'a, T: Clone> IntoIterator for &'a ObjectList<T> {
type IntoIter = ::std::slice::Iter<'a, T>;
type Item = &'a T;
fn into_iter(self) -> Self::IntoIter {
self.items.iter()
}
}
impl<'a, T: Clone> IntoIterator for &'a mut ObjectList<T> {
type IntoIter = ::std::slice::IterMut<'a, T>;
type Item = &'a mut T;
fn into_iter(self) -> Self::IntoIter {
self.items.iter_mut()
}
}
pub trait HasSpec {
type Spec;
fn spec(&self) -> &Self::Spec;
fn spec_mut(&mut self) -> &mut Self::Spec;
}
pub trait HasStatus {
type Status;
fn status(&self) -> Option<&Self::Status>;
fn status_mut(&mut self) -> &mut Option<Self::Status>;
}
#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct Object<P, U>
where
P: Clone,
U: Clone,
{
#[serde(flatten, default)]
pub types: Option<TypeMeta>,
pub metadata: ObjectMeta,
pub spec: P,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub status: Option<U>,
}
impl<P, U> Object<P, U>
where
P: Clone,
U: Clone,
{
pub fn new(name: &str, ar: &ApiResource, spec: P) -> Self {
Self {
types: Some(TypeMeta {
api_version: ar.api_version.clone(),
kind: ar.kind.clone(),
}),
metadata: ObjectMeta {
name: Some(name.to_string()),
..Default::default()
},
spec,
status: None,
}
}
pub fn within(mut self, ns: &str) -> Self {
self.metadata.namespace = Some(ns.into());
self
}
}
impl<P, U> Resource for Object<P, U>
where
P: Clone,
U: Clone,
{
type DynamicType = ApiResource;
fn group(dt: &ApiResource) -> Cow<'_, str> {
dt.group.as_str().into()
}
fn version(dt: &ApiResource) -> Cow<'_, str> {
dt.version.as_str().into()
}
fn kind(dt: &ApiResource) -> Cow<'_, str> {
dt.kind.as_str().into()
}
fn plural(dt: &ApiResource) -> Cow<'_, str> {
dt.plural.as_str().into()
}
fn api_version(dt: &ApiResource) -> Cow<'_, str> {
dt.api_version.as_str().into()
}
fn meta(&self) -> &ObjectMeta {
&self.metadata
}
fn meta_mut(&mut self) -> &mut ObjectMeta {
&mut self.metadata
}
}
impl<P, U> HasSpec for Object<P, U>
where
P: Clone,
U: Clone,
{
type Spec = P;
fn spec(&self) -> &Self::Spec {
&self.spec
}
fn spec_mut(&mut self) -> &mut Self::Spec {
&mut self.spec
}
}
impl<P, U> HasStatus for Object<P, U>
where
P: Clone,
U: Clone,
{
type Status = U;
fn status(&self) -> Option<&Self::Status> {
self.status.as_ref()
}
fn status_mut(&mut self) -> &mut Option<Self::Status> {
&mut self.status
}
}
#[derive(Clone, Deserialize, Serialize, Default, Debug)]
pub struct NotUsed {}
#[cfg(test)]
mod test {
use super::{ApiResource, NotUsed, Object};
#[test]
fn simplified_k8s_object() {
use k8s_openapi::api::core::v1::Pod;
#[derive(Clone)]
struct PodSpecSimple {
containers: Vec<ContainerSimple>,
}
#[derive(Clone)]
struct ContainerSimple {
image: String,
}
type PodSimple = Object<PodSpecSimple, NotUsed>;
let ar = ApiResource::erase::<Pod>(&());
assert_eq!(ar.group, "");
assert_eq!(ar.kind, "Pod");
let data = PodSpecSimple {
containers: vec![ContainerSimple { image: "blog".into() }],
};
let mypod = PodSimple::new("blog", &ar, data).within("dev");
assert_eq!(mypod.metadata.namespace.unwrap(), "dev");
assert_eq!(mypod.metadata.name.unwrap(), "blog");
assert_eq!(mypod.types.as_ref().unwrap().kind, "Pod");
assert_eq!(mypod.types.as_ref().unwrap().api_version, "v1");
}
}