Skip to content

Commit

Permalink
Merge pull request #371 from kdash-rs/refactor
Browse files Browse the repository at this point in the history
Refactor to traits
  • Loading branch information
deepu105 authored Aug 23, 2023
2 parents 0df7fba + 3854d08 commit e092d7a
Show file tree
Hide file tree
Showing 36 changed files with 3,164 additions and 2,647 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ base64 ="0.21.2"
openssl = { version = "0.10", features = ["vendored"] }
human-panic = "1.1"
kubectl-view-allocations = { version="0.16.3", default-features = false }
async-trait = "0.1.73"

# XCB is a PITA to compile for ARM so disabling the copy feature on ARM for now
[target.'cfg(target_arch = "x86_64")'.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ lint:

## Fix lint
lint-fix:
@cargo fix
@cargo fix --allow-staged

## Run format
fmt:
Expand Down
79 changes: 78 additions & 1 deletion src/app/configmaps.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
use std::collections::BTreeMap;

use async_trait::async_trait;
use k8s_openapi::{api::core::v1::ConfigMap, chrono::Utc};
use tui::{
backend::Backend,
layout::{Constraint, Rect},
widgets::{Cell, Row},
Frame,
};

use super::{models::KubeResource, utils};
use super::{
models::{AppResource, KubeResource},
utils, ActiveBlock, App,
};
use crate::{
draw_resource_tab,
network::Network,
ui::utils::{
draw_describe_block, draw_resource_block, get_describe_active, get_resource_title,
style_primary, title_with_dual_style, ResourceTableProps, COPY_HINT, DESCRIBE_AND_YAML_HINT,
},
};

#[derive(Clone, PartialEq, Debug)]
pub struct KubeConfigMap {
Expand Down Expand Up @@ -31,6 +49,65 @@ impl KubeResource<ConfigMap> for KubeConfigMap {
}
}

static CONFIG_MAPS_TITLE: &str = "ConfigMaps";

pub struct ConfigMapResource {}

#[async_trait]
impl AppResource for ConfigMapResource {
fn render<B: Backend>(block: ActiveBlock, f: &mut Frame<'_, B>, app: &mut App, area: Rect) {
draw_resource_tab!(
CONFIG_MAPS_TITLE,
block,
f,
app,
area,
Self::render,
draw_block,
app.data.config_maps
);
}

async fn get_resource(nw: &Network<'_>) {
let items: Vec<KubeConfigMap> = nw.get_namespaced_resources(ConfigMap::into).await;

let mut app = nw.app.lock().await;
app.data.config_maps.set_items(items);
}
}

fn draw_block<B: Backend>(f: &mut Frame<'_, B>, app: &mut App, area: Rect) {
let title = get_resource_title(app, CONFIG_MAPS_TITLE, "", app.data.config_maps.items.len());

draw_resource_block(
f,
area,
ResourceTableProps {
title,
inline_help: DESCRIBE_AND_YAML_HINT.into(),
resource: &mut app.data.config_maps,
table_headers: vec!["Namespace", "Name", "Data", "Age"],
column_widths: vec![
Constraint::Percentage(30),
Constraint::Percentage(40),
Constraint::Percentage(15),
Constraint::Percentage(15),
],
},
|c| {
Row::new(vec![
Cell::from(c.namespace.to_owned()),
Cell::from(c.name.to_owned()),
Cell::from(c.data.len().to_string()),
Cell::from(c.age.to_owned()),
])
.style(style_primary(app.light_theme))
},
app.light_theme,
app.is_loading,
);
}

#[cfg(test)]
mod tests {
use k8s_openapi::chrono::Utc;
Expand Down
69 changes: 69 additions & 0 deletions src/app/contexts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
use async_trait::async_trait;
use kube::config::{Context, Kubeconfig, NamedContext};
use tui::{
backend::Backend,
layout::{Constraint, Rect},
widgets::{Cell, Row, Table},
Frame,
};

use crate::{
network::Network,
ui::{
utils::{
layout_block_active, loading, style_highlight, style_primary, style_secondary,
table_header_style,
},
HIGHLIGHT,
},
};

use super::{models::AppResource, ActiveBlock, App};

#[derive(Clone, Default)]
pub struct KubeContext {
Expand Down Expand Up @@ -49,3 +69,52 @@ fn is_active_context(
},
}
}

pub struct ContextResource {}

#[async_trait]
impl AppResource for ContextResource {
fn render<B: Backend>(_block: ActiveBlock, f: &mut Frame<'_, B>, app: &mut App, area: Rect) {
let title = format!(" Contexts [{}] ", app.data.contexts.items.len());
let block = layout_block_active(title.as_str(), app.light_theme);

if !app.data.contexts.items.is_empty() {
let rows = app.data.contexts.items.iter().map(|c| {
let style = if c.is_active {
style_secondary(app.light_theme)
} else {
style_primary(app.light_theme)
};
Row::new(vec![
Cell::from(c.name.as_ref()),
Cell::from(c.cluster.as_ref()),
Cell::from(c.user.as_ref()),
])
.style(style)
});

let table = Table::new(rows)
.header(table_header_style(
vec!["Context", "Cluster", "User"],
app.light_theme,
))
.block(block)
.widths(&[
Constraint::Percentage(34),
Constraint::Percentage(33),
Constraint::Percentage(33),
])
.highlight_style(style_highlight())
.highlight_symbol(HIGHLIGHT);

f.render_stateful_widget(table, area, &mut app.data.contexts.state);
} else {
loading(f, block, area, app.is_loading, app.light_theme);
}
}

async fn get_resource(_nw: &Network<'_>) {
// not required
unimplemented!()
}
}
96 changes: 95 additions & 1 deletion src/app/cronjobs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
use k8s_openapi::{api::batch::v1::CronJob, chrono::Utc};

use super::{models::KubeResource, utils};
use async_trait::async_trait;
use tui::{
backend::Backend,
layout::{Constraint, Rect},
widgets::{Cell, Row},
Frame,
};

use super::{
models::{AppResource, KubeResource},
utils, ActiveBlock, App,
};
use crate::{
draw_resource_tab,
network::Network,
ui::utils::{
draw_describe_block, draw_resource_block, get_describe_active, get_resource_title,
style_primary, title_with_dual_style, ResourceTableProps, COPY_HINT,
DESCRIBE_YAML_AND_ESC_HINT,
},
};

#[derive(Clone, Debug, PartialEq)]
pub struct KubeCronJob {
Expand Down Expand Up @@ -41,12 +61,86 @@ impl From<CronJob> for KubeCronJob {
}
}
}

