sofka 0.4.1

A Kubernetes TUI, reimagined in Rust
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
use super::*;

impl App {
    // ----- actions -------------------------------------------------------

    pub(super) fn request_delete(&mut self, force: bool) {
        let targets = self.action_targets();
        if targets.is_empty() {
            return;
        }
        self.confirm_label = delete_confirm_label(&self.kind_plural, &targets, force);
        self.confirm_action = Some(ConfirmAction::Delete { targets, force });
        self.mode = Mode::Confirm;
    }

    pub(super) fn spawn_patch_action<F>(
        &self,
        kind: Kind,
        targets: Vec<(String, String)>,
        patch: Patch<Value>,
        error_message: F,
    ) where
        F: Fn(&str, kube::Error) -> String + Send + 'static,
    {
        let client = self.cluster.client.clone();
        let tx = self.tx.clone();
        let genr = self.generation;
        tokio::spawn(async move {
            for (name, ns) in targets {
                let api: Api<DynamicObject> = if kind.namespaced && !ns.is_empty() {
                    Api::namespaced_with(client.clone(), &ns, &kind.ar)
                } else {
                    Api::all_with(client.clone(), &kind.ar)
                };
                if let Err(e) = api.patch(&name, &PatchParams::default(), &patch).await {
                    let _ = tx
                        .send(Msg::Error {
                            generation: genr,
                            error: error_message(&name, e),
                        })
                        .await;
                }
            }
        });
    }

    pub(super) fn do_delete(&mut self, targets: Vec<(String, String)>, force: bool) {
        let Some(kind) = self.kind.clone() else {
            return;
        };
        let client = self.cluster.client.clone();
        let tx = self.tx.clone();
        let genr = self.generation;
        self.flash = if targets.len() == 1 {
            format!("deleting {}", targets[0].0)
        } else {
            format!("deleting {} {}", targets.len(), self.kind_plural)
        };
        self.flash_err = false;
        tokio::spawn(async move {
            let mut dp = DeleteParams::default();
            if force {
                dp = dp.grace_period(0);
            }
            for (name, ns) in targets {
                let api: Api<DynamicObject> = if kind.namespaced && !ns.is_empty() {
                    Api::namespaced_with(client.clone(), &ns, &kind.ar)
                } else {
                    Api::all_with(client.clone(), &kind.ar)
                };
                if let Err(e) = api.delete(&name, &dp).await {
                    let _ = tx
                        .send(Msg::Error {
                            generation: genr,
                            error: format!("delete {name} failed: {e}"),
                        })
                        .await;
                }
            }
        });
    }

    pub(super) fn request_cordon(&mut self, unschedulable: bool) {
        if self.kind_plural != "nodes" {
            self.flash_warn("cordon/uncordon applies to nodes");
            return;
        }
        let targets = self.node_action_targets();
        if targets.is_empty() {
            return;
        }
        self.do_cordon_nodes(targets, unschedulable);
    }

    pub(super) fn do_cordon_nodes(&mut self, targets: Vec<String>, unschedulable: bool) {
        let Some(kind) = self.kind.clone() else {
            return;
        };
        let verb = if unschedulable {
            "cordoning"
        } else {
            "uncordoning"
        };
        self.flash = if targets.len() == 1 {
            format!("{verb} {}", targets[0])
        } else {
            format!("{verb} {} nodes…", targets.len())
        };
        self.flash_err = false;
        let targets = targets
            .into_iter()
            .map(|name| (name, String::new()))
            .collect();
        self.spawn_patch_action(
            kind,
            targets,
            Patch::Merge(node_unschedulable_patch(unschedulable)),
            move |name, e| format!("{verb} {name} failed: {e}"),
        );
    }

