engenho-store 0.1.3

engenho's K8s resource store — etcd-equivalent backed by openraft. Separate Raft group from engenho-revoada (which commits role assignments); this commits K8s resource CRUD. Layer of the Pillar 7 runtime that engenho-apiserver wraps to serve the K8s API surface.
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! `ResourceCatalog` — the deterministic state machine over the
//! Raft log. Each committed [`ResourceCommand`] mutates this;
//! followers and leader converge by replay.

use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

use crate::command::{ResourceCommand, ResourceOp};
use crate::resource::{ResourceKey, ResourceValue};

/// In-memory K8s resource catalog. Keyed by [`ResourceKey`] (which
/// encodes group + version + kind + namespace + name), values are
/// opaque [`ResourceValue`] (serde_json::Value) at R6.
///
/// The catalog tracks `last_applied_index` so consumers (the
/// apiserver, watchers) can express read-after-write semantics by
/// waiting for a specific index.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ResourceCatalog {
    pub resources: BTreeMap<ResourceKey, ResourceValue>,
    pub last_applied_term: u64,
    pub last_applied_index: u64,
}

// serde_json doesn't support struct map-keys directly. Custom impl
// flattens the BTreeMap into Vec<(ResourceKey, ResourceValue)> at
// the wire — preserves order (BTreeMap iter is sorted) so the
// resulting bytes are deterministic across nodes.
impl Serialize for ResourceCatalog {
    fn serialize<S: serde::Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeStruct;
        let mut state = ser.serialize_struct("ResourceCatalog", 3)?;
        let entries: Vec<(&ResourceKey, &ResourceValue)> = self.resources.iter().collect();
        state.serialize_field("resources", &entries)?;
        state.serialize_field("last_applied_term", &self.last_applied_term)?;
        state.serialize_field("last_applied_index", &self.last_applied_index)?;
        state.end()
    }
}

impl<'de> Deserialize<'de> for ResourceCatalog {
    fn deserialize<D: serde::Deserializer<'de>>(de: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        struct Helper {
            resources: Vec<(ResourceKey, ResourceValue)>,
            last_applied_term: u64,
            last_applied_index: u64,
        }
        let h = Helper::deserialize(de)?;
        Ok(Self {
            resources: h.resources.into_iter().collect(),
            last_applied_term: h.last_applied_term,
            last_applied_index: h.last_applied_index,
        })
    }
}

/// Snapshot for serde — same shape as the catalog. Provided as a
/// distinct type for forward compat (R6.5 may store the snapshot
/// in a more compact compressed form on disk).
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct ResourceCatalogSnapshot {
    pub catalog: ResourceCatalog,
}

impl ResourceCatalog {
    /// Apply a single committed command. Pure function. Returns
    /// the operation outcome the apiserver / client gets back.
    ///
    /// Resource versioning: every successful Put / Patch /
    /// Delete writes `metadata.resourceVersion = index` (the
    /// committed Raft log index). On Put, `metadata.uid` is set
    /// to a deterministic hash of `(key, index)` if it doesn't
    /// already exist. This mirrors etcd-key-versioning behavior
    /// at the apiserver boundary.
    pub fn apply(&mut self, cmd: &ResourceCommand, term: u64, index: u64) -> ResourceOp {
        let outcome = match cmd {
            ResourceCommand::Put { key, value, .. } => self.apply_put(key, value, index),
            ResourceCommand::Patch { key, patch, .. } => self.apply_patch(key, patch, index),
            ResourceCommand::Delete { key, .. } => self.apply_delete(key),
        };
        self.last_applied_term = term;
        self.last_applied_index = index;
        outcome
    }

    fn apply_put(
        &mut self,
        key: &ResourceKey,
        value: &ResourceValue,
        index: u64,
    ) -> ResourceOp {
        let mut new_value = value.clone();
        let already_existed = self.resources.contains_key(key);
        if let Some(obj) = new_value.as_object_mut() {
            let metadata = obj
                .entry("metadata".to_string())
                .or_insert_with(|| serde_json::json!({}));
            if let Some(meta_obj) = metadata.as_object_mut() {
                meta_obj.insert(
                    "resourceVersion".to_string(),
                    serde_json::Value::String(index.to_string()),
                );
                // Set uid only if not already present + not already in store
                let needs_uid = !meta_obj.contains_key("uid")
                    && !self
                        .resources
                        .get(key)
                        .and_then(|v| v.get("metadata").and_then(|m| m.get("uid")))
                        .is_some();
                if needs_uid {
                    let uid = format!("uid-{}-{}", key.label().replace('/', "-"), index);
                    meta_obj.insert("uid".to_string(), serde_json::Value::String(uid));
                } else if let Some(prior_uid) = self
                    .resources
                    .get(key)
                    .and_then(|v| v.get("metadata"))
                    .and_then(|m| m.get("uid"))
                    .cloned()
                {
                    // Preserve uid across updates.
                    meta_obj.insert("uid".to_string(), prior_uid);
                }
            }
        }
        self.resources.insert(key.clone(), new_value);
        if already_existed {
            ResourceOp::Replaced
        } else {
            ResourceOp::Created
        }
    }

