1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use std::fmt;
use std::fmt::Debug;
use std::fmt::Display;
use std::io::Error as IoError;

use crate::K8Obj;
use crate::ObjectMeta;
use crate::Spec;

// Spec that can store in meta store
pub trait StoreSpec: Sized + Default + Debug + Clone {
    type K8Spec: Spec;
    type Status: Sized + Clone + Default + Debug;
    type Key: Ord + Clone + Debug + ToString;
    type Owner: StoreSpec;

    const LABEL: &'static str;

    // convert kubernetes objects into KV value
    fn convert_from_k8(k8_obj: K8Obj<Self::K8Spec>) -> Result<Option<MetaItem<Self>>, IoError>;
}

/// Metadata object. Used to be KVObject int sc-core
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct MetaItem<S>
where
    S: StoreSpec,
{
    pub spec: S,
    pub status: S::Status,
    pub key: S::Key,
    pub ctx: MetaItemContext,
}

impl<S> MetaItem<S>
where
    S: StoreSpec,
{
    pub fn new<J>(key: J, spec: S, status: S::Status, ctx: MetaItemContext) -> Self
    where
        J: Into<S::Key>,
    {
        Self {
            key: key.into(),
            spec,
            status,
            ctx,
        }
    }

    pub fn with_ctx(mut self, ctx: MetaItemContext) -> Self {
        self.ctx = ctx;
        self
    }

    pub fn key(&self) -> &S::Key {
        &self.key
    }

    pub fn key_owned(&self) -> S::Key {
        self.key.clone()
    }

    pub fn my_key(self) -> S::Key {
        self.key
    }

    pub fn spec(&self) -> &S {
        &self.spec
    }
    pub fn status(&self) -> &S::Status {
        &self.status
    }

    pub fn set_status(&mut self, status: S::Status) {
        self.status = status;
    }

    pub fn ctx(&self) -> &MetaItemContext {
        &self.ctx
    }

    pub fn set_ctx(&mut self, ctx: MetaItemContext) {
        self.ctx = ctx;
    }

    pub fn parts(self) -> (S::Key, S, MetaItemContext) {
        (self.key, self.spec, self.ctx)
    }

    pub fn is_owned(&self, uid: &str) -> bool {
        match &self.ctx.parent_ctx {
            Some(parent) => parent.uid == uid,
            None => false,
        }
    }

    pub fn with_spec<J>(key: J, spec: S) -> Self
    where
        J: Into<S::Key>,
    {
        Self::new(
            key.into(),
            spec,
            S::Status::default(),
            MetaItemContext::default(),
        )
    }
}

impl<S> fmt::Display for MetaItem<S>
where
    S: StoreSpec,
    S::Key: Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "MetaItem {} key: {}", S::LABEL, self.key())
    }
}

impl<S> From<MetaItem<S>> for (S::Key, S, S::Status)
where
    S: StoreSpec,
{
    fn from(val: MetaItem<S>) -> Self {
        (val.key, val.spec, val.status)
    }
}

#[derive(Default, Debug, Eq, PartialEq, Clone)]
pub struct MetaItemContext {
    pub item_ctx: Option<ObjectMeta>,
    pub parent_ctx: Option<ObjectMeta>,
}

impl MetaItemContext {
    pub fn with_ctx(mut self, ctx: ObjectMeta) -> Self {
        self.item_ctx = Some(ctx);
        self
    }

    pub fn with_parent_ctx(mut self, ctx: ObjectMeta) -> Self {
        self.parent_ctx = Some(ctx);
        self
    }

    pub fn make_parent_ctx(&self) -> Self {
        if self.item_ctx.is_some() {
            Self::default().with_parent_ctx(self.item_ctx.as_ref().unwrap().clone())
        } else {
            Self::default()
        }
    }
}

/// define default store spec assuming key is string
#[macro_export]
macro_rules! default_store_spec {
    ($spec:ident,$status:ident,$name:expr) => {
        impl $crate::store::StoreSpec for $spec {
            const LABEL: &'static str = $name;

            type K8Spec = Self;
            type Status = $status;
            type Key = String;
            type Owner = Self;

            fn convert_from_k8(
                k8_obj: $crate::K8Obj<Self::K8Spec>,
            ) -> Result<Option<$crate::store::MetaItem<Self>>, std::io::Error> {
                let ctx =
                    $crate::store::MetaItemContext::default().with_ctx(k8_obj.metadata.clone());
                Ok(Some($crate::store::MetaItem::new(
                    k8_obj.metadata.name,
                    k8_obj.spec,
                    k8_obj.status,
                    ctx,
                )))
            }
        }
    };
}