impl KubeResource<CronJob> for KubeCronJob {
fn get_k8s_obj(&self) -> &CronJob {
&self.k8s_obj
}
}

static CRON_JOBS_TITLE: &str = "CronJobs";

pub struct CronJobResource {}

#[async_trait]
impl AppResource for CronJobResource {
fn render<B: Backend>(block: ActiveBlock, f: &mut Frame<'_, B>, app: &mut App, area: Rect) {
draw_resource_tab!(
CRON_JOBS_TITLE,
block,
f,
app,
area,
Self::render,
draw_block,
app.data.cronjobs
);
}

async fn get_resource(nw: &Network<'_>) {
let items: Vec<KubeCronJob> = nw.get_namespaced_resources(CronJob::into).await;

let mut app = nw.app.lock().await;
app.data.cronjobs.set_items(items);
}
}

fn draw_block<B: Backend>(f: &mut Frame<'_, B>, app: &mut App, area: Rect) {
let title = get_resource_title(app, CRON_JOBS_TITLE, "", app.data.cronjobs.items.len());

draw_resource_block(
f,
area,
ResourceTableProps {
title,
inline_help: DESCRIBE_YAML_AND_ESC_HINT.into(),
resource: &mut app.data.cronjobs,
table_headers: vec![
"Namespace",
"Name",
"Schedule",
"Last Scheduled",
"Suspend",
"Active",
"Age",
],
column_widths: vec![
Constraint::Percentage(20),
Constraint::Percentage(25),
Constraint::Percentage(15),
Constraint::Percentage(10),
Constraint::Percentage(10),
Constraint::Percentage(10),
Constraint::Percentage(10),
],
},
|c| {
Row::new(vec![
Cell::from(c.namespace.to_owned()),
Cell::from(c.name.to_owned()),
Cell::from(c.schedule.to_owned()),
Cell::from(c.last_schedule.to_string()),
Cell::from(c.suspend.to_string()),
Cell::from(c.active.to_string()),
Cell::from(c.age.to_owned()),
])
.style(style_primary(app.light_theme))
},
app.light_theme,
app.is_loading,
);
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading

0 comments on commit e092d7a

Please sign in to comment.