    pub(super) fn request_drain(&mut self) {
        if self.kind_plural != "nodes" {
            self.flash_warn("drain applies to nodes");
            return;
        }
        let targets = self.node_action_targets();
        if targets.is_empty() {
            return;
        }
        self.confirm_label = if targets.len() == 1 {
            format!("Drain node {}? Cordon and evict eligible pods.", targets[0])
        } else {
            format!(
                "Drain {} nodes? Cordon and evict eligible pods.",
                targets.len()
            )
        };
        self.confirm_action = Some(ConfirmAction::Drain { targets });
        self.mode = Mode::Confirm;
    }

    pub(super) fn do_drain_nodes(&mut self, targets: Vec<String>) {
        let Some(kind) = self.kind.clone() else {
            return;
        };
        let client = self.cluster.client.clone();
        let tx = self.tx.clone();
        let genr = self.generation;
        self.flash = if targets.len() == 1 {
            format!("draining {}", targets[0])
        } else {
            format!("draining {} nodes…", targets.len())
        };
        self.flash_err = false;
        tokio::spawn(async move {
            let nodes: Api<DynamicObject> = Api::all_with(client.clone(), &kind.ar);
            let node_patch = Patch::Merge(node_unschedulable_patch(true));
            let pods: Api<Pod> = Api::all(client.clone());
            for node in targets {
                if let Err(e) = nodes
                    .patch(&node, &PatchParams::default(), &node_patch)
                    .await
                {
                    let _ = tx
                        .send(Msg::Error {
                            generation: genr,
                            error: format!("drain {node}: cordon failed: {e}"),
                        })
                        .await;
                    continue;
                }

                let listed = pods
                    .list(&ListParams::default().fields(&format!("spec.nodeName={node}")))
                    .await;
                let pod_list = match listed {
                    Ok(list) => list,
                    Err(e) => {
                        let _ = tx
                            .send(Msg::Error {
                                generation: genr,
                                error: format!("drain {node}: list pods failed: {e}"),
                            })
                            .await;
                        continue;
                    }
                };

                for pod in pod_list.items.iter().filter(|pod| drainable_pod(pod)) {
                    let Some(name) = pod.metadata.name.as_deref() else {
                        continue;
                    };
                    let ns = pod.metadata.namespace.as_deref().unwrap_or("default");
                    let pod_api: Api<Pod> = Api::namespaced(client.clone(), ns);
                    let evict = EvictParams {
                        delete_options: Some(DeleteParams::default()),
                        ..Default::default()
                    };
                    match pod_api.evict(name, &evict).await {
                        Ok(_) => {}
                        Err(e) if eviction_unsupported(&e) => {
                            if let Err(delete_err) =
                                pod_api.delete(name, &DeleteParams::default()).await
                            {
                                let _ = tx
                                    .send(Msg::Error {
                                        generation: genr,
                                        error: format!(
                                            "drain {node}: delete {ns}/{name} failed after eviction fallback: {delete_err}"
                                        ),
                                    })
                                    .await;
                            }
                        }
                        Err(e) => {
                            let _ = tx
                                .send(Msg::Error {
                                    generation: genr,
                                    error: format!("drain {node}: evict {ns}/{name} failed: {e}"),
                                })
                                .await;
                        }
                    }
                }
            }
        });
    }

    pub(super) fn request_attach(&mut self) {
        if self.kind_plural != "pods" {
            self.flash_warn("attach is only available for pods");
            return;
        }
        let Some(obj) = self.selected_ref() else {
            return;
        };
        let name = obj.metadata.name.clone().unwrap_or_default();
        let ns = obj.metadata.namespace.clone().unwrap_or_default();
        let mut argv = self.kubectl_base();
        argv.extend(["attach".into(), "-it".into(), "-n".into(), ns, name]);
        self.pending = Some(Suspend::Shell(argv));
    }

