/
mod.rs
822 lines (759 loc) · 24.6 KB
/
mod.rs
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
pub(crate) mod configmaps;
pub(crate) mod contexts;
pub(crate) mod cronjobs;
pub(crate) mod daemonsets;
pub(crate) mod deployments;
pub(crate) mod jobs;
pub(crate) mod key_binding;
pub(crate) mod metrics;
pub(crate) mod models;
pub(crate) mod nodes;
pub(crate) mod ns;
pub(crate) mod pods;
pub(crate) mod replicasets;
pub(crate) mod replication_controllers;
pub(crate) mod roles;
pub(crate) mod secrets;
pub(crate) mod statefulsets;
pub(crate) mod storageclass;
pub(crate) mod svcs;
mod utils;
use anyhow::anyhow;
use kube::config::Kubeconfig;
use kubectl_view_allocations::{GroupBy, QtyByQualifier};
use tokio::sync::mpsc::Sender;
use tui::layout::Rect;
use self::{
configmaps::KubeConfigMap,
contexts::KubeContext,
cronjobs::KubeCronJob,
daemonsets::KubeDaemonSet,
deployments::KubeDeployment,
jobs::KubeJob,
key_binding::DEFAULT_KEYBINDING,
metrics::KubeNodeMetrics,
models::{LogsState, ScrollableTxt, StatefulList, StatefulTable, TabRoute, TabsState},
nodes::KubeNode,
ns::KubeNs,
pods::{KubeContainer, KubePod},
replicasets::KubeReplicaSet,
replication_controllers::KubeReplicationController,
roles::{KubeClusterRoles, KubeRoles},
secrets::KubeSecret,
statefulsets::KubeStatefulSet,
storageclass::KubeStorageClass,
svcs::KubeSvc,
};
use super::{
cmd::IoCmdEvent,
network::{stream::IoStreamEvent, IoEvent},
};
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum ActiveBlock {
Help,
Pods,
Containers,
Logs,
Services,
Nodes,
Deployments,
ConfigMaps,
StatefulSets,
ReplicaSets,
Namespaces,
Describe,
Yaml,
Contexts,
Utilization,
Jobs,
DaemonSets,
CronJobs,
Secrets,
RplCtrl,
StorageClasses,
Roles,
ClusterRoles,
More,
}
#[derive(Clone, PartialEq, Debug)]
pub enum RouteId {
Home,
Contexts,
Utilization,
HelpMenu,
}
#[derive(Debug, Clone)]
pub struct Route {
pub id: RouteId,
pub active_block: ActiveBlock,
}
const DEFAULT_ROUTE: Route = Route {
id: RouteId::Home,
active_block: ActiveBlock::Pods,
};
/// Holds CLI version info
pub struct Cli {
pub name: String,
pub version: String,
pub status: bool,
}
/// Holds data state for various views
pub struct Data {
pub selected: Selected,
pub clis: Vec<Cli>,
pub kubeconfig: Option<Kubeconfig>,
pub contexts: StatefulTable<KubeContext>,
pub active_context: Option<KubeContext>,
pub node_metrics: Vec<KubeNodeMetrics>,
pub logs: LogsState,
pub describe_out: ScrollableTxt,
pub metrics: StatefulTable<(Vec<String>, Option<QtyByQualifier>)>,
pub namespaces: StatefulTable<KubeNs>,
pub nodes: StatefulTable<KubeNode>,
pub pods: StatefulTable<KubePod>,
pub containers: StatefulTable<KubeContainer>,
pub services: StatefulTable<KubeSvc>,
pub config_maps: StatefulTable<KubeConfigMap>,
pub stateful_sets: StatefulTable<KubeStatefulSet>,
pub replica_sets: StatefulTable<KubeReplicaSet>,
pub deployments: StatefulTable<KubeDeployment>,
pub jobs: StatefulTable<KubeJob>,
pub daemon_sets: StatefulTable<KubeDaemonSet>,
pub cronjobs: StatefulTable<KubeCronJob>,
pub secrets: StatefulTable<KubeSecret>,
pub rpl_ctrls: StatefulTable<KubeReplicationController>,
pub storage_classes: StatefulTable<KubeStorageClass>,
pub roles: StatefulTable<KubeRoles>,
pub clusterroles: StatefulTable<KubeClusterRoles>,
}
/// selected data items
pub struct Selected {
pub ns: Option<String>,
pub pod: Option<String>,
pub container: Option<String>,
pub context: Option<String>,
}
/// Holds main application state
pub struct App {
navigation_stack: Vec<Route>,
io_tx: Option<Sender<IoEvent>>,
io_stream_tx: Option<Sender<IoStreamEvent>>,
io_cmd_tx: Option<Sender<IoCmdEvent>>,
pub title: &'static str,
pub should_quit: bool,
pub main_tabs: TabsState,
pub context_tabs: TabsState,
pub more_resources_menu: StatefulList<(String, ActiveBlock)>,
pub show_info_bar: bool,
pub is_loading: bool,
pub is_streaming: bool,
pub is_routing: bool,
pub tick_until_poll: u64,
pub tick_count: u64,
pub enhanced_graphics: bool,
pub table_cols: u16,
pub size: Rect,
pub api_error: String,
pub dialog: Option<String>,
pub confirm: bool,
pub light_theme: bool,
pub refresh: bool,
pub log_auto_scroll: bool,
pub utilization_group_by: Vec<GroupBy>,
pub help_docs: StatefulTable<Vec<String>>,
pub data: Data,
}
impl Default for Data {
fn default() -> Self {
Data {
clis: vec![],
kubeconfig: None,
contexts: StatefulTable::new(),
active_context: None,
node_metrics: vec![],
namespaces: StatefulTable::new(),
selected: Selected {
ns: None,
pod: None,
container: None,
context: None,
},
logs: LogsState::new(String::default()),
describe_out: ScrollableTxt::new(),
metrics: StatefulTable::new(),
nodes: StatefulTable::new(),
pods: StatefulTable::new(),
containers: StatefulTable::new(),
services: StatefulTable::new(),
config_maps: StatefulTable::new(),
stateful_sets: StatefulTable::new(),
replica_sets: StatefulTable::new(),
deployments: StatefulTable::new(),
jobs: StatefulTable::new(),
daemon_sets: StatefulTable::new(),
cronjobs: StatefulTable::new(),
secrets: StatefulTable::new(),
rpl_ctrls: StatefulTable::new(),
storage_classes: StatefulTable::new(),
roles: StatefulTable::new(),
clusterroles: StatefulTable::new(),
}
}
}
impl Default for App {
fn default() -> Self {
App {
navigation_stack: vec![DEFAULT_ROUTE],
io_tx: None,
io_stream_tx: None,
io_cmd_tx: None,
title: " KDash - A simple Kubernetes dashboard ",
should_quit: false,
main_tabs: TabsState::new(vec![
TabRoute {
title: format!(
"Active Context {}",
DEFAULT_KEYBINDING.jump_to_current_context.key
),
route: Route {
active_block: ActiveBlock::Pods,
id: RouteId::Home,
},
},
TabRoute {
title: format!(
"All Contexts {}",
DEFAULT_KEYBINDING.jump_to_all_context.key
),
route: Route {
active_block: ActiveBlock::Contexts,
id: RouteId::Contexts,
},
},
TabRoute {
title: format!("Utilization {}", DEFAULT_KEYBINDING.jump_to_utilization.key),
route: Route {
active_block: ActiveBlock::Utilization,
id: RouteId::Utilization,
},
},
]),
context_tabs: TabsState::new(vec![
TabRoute {
title: format!("Pods {}", DEFAULT_KEYBINDING.jump_to_pods.key),
route: Route {
active_block: ActiveBlock::Pods,
id: RouteId::Home,
},
},
TabRoute {
title: format!("Services {}", DEFAULT_KEYBINDING.jump_to_services.key),
route: Route {
active_block: ActiveBlock::Services,
id: RouteId::Home,
},
},
TabRoute {
title: format!("Nodes {}", DEFAULT_KEYBINDING.jump_to_nodes.key),
route: Route {
active_block: ActiveBlock::Nodes,
id: RouteId::Home,
},
},
TabRoute {
title: format!("ConfigMaps {}", DEFAULT_KEYBINDING.jump_to_configmaps.key),
route: Route {
active_block: ActiveBlock::ConfigMaps,
id: RouteId::Home,
},
},
TabRoute {
title: format!(
"StatefulSets {}",
DEFAULT_KEYBINDING.jump_to_statefulsets.key
),
route: Route {
active_block: ActiveBlock::StatefulSets,
id: RouteId::Home,
},
},
TabRoute {
title: format!("ReplicaSets {}", DEFAULT_KEYBINDING.jump_to_replicasets.key),
route: Route {
active_block: ActiveBlock::ReplicaSets,
id: RouteId::Home,
},
},
TabRoute {
title: format!("Deployments {}", DEFAULT_KEYBINDING.jump_to_deployments.key),
route: Route {
active_block: ActiveBlock::Deployments,
id: RouteId::Home,
},
},
TabRoute {
title: format!("Jobs {}", DEFAULT_KEYBINDING.jump_to_jobs.key),
route: Route {
active_block: ActiveBlock::Jobs,
id: RouteId::Home,
},
},
TabRoute {
title: format!("DaemonSets {}", DEFAULT_KEYBINDING.jump_to_daemonsets.key),
route: Route {
active_block: ActiveBlock::DaemonSets,
id: RouteId::Home,
},
},
TabRoute {
title: format!("More {}", DEFAULT_KEYBINDING.jump_to_more_resources.key),
route: Route {
active_block: ActiveBlock::More,
id: RouteId::Home,
},
},
]),
more_resources_menu: StatefulList::with_items(vec![
("Cron Jobs".into(), ActiveBlock::CronJobs),
("Secrets".into(), ActiveBlock::Secrets),
("Replication Controllers".into(), ActiveBlock::RplCtrl),
// ("Persistent Volume Claims".into(), ActiveBlock::RplCtrl),
// ("Persistent Volumes".into(), ActiveBlock::RplCtrl),
("Storage Classes".into(), ActiveBlock::StorageClasses),
("Roles".into(), ActiveBlock::Roles),
// ("Role Bindings".into(), ActiveBlock::RplCtrl),
("Cluster Roles".into(), ActiveBlock::ClusterRoles),
// ("Cluster Role Bindings".into(), ActiveBlock::RplCtrl),
// ("Service Accounts".into(), ActiveBlock::RplCtrl),
// ("Ingresses".into(), ActiveBlock::RplCtrl),
// ("Network Policies".into(), ActiveBlock::RplCtrl),
]),
show_info_bar: true,
is_loading: false,
is_streaming: false,
is_routing: false,
tick_until_poll: 0,
tick_count: 0,
enhanced_graphics: false,
table_cols: 0,
size: Rect::default(),
api_error: String::new(),
dialog: None,
confirm: false,
light_theme: false,
refresh: true,
log_auto_scroll: true,
utilization_group_by: vec![
GroupBy::resource,
GroupBy::node,
GroupBy::namespace,
GroupBy::pod,
],
help_docs: StatefulTable::with_items(key_binding::get_help_docs()),
data: Data::default(),
}
}
}
impl App {
pub fn new(
io_tx: Sender<IoEvent>,
io_stream_tx: Sender<IoStreamEvent>,
io_cmd_tx: Sender<IoCmdEvent>,
enhanced_graphics: bool,
tick_until_poll: u64,
) -> Self {
App {
io_tx: Some(io_tx),
io_stream_tx: Some(io_stream_tx),
io_cmd_tx: Some(io_cmd_tx),
enhanced_graphics,
tick_until_poll,
..App::default()
}
}
pub fn reset(&mut self) {
self.tick_count = 0;
self.api_error = String::new();
self.data = Data::default();
self.route_home();
}
// Send a network event to the network thread
pub async fn dispatch(&mut self, action: IoEvent) {
// `is_loading` will be set to false again after the async action has finished in network/mod.rs
self.is_loading = true;
if let Some(io_tx) = &self.io_tx {
if let Err(e) = io_tx.send(action).await {
self.is_loading = false;
println!("Error from network dispatch {}", e);
self.handle_error(anyhow!(e));
};
}
}
// Send a stream event to the stream network thread
pub async fn dispatch_stream(&mut self, action: IoStreamEvent) {
// `is_loading` will be set to false again after the async action has finished in network/stream.rs
self.is_loading = true;
if let Some(io_stream_tx) = &self.io_stream_tx {
if let Err(e) = io_stream_tx.send(action).await {
self.is_loading = false;
println!("Error from stream dispatch {}", e);
self.handle_error(anyhow!(e));
};
}
}
// Send a cmd event to the cmd runner thread
pub async fn dispatch_cmd(&mut self, action: IoCmdEvent) {
// `is_loading` will be set to false again after the async action has finished in network/stream.rs
self.is_loading = true;
if let Some(io_cmd_tx) = &self.io_cmd_tx {
if let Err(e) = io_cmd_tx.send(action).await {
self.is_loading = false;
println!("Error from cmd dispatch {}", e);
self.handle_error(anyhow!(e));
};
}
}
pub fn set_contexts(&mut self, contexts: Vec<KubeContext>) {
self.data.active_context = contexts.iter().find_map(|ctx| {
if ctx.is_active {
Some(ctx.clone())
} else {
None
}
});
self.data.contexts.set_items(contexts);
}
pub fn handle_error(&mut self, e: anyhow::Error) {
self.api_error = e.to_string();
}
pub fn push_navigation_stack(&mut self, id: RouteId, active_block: ActiveBlock) {
self.push_navigation_route(Route { id, active_block });
}
pub fn push_navigation_route(&mut self, route: Route) {
self.navigation_stack.push(route);
self.is_routing = true;
}
pub fn pop_navigation_stack(&mut self) -> Option<Route> {
self.is_routing = true;
if self.navigation_stack.len() == 1 {
None
} else {
self.navigation_stack.pop()
}
}
pub fn get_current_route(&self) -> &Route {
// if for some reason there is no route return the default
self.navigation_stack.last().unwrap_or(&DEFAULT_ROUTE)
}
pub fn get_prev_route(&self) -> &Route {
// get the previous route
self.get_nth_route_from_last(1)
}
pub fn get_nth_route_from_last(&self, index: usize) -> &Route {
// get the previous route by index
let index = self.navigation_stack.len().saturating_sub(index + 1);
if index > 0 {
&self.navigation_stack[index]
} else {
&self.navigation_stack[0]
}
}
pub fn cycle_main_routes(&mut self) {
self.main_tabs.next();
let route = self.main_tabs.get_active_route().clone();
self.push_navigation_route(route);
}
pub fn route_home(&mut self) {
let route = self.main_tabs.set_index(0).route.clone();
self.push_navigation_route(route);
}
pub fn route_contexts(&mut self) {
let route = self.main_tabs.set_index(1).route.clone();
self.push_navigation_route(route);
}
pub fn route_utilization(&mut self) {
let route = self.main_tabs.set_index(2).route.clone();
self.push_navigation_route(route);
}
pub async fn dispatch_container_logs(&mut self, id: String) {
self.data.logs = LogsState::new(id);
self.push_navigation_stack(RouteId::Home, ActiveBlock::Logs);
self.dispatch_stream(IoStreamEvent::GetPodLogs(true)).await;
}
pub fn refresh(&mut self) {
self.refresh = true;
}
pub async fn cache_all_resource_data(&mut self) {
self.dispatch(IoEvent::GetNamespaces).await;
self.dispatch(IoEvent::GetPods).await;
self.dispatch(IoEvent::GetServices).await;
self.dispatch(IoEvent::GetConfigMaps).await;
self.dispatch(IoEvent::GetStatefulSets).await;
self.dispatch(IoEvent::GetReplicaSets).await;
self.dispatch(IoEvent::GetDeployments).await;
self.dispatch(IoEvent::GetJobs).await;
self.dispatch(IoEvent::GetDaemonSets).await;
self.dispatch(IoEvent::GetCronJobs).await;
self.dispatch(IoEvent::GetSecrets).await;
self.dispatch(IoEvent::GetReplicationControllers).await;
self.dispatch(IoEvent::GetStorageClasses).await;
self.dispatch(IoEvent::GetRoles).await;
self.dispatch(IoEvent::GetClusterRoles).await;
self.dispatch(IoEvent::GetMetrics).await;
}
pub async fn dispatch_by_active_block(&mut self, active_block: ActiveBlock) {
match active_block {
ActiveBlock::Pods | ActiveBlock::Containers => {
self.dispatch(IoEvent::GetPods).await;
}
ActiveBlock::Services => {
self.dispatch(IoEvent::GetServices).await;
}
ActiveBlock::ConfigMaps => {
self.dispatch(IoEvent::GetConfigMaps).await;
}
ActiveBlock::StatefulSets => {
self.dispatch(IoEvent::GetStatefulSets).await;
}
ActiveBlock::ReplicaSets => {
self.dispatch(IoEvent::GetReplicaSets).await;
}
ActiveBlock::Deployments => {
self.dispatch(IoEvent::GetDeployments).await;
}
ActiveBlock::Jobs => {
self.dispatch(IoEvent::GetJobs).await;
}
ActiveBlock::DaemonSets => {
self.dispatch(IoEvent::GetDaemonSets).await;
}
ActiveBlock::CronJobs => {
self.dispatch(IoEvent::GetCronJobs).await;
}
ActiveBlock::Secrets => {
self.dispatch(IoEvent::GetSecrets).await;
}
ActiveBlock::RplCtrl => {
self.dispatch(IoEvent::GetReplicationControllers).await;
}
ActiveBlock::StorageClasses => {
self.dispatch(IoEvent::GetStorageClasses).await;
}
ActiveBlock::Roles => {
self.dispatch(IoEvent::GetRoles).await;
}
ActiveBlock::ClusterRoles => {
self.dispatch(IoEvent::GetClusterRoles).await;
}
ActiveBlock::Logs => {
if !self.is_streaming {
// do not tail to avoid duplicates
self.dispatch_stream(IoStreamEvent::GetPodLogs(false)).await;
}
}
_ => {}
}
}
pub async fn on_tick(&mut self, first_render: bool) {
// Make one time requests on first render or refresh
if self.refresh {
if !first_render {
self.dispatch(IoEvent::RefreshClient).await;
self.dispatch_stream(IoStreamEvent::RefreshClient).await;
}
self.dispatch(IoEvent::GetKubeConfig).await;
// call these once to pre-load data
self.cache_all_resource_data().await;
self.refresh = false;
}
// make network requests only in intervals to avoid hogging up the network
if self.tick_count % self.tick_until_poll == 0 || self.is_routing {
// make periodic network calls based on active route and active block to avoid hogging
match self.get_current_route().id {
RouteId::Home => {
if self.data.clis.is_empty() {
self.dispatch_cmd(IoCmdEvent::GetCliInfo).await;
}
self.dispatch(IoEvent::GetNamespaces).await;
self.dispatch(IoEvent::GetNodes).await;
let active_block = self.get_current_route().active_block;
if active_block == ActiveBlock::Namespaces {
self
.dispatch_by_active_block(self.get_prev_route().active_block)
.await;
} else {
self.dispatch_by_active_block(active_block).await;
}
}
RouteId::Utilization => {
self.dispatch(IoEvent::GetMetrics).await;
}
_ => {}
}
self.is_routing = false;
}
self.tick_count += 1;
}
}
/// utility methods for tests
#[cfg(test)]
#[macro_use]
mod test_utils {
use std::{fmt, fs};
use k8s_openapi::{
apimachinery::pkg::apis::meta::v1::Time,
chrono::{DateTime, TimeZone, Utc},
};
use kube::{api::ObjectList, Resource};
use serde::{de::DeserializeOwned, Serialize};
use super::models::KubeResource;
pub fn convert_resource_from_file<K: Serialize, T>(filename: &str) -> (Vec<T>, Vec<K>)
where
<K as Resource>::DynamicType: Default,
K: Clone + DeserializeOwned + fmt::Debug,
K: Resource,
T: KubeResource<K>,
{
let res_list = load_resource_from_file(filename);
let original_res_list = res_list.items.clone();
let resources: Vec<T> = res_list
.iter()
.map(|it| T::from_api(it))
.collect::<Vec<_>>();
(resources, original_res_list)
}
pub fn load_resource_from_file<K>(filename: &str) -> ObjectList<K>
where
<K as Resource>::DynamicType: Default,
K: Clone + DeserializeOwned + fmt::Debug,
K: Resource,
{
let yaml = fs::read_to_string(format!("./test_data/{}.yaml", filename))
.expect("Something went wrong reading yaml file");
assert_ne!(yaml, "".to_string());
let res_list: serde_yaml::Result<ObjectList<K>> = serde_yaml::from_str(&*yaml);
assert!(res_list.is_ok(), "{:?}", res_list.err());
res_list.unwrap()
}
pub fn get_time(s: &str) -> Time {
Time(to_utc(s))
}
fn to_utc(s: &str) -> DateTime<Utc> {
Utc.datetime_from_str(s, "%Y-%m-%dT%H:%M:%SZ").unwrap()
}
#[macro_export]
macro_rules! map_string_object {
// map-like
($($k:expr => $v:expr),* $(,)?) => {
std::iter::Iterator::collect(IntoIterator::into_iter([$(($k.to_string(), $v),)*]))
};
}
}
#[cfg(test)]
mod tests {
use tokio::sync::mpsc;
use super::*;
#[tokio::test]
async fn test_on_tick_first_render() {
let (sync_io_tx, mut sync_io_rx) = mpsc::channel::<IoEvent>(500);
let (sync_io_cmd_tx, mut sync_io_cmd_rx) = mpsc::channel::<IoCmdEvent>(500);
let mut app = App {
tick_until_poll: 2,
io_tx: Some(sync_io_tx),
io_cmd_tx: Some(sync_io_cmd_tx),
..App::default()
};
assert_eq!(app.tick_count, 0);
// test first render
app.on_tick(true).await;
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetKubeConfig);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetNamespaces);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetPods);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetServices);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetConfigMaps);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetStatefulSets);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetReplicaSets);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetDeployments);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetJobs);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetDaemonSets);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetCronJobs);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetSecrets);
assert_eq!(
sync_io_rx.recv().await.unwrap(),
IoEvent::GetReplicationControllers
);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetStorageClasses);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetRoles);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetClusterRoles);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetMetrics);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetNamespaces);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetNodes);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetPods);
assert_eq!(sync_io_cmd_rx.recv().await.unwrap(), IoCmdEvent::GetCliInfo);
assert!(!app.refresh);
assert!(!app.is_routing);
assert_eq!(app.tick_count, 1);
}
#[tokio::test]
async fn test_on_tick_refresh_tick_limit() {
let (sync_io_tx, mut sync_io_rx) = mpsc::channel::<IoEvent>(500);
let (sync_io_stream_tx, mut sync_io_stream_rx) = mpsc::channel::<IoStreamEvent>(500);
let (sync_io_cmd_tx, mut sync_io_cmd_rx) = mpsc::channel::<IoCmdEvent>(500);
let mut app = App {
tick_until_poll: 2,
tick_count: 2,
refresh: true,
io_tx: Some(sync_io_tx),
io_stream_tx: Some(sync_io_stream_tx),
io_cmd_tx: Some(sync_io_cmd_tx),
..App::default()
};
assert_eq!(app.tick_count, 2);
// test first render
app.on_tick(false).await;
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::RefreshClient);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetKubeConfig);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetNamespaces);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetPods);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetServices);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetConfigMaps);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetStatefulSets);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetReplicaSets);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetDeployments);
assert_eq!(
sync_io_stream_rx.recv().await.unwrap(),
IoStreamEvent::RefreshClient
);
assert_eq!(sync_io_cmd_rx.recv().await.unwrap(), IoCmdEvent::GetCliInfo);
assert!(!app.refresh);
assert!(!app.is_routing);
assert_eq!(app.tick_count, 3);
}
#[tokio::test]
async fn test_on_tick_routing() {
let (sync_io_tx, mut sync_io_rx) = mpsc::channel::<IoEvent>(500);
let (sync_io_stream_tx, mut sync_io_stream_rx) = mpsc::channel::<IoStreamEvent>(500);
let mut app = App {
tick_until_poll: 2,
tick_count: 2,
is_routing: true,
refresh: false,
io_tx: Some(sync_io_tx),
io_stream_tx: Some(sync_io_stream_tx),
..App::default()
};
app.push_navigation_stack(RouteId::Home, ActiveBlock::Logs);
assert_eq!(app.tick_count, 2);
// test first render
app.on_tick(false).await;
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetNamespaces);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetNodes);
assert_eq!(
sync_io_stream_rx.recv().await.unwrap(),
IoStreamEvent::GetPodLogs(false)
);
assert!(!app.refresh);
assert!(!app.is_routing);
assert_eq!(app.tick_count, 3);
}
}