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
use std::collections::HashSet;

use kube::core::ObjectMeta;

#[derive(Default)]
pub struct MetaCache {
    pub cache: HashSet<MetaDescribe>,
}

impl MetaCache {
    pub fn new() -> Self {
        Self {
            cache: HashSet::new(),
        }
    }
    pub fn store(&mut self, meta_desc: MetaDescribe) {
        self.cache.insert(meta_desc);
    }
    pub fn delete(&mut self, meta_desc: &MetaDescribe) {
        self.cache.remove(meta_desc);
    }
    pub fn contains(&self, meta_desc: &MetaDescribe) -> bool {
        self.cache.contains(meta_desc)
    }
    pub fn clear(&mut self) {
        self.cache.clear();
    }
}

#[derive(PartialEq, Eq, Hash, Clone)]
pub struct MetaDescribe {
    name: String,
    namespace: String,
}

impl MetaDescribe {
    pub fn from_meta(meta: &ObjectMeta) -> Self {
        let name = meta.name.clone().unwrap_or_default();
        let namespace = meta.namespace.clone().unwrap_or_default();

        Self { name, namespace }
    }
}

#[cfg(test)]
mod tests {
    use kube::core::ObjectMeta;

    use super::{MetaCache, MetaDescribe};

    #[test]
    fn cache_should_store_data() {
        let mut meta_cache = MetaCache::new();

        let obj_meta = ObjectMeta {
            name: Some("a".to_string()),
            namespace: Some("b".to_string()),
            ..ObjectMeta::default()
        };
        let meta_desc = MetaDescribe::from_meta(&obj_meta);

        meta_cache.store(meta_desc);
        assert_eq!(1, meta_cache.cache.len());
    }

    #[test]
    fn cache_should_delete_data() {
        let mut meta_cache = MetaCache::new();

        let obj_meta = ObjectMeta {
            name: Some("a".to_string()),
            namespace: Some("b".to_string()),
            ..ObjectMeta::default()
        };
        let meta_desc = MetaDescribe::from_meta(&obj_meta);

        meta_cache.store(meta_desc.clone());
        assert_eq!(1, meta_cache.cache.len());
        meta_cache.delete(&meta_desc);
        assert_eq!(0, meta_cache.cache.len());
    }

    #[test]
    fn cache_should_check_active() {
        let mut meta_cache = MetaCache::new();

        let obj_meta = ObjectMeta {
            name: Some("a".to_string()),
            namespace: Some("b".to_string()),
            ..ObjectMeta::default()
        };
        let meta_desc = MetaDescribe::from_meta(&obj_meta);

        meta_cache.store(meta_desc.clone());
        assert!(meta_cache.contains(&meta_desc));
    }
}