    /// Navigate to the node hosting the selected pod (k9s `o`).
    pub(super) fn show_node(&mut self) {
        if self.kind_plural != "pods" {
            self.flash_warn("'o' shows the node for a pod");
            return;
        }
        let Some(obj) = self.selected_ref() else {
            return;
        };
        let Some(node) = obj.data.pointer("/spec/nodeName").and_then(Value::as_str) else {
            self.flash_warn("pod has no node assigned");
            return;
        };
        let node = node.to_string();
        let pod_name = obj.metadata.name.clone().unwrap_or_default();
        let Some(nodes) = self.cluster.resolve("nodes") else {
            self.flash_warn("nodes kind unavailable");
            return;
        };
        self.push_frame();
        self.kind = Some(nodes);
        self.kind_plural = "nodes".into();
        self.namespace = String::new();
        self.labels = None;
        self.fields = Some(format!("metadata.name={node}"));
        self.scope_label = Some(format!("host of {pod_name}"));
        self.filter.clear();
        self.reset_sort();
        self.table_state.select(Some(0));
        self.start_watch();
    }

    /// Jump to the selected object's controller/owner (k9s Shift-J).
    pub(super) fn jump_owner(&mut self) {
        let Some(obj) = self.selected_ref() else {
            return;
        };
        let owners = obj
            .metadata
            .owner_references
            .as_ref()
            .filter(|o| !o.is_empty());
        let Some(owner) = owners.and_then(|o| o.first()) else {
            self.flash_warn("no owner reference");
            return;
        };
        let Some(kind) = self.cluster.resolve(&owner.kind.to_lowercase()) else {
            self.flash_warn(&format!("owner kind {} unresolved", owner.kind));
            return;
        };
        let ns = obj.metadata.namespace.clone().unwrap_or_default();
        let owner_name = owner.name.clone();
        let child_name = obj.metadata.name.clone().unwrap_or_default();
        self.push_frame();
        self.kind_plural = kind.ar.plural.to_lowercase();
        self.kind = Some(kind);
        self.namespace = ns;
        self.labels = None;
        self.fields = Some(format!("metadata.name={owner_name}"));
        self.scope_label = Some(format!("owner of {child_name}"));
        self.filter.clear();
        self.reset_sort();
        self.table_state.select(Some(0));
        self.start_watch();
    }

    /// Copy the (filtered) log buffer to the clipboard (k9s `c` in logs).
    pub(super) fn copy_logs(&mut self) {
        let text = self.filtered_log_text();
        if text.is_empty() {
            self.flash_warn("no log lines to copy");
            return;
        }
        let n = text.lines().count();
        self.copy_to_clipboard_async(
            text,
            format!("copied {n} log lines"),
            "no clipboard target found (pbcopy/xclip/wl-copy/OSC 52)",
        );
    }

    /// Save the filtered log buffer to a temp file (k9s Ctrl-S).
    pub(super) fn save_logs(&mut self) {
        let text = self.filtered_log_text();
        if text.is_empty() {
            self.flash_warn("no log lines to save");
            return;
        }
        let genr = self.log_gen;
        let tx = self.tx.clone();
        let ts = k8s_openapi::jiff::Timestamp::now().as_second();
        let safe: String = self
            .logs
            .view
            .title
            .chars()
            .map(|c| if c.is_alphanumeric() { c } else { '-' })
            .collect();
        let path = std::env::temp_dir().join(format!("sofka-{safe}-{ts}.log"));
        tokio::spawn(async move {
            let result = tokio::fs::write(&path, text)
                .await
                .map(|_| path)
                .map_err(|e| e.to_string());
            let _ = tx
                .send(Msg::LogsSaved {
                    generation: genr,
                    result,
                })
                .await;
        });
    }

    pub(super) fn filtered_log_text(&self) -> String {
        let f = self.logs.filter.to_lowercase();
        self.logs
            .view
            .lines
            .iter()
            .filter(|l| f.is_empty() || l.to_lowercase().contains(&f))
            .cloned()
            .collect::<Vec<_>>()
            .join("\n")
    }

