• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kdash-rs / kdash / 12205394102

06 Dec 2024 08:06PM UTC coverage: 58.458% (+21.7%) from 36.747%
12205394102

push

github

deepu105
fix CI

4337 of 7419 relevant lines covered (58.46%)

7.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.64
/src/app/replication_controllers.rs
1
use std::collections::BTreeMap;
2

3
use async_trait::async_trait;
4
use k8s_openapi::{api::core::v1::ReplicationController, chrono::Utc};
5
use ratatui::{
6
  layout::{Constraint, Rect},
7
  widgets::{Cell, Row},
8
  Frame,
9
};
10

11
use super::{
12
  models::{AppResource, KubeResource},
13
  utils::{self},
14
  ActiveBlock, App,
15
};
16
use crate::{
17
  draw_resource_tab,
18
  network::Network,
19
  ui::utils::{
20
    draw_describe_block, draw_resource_block, draw_yaml_block, get_describe_active,
21
    get_resource_title, style_primary, title_with_dual_style, ResourceTableProps, COPY_HINT,
22
    DESCRIBE_YAML_AND_ESC_HINT,
23
  },
24
};
25

26
#[derive(Clone, Debug, PartialEq)]
27
pub struct KubeReplicationController {
28
  pub name: String,
29
  pub namespace: String,
30
  pub desired: i32,
31
  pub current: i32,
32
  pub ready: i32,
33
  pub containers: String,
34
  pub images: String,
35
  pub selector: String,
36
  pub age: String,
37
  k8s_obj: ReplicationController,
38
}
39

40
impl From<ReplicationController> for KubeReplicationController {
41
  fn from(rplc: ReplicationController) -> Self {
2✔
42
    let (current, ready) = match rplc.status.as_ref() {
2✔
43
      Some(s) => (s.replicas, s.ready_replicas.unwrap_or_default()),
2✔
44
      _ => (0, 0),
×
45
    };
46

47
    let (desired, selector, (containers, images)) = match rplc.spec.as_ref() {
2✔
48
      Some(spec) => (
2✔
49
        spec.replicas.unwrap_or_default(),
2✔
50
        spec
2✔
51
          .selector
2✔
52
          .as_ref()
2✔
53
          .unwrap_or(&BTreeMap::new())
2✔
54
          .iter()
2✔
55
          .map(|(key, val)| format!("{}={}", key, val))
2✔
56
          .collect::<Vec<String>>()
2✔
57
          .join(","),
2✔
58
        match spec.template.as_ref() {
2✔
59
          Some(tmpl) => match tmpl.spec.as_ref() {
2✔
60
            Some(pspec) => (
2✔
61
              pspec
2✔
62
                .containers
2✔
63
                .iter()
2✔
64
                .map(|c| c.name.to_owned())
3✔
65
                .collect::<Vec<String>>()
2✔
66
                .join(","),
2✔
67
              pspec
2✔
68
                .containers
2✔
69
                .iter()
2✔
70
                .filter_map(|c| c.image.to_owned())
3✔
71
                .collect::<Vec<String>>()
2✔
72
                .join(","),
2✔
73
            ),
2✔
74
            None => ("".into(), "".into()),
×
75
          },
76
          None => ("".into(), "".into()),
×
77
        },
78
      ),
79
      None => (0, "".into(), ("".into(), "".into())),
×
80
    };
81

82
    KubeReplicationController {
2✔
83
      name: rplc.metadata.name.clone().unwrap_or_default(),
2✔
84
      namespace: rplc.metadata.namespace.clone().unwrap_or_default(),
2✔
85
      age: utils::to_age(rplc.metadata.creation_timestamp.as_ref(), Utc::now()),
2✔
86
      desired,
2✔
87
      current,
2✔
88
      ready,
2✔
89
      containers,
2✔
90
      images,
2✔
91
      selector,
2✔
92
      k8s_obj: utils::sanitize_obj(rplc),
2✔
93
    }
2✔
94
  }
2✔
95
}
96

97
impl KubeResource<ReplicationController> for KubeReplicationController {
98
  fn get_name(&self) -> &String {
×
99
    &self.name
×
100
  }
×
101
  fn get_k8s_obj(&self) -> &ReplicationController {
×
102
    &self.k8s_obj
×
103
  }
×
104
}
105

106
static RPL_CTRL_TITLE: &str = "ReplicationControllers";
107

108
pub struct ReplicationControllerResource {}
109

