Skip to main content

rkubectl_app/cli/command/
node.rs

1use std::collections::BTreeMap;
2use std::collections::BTreeSet;
3
4use super::*;
5
6use image::ImageInfo;
7use resources::Resources;
8
9mod image;
10mod resources;
11
12#[derive(Clone, Debug, Subcommand)]
13pub enum Node {
14    /// Show node info.
15    Info,
16    /// List node images.
17    #[command(visible_aliases = ["images", "img"])]
18    ListImages,
19
20    /// Show node resources (allocatable by default or capacity).
21    #[command(visible_aliases = ["resource", "res"])]
22    Resources {
23        /// Show node capacity instead of allocatable resources.
24        #[arg(short, long)]
25        capacity: bool,
26    },
27}
28
29impl Node {
30    pub async fn exec(&self, context: &Context) -> RkResult<()> {
31        match self {
32            Self::Info => self.info(context).await,
33            Self::ListImages => self.list_images(context).await,
34            Self::Resources { capacity } => self.resources(context, *capacity).await,
35        }
36    }
37
38    pub async fn info(&self, context: &Context) -> RkResult<()> {
39        for node in self.nodes(context).await? {
40            let name = node.name_any();
41            if let Some(info) = node_info(node) {
42                context.print_deprecated(format!("\n{name}"));
43                context.show(info);
44            }
45        }
46
47        Ok(())
48    }
49
50    pub async fn list_images(&self, context: &Context) -> RkResult<()> {
51        for node in self.nodes(context).await? {
52            println!("\n{}\n", node.name_any());
53            node.status
54                .unwrap_or_default()
55                .images
56                .unwrap_or_default()
57                .into_iter()
58                .filter_map(ImageInfo::from_container_image)
59                .for_each(|info| info.print());
60        }
61
62        Ok(())
63    }
64
65    async fn resources(&self, context: &Context, capacity: bool) -> RkResult<()> {
66        let nodes = self.nodes(context).await?;
67        let resources = Resources::from_nodes(nodes, capacity);
68        context.show(resources);
69
70        Ok(())
71    }
72
73    async fn nodes(&self, context: &Context) -> kube::Result<Vec<corev1::Node>> {
74        let kubeapi = context.kubeapi();
75        let lp = kubeapi.list_params();
76        kubeapi.nodes()?.list(&lp).await.map(|list| list.items)
77    }
78}
79
80fn node_info(node: corev1::Node) -> Option<corev1::NodeSystemInfo> {
81    node.status?.node_info
82}