autumn-plugin-kubernetes 0.2.0

Kubernetes plugin for autumn-web: pod identity, Lease leader election, live ConfigMaps, Events, health, metrics, and manifests
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//! In-memory fake of the Kubernetes API.

use std::collections::{BTreeMap, HashMap};
use std::sync::{Arc, Mutex, PoisonError};

use futures::stream::BoxStream;
use tokio::sync::watch;

use super::{ApiFuture, ConfigMapEvent, KubeApi, LeaseRecord, PodEvent, PodRef};
use crate::error::KubeError;

type Key = (String, String);

/// Fake state. The flags are independent fault switches.
#[derive(Default)]
#[allow(clippy::struct_excessive_bools)]
struct State {
    leases: HashMap<Key, LeaseRecord>,
    next_version: u64,
    down: bool,
    failing_lease_writes: u32,
    forbidden_leases: bool,
    events_failing: bool,
    config_maps: HashMap<Key, watch::Sender<Option<BTreeMap<String, String>>>>,
    events: Vec<(PodRef, PodEvent)>,
    lease_writes: u64,
    latency: std::time::Duration,
    write_reply_delay: std::time::Duration,
    conflicting_lease_writes: u32,
    forbidden_config_maps: bool,
}

/// In-memory fake of the Kubernetes API. Clones share state.
///
/// It follows the API server rules that `tests/common/contract.rs` checks:
/// create fails when the Lease exists, and replace needs the current
/// `resourceVersion`. It does not model metadata, microsecond time, or no-op
/// updates.
#[derive(Clone, Default)]
pub struct MemoryKubeApi {
    state: Arc<Mutex<State>>,
    namespace: Arc<str>,
}

impl std::fmt::Debug for MemoryKubeApi {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MemoryKubeApi")
            .field("namespace", &self.namespace)
            .finish_non_exhaustive()
    }
}

fn key(namespace: &str, name: &str) -> Key {
    (namespace.to_owned(), name.to_owned())
}

fn down_error() -> KubeError {
    KubeError::Api("connection refused (MemoryKubeApi is down)".to_owned())
}

impl MemoryKubeApi {
    /// Makes an empty fake. The default namespace is `default`.
    #[must_use]
    pub fn new() -> Self {
        Self {
            state: Arc::default(),
            namespace: Arc::from("default"),
        }
    }

    /// Sets the default namespace.
    #[must_use]
    pub fn with_namespace(mut self, namespace: &str) -> Self {
        self.namespace = Arc::from(namespace);
        self
    }

