Skip to main content

rkubectl_app/cli/
command.rs

1use tabled::settings::Padding;
2use tabled::settings::Remove;
3use tabled::settings::Style;
4use tabled::settings::location::ByColumnName;
5
6use k8s::ClusterRoleExt;
7use k8s::NamespaceExt;
8use k8s::PolicyRuleExt;
9use k8s::SecretExt;
10
11use super::*;
12
13pub use api_resource::ApiResource;
14pub use api_resource::ApiResources;
15pub use basic::Basic;
16pub use basic::Create;
17pub use basic::CreateResource;
18pub use cluster::ClusterInfo;
19pub use cluster::ClusterManagement;
20pub use cluster::Dump;
21pub use config::Config;
22pub use configmap::ConfigMap;
23pub use delete::Delete;
24pub use get::Get;
25pub use node::Node;
26pub use secret::Secret;
27pub use troubleshoot::Auth;
28pub use troubleshoot::Debug;
29pub use troubleshoot::TroubleshootingDebugging;
30
31mod api_resource;
32mod basic;
33mod cluster;
34mod config;
35mod configmap;
36mod delete;
37mod get;
38mod node;
39mod secret;
40mod troubleshoot;
41
42#[derive(Clone, Debug, Subcommand)]
43pub enum Command {
44    #[command(flatten,
45        // next_help_heading = "Basic Commands (Beginner)"
46    )]
47    Basic(Basic),
48
49    #[command(flatten)]
50    Intermediate(Intermediate),
51
52    #[command(flatten)]
53    Deploy(Deploy),
54
55    #[command(flatten)]
56    ClusterManagement(ClusterManagement),
57
58    #[command(flatten)]
59    TroubleshootingDebugging(TroubleshootingDebugging),
60
61    /// Print the supported API resources on the server.
62    ApiResources(ApiResources),
63
64    /// Print the supported API versions on the server, in the form of "group/version".
65    ApiVersions,
66
67    /// Modify kubeconfig file.
68    #[command(subcommand, visible_alias = "cfg")]
69    Config(Config),
70
71    /// Print status of the K8s feaures in the current cluster.
72    Features,
73
74    /// Print brief server info
75    Info,
76
77    /// Print Node related info
78    #[command(subcommand, visible_aliases = ["no", "nodes"])]
79    Node(Node),
80
81    /// Print Secret related info
82    #[command(subcommand, visible_aliases = ["se", "sec", "secrets"])]
83    Secret(Secret),
84
85    /// Print ConfigMap related info
86    #[command(subcommand, visible_aliases = ["cm", "configmaps"])]
87    ConfigMap(ConfigMap),
88
89    /// Print client and server version
90    Version,
91}
92
93impl Command {
94    pub async fn exec(self, context: &Context) -> RkResult<()> {
95        context.kubeapi().debug(&self);
96        match self {
97            Self::Basic(basic) => basic.exec(context).await,
98            Self::Intermediate(intermediate) => intermediate.exec(context).await,
99            Self::Deploy(deploy) => deploy.exec(context).await,
100            Self::ClusterManagement(cluster_management) => cluster_management.exec(context).await,
101            Self::TroubleshootingDebugging(tsd) => tsd.exec(context).await,
102            Self::ApiResources(api_resources) => api_resources.exec(context).await,
103            Self::ApiVersions => self.api_versions(context).await,
104            Self::Config(config) => config.exec(context),
105            Self::Features => self.features(context).await,
106            Self::Info => self.info(context).await,
107            Self::Node(node) => node.exec(context).await,
108            Self::Secret(secret) => secret.exec(context).await,
109            Self::ConfigMap(configmap) => configmap.exec(context).await,
110            Self::Version => self.version(context).await,
111        }
112    }
113
114    async fn api_versions(&self, context: &Context) -> RkResult<()> {
115        context
116            .kubeapi()
117            .api_versions()
118            .await?
119            .for_each(|version| context.print_deprecated(version.group_version));
120        Ok(())
121    }
122
123    async fn features(&self, context: &Context) -> RkResult<()> {
124        let features = context.kubeapi().features().await?;
125        context.show(features);
126        Ok(())
127    }
128
129    async fn info(&self, context: &Context) -> RkResult<()> {
130        let info = context.kubeapi().info().await?;
131
132        context.print_deprecated(format!("build date:     {}", info.build_date));
133        context.print_deprecated(format!("compiler:       {}", info.compiler));
134        context.print_deprecated(format!("compiler:       {}", info.compiler));
135        context.print_deprecated(format!("git_commit:     {}", info.git_commit));
136        context.print_deprecated(format!("git_tree_state: {}", info.git_tree_state));
137        context.print_deprecated(format!("git_version:    {}", info.git_version));
138        context.print_deprecated(format!("go_version:     {}", info.go_version));
139        context.print_deprecated(format!("major:          {}", info.major));
140        context.print_deprecated(format!("minor:          {}", info.minor));
141        context.print_deprecated(format!("platform:       {}", info.platform));
142
143        Ok(())
144    }
145
146    async fn version(&self, context: &Context) -> RkResult<()> {
147        let text = context.kubeapi().version().await?;
148        context.print_deprecated(text);
149        Ok(())
150    }
151}
152
153/// Basic Commands (Intermediate)
154#[derive(Clone, Debug, Subcommand)]
155// #[command(subcommand_help_heading = "Basic Commands (Intermediate)")]
156pub enum Intermediate {
157    ///  Get documentation for a resource
158    Explain,
159    Get(Get),
160    /// Edit a resource on the server
161    Edit,
162    /// Delete resources by file names, stdin, resources and names, or by resources and label selector
163    Delete(Delete),
164}
165
166impl Intermediate {
167    async fn exec(self, context: &Context) -> RkResult<()> {
168        match self {
169            Self::Explain => Err(RkError::todo()),
170            Self::Get(get) => get.exec(context).await,
171            Self::Edit => Err(RkError::todo()),
172            Self::Delete(delete) => delete.exec(context).await,
173        }
174    }
175}
176
177/// Deploy Commands
178#[derive(Clone, Debug, Subcommand)]
179
180pub enum Deploy {
181    /// Manage the rollout of a resource
182    Rollout,
183
184    /// Set a new size for a deployment, replica set, or replication controller
185    Scale,
186
187    /// Auto-scale a deployment, replica set, stateful set, or replication controller
188    Autoscale,
189}
190
191impl Deploy {
192    async fn exec(self, context: &Context) -> RkResult<()> {
193        context.not_implemented(&self);
194        match self {
195            Self::Rollout => Err(RkError::todo()),
196            Self::Scale => Err(RkError::todo()),
197            Self::Autoscale => Err(RkError::todo()),
198        }
199    }
200}
201
202// Troubleshooting and Debugging Commands:
203//   describe        Show details of a specific resource or group of resources
204//   logs            Print the logs for a container in a pod
205//   attach          Attach to a running container
206//   exec            Execute a command in a container
207//   port-forward    Forward one or more local ports to a pod
208//   proxy           Run a proxy to the Kubernetes API server
209//   cp              Copy files and directories to and from containers
210//   auth            Inspect authorization
211//   debug           Create debugging sessions for troubleshooting workloads and nodes
212//   events          List events
213
214// Advanced Commands:
215//   diff            Diff the live version against a would-be applied version
216//   apply           Apply a configuration to a resource by file name or stdin
217//   patch           Update fields of a resource
218//   replace         Replace a resource by file name or stdin
219//   wait            Experimental: Wait for a specific condition on one or many resources
220//   kustomize       Build a kustomization target from a directory or URL
221
222// Settings Commands:
223//   label           Update the labels on a resource
224//   annotate        Update the annotations on a resource
225//   completion      Output shell completion code for the specified shell (bash, zsh, fish, or powershell)
226
227// Subcommands provided by plugins:
228//   features      The command features is a plugin installed by the user
229
230// Other Commands:
231//   api-resources   Print the supported API resources on the server
232//   api-versions    Print the supported API versions on the server, in the form of "group/version"
233//   config          Modify kubeconfig files
234//   plugin          Provides utilities for interacting with plugins
235//   version         Print the client and server version information