    fn apply_patch(
        &mut self,
        key: &ResourceKey,
        patch: &ResourceValue,
        index: u64,
    ) -> ResourceOp {
        let Some(existing) = self.resources.get_mut(key) else {
            return ResourceOp::NoOp;
        };
        merge_json(existing, patch);
        if let Some(obj) = existing.as_object_mut() {
            let metadata = obj
                .entry("metadata".to_string())
                .or_insert_with(|| serde_json::json!({}));
            if let Some(meta_obj) = metadata.as_object_mut() {
                meta_obj.insert(
                    "resourceVersion".to_string(),
                    serde_json::Value::String(index.to_string()),
                );
            }
        }
        ResourceOp::Patched
    }

    fn apply_delete(&mut self, key: &ResourceKey) -> ResourceOp {
        if self.resources.remove(key).is_some() {
            ResourceOp::Deleted
        } else {
            ResourceOp::NoOp
        }
    }

    /// Read a single resource by key.
    #[must_use]
    pub fn get(&self, key: &ResourceKey) -> Option<&ResourceValue> {
        self.resources.get(key)
    }

    /// List resources matching (group, version, kind), optionally
    /// scoped to a namespace.
    #[must_use]
    pub fn list(
        &self,
        group: &str,
        version: &str,
        kind: &str,
        namespace: Option<&str>,
    ) -> Vec<(&ResourceKey, &ResourceValue)> {
        self.resources
            .iter()
            .filter(|(k, _)| k.group == group && k.version == version && k.kind == kind)
            .filter(|(k, _)| match (namespace, k.namespace.as_deref()) {
                (None, _) => true,
                (Some(want), Some(have)) => want == have,
                (Some(_), None) => false,
            })
            .collect()
    }

    /// Total resource count (across all kinds + namespaces).
    #[must_use]
    pub fn len(&self) -> usize {
        self.resources.len()
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.resources.is_empty()
    }
}