    fn lock(&self) -> std::sync::MutexGuard<'_, State> {
        self.state.lock().unwrap_or_else(PoisonError::into_inner)
    }

    /// Returns the stored Lease.
    #[must_use]
    pub fn lease(&self, namespace: &str, name: &str) -> Option<LeaseRecord> {
        self.lock().leases.get(&key(namespace, name)).cloned()
    }

    /// Writes a Lease as another writer would. It gets a new version.
    pub fn put_lease(&self, namespace: &str, name: &str, mut record: LeaseRecord) {
        let mut s = self.lock();
        s.next_version += 1;
        record.resource_version = Some(s.next_version.to_string());
        s.leases.insert(key(namespace, name), record);
    }

    /// Number of successful Lease creates and replaces.
    #[must_use]
    pub fn lease_writes(&self) -> u64 {
        self.lock().lease_writes
    }

    /// When `true`, every call fails like a network outage.
    pub fn set_down(&self, down: bool) {
        self.lock().down = down;
        self.wake_watchers();
    }

    /// The next `n` Lease writes fail with an API error.
    pub fn fail_lease_writes(&self, n: u32) {
        self.lock().failing_lease_writes = n;
    }

    /// When `true`, Lease calls fail with HTTP 403.
    pub fn set_leases_forbidden(&self, forbidden: bool) {
        self.lock().forbidden_leases = forbidden;
    }

    /// Every Lease call and version check waits this long first.
    pub fn set_latency(&self, latency: std::time::Duration) {
        self.lock().latency = latency;
    }

    fn latency(&self) -> std::time::Duration {
        self.lock().latency
    }

    /// Lease writes land at once, but their reply waits this long.
    pub fn set_write_reply_delay(&self, delay: std::time::Duration) {
        self.lock().write_reply_delay = delay;
    }

    /// The next `n` Lease writes fail with a write conflict (HTTP 409).
    pub fn conflict_lease_writes(&self, n: u32) {
        self.lock().conflicting_lease_writes = n;
    }

    fn write_reply_delay(&self) -> std::time::Duration {
        self.lock().write_reply_delay
    }

    /// When `true`, ConfigMap watches fail with HTTP 403.
    pub fn set_config_maps_forbidden(&self, forbidden: bool) {
        self.lock().forbidden_config_maps = forbidden;
        self.wake_watchers();
    }

    /// Wakes every watch, so it sees a changed fault state.
    fn wake_watchers(&self) {
        let senders: Vec<_> = self.lock().config_maps.values().cloned().collect();
        for tx in senders {
            tx.send_modify(|_| {});
        }
    }

    /// The watch error now, if any.
    fn watch_fault(&self) -> Option<KubeError> {
        let s = self.lock();
        if s.down {
            Some(down_error())
        } else if s.forbidden_config_maps {
            Some(KubeError::Forbidden {
                verb: "list".to_owned(),
                resource: "configmaps".to_owned(),
            })
        } else {
            None
        }
    }

    /// When `true`, event writes fail.
    pub fn set_events_failing(&self, failing: bool) {
        self.lock().events_failing = failing;
    }

    fn config_map_sender(
        &self,
        namespace: &str,
        name: &str,
    ) -> watch::Sender<Option<BTreeMap<String, String>>> {
        self.lock()
            .config_maps
            .entry(key(namespace, name))
            .or_insert_with(|| watch::channel(None).0)
            .clone()
    }

    /// Creates or updates a ConfigMap.
    pub fn put_config_map(&self, namespace: &str, name: &str, data: BTreeMap<String, String>) {
        self.config_map_sender(namespace, name)
            .send_replace(Some(data));
    }

    /// Deletes a ConfigMap.
    pub fn delete_config_map(&self, namespace: &str, name: &str) {
        self.config_map_sender(namespace, name).send_replace(None);
    }

    /// Returns the published events.
    #[must_use]
    pub fn events(&self) -> Vec<(PodRef, PodEvent)> {
        self.lock().events.clone()
    }

    /// Checks outage and RBAC faults for a Lease call.
    fn lease_guard(s: &State, verb: &str) -> Result<(), KubeError> {
        if s.down {
            return Err(down_error());
        }
        if s.forbidden_leases {
            return Err(KubeError::Forbidden {
                verb: verb.to_owned(),
                resource: "leases.coordination.k8s.io".to_owned(),
            });
        }
        Ok(())
    }

    /// Checks faults for a Lease write. Uses up one injected failure.
    fn write_guard(s: &mut State, verb: &str) -> Result<(), KubeError> {
        Self::lease_guard(s, verb)?;
        if s.failing_lease_writes > 0 {
            s.failing_lease_writes -= 1;
            return Err(KubeError::Api("injected lease write failure".to_owned()));
        }
        if s.conflicting_lease_writes > 0 {
            s.conflicting_lease_writes -= 1;
            return Err(KubeError::Conflict(
                "injected lease write conflict".to_owned(),
            ));
        }
        Ok(())
    }

    fn record_event(&self, pod: &PodRef, event: &PodEvent) -> Result<(), KubeError> {
        let mut s = self.lock();
        if s.down {
            return Err(down_error());
        }
        if s.events_failing {
            return Err(KubeError::Api("injected event failure".to_owned()));
        }
        s.events.push((pod.clone(), event.clone()));
        drop(s);
        Ok(())
    }

    fn store(s: &mut State, k: Key, record: &LeaseRecord) -> LeaseRecord {
        s.next_version += 1;
        s.lease_writes += 1;
        let mut stored = record.clone();
        stored.resource_version = Some(s.next_version.to_string());
        s.leases.insert(k, stored.clone());
        stored
    }
}

impl MemoryKubeApi {
    fn do_get(&self, namespace: &str, name: &str) -> Result<Option<LeaseRecord>, KubeError> {
        let s = self.lock();
        Self::lease_guard(&s, "get")?;
        Ok(s.leases.get(&key(namespace, name)).cloned())
    }

    fn do_create(
        &self,
        namespace: &str,
        name: &str,
        record: &LeaseRecord,
    ) -> Result<LeaseRecord, KubeError> {
        let mut s = self.lock();
        Self::write_guard(&mut s, "create")?;
        let k = key(namespace, name);
        let result = if s.leases.contains_key(&k) {
            Err(KubeError::Conflict(format!(
                "leases.coordination.k8s.io \"{name}\" already exists"
            )))
        } else {
            Ok(Self::store(&mut s, k, record))
        };
        drop(s);
        result
    }

    fn do_replace(
        &self,
        namespace: &str,
        name: &str,
        record: &LeaseRecord,
    ) -> Result<LeaseRecord, KubeError> {
        let mut s = self.lock();
        Self::write_guard(&mut s, "update")?;
        let k = key(namespace, name);
        // Like a merge patch with a version check: no create.
        let result = match s.leases.get(&k) {
            None => Err(KubeError::NotFound(format!(
                "leases.coordination.k8s.io \"{name}\" not found"
            ))),
            Some(current)
                if record.resource_version.is_none()
                    || record.resource_version != current.resource_version =>
            {
                Err(KubeError::Conflict(format!(
                    "Operation cannot be fulfilled on leases.coordination.k8s.io \"{name}\": \
                     the object has been modified"
                )))
            }
            Some(_) => Ok(Self::store(&mut s, k, record)),
        };
        drop(s);
        result
    }
}

