netget-tool 0.3.4

Network protocol stubbing toolkit: ~280 protocol servers and clients whose responses come from an LLM, a script, a static rule or a person.
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
//! Server-side `Table` rendering (`application/json;as=Table;v=1;g=meta.k8s.io`).
//!
//! This is what `kubectl get` actually asks for: it sends `Accept: application/json;as=Table;…`
//! and prints the returned cells verbatim. A real apiserver produces these with a per-kind
//! `TableConvertor`; this module is the same idea, applied to whatever objects the model
//! invented. It is presentation, not content — every value in every cell comes out of the
//! model's object.
//!
//! A model that wants full control skips this entirely and answers with `k8s_table_response`,
//! which carries explicit `columns` and `rows`.

use serde_json::{json, Value};

/// Build a `Table` from a list of objects the model supplied.
pub fn table_from_items(kind: &str, items: &[Value]) -> Value {
    let columns = columns_for_kind(kind);
    let rows: Vec<Value> = items
        .iter()
        .map(|item| {
            let cells: Vec<Value> = columns
                .iter()
                .map(|col| Value::String(cell_value(kind, col, item)))
                .collect();
            json!({
                "cells": cells,
                "object": partial_object_metadata(item),
            })
        })
        .collect();

    json!({
        "kind": "Table",
        "apiVersion": "meta.k8s.io/v1",
        "metadata": {"resourceVersion": "1"},
        "columnDefinitions": columns
            .iter()
            .enumerate()
            .map(|(i, name)| json!({
                "name": name,
                "type": "string",
                "format": if i == 0 { "name" } else { "" },
                "description": format!("{name} column"),
                "priority": 0,
            }))
            .collect::<Vec<_>>(),
        "rows": rows,
    })
}

/// Build a `Table` from explicit `columns` / `rows` supplied by the model.
///
/// `columns` is an array of strings (or objects with a `name`); `rows` is an array of objects
/// with a `cells` array, optionally `name` and `namespace` used to synthesise the row's
/// `PartialObjectMetadata`. Everything is plain structured JSON — nothing is encoded, so
/// nothing needs decoding.
pub fn table_from_columns_and_rows(columns: &[Value], rows: &[Value]) -> Value {
    let column_names: Vec<String> = columns
        .iter()
        .map(|c| match c {
            Value::String(s) => s.clone(),
            other => other
                .get("name")
                .and_then(Value::as_str)
                .unwrap_or("")
                .to_string(),
        })
        .collect();

    let column_defs: Vec<Value> = columns
        .iter()
        .enumerate()
        .map(|(i, c)| {
            let name = column_names.get(i).cloned().unwrap_or_default();
            let type_hint = c
                .get("type")
                .and_then(Value::as_str)
                .unwrap_or("string")
                .to_string();
            json!({
                "name": name,
                "type": type_hint,
                "format": if i == 0 { "name" } else { "" },
                "description": format!("{name} column"),
                "priority": 0,
            })
        })
        .collect();

    let row_values: Vec<Value> = rows
        .iter()
        .map(|row| {
            let cells: Vec<Value> = row
                .get("cells")
                .and_then(Value::as_array)
                .map(|cells| {
                    cells
                        .iter()
                        .map(|c| match c {
                            Value::String(s) => Value::String(s.clone()),
                            Value::Null => Value::String("<none>".to_string()),
                            other => Value::String(other.to_string()),
                        })
                        .collect()
                })
                .unwrap_or_default();

            let mut metadata = json!({});
            if let Some(name) = row.get("name").and_then(Value::as_str) {
                metadata["name"] = json!(name);
            } else if let Some(Value::String(first)) = cells.first() {
                metadata["name"] = json!(first);
            }
            if let Some(ns) = row.get("namespace").and_then(Value::as_str) {
                metadata["namespace"] = json!(ns);
            }

            json!({
                "cells": cells,
                "object": {
                    "kind": "PartialObjectMetadata",
                    "apiVersion": "meta.k8s.io/v1",
                    "metadata": metadata,
                },
            })
        })
        .collect();

    json!({
        "kind": "Table",
        "apiVersion": "meta.k8s.io/v1",
        "metadata": {"resourceVersion": "1"},
        "columnDefinitions": column_defs,
        "rows": row_values,
    })
}

fn partial_object_metadata(item: &Value) -> Value {
    json!({
        "kind": "PartialObjectMetadata",
        "apiVersion": "meta.k8s.io/v1",
        "metadata": item.get("metadata").cloned().unwrap_or_else(|| json!({})),
    })
}