/// Recursive JSON merge — fields in `patch` overwrite + null in
/// patch deletes. Mirrors RFC 7396 (JSON Merge Patch).
fn merge_json(target: &mut serde_json::Value, patch: &serde_json::Value) {
    if !patch.is_object() {
        *target = patch.clone();
        return;
    }
    if !target.is_object() {
        *target = serde_json::Value::Object(serde_json::Map::new());
    }
    let target_obj = target.as_object_mut().unwrap();
    for (k, v) in patch.as_object().unwrap() {
        if v.is_null() {
            target_obj.remove(k);
        } else {
            let entry = target_obj
                .entry(k.clone())
                .or_insert_with(|| serde_json::Value::Null);
            merge_json(entry, v);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::command::Reason;

    fn pod_key(name: &str) -> ResourceKey {
        ResourceKey::namespaced("", "v1", "Pod", "default", name)
    }

    #[test]
    fn put_creates_then_replaces() {
        let mut cat = ResourceCatalog::default();
        let k = pod_key("podinfo");
        let v = serde_json::json!({"spec": {"image": "v1"}});
        let op = cat.apply(
            &ResourceCommand::Put {
                key: k.clone(),
                value: v.clone(),
                reason: Reason::Operator,
            },
            1,
            1,
        );
        assert_eq!(op, ResourceOp::Created);
        assert_eq!(cat.len(), 1);

        // Replace
        let v2 = serde_json::json!({"spec": {"image": "v2"}});
        let op = cat.apply(
            &ResourceCommand::Put {
                key: k.clone(),
                value: v2,
                reason: Reason::Operator,
            },
            1,
            2,
        );
        assert_eq!(op, ResourceOp::Replaced);
        assert_eq!(cat.len(), 1);
    }

    #[test]
    fn put_sets_resource_version_and_uid() {
        let mut cat = ResourceCatalog::default();
        let k = pod_key("podinfo");
        cat.apply(
            &ResourceCommand::Put {
                key: k.clone(),
                value: serde_json::json!({"spec": {}}),
                reason: Reason::Operator,
            },
            1,
            42,
        );
        let stored = cat.get(&k).unwrap();
        let metadata = stored.get("metadata").unwrap();
        assert_eq!(metadata.get("resourceVersion").unwrap(), &serde_json::json!("42"));
        // uid must be set + stable across replaces.
        let uid_1 = metadata.get("uid").unwrap().as_str().unwrap().to_string();
        assert!(uid_1.starts_with("uid-"));
        // Replace; uid should survive.
        cat.apply(
            &ResourceCommand::Put {
                key: k.clone(),
                value: serde_json::json!({"spec": {"replaced": true}}),
                reason: Reason::Operator,
            },
            1,
            100,
        );
        let uid_2 = cat
            .get(&k)
            .unwrap()
            .get("metadata")
            .unwrap()
            .get("uid")
            .unwrap()
            .as_str()
            .unwrap()
            .to_string();
        assert_eq!(uid_1, uid_2);
    }

    #[test]
    fn patch_merges_into_existing() {
        let mut cat = ResourceCatalog::default();
        let k = pod_key("podinfo");
        cat.apply(
            &ResourceCommand::Put {
                key: k.clone(),
                value: serde_json::json!({"spec": {"image": "v1", "replicas": 2}}),
                reason: Reason::Operator,
            },
            1,
            1,
        );
        let op = cat.apply(
            &ResourceCommand::Patch {
                key: k.clone(),
                patch: serde_json::json!({"spec": {"image": "v2"}}),
                reason: Reason::Operator,
            },
            1,
            2,
        );
        assert_eq!(op, ResourceOp::Patched);
        let stored = cat.get(&k).unwrap();
        assert_eq!(stored.get("spec").unwrap().get("image").unwrap(), "v2");
        // replicas survived the merge
        assert_eq!(stored.get("spec").unwrap().get("replicas").unwrap(), 2);
    }

    #[test]
    fn patch_on_missing_is_noop() {
        let mut cat = ResourceCatalog::default();
        let op = cat.apply(
            &ResourceCommand::Patch {
                key: pod_key("ghost"),
                patch: serde_json::json!({"x": 1}),
                reason: Reason::Operator,
            },
            1,
            1,
        );
        assert_eq!(op, ResourceOp::NoOp);
    }

    #[test]
    fn delete_removes_then_noop() {
        let mut cat = ResourceCatalog::default();
        let k = pod_key("podinfo");
        cat.apply(
            &ResourceCommand::Put {
                key: k.clone(),
                value: serde_json::json!({}),
                reason: Reason::Operator,
            },
            1,
            1,
        );
        assert_eq!(cat.len(), 1);
        let op = cat.apply(
            &ResourceCommand::Delete {
                key: k.clone(),
                reason: Reason::Operator,
            },
            1,
            2,
        );
        assert_eq!(op, ResourceOp::Deleted);
        assert_eq!(cat.len(), 0);
        // Second delete is no-op.
        let op = cat.apply(
            &ResourceCommand::Delete {
                key: k.clone(),
                reason: Reason::GarbageCollector,
            },
            1,
            3,
        );
        assert_eq!(op, ResourceOp::NoOp);
    }

    #[test]
    fn list_filters_by_gvk_and_namespace() {
        let mut cat = ResourceCatalog::default();
        for name in ["a", "b", "c"] {
            cat.apply(
                &ResourceCommand::Put {
                    key: ResourceKey::namespaced("", "v1", "Pod", "default", name),
                    value: serde_json::json!({}),
                    reason: Reason::Operator,
                },
                1,
                1,
            );
        }
        cat.apply(
            &ResourceCommand::Put {
                key: ResourceKey::namespaced("", "v1", "Pod", "kube-system", "coredns"),
                value: serde_json::json!({}),
                reason: Reason::Operator,
            },
            1,
            1,
        );
        cat.apply(
            &ResourceCommand::Put {
                key: ResourceKey::namespaced("", "v1", "ConfigMap", "default", "cm-1"),
                value: serde_json::json!({}),
                reason: Reason::Operator,
            },
            1,
            1,
        );
        // List all Pods in default → a, b, c
        let pods = cat.list("", "v1", "Pod", Some("default"));
        assert_eq!(pods.len(), 3);
        // List ConfigMaps in default → 1
        let cms = cat.list("", "v1", "ConfigMap", Some("default"));
        assert_eq!(cms.len(), 1);
        // List all Pods (any namespace) → 4
        let all_pods = cat.list("", "v1", "Pod", None);
        assert_eq!(all_pods.len(), 4);
    }

    #[test]
    fn json_merge_patch_null_deletes_field() {
        let mut cat = ResourceCatalog::default();
        let k = pod_key("p");
        cat.apply(
            &ResourceCommand::Put {
                key: k.clone(),
                value: serde_json::json!({"spec": {"image": "v1", "annotations": {"a": "b"}}}),
                reason: Reason::Operator,
            },
            1,
            1,
        );
        cat.apply(
            &ResourceCommand::Patch {
                key: k.clone(),
                patch: serde_json::json!({"spec": {"annotations": null}}),
                reason: Reason::Operator,
            },
            1,
            2,
        );
        let stored = cat.get(&k).unwrap();
        assert!(stored
            .get("spec")
            .unwrap()
            .as_object()
            .unwrap()
            .get("annotations")
            .is_none());
        // Image survives
        assert_eq!(stored.get("spec").unwrap().get("image").unwrap(), "v1");
    }
}