Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for storageclass #222

Merged
merged 3 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ pub(crate) mod replicasets;
pub(crate) mod replication_controllers;
pub(crate) mod secrets;
pub(crate) mod statefulsets;
pub(crate) mod storageclass;
pub(crate) mod svcs;
mod utils;

use crate::app::storageclass::KubeStorageClass;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move it to existing import grouping below in line 40. If you are using VSCode it should do that when you format

use anyhow::anyhow;
use kube::config::Kubeconfig;
use kubectl_view_allocations::{GroupBy, QtyByQualifier};
Expand Down Expand Up @@ -69,6 +71,7 @@ pub enum ActiveBlock {
CronJobs,
Secrets,
RplCtrl,
StorageClasses,
More,
}

Expand Down Expand Up @@ -123,6 +126,7 @@ pub struct Data {
pub logs: LogsState,
pub describe_out: ScrollableTxt,
pub metrics: StatefulTable<(Vec<String>, Option<QtyByQualifier>)>,
pub storageclasses: StatefulTable<KubeStorageClass>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to line 125 for consistency plz

}

/// selected data items
Expand Down Expand Up @@ -195,6 +199,7 @@ impl Default for Data {
logs: LogsState::new(String::default()),
describe_out: ScrollableTxt::new(),
metrics: StatefulTable::new(),
storageclasses: StatefulTable::new(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to line 192 plz

}
}
}
Expand Down Expand Up @@ -318,7 +323,7 @@ impl Default for App {
("Replication Controllers".into(), ActiveBlock::RplCtrl),
// ("Persistent Volume Claims".into(), ActiveBlock::RplCtrl),
// ("Persistent Volumes".into(), ActiveBlock::RplCtrl),
// ("Storage Classes".into(), ActiveBlock::RplCtrl),
("Storage Classes".into(), ActiveBlock::StorageClasses),
// ("Roles".into(), ActiveBlock::RplCtrl),
// ("Role Bindings".into(), ActiveBlock::RplCtrl),
// ("Cluster Roles".into(), ActiveBlock::RplCtrl),
Expand Down Expand Up @@ -515,6 +520,7 @@ impl App {
self.dispatch(IoEvent::GetCronJobs).await;
self.dispatch(IoEvent::GetSecrets).await;
self.dispatch(IoEvent::GetReplicationControllers).await;
self.dispatch(IoEvent::GetStorageClasses).await;
self.dispatch(IoEvent::GetMetrics).await;
}

Expand Down Expand Up @@ -553,6 +559,9 @@ impl App {
ActiveBlock::RplCtrl => {
self.dispatch(IoEvent::GetReplicationControllers).await;
}
ActiveBlock::StorageClasses => {
self.dispatch(IoEvent::GetStorageClasses).await;
}
ActiveBlock::Logs => {
if !self.is_streaming {
// do not tail to avoid duplicates
Expand Down Expand Up @@ -709,6 +718,7 @@ mod tests {
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::GetMetrics);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetNamespaces);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetNodes);
Expand Down
66 changes: 66 additions & 0 deletions src/app/storageclass.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use crate::app::models::KubeResource;
use crate::app::utils;
use k8s_openapi::api::storage::v1::StorageClass;
use k8s_openapi::chrono::Utc;

#[derive(Clone, Debug, PartialEq)]
pub struct KubeStorageClass {
pub name: String,
pub provisioner: String,
pub reclaim_policy: String,
pub volume_binding_mode: String,
pub allow_volume_expansion: bool,
pub age: String,
k8s_obj: StorageClass,
}

impl KubeResource<StorageClass> for KubeStorageClass {
fn from_api(storage_class: &StorageClass) -> Self {
KubeStorageClass {
name: storage_class.metadata.name.clone().unwrap_or_default(),
provisioner: storage_class.provisioner.clone(),
reclaim_policy: storage_class.reclaim_policy.clone().unwrap_or_default(),
volume_binding_mode: storage_class
.volume_binding_mode
.clone()
.unwrap_or_default(),
allow_volume_expansion: storage_class.allow_volume_expansion.unwrap_or_default(),
age: utils::to_age(
storage_class.metadata.creation_timestamp.as_ref(),
Utc::now(),
),
k8s_obj: storage_class.to_owned(),
}
}

fn get_k8s_obj(&self) -> &StorageClass {
&self.k8s_obj
}
}

#[cfg(test)]
mod tests {
use crate::app::storageclass::KubeStorageClass;
use crate::app::test_utils::{convert_resource_from_file, get_time};
use crate::app::utils;
use k8s_openapi::chrono::Utc;

#[tokio::test]
async fn test_storageclass_from_api() {
let (storage_classes, storage_classes_list): (Vec<KubeStorageClass>, Vec<_>) =
convert_resource_from_file("storageclass");
assert_eq!(storage_classes_list.len(), 4);
assert_eq!(
storage_classes[0],
KubeStorageClass {
name: "ebs-performance".into(),
provisioner: "kubernetes.io/aws-ebs".into(),
reclaim_policy: "Delete".into(),
volume_binding_mode: "Immediate".into(),
allow_volume_expansion: false,
age: utils::to_age(Some(&get_time("2021-12-14T11:08:59Z")), Utc::now()),
k8s_obj: storage_classes_list[0].clone(),
}
);
}
}
16 changes: 16 additions & 0 deletions src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,21 @@ async fn handle_route_events(key: Key, app: &mut App) {
.await;
}
}
ActiveBlock::StorageClasses => {
if let Some(res) = handle_block_action(key, &mut app.data.storageclasses) {
let _ok = handle_describe_or_yaml_action(
key,
app,
&res,
IoCmdEvent::GetDescribe {
kind: "storageclass".to_owned(),
value: res.name.to_owned(),
ns: None,
},
)
.await;
}
}
ActiveBlock::Contexts | ActiveBlock::Utilization | ActiveBlock::Help => { /* Do nothing */ }
}
}
Expand Down Expand Up @@ -479,6 +494,7 @@ async fn handle_block_scroll(app: &mut App, up: bool, is_mouse: bool, page: bool
ActiveBlock::RplCtrl => app.data.rpl_ctrls.handle_scroll(up, page),
ActiveBlock::Contexts => app.data.contexts.handle_scroll(up, page),
ActiveBlock::Utilization => app.data.metrics.handle_scroll(up, page),
ActiveBlock::StorageClasses => app.data.storageclasses.handle_scroll(up, page),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to line 494

ActiveBlock::Help => app.help_docs.handle_scroll(up, page),
ActiveBlock::More => app.more_resources_menu.handle_scroll(up, page),
ActiveBlock::Logs => {
Expand Down
10 changes: 10 additions & 0 deletions src/network/kube_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::app::{
replication_controllers::KubeReplicationController,
secrets::KubeSecret,
statefulsets::KubeStatefulSet,
storageclass::KubeStorageClass,
svcs::KubeSvc,
};

Expand Down Expand Up @@ -332,4 +333,13 @@ impl<'a> Network<'a> {
None => Api::all(self.client.clone()),
}
}

pub async fn get_storage_classes(&self) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to line 301 to seperate pub and private functions visually

let items: Vec<KubeStorageClass> = self
.get_namespaced_resources(|it| KubeStorageClass::from_api(it))
.await;

let mut app = self.app.lock().await;
app.data.storageclasses.set_items(items);
}
}
4 changes: 4 additions & 0 deletions src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub enum IoEvent {
GetCronJobs,
GetSecrets,
GetReplicationControllers,
GetStorageClasses,
GetMetrics,
RefreshClient,
}
Expand Down Expand Up @@ -154,6 +155,9 @@ impl<'a> Network<'a> {
IoEvent::GetMetrics => {
self.get_utilizations().await;
}
IoEvent::GetStorageClasses => {
self.get_storage_classes().await;
}
};

