Skip to content

Commit

Permalink
Merge 3317c95 into 3a64b77
Browse files Browse the repository at this point in the history
  • Loading branch information
shinusuresh committed Aug 19, 2022
2 parents 3a64b77 + 3317c95 commit 7fee4f3
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/app/mod.rs
Expand Up @@ -40,7 +40,7 @@ use self::{
pods::{KubeContainer, KubePod},
replicasets::KubeReplicaSet,
replication_controllers::KubeReplicationController,
roles::{KubeClusterRoles, KubeRoles},
roles::{KubeClusterRoles, KubeRoleBindings, KubeRoles},
secrets::KubeSecret,
statefulsets::KubeStatefulSet,
storageclass::KubeStorageClass,
Expand Down Expand Up @@ -75,6 +75,7 @@ pub enum ActiveBlock {
RplCtrl,
StorageClasses,
Roles,
RoleBindings,
ClusterRoles,
More,
}
Expand Down Expand Up @@ -132,6 +133,7 @@ pub struct Data {
pub rpl_ctrls: StatefulTable<KubeReplicationController>,
pub storage_classes: StatefulTable<KubeStorageClass>,
pub roles: StatefulTable<KubeRoles>,
pub role_bindings: StatefulTable<KubeRoleBindings>,
pub clusterroles: StatefulTable<KubeClusterRoles>,
}

Expand Down Expand Up @@ -207,6 +209,7 @@ impl Default for Data {
rpl_ctrls: StatefulTable::new(),
storage_classes: StatefulTable::new(),
roles: StatefulTable::new(),
role_bindings: StatefulTable::new(),
clusterroles: StatefulTable::new(),
}
}
Expand Down Expand Up @@ -333,7 +336,7 @@ impl Default for App {
// ("Persistent Volumes".into(), ActiveBlock::RplCtrl),
("Storage Classes".into(), ActiveBlock::StorageClasses),
("Roles".into(), ActiveBlock::Roles),
// ("Role Bindings".into(), ActiveBlock::RplCtrl),
("Role Bindings".into(), ActiveBlock::RoleBindings),
("Cluster Roles".into(), ActiveBlock::ClusterRoles),
// ("Cluster Role Bindings".into(), ActiveBlock::RplCtrl),
// ("Service Accounts".into(), ActiveBlock::RplCtrl),
Expand Down Expand Up @@ -530,6 +533,7 @@ impl App {
self.dispatch(IoEvent::GetReplicationControllers).await;
self.dispatch(IoEvent::GetStorageClasses).await;
self.dispatch(IoEvent::GetRoles).await;
self.dispatch(IoEvent::GetRoleBindings).await;
self.dispatch(IoEvent::GetClusterRoles).await;
self.dispatch(IoEvent::GetMetrics).await;
}
Expand Down Expand Up @@ -575,6 +579,9 @@ impl App {
ActiveBlock::Roles => {
self.dispatch(IoEvent::GetRoles).await;
}
ActiveBlock::RoleBindings => {
self.dispatch(IoEvent::GetRoleBindings).await;
}
ActiveBlock::ClusterRoles => {
self.dispatch(IoEvent::GetClusterRoles).await;
}
Expand Down Expand Up @@ -736,6 +743,7 @@ mod tests {
);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetStorageClasses);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetRoles);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetRoleBindings);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetClusterRoles);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetMetrics);
assert_eq!(sync_io_rx.recv().await.unwrap(), IoEvent::GetNamespaces);
Expand Down
49 changes: 47 additions & 2 deletions src/app/roles.rs
@@ -1,4 +1,6 @@
use k8s_openapi::{api::rbac::v1::ClusterRole, api::rbac::v1::Role, chrono::Utc};
use k8s_openapi::{
api::rbac::v1::ClusterRole, api::rbac::v1::Role, api::rbac::v1::RoleBinding, chrono::Utc,
};

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

Expand All @@ -17,6 +19,15 @@ pub struct KubeClusterRoles {
k8s_obj: ClusterRole,
}

#[derive(Clone, Debug, PartialEq)]
pub struct KubeRoleBindings {
pub namespace: String,
pub name: String,
pub role: String,
pub age: String,
k8s_obj: RoleBinding,
}

