koprs 0.8.3

A reusable, ergonomic library that streamlines Kubernetes operator development, allowing developers to build controllers with significantly less code.
Documentation
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
// src/tests/finalizers.rs

#[cfg(test)]
mod finalizers_tests {
    use http::{Request, Response, StatusCode};
    use k8s_openapi::api::core::v1::{ConfigMap, Node};
    use kube::Client;
    use kube::client::Body;
    use serde_json::json;
    use tower_test::mock;

    use crate::finalizers::{add_finalizer, add_finalizer_namespaced, remove_finalizers};
    use crate::scope::{Cluster, Namespaced};

    // -----------------------------------------------------------------------
    // Harness
    // -----------------------------------------------------------------------

    type MockHandle = mock::Handle<Request<Body>, Response<Body>>;

    fn mock_client() -> (Client, MockHandle) {
        let (svc, handle) = mock::pair::<Request<Body>, Response<Body>>();
        (Client::new(svc, "default"), handle)
    }

    fn json_response(body: serde_json::Value) -> Response<Body> {
        let bytes = serde_json::to_vec(&body).unwrap();
        Response::builder()
            .status(StatusCode::OK)
            .header("Content-Type", "application/json")
            .body(Body::from(bytes))
            .unwrap()
    }

    fn server_error_response() -> Response<Body> {
        let body = json!({
            "apiVersion": "v1",
            "kind": "Status",
            "status": "Failure",
            "reason": "InternalError",
            "code": 500
        });
        Response::builder()
            .status(StatusCode::INTERNAL_SERVER_ERROR)
            .header("Content-Type", "application/json")
            .body(Body::from(serde_json::to_vec(&body).unwrap()))
            .unwrap()
    }

    fn configmap_json(name: &str, namespace: &str, finalizers: &[&str]) -> serde_json::Value {
        json!({
            "apiVersion": "v1",
            "kind": "ConfigMap",
            "metadata": {
                "name": name,
                "namespace": namespace,
                "resourceVersion": "1",
                "finalizers": finalizers
            }
        })
    }

    fn node_json(name: &str, finalizers: &[&str]) -> serde_json::Value {
        json!({
            "apiVersion": "v1",
            "kind": "Node",
            "metadata": {
                "name": name,
                "resourceVersion": "1",
                "finalizers": finalizers
            }
        })
    }

    fn configmap(name: &str, namespace: &str, finalizers: &[&str]) -> ConfigMap {
        serde_json::from_value(configmap_json(name, namespace, finalizers)).unwrap()
    }

    fn node(name: &str, finalizers: &[&str]) -> Node {
        serde_json::from_value(node_json(name, finalizers)).unwrap()
    }

    async fn read_body_json(req: Request<Body>) -> serde_json::Value {
        use http_body_util::BodyExt as _;
        let bytes = req.into_body().collect().await.unwrap().to_bytes();
        serde_json::from_slice(&bytes).unwrap()
    }

    // -----------------------------------------------------------------------
    // add_finalizer — guard: no API call when already present
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn add_finalizer_is_noop_when_finalizer_already_present() {
        let (client, handle) = mock_client();
        let cm = configmap("my-cm", "my-ns", &["my-op/cleanup"]);

        // Spawn server side — it must NOT receive any request.
        let server = tokio::spawn(async move {
            // Give the client side time to complete without a request.
            tokio::time::sleep(std::time::Duration::from_millis(50)).await;
            // If a request arrives after this point, next_request would return
            // Some, but we simply don't await it — the assertion is that the
            // client already returned Ok(()) without touching the handle.
            drop(handle);
        });

        let result = add_finalizer_namespaced::<ConfigMap>(client, &cm, "my-op/cleanup")
            .await
            .unwrap();

        // Returned resource is the same as the input (cloned, no server round-trip).
        assert_eq!(
            result.metadata.finalizers.as_deref(),
            Some(&["my-op/cleanup".to_string()][..])
        );
        server.await.unwrap();
    }

    // -----------------------------------------------------------------------
    // add_finalizer — sends patch when finalizer is absent
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn add_finalizer_namespaced_sends_merge_patch_to_correct_uri() {
        let (client, mut handle) = mock_client();
        let cm = configmap("my-cm", "my-ns", &[]);

        let server = tokio::spawn(async move {
            let (req, send) = handle.next_request().await.unwrap();
            assert_eq!(req.method(), http::Method::PATCH);
            let uri = req.uri().to_string();
            assert!(
                uri.contains("/namespaces/my-ns/configmaps/my-cm"),
                "uri={uri}"
            );
            let body = read_body_json(req).await;
            assert_eq!(
                body["metadata"]["finalizers"],
                json!(["my-op/cleanup"]),
                "patch body did not contain expected finalizer"
            );
            send.send_response(json_response(configmap_json(
                "my-cm",
                "my-ns",
                &["my-op/cleanup"],
            )));
        });

        let result =
            add_finalizer::<ConfigMap, _>(client, Namespaced("my-ns"), &cm, "my-op/cleanup")
                .await
                .unwrap();

        assert_eq!(
            result.metadata.finalizers.as_deref(),
            Some(&["my-op/cleanup".to_string()][..])
        );
        server.await.unwrap();
    }