let mut app = self.app.lock().await;
Expand Down
77 changes: 77 additions & 0 deletions src/ui/resource_tabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ static SECRETS_TITLE: &str = "Secrets";
static RPL_CTRL_TITLE: &str = "ReplicationControllers";
static DESCRIBE_ACTIVE: &str = "-> Describe ";
static YAML_ACTIVE: &str = "-> YAML ";
static STORAGE_CLASSES_LABEL: &str = "Storage Classes";

pub fn draw_resource_tabs_block<B: Backend>(f: &mut Frame<'_, B>, app: &mut App, area: Rect) {
let chunks =
Expand Down Expand Up @@ -80,6 +81,7 @@ fn draw_more<B: Backend>(block: ActiveBlock, f: &mut Frame<'_, B>, app: &mut App
ActiveBlock::CronJobs => draw_cronjobs_tab(block, f, app, area),
ActiveBlock::Secrets => draw_secrets_tab(block, f, app, area),
ActiveBlock::RplCtrl => draw_replication_controllers_tab(block, f, app, area),
ActiveBlock::StorageClasses => draw_storage_classes_tab(block, f, app, area),
ActiveBlock::Describe | ActiveBlock::Yaml => {
let mut prev_route = app.get_prev_route();
if prev_route.active_block == block {
Expand All @@ -89,6 +91,7 @@ fn draw_more<B: Backend>(block: ActiveBlock, f: &mut Frame<'_, B>, app: &mut App
ActiveBlock::CronJobs => draw_cronjobs_tab(block, f, app, area),
ActiveBlock::Secrets => draw_secrets_tab(block, f, app, area),
ActiveBlock::RplCtrl => draw_replication_controllers_tab(block, f, app, area),
ActiveBlock::StorageClasses => draw_storage_classes_tab(block, f, app, area),
_ => { /* do nothing */ }
}
}
Expand Down Expand Up @@ -939,6 +942,68 @@ fn draw_replication_controllers_block<B: Backend>(f: &mut Frame<'_, B>, app: &mu
);
}

fn draw_storage_classes_tab<B: Backend>(
block: ActiveBlock,
f: &mut Frame<'_, B>,
app: &mut App,
area: Rect,
) {
draw_resource_tab!(
STORAGE_CLASSES_LABEL,
block,
f,
app,
area,
draw_storage_classes_tab,
draw_storage_classes_block,
app.data.secrets
);
}

fn draw_storage_classes_block<B: Backend>(f: &mut Frame<'_, B>, app: &mut App, area: Rect) {
let title =
get_cluster_wide_resource_title(STORAGE_CLASSES_LABEL, app.data.storageclasses.items.len());

draw_resource_block(
f,
area,
ResourceTableProps {
title,
inline_help: DESCRIBE_YAML_AND_ESC_HINT.into(),
resource: &mut app.data.storageclasses,
table_headers: vec![
"Name",
"Provisioner",
"Reclaim Policy",
"Volume binding mode",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Volume binding mode",
"Volume Binding Mode",

"Allow volume expansion",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Allow volume expansion",
"Allow Volume Expansion",

"Age",
],
column_widths: vec![
Constraint::Percentage(10),
Constraint::Percentage(20),
Constraint::Percentage(10),
Constraint::Percentage(20),
Constraint::Percentage(20),
Constraint::Percentage(10),
],
},
|c| {
Row::new(vec![
Cell::from(c.name.to_owned()),
Cell::from(c.provisioner.to_owned()),
Cell::from(c.reclaim_policy.to_owned()),
Cell::from(c.volume_binding_mode.to_owned()),
Cell::from(c.allow_volume_expansion.to_string()),
Cell::from(c.age.to_owned()),
])
.style(style_primary(app.light_theme))
},
app.light_theme,
app.is_loading,
);
}

/// common for all resources
fn draw_describe_block<B: Backend>(
f: &mut Frame<'_, B>,
Expand Down Expand Up @@ -1037,6 +1102,10 @@ fn get_node_title<S: AsRef<str>>(app: &App, suffix: S) -> String {
)
}

fn get_cluster_wide_resource_title<S: AsRef<str>>(title: S, items_len: usize) -> String {
format!("{} [{}]", title.as_ref(), items_len,)
}

fn get_resource_title<S: AsRef<str>>(app: &App, title: S, suffix: S, items_len: usize) -> String {
format!(
" {} {}",
Expand Down Expand Up @@ -1370,4 +1439,12 @@ mod tests {
fn test_title_with_ns() {
assert_eq!(title_with_ns("Title", "hello", 3), "Title (ns: hello) [3]");
}

#[test]
fn test_get_cluster_wide_resource_title() {
assert_eq!(
get_cluster_wide_resource_title("Cluster Resource", 3),
"Cluster Resource [3]"
);
}
}
Loading