impl KubeResource<Role> for KubeRoles {
fn from_api(role: &Role) -> Self {
KubeRoles {
Expand Down Expand Up @@ -46,9 +57,25 @@ impl KubeResource<ClusterRole> for KubeClusterRoles {
}
}

impl KubeResource<RoleBinding> for KubeRoleBindings {
fn from_api(rolebinding: &RoleBinding) -> Self {
KubeRoleBindings {
namespace: rolebinding.metadata.namespace.clone().unwrap_or_default(),
name: rolebinding.metadata.name.clone().unwrap_or_default(),
role: rolebinding.role_ref.name.clone(),
age: utils::to_age(rolebinding.metadata.creation_timestamp.as_ref(), Utc::now()),
k8s_obj: rolebinding.to_owned(),
}
}

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

#[cfg(test)]
mod tests {
use crate::app::roles::{KubeClusterRoles, KubeRoles};
use crate::app::roles::{KubeClusterRoles, KubeRoleBindings, KubeRoles};
use crate::app::test_utils::{convert_resource_from_file, get_time};
use crate::app::utils;
use k8s_openapi::chrono::Utc;
Expand Down Expand Up @@ -84,4 +111,22 @@ mod tests {
}
)
}

#[test]
fn test_role_binding_from_rbac_api() {
let (rolebindings, rolebindings_list): (Vec<KubeRoleBindings>, Vec<_>) =
convert_resource_from_file("role_bindings");

assert_eq!(rolebindings.len(), 1);
assert_eq!(
rolebindings[0],
KubeRoleBindings {
namespace: "default".to_string(),
name: "kiali".into(),
role: "kiali-viewer".into(),
age: utils::to_age(Some(&get_time("2022-06-27T16:33:07Z")), Utc::now()),
k8s_obj: rolebindings_list[0].clone(),
}
)
}
}
16 changes: 16 additions & 0 deletions src/handlers/mod.rs
Expand Up @@ -475,6 +475,21 @@ async fn handle_route_events(key: Key, app: &mut App) {
.await;
}
}
ActiveBlock::RoleBindings => {
if let Some(res) = handle_block_action(key, &mut app.data.role_bindings) {
let _ok = handle_describe_decode_or_yaml_action(
key,
app,
&res,
IoCmdEvent::GetDescribe {
kind: "rolebindings".to_owned(),
value: res.name.to_owned(),
ns: Some(res.namespace.to_owned()),
},
)
.await;
}
}
ActiveBlock::ClusterRoles => {
if let Some(res) = handle_block_action(key, &mut app.data.clusterroles) {
let _ok = handle_describe_decode_or_yaml_action(
Expand Down Expand Up @@ -554,6 +569,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::StorageClasses => app.data.storage_classes.handle_scroll(up, page),
ActiveBlock::Roles => app.data.roles.handle_scroll(up, page),
ActiveBlock::RoleBindings => app.data.role_bindings.handle_scroll(up, page),
ActiveBlock::ClusterRoles => app.data.clusterroles.handle_scroll(up, page),
ActiveBlock::Contexts => app.data.contexts.handle_scroll(up, page),
ActiveBlock::Utilization => app.data.metrics.handle_scroll(up, page),
Expand Down
11 changes: 10 additions & 1 deletion src/network/kube_api.rs
Expand Up @@ -28,7 +28,7 @@ use crate::app::{
pods::KubePod,
replicasets::KubeReplicaSet,
replication_controllers::KubeReplicationController,
roles::{KubeClusterRoles, KubeRoles},
roles::{KubeClusterRoles, KubeRoleBindings, KubeRoles},
secrets::KubeSecret,
statefulsets::KubeStatefulSet,
storageclass::KubeStorageClass,
Expand Down Expand Up @@ -318,6 +318,15 @@ impl<'a> Network<'a> {
app.data.roles.set_items(items);
}

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

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

pub async fn get_cluster_roles(&self) {
let items: Vec<KubeClusterRoles> = self
.get_namespaced_resources(|it| KubeClusterRoles::from_api(it))
Expand Down
4 changes: 4 additions & 0 deletions src/network/mod.rs
Expand Up @@ -29,6 +29,7 @@ pub enum IoEvent {
GetReplicationControllers,
GetStorageClasses,
GetRoles,
GetRoleBindings,
GetClusterRoles,
GetMetrics,
RefreshClient,
Expand Down Expand Up @@ -163,6 +164,9 @@ impl<'a> Network<'a> {
IoEvent::GetRoles => {
self.get_roles().await;
}
IoEvent::GetRoleBindings => {
self.get_role_bindings().await;
}
IoEvent::GetClusterRoles => {
self.get_cluster_roles().await;
}
Expand Down
56 changes: 56 additions & 0 deletions src/ui/resource_tabs.rs
Expand Up @@ -36,6 +36,7 @@ static SECRETS_TITLE: &str = "Secrets";
static RPL_CTRL_TITLE: &str = "ReplicationControllers";
static STORAGE_CLASSES_LABEL: &str = "StorageClasses";
static ROLES_TITLE: &str = "Roles";
static ROLE_BINDINGS_TITLE: &str = "Role bindings";
static CLUSTER_ROLES_TITLE: &str = "ClusterRoles";
static DESCRIBE_ACTIVE: &str = "-> Describe ";
static YAML_ACTIVE: &str = "-> YAML ";
Expand Down Expand Up @@ -87,6 +88,7 @@ fn draw_more<B: Backend>(block: ActiveBlock, f: &mut Frame<'_, B>, app: &mut App
ActiveBlock::RplCtrl => draw_replication_controllers_tab(block, f, app, area),
ActiveBlock::StorageClasses => draw_storage_classes_tab(block, f, app, area),
ActiveBlock::Roles => draw_roles_tab(block, f, app, area),
ActiveBlock::RoleBindings => draw_role_bindings_tab(block, f, app, area),
ActiveBlock::ClusterRoles => draw_cluster_roles_tab(block, f, app, area),
ActiveBlock::Describe | ActiveBlock::Yaml => {
let mut prev_route = app.get_prev_route();
Expand All @@ -99,6 +101,7 @@ fn draw_more<B: Backend>(block: ActiveBlock, f: &mut Frame<'_, B>, app: &mut App
ActiveBlock::RplCtrl => draw_replication_controllers_tab(block, f, app, area),
ActiveBlock::StorageClasses => draw_storage_classes_tab(block, f, app, area),
ActiveBlock::Roles => draw_roles_tab(block, f, app, area),
ActiveBlock::RoleBindings => draw_role_bindings_tab(block, f, app, area),
ActiveBlock::ClusterRoles => draw_cluster_roles_tab(block, f, app, area),
_ => { /* do nothing */ }
}
Expand Down Expand Up @@ -1062,6 +1065,59 @@ fn draw_roles_block<B: Backend>(f: &mut Frame<'_, B>, app: &mut App, area: Rect)
);
}

fn draw_role_bindings_tab<B: Backend>(
block: ActiveBlock,
f: &mut Frame<'_, B>,
app: &mut App,
area: Rect,
) {
draw_resource_tab!(
ROLE_BINDINGS_TITLE,
block,
f,
app,
area,
draw_role_bindings_tab,
draw_role_bindings_block,
app.data.role_bindings
);
}

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

draw_resource_block(
f,
area,
ResourceTableProps {
title,
inline_help: DESCRIBE_YAML_AND_ESC_HINT.into(),
resource: &mut app.data.role_bindings,
table_headers: vec!["Name", "Role", "Age"],
column_widths: vec![
Constraint::Percentage(40),
Constraint::Percentage(40),
Constraint::Percentage(20),
],
},
|c| {
Row::new(vec![
Cell::from(c.name.to_owned()),
Cell::from(c.role.to_owned()),
Cell::from(c.age.to_owned()),
])
.style(style_primary(app.light_theme))
},
app.light_theme,
app.is_loading,
);
}

fn draw_cluster_roles_tab<B: Backend>(
block: ActiveBlock,
f: &mut Frame<'_, B>,
Expand Down
32 changes: 32 additions & 0 deletions test_data/role_bindings.yaml
@@ -0,0 +1,32 @@
apiVersion: v1
items:
- apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
annotations:
operator-sdk/primary-resource: monitoring/kiali
operator-sdk/primary-resource-type: Kiali.kiali.io
creationTimestamp: "2022-06-27T16:33:07Z"
labels:
app: kiali
app.kubernetes.io/instance: kiali
app.kubernetes.io/name: kiali
app.kubernetes.io/part-of: kiali
app.kubernetes.io/version: v1.41.0
version: v1.41.0
name: kiali
namespace: default
resourceVersion: "102143364"
uid: f0de3ac8-1680-4794-88c3-927a42cccce0
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: kiali-viewer
subjects:
- kind: ServiceAccount
name: kiali-service-account
namespace: monitoring
kind: List
metadata:
resourceVersion: ""
selfLink: ""

0 comments on commit 7fee4f3

Please sign in to comment.