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

kdash-rs / kdash / 18585133834

17 Oct 2025 07:02AM UTC coverage: 57.358% (-1.0%) from 58.34%
18585133834

push

github

web-flow
Merge pull request #502 from mloskot/ml/feat/configurable-install-dir

feat: Allow getLatest.sh to deploy binary to custom location

3925 of 6843 relevant lines covered (57.36%)

7.77 hits per line

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

37.65
/src/app/network_policies.rs
1
use std::vec;
2

3
use async_trait::async_trait;
4
use k8s_openapi::{api::networking::v1::NetworkPolicy, 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, ActiveBlock, App,
14
};
15
use crate::{
16
  draw_resource_tab,
17
  network::Network,
18
  ui::utils::{
19
    draw_describe_block, draw_resource_block, draw_yaml_block, get_describe_active,
20
    get_resource_title, style_primary, title_with_dual_style, ResourceTableProps, COPY_HINT,
21
    DESCRIBE_YAML_AND_ESC_HINT,
22
  },
23
};
24

25
#[derive(Clone, Debug, PartialEq)]
26
pub struct KubeNetworkPolicy {
27
  pub name: String,
28
  pub namespace: String,
29
  pub pod_selector: String,
30
  pub policy_types: String,
31
  pub age: String,
32
  k8s_obj: NetworkPolicy,
33
}
34

35
impl From<NetworkPolicy> for KubeNetworkPolicy {
36
  fn from(nw_policy: NetworkPolicy) -> Self {
4✔
37
    let pod_selector = match &nw_policy.spec {
4✔
38
      Some(s) => {
4✔
39
        let mut pod_selector = vec![];
4✔
40
        if let Some(match_labels) = &s.pod_selector.match_labels {
4✔
41
          for (k, v) in match_labels {
9✔
42
            pod_selector.push(format!("{}={}", k, v));
5✔
43
          }
5✔
44
        }
×
45
        pod_selector
4✔
46
      }
47
      _ => vec![],
×
48
    };
49

50
    Self {
51
      name: nw_policy.metadata.name.clone().unwrap_or_default(),
4✔
52
      namespace: nw_policy.metadata.namespace.clone().unwrap_or_default(),
4✔
53
      age: utils::to_age(nw_policy.metadata.creation_timestamp.as_ref(), Utc::now()),
4✔
54
      pod_selector: pod_selector.join(","),
4✔
55
      policy_types: nw_policy.spec.as_ref().map_or_else(
4✔
56
        || "".into(),
×
57
        |s| s.policy_types.clone().unwrap_or_default().join(","),
4✔
58
      ),
59
      k8s_obj: utils::sanitize_obj(nw_policy),
4✔
60
    }
61
  }
4✔
62
}
63

64
impl KubeResource<NetworkPolicy> for KubeNetworkPolicy {
65
  fn get_name(&self) -> &String {
×
66
    &self.name
×
67
  }
×
68
  fn get_k8s_obj(&self) -> &NetworkPolicy {
×
69
    &self.k8s_obj
×
70
  }
×
71
}
72

73
static NW_POLICY_TITLE: &str = "NetworkPolicies";
74

75
pub struct NetworkPolicyResource {}
76

77
#[async_trait]
78
impl AppResource for NetworkPolicyResource {
79
  fn render(block: ActiveBlock, f: &mut Frame<'_>, app: &mut App, area: Rect) {
×
80
    draw_resource_tab!(
×
81
      NW_POLICY_TITLE,
×
82
      block,
×
83
      f,
×
84
      app,
×
85
      area,
×
86
      Self::render,
87
      draw_block,
88
      app.data.nw_policies
89
    );
90
  }
×
91

92
  async fn get_resource(nw: &Network<'_>) {
×
93
    let items: Vec<KubeNetworkPolicy> = nw.get_namespaced_resources(NetworkPolicy::into).await;
×
94

95
    let mut app = nw.app.lock().await;
×
96
    app.data.nw_policies.set_items(items);
×
97
  }
×
98
}
99

100
fn draw_block(f: &mut Frame<'_>, app: &mut App, area: Rect) {
×
101
  let title = get_resource_title(app, NW_POLICY_TITLE, "", app.data.nw_policies.items.len());
×
102

103
  draw_resource_block(
×
104
    f,
×
105
    area,
×
106
    ResourceTableProps {
×
107
      title,
×
108
      inline_help: DESCRIBE_YAML_AND_ESC_HINT.into(),
×
109
      resource: &mut app.data.nw_policies,
×
110
      table_headers: vec!["Namespace", "Name", "Pod Selector", "Policy Types", "Age"],
×
111
      column_widths: vec![
×
112
        Constraint::Percentage(20),
×
113
        Constraint::Percentage(20),
×
114
        Constraint::Percentage(30),
×
115
        Constraint::Percentage(20),
×
116
        Constraint::Percentage(10),
×
117
      ],
×
118
    },
×
119
    |c| {
×
120
      Row::new(vec![
×
121
        Cell::from(c.namespace.to_owned()),
×
122
        Cell::from(c.name.to_owned()),
×
123
        Cell::from(c.pod_selector.to_owned()),
×
124
        Cell::from(c.policy_types.to_owned()),
×
125
        Cell::from(c.age.to_owned()),
×
126
      ])
127
      .style(style_primary(app.light_theme))
×
128
    },
×
129
    app.light_theme,
×
130
    app.is_loading,
×
131
    app.data.selected.filter.to_owned(),
×
132
  );
133
}
×
134

135
#[cfg(test)]
136
mod tests {
137
  use super::*;
138
  use crate::app::test_utils::*;
139

140
  #[test]
141
  fn test_nw_policys_from_api() {
1✔
142
    let (nw_policys, nw_policy_list): (Vec<KubeNetworkPolicy>, Vec<_>) =
1✔
143
      convert_resource_from_file("network_policy");
1✔
144

145
    assert_eq!(nw_policys.len(), 4);
1✔
146
    assert_eq!(
1✔
147
      nw_policys[3],
1✔
148
      KubeNetworkPolicy {
1✔
149
        name: "sample-network-policy-4".into(),
1✔
150
        namespace: "default".into(),
1✔
151
        age: utils::to_age(Some(&get_time("2023-07-04T17:04:33Z")), Utc::now()),
1✔
152
        k8s_obj: nw_policy_list[3].clone(),
1✔
153
        pod_selector: "app=webapp,app3=webapp3".into(),
1✔
154
        policy_types: "Egress,Ingress".into(),
1✔
155
      }
1✔
156
    );
157
  }
1✔
158
}
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

© 2025 Coveralls, Inc