Skip to content

Commit

Permalink
Added support for CronJobs
Browse files Browse the repository at this point in the history
  • Loading branch information
somayaj committed Jun 26, 2021
1 parent b5ec9e6 commit 5919837
Show file tree
Hide file tree
Showing 7 changed files with 446 additions and 1 deletion.
59 changes: 59 additions & 0 deletions src/app/cronjobs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use k8s_openapi::{api::batch::v2alpha1::CronJob, chrono::Utc};

use super::{models::KubeResource, utils};

#[derive(Clone, Debug, PartialEq)]
pub struct KubeCronJob {
pub name: String,
pub namespace: String,
pub metadata: String,
pub completions: String,
pub duration: String,
pub age: String,
k8s_obj: CronJob,
}

impl KubeResource<CronJob> for KubeCronJob {
fn from_api(job: &CronJob) -> Self {
let completions = match (job.spec.as_ref(), job.status.as_ref()) {
(Some(spc), Some(stat)) => match spc.successful_jobs_history_limit {
Some(c) => format!("{:?}/{:?}", stat.active.as_ref(), c),
None => "".to_string(),
},
(None, Some(stat)) => format!("{:?}/1", stat.active.as_ref()),
_ => "".into(),
};

let duration = match job.status.as_ref() {
Some(stat) => match stat.last_schedule_time.as_ref() {
Some(st) => match stat.last_schedule_time.as_ref() {
Some(ct) => {
let duration = ct.0.signed_duration_since(st.0);
utils::duration_to_age(duration)
}
None => utils::to_age(stat.last_schedule_time.as_ref(), Utc::now()),
},
None => "<none>".to_string(),
},
None => "<none>".to_string(),
};

KubeCronJob {
name: job.metadata.name.clone().unwrap_or_default(),
namespace: job.metadata.namespace.clone().unwrap_or_default(),
metadata: job.metadata.clone().generate_name.unwrap(),
completions,
duration,
age: utils::to_age(job.metadata.creation_timestamp.as_ref(), Utc::now()),
k8s_obj: job.to_owned(),
}
}

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

#[cfg(test)]
mod tests {
}
8 changes: 8 additions & 0 deletions src/app/key_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ generate_keybindings! {
jump_to_replicasets,
jump_to_deployments,
jump_to_jobs,
jump_to_cron_jobs,
cycle_group_by
}
#[derive(PartialEq, Eq, Clone, Copy, Hash, Debug)]
Expand Down Expand Up @@ -245,6 +246,13 @@ pub const DEFAULT_KEYBINDING: KeyBindings = KeyBindings {
desc: "Select jobs tab",
context: HContext::Overview,
},
jump_to_cron_jobs: KeyBinding {
key: Key::Char('9'),
alt: None,
desc: "Select cron jobs tab",
context: HContext::Overview,
},

cycle_group_by: KeyBinding {
key: Key::Char('g'),
alt: None,
Expand Down
15 changes: 14 additions & 1 deletion src/app/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub(crate) mod configmaps;
pub(crate) mod contexts;
pub(crate) mod cronjobs;
pub(crate) mod deployments;
pub(crate) mod jobs;
pub(crate) mod key_binding;
Expand All @@ -16,6 +17,7 @@ mod utils;
use self::{
configmaps::KubeConfigMap,
contexts::KubeContext,
cronjobs::KubeCronJob,
deployments::KubeDeployment,
jobs::KubeJob,
key_binding::DEFAULT_KEYBINDING,
Expand Down Expand Up @@ -55,6 +57,7 @@ pub enum ActiveBlock {
Contexts,
Utilization,
Jobs,
CronJobs,
}

#[derive(Clone, PartialEq, Debug)]
Expand Down Expand Up @@ -101,6 +104,7 @@ pub struct Data {
pub replica_sets: StatefulTable<KubeReplicaSet>,
pub deployments: StatefulTable<KubeDeployment>,
pub jobs: StatefulTable<KubeJob>,
pub cronjobs: StatefulTable<KubeCronJob>,
pub logs: LogsState,
pub describe_out: ScrollableTxt,
pub metrics: StatefulTable<(Vec<String>, Option<QtyByQualifier>)>,
Expand Down Expand Up @@ -162,6 +166,7 @@ impl Default for Data {
replica_sets: StatefulTable::new(),
deployments: StatefulTable::new(),
jobs: StatefulTable::new(),
cronjobs: StatefulTable::new(),
selected: Selected {
ns: None,
pod: None,
Expand Down Expand Up @@ -273,6 +278,13 @@ impl Default for App {
id: RouteId::Home,
},
},
TabRoute {
title: format!("Cron Jobs {}", DEFAULT_KEYBINDING.jump_to_cron_jobs.key),
route: Route {
active_block: ActiveBlock::CronJobs,
id: RouteId::Home,
},
},
]),
show_info_bar: true,
is_loading: false,
Expand Down Expand Up @@ -455,6 +467,7 @@ impl App {
self.dispatch(IoEvent::GetReplicaSets).await;
self.dispatch(IoEvent::GetDeployments).await;
self.dispatch(IoEvent::GetJobs).await;
self.dispatch(IoEvent::GetCronJobs).await;
self.refresh = false;
}
// make network requests only in intervals to avoid hogging up the network
Expand Down Expand Up @@ -555,7 +568,6 @@ mod test_utils {

let res_list: serde_yaml::Result<ObjectList<K>> = serde_yaml::from_str(&*yaml);
assert!(res_list.is_ok());

res_list.unwrap()
}

Expand Down Expand Up @@ -605,6 +617,7 @@ mod tests {
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::GetCronJobs);
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);
Expand Down
20 changes: 20 additions & 0 deletions src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ async fn handle_route_events(key: Key, app: &mut App) {
let route = app.context_tabs.set_index(7).route.clone();
app.push_navigation_route(route);
}
_ if key == DEFAULT_KEYBINDING.jump_to_cron_jobs.key => {
let route = app.context_tabs.set_index(8).route.clone();
app.push_navigation_route(route);
}
_ => {}
};

Expand Down Expand Up @@ -318,6 +322,21 @@ async fn handle_route_events(key: Key, app: &mut App) {
.await;
}
}
ActiveBlock::CronJobs => {
if let Some(res) = handle_table_action(key, &mut app.data.cronjobs) {
let _ok = handle_describe_or_yaml_action(
key,
app,
&res,
IoCmdEvent::GetDescribe {
kind: "cronjob".to_owned(),
value: res.name.to_owned(),
ns: Some(res.namespace.to_owned()),
},
)
.await;
}
}
ActiveBlock::Contexts | ActiveBlock::Utilization | ActiveBlock::Help => { /* Do nothing */ }
}
}
Expand Down Expand Up @@ -392,6 +411,7 @@ async fn handle_scroll(app: &mut App, down: bool, is_mouse: bool) {
ActiveBlock::ReplicaSets => handle_table_scroll(&mut app.data.replica_sets, down),
ActiveBlock::Deployments => handle_table_scroll(&mut app.data.deployments, down),
ActiveBlock::Jobs => handle_table_scroll(&mut app.help_docs, down),
ActiveBlock::CronJobs => handle_table_scroll(&mut app.help_docs, down),
ActiveBlock::Contexts => handle_table_scroll(&mut app.data.contexts, down),
ActiveBlock::Utilization => handle_table_scroll(&mut app.data.metrics, down),
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
@@ -1,6 +1,7 @@
use super::super::app::{
configmaps::KubeConfigMap,
contexts,
cronjobs::KubeCronJob,
deployments::KubeDeployment,
jobs::KubeJob,
metrics::{self, KubeNodeMetrics},
Expand Down Expand Up @@ -253,6 +254,15 @@ impl<'a> Network<'a> {
app.data.jobs.set_items(items);
}

pub async fn get_cron_jobs(&self) {
let items: Vec<KubeCronJob> = self
.get_namespaced_resources(|it| KubeCronJob::from_api(it))
.await;

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

pub async fn get_deployments(&self) {
let items: Vec<KubeDeployment> = self
.get_namespaced_resources(|it| KubeDeployment::from_api(it))
Expand Down
4 changes: 4 additions & 0 deletions src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub enum IoEvent {
GetReplicaSets,
GetDeployments,
GetJobs,
GetCronJobs,
GetMetrics,
RefreshClient,
}
Expand Down Expand Up @@ -131,6 +132,9 @@ impl<'a> Network<'a> {
IoEvent::GetJobs => {
self.get_jobs().await;
}
IoEvent::GetCronJobs => {
self.get_cron_jobs().await;
}
IoEvent::GetDeployments => {
self.get_deployments().await;
}
Expand Down

0 comments on commit 5919837

Please sign in to comment.