110
#[async_trait]
111
impl AppResource for ReplicationControllerResource {
112
  fn render(block: ActiveBlock, f: &mut Frame<'_>, app: &mut App, area: Rect) {
×
113
    draw_resource_tab!(
×
114
      RPL_CTRL_TITLE,
×
115
      block,
×
116
      f,
×
117
      app,
×
118
      area,
×
119
      Self::render,
120
      draw_block,
121
      app.data.rpl_ctrls
122
    );
123
  }
×
124

125
  async fn get_resource(nw: &Network<'_>) {
×
126
    let items: Vec<KubeReplicationController> = nw
×
127
      .get_namespaced_resources(ReplicationController::into)
×
128
      .await;
×
129

130
    let mut app = nw.app.lock().await;
×
131
    app.data.rpl_ctrls.set_items(items);
×
132
  }
×
133
}
134

135
fn draw_block(f: &mut Frame<'_>, app: &mut App, area: Rect) {
×
136
  let title = get_resource_title(app, RPL_CTRL_TITLE, "", app.data.rpl_ctrls.items.len());
×
137

×
138
  draw_resource_block(
×
139
    f,
×
140
    area,
×
141
    ResourceTableProps {
×
142
      title,
×
143
      inline_help: DESCRIBE_YAML_AND_ESC_HINT.into(),
×
144
      resource: &mut app.data.rpl_ctrls,
×
145
      table_headers: vec![
×
146
        "Namespace",
×
147
        "Name",
×
148
        "Desired",
×
149
        "Current",
×
150
        "Ready",
×
151
        "Containers",
×
152
        "Images",
×
153
        "Selector",
×
154
        "Age",
×
155
      ],
×
156
      column_widths: vec![
×
157
        Constraint::Percentage(15),
×
158
        Constraint::Percentage(15),
×
159
        Constraint::Percentage(10),
×
160
        Constraint::Percentage(10),
×
161
        Constraint::Percentage(10),
×
162
        Constraint::Percentage(10),
×
163
        Constraint::Percentage(10),
×
164
        Constraint::Percentage(10),
×
165
        Constraint::Percentage(10),
×
166
      ],
×
167
    },
×
168
    |c| {
×
169
      Row::new(vec![
×
170
        Cell::from(c.namespace.to_owned()),
×
171
        Cell::from(c.name.to_owned()),
×
172
        Cell::from(c.desired.to_string()),
×
173
        Cell::from(c.current.to_string()),
×
174
        Cell::from(c.ready.to_string()),
×
175
        Cell::from(c.containers.to_owned()),
×
176
        Cell::from(c.images.to_owned()),
×
177
        Cell::from(c.selector.to_owned()),
×
178
        Cell::from(c.age.to_owned()),
×
179
      ])
×
180
      .style(style_primary(app.light_theme))
×
181
    },
×
182
    app.light_theme,
×
183
    app.is_loading,
×
184
    app.data.selected.filter.to_owned(),
×
185
  );
×
186
}
×
187

188
#[cfg(test)]
189
mod tests {
190
  use super::*;
191
  use crate::app::test_utils::*;
192

193
  #[test]
194
  fn test_replica_sets_from_api() {
1✔
195
    let (rplc, rplc_list): (Vec<KubeReplicationController>, Vec<_>) =
1✔
196
      convert_resource_from_file("replication_controllers");
1✔
197

1✔
198
    assert_eq!(rplc.len(), 2);
1✔
199
    assert_eq!(
1✔
200
      rplc[0],
1✔
201
      KubeReplicationController {
1✔
202
        name: "nginx".into(),
1✔
203
        namespace: "default".into(),
1✔
204
        age: utils::to_age(Some(&get_time("2021-07-27T14:37:49Z")), Utc::now()),
1✔
205
        k8s_obj: rplc_list[0].clone(),
1✔
206
        desired: 3,
1✔
207
        current: 3,
1✔
208
        ready: 3,
1✔
209
        containers: "nginx".into(),
1✔
210
        images: "nginx".into(),
1✔
211
        selector: "app=nginx".into(),
1✔
212
      }
1✔
213
    );
1✔
214
    assert_eq!(
1✔
215
      rplc[1],
1✔
216
      KubeReplicationController {
1✔
217
        name: "nginx-new".into(),
1✔
218
        namespace: "default".into(),
1✔
219
        age: utils::to_age(Some(&get_time("2021-07-27T14:45:24Z")), Utc::now()),
1✔
220
        k8s_obj: rplc_list[1].clone(),
1✔
221
        desired: 3,
1✔
222
        current: 3,
1✔
223
        ready: 0,
1✔
224
        containers: "nginx,nginx2".into(),
1✔
225
        images: "nginx,nginx".into(),
1✔
226
        selector: "app=nginx".into(),
1✔
227
      }
1✔
228
    );
1✔
229
  }
1✔
230
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc