rkubectl_kubeapi/
options.rs

1use clap::Args;
2
3use super::*;
4
5#[derive(Clone, Debug, Default, Args)]
6pub struct KubeapiOptions {
7    /// Default cache directory
8    #[arg(long, global = true)]
9    pub cache_dir: Option<PathBuf>,
10
11    /// Username to impersonate for the operation. User could be a regular user or a service account in a namespace.
12    #[arg(long = "as", global = true)]
13    pub as_user: Option<String>,
14
15    /// Group to impersonate for the operation, this flag can be repeated to specify multiple groups.
16    #[arg(long, global = true)]
17    pub as_group: Option<Vec<String>>,
18
19    /// UID to impersonate for the operation.
20    #[arg(long, global = true)]
21    pub as_uid: Option<String>,
22}
23
24impl KubeapiOptions {
25    pub fn cache_dir(&self) -> PathBuf {
26        self.cache_dir
27            .clone()
28            .or_else(|| etcetera::home_dir().ok())
29            .unwrap_or_default()
30            .join(".kube")
31            .join("cache")
32    }
33
34    pub fn discovery_cache_for_config(
35        &self,
36        config: &kube::Config,
37    ) -> Result<PathBuf, kube::config::KubeconfigError> {
38        config
39            .cluster_url
40            .host()
41            .map(|server| self.cache_dir().join("discovery").join(server))
42            .ok_or(kube::config::KubeconfigError::MissingClusterUrl)
43    }
44}
45
46/// This struct mirrors `KubeConfigOptions` from `kube::config` crate.
47/// It exists here to allow using the same struct in the CLI since
48/// `kube::config::KubeConfigOptions` does not derive `clap::Args`.
49#[derive(Clone, Debug, Default, Args)]
50pub struct KubeConfigOptions {
51    /// The name of the kubeconfig cluster to use
52    #[arg(long, global = true)]
53    pub cluster: Option<String>,
54
55    /// The name of the kubeconfig context to use
56    #[arg(long, global = true)]
57    pub context: Option<String>,
58
59    /// The name of the kubeconfig user to use
60    #[arg(long, global = true)]
61    pub user: Option<String>,
62}
63
64impl KubeConfigOptions {
65    pub fn kube_config_options(&self) -> kube::config::KubeConfigOptions {
66        kube::config::KubeConfigOptions {
67            context: self.context.clone(),
68            cluster: self.cluster.clone(),
69            user: self.user.clone(),
70        }
71    }
72}