    /// Copy the selected resource's name to the system clipboard (k9s `c`).
    pub(super) fn copy_name(&mut self) {
        let Some(obj) = self.selected_ref() else {
            return;
        };
        let name = obj.metadata.name.clone().unwrap_or_default();
        self.copy_to_clipboard_async(
            name.clone(),
            format!("copied: {name}"),
            "no clipboard target found (pbcopy/xclip/wl-copy/OSC 52)",
        );
    }

    pub(super) fn copy_to_clipboard_async(&self, text: String, success: String, failure: &str) {
        let tx = self.tx.clone();
        let genr = self.generation;
        let failure = failure.to_string();
        tokio::spawn(async move {
            let copied = tokio::task::spawn_blocking(move || copy_to_clipboard(&text))
                .await
                .unwrap_or(false);
            let _ = tx
                .send(Msg::ClipboardCopied {
                    generation: genr,
                    copied,
                    success,
                    failure,
                })
                .await;
        });
    }

    /// Previous-container logs for the selected pod (k9s `p` on a pod row).
    pub(super) fn open_previous_logs(&mut self) {
        if self.kind_plural != "pods" {
            self.flash_warn("previous logs are for pods (use the container picker elsewhere)");
            return;
        }
        let Some(obj) = self.selected_ref() else {
            return;
        };
        let name = obj.metadata.name.clone().unwrap_or_default();
        let ns = obj.metadata.namespace.clone().unwrap_or_default();
        let containers = container_names(obj);
        let container = containers.into_iter().next();
        self.launch_logs(
            LogSource::Single {
                ns,
                pod: name.clone(),
                container,
                previous: true,
            },
            format!("{name} — previous logs"),
        );
    }

    /// Rollout-restart a workload by stamping the template annotation (k9s `r`).
    pub(super) fn request_restart(&mut self) {
        let Some(obj) = self.selected_ref() else {
            return;
        };
        let Some(kind) = self.kind.clone() else {
            return;
        };
        let name = obj.metadata.name.clone().unwrap_or_default();
        let ns = obj.metadata.namespace.clone().unwrap_or_default();
        let now = k8s_openapi::jiff::Timestamp::now().to_string();
        self.flash = format!("restarting {name}");
        self.flash_err = false;
        self.spawn_patch_action(
            kind,
            vec![(name, ns)],
            Patch::Strategic(restart_patch(&now)),
            |_, e| format!("restart failed: {e}"),
        );
    }

    /// Open the Set-Image picker for the selected workload/pod (k9s `i`).
    pub(super) fn request_set_image(&mut self) {
        let is_pod = self.kind_plural == "pods";
        let workload = matches!(
            self.kind_plural.as_str(),
            "deployments"
                | "statefulsets"
                | "daemonsets"
                | "replicasets"
                | "replicationcontrollers"
        );
        if !is_pod && !workload {
            self.flash_warn("set image applies to pods and workload controllers");
            return;
        }
        let Some(obj) = self.selected_ref() else {
            return;
        };
        let ptr = if is_pod {
            "/spec/containers"
        } else {
            "/spec/template/spec/containers"
        };
        let Some(cs) = obj.data.pointer(ptr).and_then(Value::as_array) else {
            self.flash_warn("no containers found");
            return;
        };
        let mut names = Vec::new();
        let mut images = Vec::new();
        for c in cs {
            names.push(
                c.get("name")
                    .and_then(Value::as_str)
                    .unwrap_or("?")
                    .to_string(),
            );
            images.push(
                c.get("image")
                    .and_then(Value::as_str)
                    .unwrap_or("")
                    .to_string(),
            );
        }
        if names.is_empty() {
            self.flash_warn("no containers found");
            return;
        }
        let target = (
            obj.metadata.namespace.clone().unwrap_or_default(),
            obj.metadata.name.clone().unwrap_or_default(),
            self.kind_plural.clone(),
        );
        self.container_list = names;
        self.image_values = images;
        self.image_target = Some(target);
        self.container_state.select(Some(0));
        self.mode = Mode::SetImage;
    }