    #[tokio::test]
    async fn add_finalizer_cluster_scoped_sends_patch_without_namespace_segment() {
        let (client, mut handle) = mock_client();
        let n = node("my-node", &[]);

        let server = tokio::spawn(async move {
            let (req, send) = handle.next_request().await.unwrap();
            assert_eq!(req.method(), http::Method::PATCH);
            let uri = req.uri().to_string();
            assert!(uri.contains("/api/v1/nodes/my-node"), "uri={uri}");
            assert!(
                !uri.contains("namespaces"),
                "unexpected namespace in uri={uri}"
            );
            let body = read_body_json(req).await;
            assert_eq!(body["metadata"]["finalizers"], json!(["my-op/cleanup"]));
            send.send_response(json_response(node_json("my-node", &["my-op/cleanup"])));
        });

        add_finalizer::<Node, _>(client, Cluster, &n, "my-op/cleanup")
            .await
            .unwrap();
        server.await.unwrap();
    }

    // -----------------------------------------------------------------------
    // add_finalizer — convenience wrappers
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn add_finalizer_namespaced_wrapper_delegates_correctly() {
        let (client, mut handle) = mock_client();
        let cm = configmap("cm1", "ns1", &[]);

        let server = tokio::spawn(async move {
            let (req, send) = handle.next_request().await.unwrap();
            let uri = req.uri().to_string();
            assert!(uri.contains("/namespaces/ns1/configmaps/cm1"), "uri={uri}");
            let body = read_body_json(req).await;
            assert_eq!(body["metadata"]["finalizers"], json!(["op/fin"]));
            send.send_response(json_response(configmap_json("cm1", "ns1", &["op/fin"])));
        });

        add_finalizer_namespaced::<ConfigMap>(client, &cm, "op/fin")
            .await
            .unwrap();
        server.await.unwrap();
    }

    #[tokio::test]
    async fn add_finalizer_cluster_wrapper_delegates_correctly() {
        let (client, mut handle) = mock_client();
        let n = node("n1", &[]);

        let server = tokio::spawn(async move {
            let (req, send) = handle.next_request().await.unwrap();
            let uri = req.uri().to_string();
            assert!(uri.contains("/api/v1/nodes/n1"), "uri={uri}");
            assert!(!uri.contains("namespaces"), "uri={uri}");
            let body = read_body_json(req).await;
            assert_eq!(body["metadata"]["finalizers"], json!(["op/fin"]));
            send.send_response(json_response(node_json("n1", &["op/fin"])));
        });

        add_finalizer::<Node, _>(client, Cluster, &n, "op/fin")
            .await
            .unwrap();
        server.await.unwrap();
    }

    // -----------------------------------------------------------------------
    // remove_finalizers — generic, scope-dispatched
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn remove_finalizers_namespaced_patches_finalizers_to_null() {
        let (client, mut handle) = mock_client();

        let server = tokio::spawn(async move {
            let (req, send) = handle.next_request().await.unwrap();
            assert_eq!(req.method(), http::Method::PATCH);
            let uri = req.uri().to_string();
            assert!(
                uri.contains("/namespaces/my-ns/configmaps/my-cm"),
                "uri={uri}"
            );
            let body = read_body_json(req).await;
            assert_eq!(
                body["metadata"]["finalizers"],
                serde_json::Value::Null,
                "expected finalizers to be null in patch body"
            );
            send.send_response(json_response(configmap_json("my-cm", "my-ns", &[])));
        });

        let result = remove_finalizers::<ConfigMap, _>(client, Namespaced("my-ns"), "my-cm")
            .await
            .unwrap();
        assert!(
            result
                .metadata
                .finalizers
                .as_ref()
                .map_or(true, |f| f.is_empty()),
            "expected no finalizers on returned resource"
        );
        server.await.unwrap();
    }

    #[tokio::test]
    async fn remove_finalizers_cluster_scoped_patches_without_namespace_segment() {
        let (client, mut handle) = mock_client();

        let server = tokio::spawn(async move {
            let (req, send) = handle.next_request().await.unwrap();
            let uri = req.uri().to_string();
            assert!(uri.contains("/api/v1/nodes/my-node"), "uri={uri}");
            assert!(!uri.contains("namespaces"), "uri={uri}");
            let body = read_body_json(req).await;
            assert_eq!(body["metadata"]["finalizers"], serde_json::Value::Null);
            send.send_response(json_response(node_json("my-node", &[])));
        });

        remove_finalizers::<Node, _>(client, Cluster, "my-node")
            .await
            .unwrap();
        server.await.unwrap();
    }

