1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
pub use kube_client::*;
use std::path::PathBuf;
use thiserror::Error;
#[derive(Clone, Debug, Default)]
#[cfg_attr(docsrs, doc(cfg(feature = "client")))]
#[cfg_attr(feature = "clap", derive(clap::Parser))]
pub struct ClientArgs {
#[cfg_attr(feature = "clap", clap(long))]
pub cluster: Option<String>,
#[cfg_attr(feature = "clap", clap(long))]
pub context: Option<String>,
#[cfg_attr(feature = "clap", clap(long))]
pub user: Option<String>,
#[cfg_attr(feature = "clap", clap(long))]
pub kubeconfig: Option<PathBuf>,
#[cfg_attr(feature = "clap", clap(long = "as"))]
pub impersonate_user: Option<String>,
#[cfg_attr(feature = "clap", clap(long = "as-group"))]
pub impersonate_group: Option<String>,
}
#[derive(Debug, Error)]
#[cfg_attr(docsrs, doc(cfg(feature = "client")))]
pub enum ConfigError {
#[error(transparent)]
Kubeconfig(#[from] config::KubeconfigError),
#[error(transparent)]
InCluster(#[from] config::InClusterError),
#[error(transparent)]
Client(#[from] Error),
}
impl ClientArgs {
pub async fn try_client(self) -> Result<Client, ConfigError> {
let client = match self.load_local_config().await {
Ok(client) => client,
Err(e) if self.is_customized() => return Err(e),
Err(_) => Config::from_cluster_env()?,
};
client.try_into().map_err(Into::into)
}
fn is_customized(&self) -> bool {
self.context.is_some()
|| self.cluster.is_some()
|| self.user.is_some()
|| self.impersonate_user.is_some()
|| self.impersonate_group.is_some()
|| self.kubeconfig.is_some()
}
async fn load_local_config(&self) -> Result<Config, ConfigError> {
let options = config::KubeConfigOptions {
context: self.context.clone(),
cluster: self.cluster.clone(),
user: self.user.clone(),
};
let mut kubeconfig = match &self.kubeconfig {
Some(path) => config::Kubeconfig::read_from(path.as_path())?,
None => config::Kubeconfig::read()?,
};
if let Some(user) = &self.impersonate_user {
for auth in kubeconfig.auth_infos.iter_mut() {
auth.auth_info.impersonate = Some(user.clone());
}
}
if let Some(group) = &self.impersonate_group {
for mut auth in kubeconfig.auth_infos.iter_mut() {
auth.auth_info.impersonate_groups = Some(vec![group.clone()]);
}
}
Config::from_custom_kubeconfig(kubeconfig, &options)
.await
.map_err(Into::into)
}
}