    pub(super) fn key_set_image(&mut self, key: KeyEvent) {
        let len = self.container_list.len();
        match key.code {
            KeyCode::Esc | KeyCode::Char('q') => self.mode = Mode::Table,
            KeyCode::Char('j') | KeyCode::Down => list_step(&mut self.container_state, len, true),
            KeyCode::Char('k') | KeyCode::Up => list_step(&mut self.container_state, len, false),
            KeyCode::Enter => {
                if let Some(i) = self.container_state.selected()
                    && let Some(container) = self.container_list.get(i).cloned()
                    && let Some((ns, name, plural)) = self.image_target.clone()
                {
                    self.prompt_label = format!("New image for {container}:");
                    self.prompt_input = self.image_values.get(i).cloned().unwrap_or_default();
                    self.prompt_kind = Some(PromptKind::SetImage {
                        ns,
                        name,
                        plural,
                        container,
                    });
                    self.mode = Mode::Prompt;
                }
            }
            _ => {}
        }
    }

    pub(super) fn do_set_image(
        &mut self,
        ns: String,
        name: String,
        plural: String,
        container: String,
        image: String,
    ) {
        let Some(kind) = self.kind.clone() else {
            return;
        };
        self.flash = format!("setting image: {container}{image}");
        self.flash_err = false;
        self.spawn_patch_action(
            kind,
            vec![(name, ns)],
            Patch::Strategic(set_image_patch(&plural, &container, &image)),
            |_, e| format!("set image failed: {e}"),
        );
    }

    pub(super) fn request_edit(&mut self) {
        let Some(obj) = self.selected_ref() else {
            return;
        };
        let name = obj.metadata.name.clone().unwrap_or_default();
        let mut argv = self.kubectl_base();
        argv.extend(["edit".into(), self.kind_plural.clone(), name]);
        if let Some(ns) = &obj.metadata.namespace {
            argv.push("-n".into());
            argv.push(ns.clone());
        }
        self.pending = Some(Suspend::Shell(argv));
    }

    pub(super) fn request_exec(&mut self) {
        if self.kind_plural != "pods" {
            self.flash_warn("shell is only available for pods");
            return;
        }
        let Some(obj) = self.selected_ref() else {
            return;
        };
        let name = obj.metadata.name.clone().unwrap_or_default();
        let ns = obj.metadata.namespace.clone().unwrap_or_default();
        let mut argv = self.kubectl_base();
        argv.extend([
            "exec".into(),
            "-it".into(),
            "-n".into(),
            ns,
            name,
            "--".into(),
            "sh".into(),
            "-c".into(),
            "command -v bash >/dev/null 2>&1 && exec bash || exec sh".into(),
        ]);
        self.pending = Some(Suspend::Shell(argv));
    }

    pub(super) fn request_scale(&mut self) {
        if !matches!(
            self.kind_plural.as_str(),
            "deployments" | "statefulsets" | "replicasets"
        ) {
            self.flash_warn("scale applies to deployments/statefulsets/replicasets");
            return;
        }
        let Some(obj) = self.selected_ref() else {
            return;
        };
        let name = obj.metadata.name.clone().unwrap_or_default();
        let ns = obj.metadata.namespace.clone().unwrap_or_default();
        let cur = obj
            .data
            .pointer("/spec/replicas")
            .and_then(Value::as_i64)
            .unwrap_or(0);
        self.prompt_label = format!("Scale {name} to replicas (current {cur}):");
        self.prompt_input.clear();
        self.prompt_kind = Some(PromptKind::Scale { ns, name });
        self.mode = Mode::Prompt;
    }