    // -----------------------------------------------------------------------
    // remove_finalizers — convenience wrappers
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn remove_finalizers_namespaced_wrapper_delegates_correctly() {
        let (client, mut handle) = mock_client();

        let server = tokio::spawn(async move {
            let (req, send) = handle.next_request().await.unwrap();
            let uri = req.uri().to_string();
            assert!(uri.contains("/namespaces/ns1/configmaps/cm1"), "uri={uri}");
            let body = read_body_json(req).await;
            assert_eq!(body["metadata"]["finalizers"], serde_json::Value::Null);
            send.send_response(json_response(configmap_json("cm1", "ns1", &[])));
        });

        remove_finalizers::<ConfigMap, _>(client, Namespaced("ns1"), "cm1")
            .await
            .unwrap();
        server.await.unwrap();
    }

    #[tokio::test]
    async fn remove_finalizers_cluster_wrapper_delegates_correctly() {
        let (client, mut handle) = mock_client();

        let server = tokio::spawn(async move {
            let (req, send) = handle.next_request().await.unwrap();
            let uri = req.uri().to_string();
            assert!(uri.contains("/api/v1/nodes/n1"), "uri={uri}");
            assert!(!uri.contains("namespaces"), "uri={uri}");
            let body = read_body_json(req).await;
            assert_eq!(body["metadata"]["finalizers"], serde_json::Value::Null);
            send.send_response(json_response(node_json("n1", &[])));
        });

        remove_finalizers::<Node, _>(client, Cluster, "n1")
            .await
            .unwrap();
        server.await.unwrap();
    }

    // -----------------------------------------------------------------------
    // add_finalizer — server response and error propagation
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn add_finalizer_returns_resource_with_finalizer_from_server_response() {
        let (client, mut handle) = mock_client();
        let cm = configmap("cm1", "ns1", &["existing/fin"]);

        let server = tokio::spawn(async move {
            let (_req, send) = handle.next_request().await.unwrap();
            send.send_response(json_response(configmap_json(
                "cm1",
                "ns1",
                &["existing/fin", "my-op/cleanup"],
            )));
        });

        let result = add_finalizer_namespaced::<ConfigMap>(client, &cm, "my-op/cleanup")
            .await
            .unwrap();

        let fins = result.metadata.finalizers.unwrap();
        assert!(fins.contains(&"existing/fin".to_string()));
        assert!(fins.contains(&"my-op/cleanup".to_string()));
        server.await.unwrap();
    }

    // -----------------------------------------------------------------------
    // add_finalizer — preserves existing finalizers in the patch body
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn add_finalizer_preserves_existing_finalizers_in_patch_body() {
        let (client, mut handle) = mock_client();
        // Resource already has one finalizer; adding a second must not drop the first.
        let cm = configmap("cm1", "ns1", &["existing/fin"]);

        let server = tokio::spawn(async move {
            let (req, send) = handle.next_request().await.unwrap();
            let body = read_body_json(req).await;
            let fins = body["metadata"]["finalizers"].as_array().unwrap().clone();
            let fin_strs: Vec<&str> = fins.iter().map(|v| v.as_str().unwrap()).collect();
            assert!(
                fin_strs.contains(&"existing/fin"),
                "existing finalizer must be preserved: {fin_strs:?}"
            );
            assert!(
                fin_strs.contains(&"my-op/cleanup"),
                "new finalizer must be present: {fin_strs:?}"
            );
            send.send_response(json_response(configmap_json(
                "cm1",
                "ns1",
                &["existing/fin", "my-op/cleanup"],
            )));
        });

        add_finalizer_namespaced::<ConfigMap>(client, &cm, "my-op/cleanup")
            .await
            .unwrap();
        server.await.unwrap();
    }

    #[tokio::test]
    async fn add_finalizer_propagates_server_errors() {
        let (client, mut handle) = mock_client();
        let cm = configmap("cm1", "ns1", &[]);

        let server = tokio::spawn(async move {
            let (_req, send) = handle.next_request().await.unwrap();
            send.send_response(server_error_response());
        });

        let result = add_finalizer::<ConfigMap, _>(client, Namespaced("ns1"), &cm, "op/fin").await;
        assert!(result.is_err(), "expected Err on 500, got Ok");
        server.await.unwrap();
    }

    #[tokio::test]
    async fn remove_finalizers_propagates_server_errors() {
        let (client, mut handle) = mock_client();

        let server = tokio::spawn(async move {
            let (_req, send) = handle.next_request().await.unwrap();
            send.send_response(server_error_response());
        });

        let result = remove_finalizers::<ConfigMap, _>(client, Namespaced("ns1"), "cm1").await;
        assert!(result.is_err(), "expected Err on 500, got Ok");
        server.await.unwrap();
    }
}