/// Column headers per kind, matching what a real apiserver prints.
///
/// `kind` is the *item* kind (`Pod`), not the list kind (`PodList`); callers strip the suffix.
fn columns_for_kind(kind: &str) -> Vec<String> {
    let cols: &[&str] = match kind {
        "Pod" => &["NAME", "READY", "STATUS", "RESTARTS", "AGE"],
        "Node" => &["NAME", "STATUS", "ROLES", "AGE", "VERSION"],
        "Namespace" => &["NAME", "STATUS", "AGE"],
        "Service" => &[
            "NAME",
            "TYPE",
            "CLUSTER-IP",
            "EXTERNAL-IP",
            "PORT(S)",
            "AGE",
        ],
        "Deployment" | "ReplicaSet" | "StatefulSet" => {
            &["NAME", "READY", "UP-TO-DATE", "AVAILABLE", "AGE"]
        }
        _ => &["NAME", "AGE"],
    };
    cols.iter().map(|s| s.to_string()).collect()
}

fn cell_value(kind: &str, column: &str, item: &Value) -> String {
    match column {
        "NAME" => string_at(item, &["metadata", "name"]).unwrap_or_else(|| "<unknown>".into()),
        "AGE" => age_cell(item),
        "STATUS" => status_cell(kind, item),
        "READY" => ready_cell(kind, item),
        "RESTARTS" => restarts_cell(item),
        "ROLES" => roles_cell(item),
        "VERSION" => string_at(item, &["status", "nodeInfo", "kubeletVersion"])
            .unwrap_or_else(|| "<unknown>".into()),
        "TYPE" => string_at(item, &["spec", "type"]).unwrap_or_else(|| "ClusterIP".into()),
        "CLUSTER-IP" => string_at(item, &["spec", "clusterIP"]).unwrap_or_else(|| "<none>".into()),
        "EXTERNAL-IP" => external_ip_cell(item),
        "PORT(S)" => ports_cell(item),
        "UP-TO-DATE" => number_at(item, &["status", "updatedReplicas"]),
        "AVAILABLE" => number_at(item, &["status", "availableReplicas"]),
        _ => "<none>".into(),
    }
}

fn string_at(item: &Value, path: &[&str]) -> Option<String> {
    let mut cursor = item;
    for key in path {
        cursor = cursor.get(key)?;
    }
    cursor.as_str().map(|s| s.to_string())
}

fn number_at(item: &Value, path: &[&str]) -> String {
    let mut cursor = item;
    for key in path {
        match cursor.get(key) {
            Some(next) => cursor = next,
            None => return "0".to_string(),
        }
    }
    cursor
        .as_i64()
        .map(|n| n.to_string())
        .unwrap_or_else(|| "0".to_string())
}

fn status_cell(kind: &str, item: &Value) -> String {
    if item
        .get("metadata")
        .and_then(|m| m.get("deletionTimestamp"))
        .is_some()
    {
        return "Terminating".to_string();
    }
    if kind == "Node" {
        if let Some(conditions) = item
            .get("status")
            .and_then(|s| s.get("conditions"))
            .and_then(Value::as_array)
        {
            for condition in conditions {
                if condition.get("type").and_then(Value::as_str) == Some("Ready") {
                    return match condition.get("status").and_then(Value::as_str) {
                        Some("True") => "Ready".to_string(),
                        _ => "NotReady".to_string(),
                    };
                }
            }
        }
        return "Unknown".to_string();
    }
    string_at(item, &["status", "phase"])
        .or_else(|| string_at(item, &["status", "reason"]))
        .unwrap_or_else(|| "Unknown".into())
}

fn ready_cell(kind: &str, item: &Value) -> String {
    if kind == "Pod" {
        let statuses = item
            .get("status")
            .and_then(|s| s.get("containerStatuses"))
            .and_then(Value::as_array);
        let total_spec = item
            .get("spec")
            .and_then(|s| s.get("containers"))
            .and_then(Value::as_array)
            .map(|c| c.len());
        if let Some(statuses) = statuses {
            let ready = statuses
                .iter()
                .filter(|s| s.get("ready").and_then(Value::as_bool).unwrap_or(false))
                .count();
            let total = total_spec.unwrap_or(statuses.len());
            return format!("{ready}/{total}");
        }
        // No containerStatuses: a model that returned only spec+phase still deserves a
        // plausible column rather than "0/1" for a Running pod.
        let total = total_spec.unwrap_or(1);
        let phase = string_at(item, &["status", "phase"]).unwrap_or_default();
        let ready = if phase == "Running" || phase == "Succeeded" {
            total
        } else {
            0
        };
        return format!("{ready}/{total}");
    }
    // Workload kinds: readyReplicas/replicas.
    let ready = number_at(item, &["status", "readyReplicas"]);
    let desired = item
        .get("spec")
        .and_then(|s| s.get("replicas"))
        .and_then(Value::as_i64)
        .or_else(|| {
            item.get("status")
                .and_then(|s| s.get("replicas"))
                .and_then(Value::as_i64)
        })
        .unwrap_or(0);
    format!("{ready}/{desired}")
}