    pub(super) fn request_port_forward(&mut self) {
        let Some(obj) = self.selected_ref() else {
            return;
        };
        if !matches!(self.kind_plural.as_str(), "pods" | "services") {
            self.flash_warn("port-forward applies to pods/services");
            return;
        }
        let name = obj.metadata.name.clone().unwrap_or_default();
        let ns = obj.metadata.namespace.clone().unwrap_or_default();
        self.prompt_label = format!("Port-forward {name} (LOCAL:REMOTE, e.g. 8080:80):");
        self.prompt_input.clear();
        self.prompt_kind = Some(PromptKind::PortForward { ns, name });
        self.mode = Mode::Prompt;
    }

    /// Start `kubectl port-forward` in the background (not a foreground
    /// `Suspend::Shell` — a forward should keep running while you keep
    /// browsing). stdio is nulled since the TUI still owns the terminal.
    pub(super) fn start_port_forward(&mut self, ns: String, target: String, ports: String) {
        let mut argv = self.kubectl_base();
        argv.extend([
            "port-forward".into(),
            "-n".into(),
            ns.clone(),
            target.clone(),
            ports.clone(),
        ]);
        let mut cmd = tokio::process::Command::new(&argv[0]);
        cmd.args(&argv[1..])
            .stdin(std::process::Stdio::null())
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null());
        match cmd.spawn() {
            Ok(child) => {
                let pf = PortForward {
                    ns,
                    target,
                    ports,
                    child,
                };
                self.flash = format!("port-forwarding {} (:pf to view/stop)", pf.label());
                self.flash_err = false;
                self.port_forwards.push(pf);
            }
            Err(e) => self.flash_warn(&format!("port-forward failed to start: {e}")),
        }
    }

    /// Drop any forward whose `kubectl` process has already exited (pod
    /// restarted, connection dropped, port in use, …), flashing a heads-up.
    /// Called on every tick, so a dead forward doesn't linger in the list.
    pub fn reap_port_forwards(&mut self) {
        let mut i = 0;
        while i < self.port_forwards.len() {
            match self.port_forwards[i].child.try_wait() {
                Ok(Some(_)) => {
                    let pf = self.port_forwards.remove(i);
                    self.flash_warn(&format!("port-forward {} exited", pf.label()));
                }
                _ => i += 1,
            }
        }
    }

    pub(super) fn open_port_forwards(&mut self) {
        self.pf_state.select(if self.port_forwards.is_empty() {
            None
        } else {
            Some(0)
        });
        self.mode = Mode::PortForwards;
    }

    pub(super) fn key_port_forwards(&mut self, key: KeyEvent) {
        let len = self.port_forwards.len();
        match key.code {
            KeyCode::Esc | KeyCode::Char('q') => self.mode = Mode::Table,
            KeyCode::Char('j') | KeyCode::Down => list_step(&mut self.pf_state, len, true),
            KeyCode::Char('k') | KeyCode::Up => list_step(&mut self.pf_state, len, false),
            KeyCode::Char('x') | KeyCode::Char('s') => self.stop_selected_port_forward(),
            _ => {}
        }
    }

    pub(super) fn open_skins(&mut self) {
        self.skin_state.select(if self.skin_list.is_empty() {
            None
        } else {
            Some(0)
        });
        self.mode = Mode::Skins;
    }

    pub(super) fn key_skins(&mut self, key: KeyEvent) {
        let len = self.skin_list.len();
        match key.code {
            KeyCode::Esc | KeyCode::Char('q') => self.mode = Mode::Table,
            KeyCode::Char('j') | KeyCode::Down => list_step(&mut self.skin_state, len, true),
            KeyCode::Char('k') | KeyCode::Up => list_step(&mut self.skin_state, len, false),
            KeyCode::Enter => {
                if let Some(name) = self
                    .skin_state
                    .selected()
                    .and_then(|i| self.skin_list.get(i).cloned())
                {
                    self.apply_skin(&name);
                }
                self.mode = Mode::Table;
            }
            _ => {}
        }
    }

    pub(super) fn apply_skin(&mut self, name: &str) {
        let name = name.trim();
        if name.is_empty() {
            self.open_skins();
            return;
        }
        if crate::theme::builtin(&name.to_ascii_lowercase()).is_none() {
            self.flash_warn(&format!("unknown skin: {name}"));
            return;
        }
        let palette = crate::theme::resolve_skin(Some(name), &self.skin_colors);
        crate::theme::set(palette);
        // A manual choice becomes the session skin, so it survives context
        // switches into contexts without a `[skin.contexts]` override.
        self.session_skin = Some(name.to_string());
        self.flash = format!("skin: {name}");
        self.flash_err = false;
    }

    /// Re-resolve the skin when the context changes: a `[skin.contexts]`
    /// override for the new context wins, otherwise the session skin (config
    /// `skin.name`, the auto-detected default, or the last `:skin` choice).
    pub(super) fn apply_context_skin(&mut self, context: &str) {
        let Some(name) = self
            .context_skins
            .get(context)
            .cloned()
            .or_else(|| self.session_skin.clone())
        else {
            return;
        };
        if crate::theme::builtin(&name.trim().to_ascii_lowercase()).is_none() {
            self.flash_warn(&format!("unknown skin '{name}' for context {context}"));
            return;
        }
        let palette = crate::theme::resolve_skin(Some(&name), &self.skin_colors);
        crate::theme::set(palette);
    }

    /// Stop (kill) the selected forward. Others keep running.
    pub(super) fn stop_selected_port_forward(&mut self) {
        let Some(i) = self.pf_state.selected() else {
            return;
        };
        if i >= self.port_forwards.len() {
            return;
        }
        let pf = self.port_forwards.remove(i); // dropped -> Drop kills the child
        self.flash = format!("stopped port-forward {}", pf.label());
        self.flash_err = false;
        self.pf_state.select(if self.port_forwards.is_empty() {
            None
        } else {
            Some(i.min(self.port_forwards.len() - 1))
        });
    }

    pub(super) fn do_scale(&mut self, ns: String, name: String, replicas: i32) {
        let Some(kind) = self.kind.clone() else {
            return;
        };
        self.flash = format!("scaling {name}{replicas}");
        self.flash_err = false;
        self.spawn_patch_action(
            kind,
            vec![(name, ns)],
            Patch::Merge(scale_patch(replicas)),
            |_, e| format!("scale failed: {e}"),
        );
    }

    /// Open the Flux suspend/resume menu (`t`) for the marked rows, or the
    /// current selection if none are marked. A menu, not a single-key
    /// toggle — suspending something always takes an explicit, visible
    /// choice (`j`/`k` + Enter) rather than one accidental keystroke.
    pub(super) fn request_flux_menu(&mut self) {
        if !FLUX_SUSPENDABLE_KINDS.contains(&self.kind_plural.as_str()) {
            self.flash_warn("suspend/resume only applies to Flux resources (ks/hr/git-, helm-, oci-repos, buckets, image automation, alerts, receivers)");
            return;
        }
        if self.action_targets().is_empty() {
            return;
        }
        self.flux_menu_state.select(Some(0));
        self.mode = Mode::FluxMenu;
    }

    pub(super) fn key_flux_menu(&mut self, key: KeyEvent) {
        let len = FLUX_MENU_ITEMS.len();
        match key.code {
            KeyCode::Esc | KeyCode::Char('q') => self.mode = Mode::Table,
            KeyCode::Char('j') | KeyCode::Down => list_step(&mut self.flux_menu_state, len, true),
            KeyCode::Char('k') | KeyCode::Up => list_step(&mut self.flux_menu_state, len, false),
            KeyCode::Enter => {
                let choice = self
                    .flux_menu_state
                    .selected()
                    .and_then(|i| FLUX_MENU_ITEMS.get(i))
                    .copied();
                self.mode = Mode::Table;
                match choice {
                    Some("Suspend") => {
                        let targets = self.action_targets();
                        self.do_set_suspend(targets, true);
                    }
                    Some("Resume") => {
                        let targets = self.action_targets();
                        self.do_set_suspend(targets, false);
                    }
                    Some("Reconcile now") => {
                        let targets = self.action_targets();
                        self.do_reconcile(targets);
                    }
                    _ => {} // "Cancel" or nothing selected — do nothing.
                }
            }
            _ => {}
        }
    }

    pub(super) fn do_set_suspend(&mut self, targets: Vec<(String, String)>, suspend: bool) {
        let Some(kind) = self.kind.clone() else {
            return;
        };
        let verb = if suspend { "suspending" } else { "resuming" };
        self.flash = if targets.len() == 1 {
            format!("{verb} {}", targets[0].0)
        } else {
            format!("{verb} {} {}", targets.len(), self.kind_plural)
        };
        self.flash_err = false;
        self.marked.clear();
        self.spawn_patch_action(
            kind,
            targets,
            Patch::Merge(suspend_patch(suspend)),
            move |name, e| format!("{verb} {name} failed: {e}"),
        );
    }

    /// Force an immediate Flux reconciliation, bypassing the normal interval —
    /// patches `reconcile.fluxcd.io/requestedAt`, the same annotation `flux
    /// reconcile` sets, watched by every toolkit controller.
    pub(super) fn do_reconcile(&mut self, targets: Vec<(String, String)>) {
        let Some(kind) = self.kind.clone() else {
            return;
        };
        let now = k8s_openapi::jiff::Timestamp::now().to_string();
        self.flash = if targets.len() == 1 {
            format!("reconciling {}", targets[0].0)
        } else {
            format!("reconciling {} {}", targets.len(), self.kind_plural)
        };
        self.flash_err = false;
        self.marked.clear();
        self.spawn_patch_action(
            kind,
            targets,
            Patch::Merge(reconcile_patch(&now)),
            |name, e| format!("reconcile {name} failed: {e}"),
        );
    }

    /// Force an immediate External Secrets Operator refresh on the marked rows
    /// (or the current selection), matching the k9s external-secrets plugin.
    pub(super) fn request_refresh_es(&mut self) {
        if !EXTERNAL_SECRET_KINDS.contains(&self.kind_plural.as_str()) {
            self.flash_warn(
                "refresh only applies to external secrets (externalsecrets, pushsecrets)",
            );
            return;
        }
        let targets = self.action_targets();
        if targets.is_empty() {
            return;
        }
        self.do_refresh_es(targets);
    }

    /// Stamp the `force-sync` annotation ESO watches to reconcile a secret out
    /// of band — the same annotation the k9s plugin overwrites. The value only
    /// has to change to trigger a sync; a unix timestamp mirrors k9s' `date +%s`.
    pub(super) fn do_refresh_es(&mut self, targets: Vec<(String, String)>) {
        let Some(kind) = self.kind.clone() else {
            return;
        };
        let now = k8s_openapi::jiff::Timestamp::now().as_second().to_string();
        self.flash = if targets.len() == 1 {
            format!("refreshing {}", targets[0].0)
        } else {
            format!("refreshing {} {}", targets.len(), self.kind_plural)
        };
        self.flash_err = false;
        self.marked.clear();
        self.spawn_patch_action(
            kind,
            targets,
            Patch::Merge(external_secret_refresh_patch(&now)),
            |name, e| format!("refresh {name} failed: {e}"),
        );
    }

    pub(super) fn flash_warn(&mut self, msg: &str) {
        self.flash = msg.to_string();
        self.flash_err = true;
    }

    /// Base argv for a `kubectl` shell-out, pinned to the active context so it
    /// can't target a different cluster than the one we're viewing.
    pub(super) fn kubectl_base(&self) -> Vec<String> {
        let mut argv = vec!["kubectl".to_string()];
        if let Some(ctx) = self.cluster.kubectl_context() {
            argv.push("--context".to_string());
            argv.push(ctx.to_string());
        }
        argv
    }
}