async fn wait(latency: std::time::Duration) {
    if !latency.is_zero() {
        tokio::time::sleep(latency).await;
    }
}

impl KubeApi for MemoryKubeApi {
    fn server_version(&self) -> ApiFuture<'_, String> {
        let latency = self.latency();
        Box::pin(async move {
            wait(latency).await;
            if self.lock().down {
                Err(down_error())
            } else {
                Ok("v1.34.0-memory".to_owned())
            }
        })
    }

    fn get_lease<'a>(
        &'a self,
        namespace: &'a str,
        name: &'a str,
    ) -> ApiFuture<'a, Option<LeaseRecord>> {
        let latency = self.latency();
        Box::pin(async move {
            wait(latency).await;
            self.do_get(namespace, name)
        })
    }

    fn create_lease<'a>(
        &'a self,
        namespace: &'a str,
        name: &'a str,
        record: &'a LeaseRecord,
    ) -> ApiFuture<'a, LeaseRecord> {
        let latency = self.latency();
        let reply = self.write_reply_delay();
        Box::pin(async move {
            wait(latency).await;
            let result = self.do_create(namespace, name, record);
            wait(reply).await;
            result
        })
    }

    fn replace_lease<'a>(
        &'a self,
        namespace: &'a str,
        name: &'a str,
        record: &'a LeaseRecord,
    ) -> ApiFuture<'a, LeaseRecord> {
        let latency = self.latency();
        let reply = self.write_reply_delay();
        Box::pin(async move {
            wait(latency).await;
            let result = self.do_replace(namespace, name, record);
            wait(reply).await;
            result
        })
    }

    fn watch_config_map(
        &self,
        namespace: &str,
        name: &str,
    ) -> BoxStream<'static, Result<ConfigMapEvent, KubeError>> {
        // Like the kube watcher with backoff, the stream does not end. While
        // a fault is active, the stream sends one error each second. After
        // the fault, it sends the current state again (a relist).
        let rx = self.config_map_sender(namespace, name).subscribe();
        let api = self.clone();
        Box::pin(futures::stream::unfold(
            (rx, api, true, false),
            |(mut rx, api, mut relist, mut erred)| async move {
                loop {
                    if let Some(err) = api.watch_fault() {
                        if erred {
                            tokio::time::sleep(std::time::Duration::from_secs(1)).await;
                            if api.watch_fault().is_none() {
                                continue;
                            }
                        }
                        return Some((Err(err), (rx, api, true, true)));
                    }
                    if relist {
                        let event = rx
                            .borrow_and_update()
                            .clone()
                            .map_or(ConfigMapEvent::Deleted, ConfigMapEvent::Applied);
                        return Some((Ok(event), (rx, api, false, false)));
                    }
                    erred = false;
                    if rx.changed().await.is_err() {
                        return None;
                    }
                    // A fault can wake the stream. Check for a fault first.
                    relist = api.watch_fault().is_none();
                }
            },
        ))
    }

    fn publish_event<'a>(&'a self, pod: &'a PodRef, event: &'a PodEvent) -> ApiFuture<'a, ()> {
        let result = self.record_event(pod, event);
        Box::pin(async move { result })
    }

    fn default_namespace(&self) -> String {
        self.namespace.to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::api::EventKind;
    use futures::StreamExt;

    fn rec(holder: &str) -> LeaseRecord {
        LeaseRecord {
            holder: Some(holder.to_owned()),
            lease_duration_secs: 15,
            ..LeaseRecord::default()
        }
    }

    #[tokio::test]
    async fn create_then_get_sets_a_version() {
        let api = MemoryKubeApi::new();
        assert_eq!(api.get_lease("ns", "l").await.unwrap(), None);
        let created = api.create_lease("ns", "l", &rec("a")).await.unwrap();
        assert!(created.resource_version.is_some());
        assert_eq!(api.get_lease("ns", "l").await.unwrap(), Some(created));
        assert_eq!(api.lease_writes(), 1);
    }

    #[tokio::test]
    async fn create_twice_is_a_conflict() {
        let api = MemoryKubeApi::new();
        api.create_lease("ns", "l", &rec("a")).await.unwrap();
        let err = api.create_lease("ns", "l", &rec("b")).await.unwrap_err();
        assert!(matches!(err, KubeError::Conflict(_)), "{err}");
        assert!(api.lease("ns", "l").unwrap().held_by("a"));
    }

    #[tokio::test]
    async fn replace_needs_current_version() {
        let api = MemoryKubeApi::new();
        let v1 = api.create_lease("ns", "l", &rec("a")).await.unwrap();
        let mut next = v1.clone();
        next.holder = Some("b".into());
        let v2 = api.replace_lease("ns", "l", &next).await.unwrap();
        assert_ne!(v1.resource_version, v2.resource_version);
        // Old version loses.
        let err = api.replace_lease("ns", "l", &v1).await.unwrap_err();
        assert!(matches!(err, KubeError::Conflict(_)), "{err}");
        // No version loses.
        let mut none = v2.clone();
        none.resource_version = None;
        assert!(matches!(
            api.replace_lease("ns", "l", &none).await,
            Err(KubeError::Conflict(_))
        ));
    }

    #[tokio::test]
    async fn replace_missing_is_not_found() {
        let api = MemoryKubeApi::new();
        let mut with_version = rec("a");
        with_version.resource_version = Some("9".into());
        let err = api
            .replace_lease("ns", "l", &with_version)
            .await
            .unwrap_err();
        assert!(matches!(err, KubeError::NotFound(_)), "{err}");
    }

    #[tokio::test]
    async fn put_lease_bumps_version() {
        let api = MemoryKubeApi::new();
        let v1 = api.create_lease("ns", "l", &rec("a")).await.unwrap();
        api.put_lease("ns", "l", rec("other"));
        let now = api.lease("ns", "l").unwrap();
        assert!(now.held_by("other"));
        assert_ne!(now.resource_version, v1.resource_version);
        assert!(matches!(
            api.replace_lease("ns", "l", &v1).await,
            Err(KubeError::Conflict(_))
        ));
    }

    #[tokio::test]
    async fn outage_and_faults() {
        let api = MemoryKubeApi::new();
        api.set_down(true);
        assert!(matches!(api.server_version().await, Err(KubeError::Api(_))));
        assert!(api.get_lease("ns", "l").await.is_err());
        api.set_down(false);
        assert!(api.server_version().await.unwrap().starts_with('v'));

        api.fail_lease_writes(1);
        assert!(matches!(
            api.create_lease("ns", "l", &rec("a")).await,
            Err(KubeError::Api(_))
        ));
        api.create_lease("ns", "l", &rec("a")).await.unwrap();

        api.set_leases_forbidden(true);
        let err = api.get_lease("ns", "l").await.unwrap_err();
        assert!(matches!(err, KubeError::Forbidden { .. }), "{err}");
    }

    #[tokio::test]
    async fn config_map_watch_sees_state_then_changes() {
        let api = MemoryKubeApi::new();
        let data = BTreeMap::from([("k".to_owned(), "v1".to_owned())]);
        api.put_config_map("ns", "flags", data.clone());
        let mut w = api.watch_config_map("ns", "flags");
        assert_eq!(
            w.next().await.unwrap().unwrap(),
            ConfigMapEvent::Applied(data)
        );
        let data2 = BTreeMap::from([("k".to_owned(), "v2".to_owned())]);
        api.put_config_map("ns", "flags", data2.clone());
        assert_eq!(
            w.next().await.unwrap().unwrap(),
            ConfigMapEvent::Applied(data2)
        );
        api.delete_config_map("ns", "flags");
        assert_eq!(w.next().await.unwrap().unwrap(), ConfigMapEvent::Deleted);
    }

    #[tokio::test]
    async fn watch_of_missing_config_map_starts_deleted() {
        let api = MemoryKubeApi::new();
        let mut w = api.watch_config_map("ns", "none");
        assert_eq!(w.next().await.unwrap().unwrap(), ConfigMapEvent::Deleted);
    }

    #[tokio::test]
    async fn events_are_recorded_or_fail() {
        let api = MemoryKubeApi::new().with_namespace("shop");
        assert_eq!(api.default_namespace(), "shop");
        let pod = PodRef {
            namespace: "shop".into(),
            name: "p".into(),
            uid: None,
        };
        let ev = PodEvent {
            kind: EventKind::Normal,
            reason: "Started".into(),
            action: "Start".into(),
            note: None,
        };
        api.publish_event(&pod, &ev).await.unwrap();
        assert_eq!(api.events(), vec![(pod.clone(), ev.clone())]);
        api.set_events_failing(true);
        assert!(api.publish_event(&pod, &ev).await.is_err());
        assert_eq!(api.events().len(), 1);
    }

    #[test]
    fn default_namespace_is_default() {
        assert_eq!(MemoryKubeApi::new().default_namespace(), "default");
        assert!(format!("{:?}", MemoryKubeApi::new()).contains("default"));
    }
}