fn restarts_cell(item: &Value) -> String {
    // Saturating, not `sum()`. `restartCount` comes out of the model's object, so two
    // containers claiming `i64::MAX` is reachable — and `Cargo.toml` has no `[profile.dev]`, so
    // `overflow-checks` is on in every debug and test build: a plain `sum()` would panic there
    // and wrap silently in the shipped binary, which is exactly the wrong way round.
    let total: i64 = item
        .get("status")
        .and_then(|s| s.get("containerStatuses"))
        .and_then(Value::as_array)
        .map(|statuses| {
            statuses
                .iter()
                .filter_map(|s| s.get("restartCount").and_then(Value::as_i64))
                .fold(0i64, i64::saturating_add)
        })
        .unwrap_or(0);
    total.to_string()
}

fn roles_cell(item: &Value) -> String {
    let labels = item
        .get("metadata")
        .and_then(|m| m.get("labels"))
        .and_then(Value::as_object);
    let Some(labels) = labels else {
        return "<none>".to_string();
    };
    let mut roles: Vec<String> = labels
        .keys()
        .filter_map(|k| k.strip_prefix("node-role.kubernetes.io/"))
        .filter(|r| !r.is_empty())
        .map(|r| r.to_string())
        .collect();
    roles.sort();
    if roles.is_empty() {
        "<none>".to_string()
    } else {
        roles.join(",")
    }
}

fn external_ip_cell(item: &Value) -> String {
    if let Some(ips) = item
        .get("spec")
        .and_then(|s| s.get("externalIPs"))
        .and_then(Value::as_array)
    {
        let joined: Vec<&str> = ips.iter().filter_map(Value::as_str).collect();
        if !joined.is_empty() {
            return joined.join(",");
        }
    }
    if let Some(ingress) = item
        .get("status")
        .and_then(|s| s.get("loadBalancer"))
        .and_then(|lb| lb.get("ingress"))
        .and_then(Value::as_array)
    {
        let joined: Vec<String> = ingress
            .iter()
            .filter_map(|i| {
                i.get("ip")
                    .and_then(Value::as_str)
                    .or_else(|| i.get("hostname").and_then(Value::as_str))
            })
            .map(|s| s.to_string())
            .collect();
        if !joined.is_empty() {
            return joined.join(",");
        }
    }
    "<none>".to_string()
}

fn ports_cell(item: &Value) -> String {
    let Some(ports) = item
        .get("spec")
        .and_then(|s| s.get("ports"))
        .and_then(Value::as_array)
    else {
        return "<none>".to_string();
    };
    let rendered: Vec<String> = ports
        .iter()
        .map(|p| {
            let port = p.get("port").and_then(Value::as_i64).unwrap_or(0);
            let proto = p
                .get("protocol")
                .and_then(Value::as_str)
                .unwrap_or("TCP")
                .to_string();
            match p.get("nodePort").and_then(Value::as_i64) {
                Some(node_port) => format!("{port}:{node_port}/{proto}"),
                None => format!("{port}/{proto}"),
            }
        })
        .collect();
    if rendered.is_empty() {
        "<none>".to_string()
    } else {
        rendered.join(",")
    }
}

/// `metadata.creationTimestamp` → the compact duration kubectl shows in the AGE column.
fn age_cell(item: &Value) -> String {
    let Some(created) = string_at(item, &["metadata", "creationTimestamp"]) else {
        return "<unknown>".to_string();
    };
    let Ok(created) = chrono::DateTime::parse_from_rfc3339(&created) else {
        return "<unknown>".to_string();
    };
    let seconds = (chrono::Utc::now() - created.with_timezone(&chrono::Utc)).num_seconds();
    human_duration(seconds)
}

/// A close-enough port of Kubernetes' `duration.HumanDuration`.
pub fn human_duration(seconds: i64) -> String {
    if seconds < 0 {
        return "<invalid>".to_string();
    }
    if seconds < 60 {
        return format!("{seconds}s");
    }
    let minutes = seconds / 60;
    if minutes < 60 {
        return format!("{minutes}m");
    }
    let hours = minutes / 60;
    if hours < 48 {
        return format!("{hours}h");
    }
    let days = hours / 24;
    if days < 365 {
        return format!("{days}d");
    }
    let years = days / 365;
    format!("{}y{}d", years, days % 365)
}