Skip to content

Commit cdb786a

Browse files
committed
Add load_with function to load config with context, cluster, and user
1 parent 97f76bb commit cdb786a

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

src/config/kube_config.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,32 @@ pub struct KubeConfigLoader {
1616
}
1717

1818
impl KubeConfigLoader {
19-
pub fn load<P: AsRef<Path>>(path: P) -> Result<KubeConfigLoader, Error> {
19+
pub fn load<P: AsRef<Path>>(
20+
path: P,
21+
context: Option<String>,
22+
cluster: Option<String>,
23+
user: Option<String>,
24+
) -> Result<KubeConfigLoader, Error> {
2025
let config = Config::load_config(path)?;
26+
let context_name = context.as_ref().unwrap_or(&config.current_context);
2127
let current_context = config
2228
.contexts
2329
.iter()
24-
.find(|named_context| named_context.name == config.current_context)
30+
.find(|named_context| &named_context.name == context_name)
2531
.map(|named_context| &named_context.context)
2632
.ok_or(format_err!("Unable to load current context"))?;
33+
let cluster_name = cluster.as_ref().unwrap_or(&current_context.cluster);
2734
let cluster = config
2835
.clusters
2936
.iter()
30-
.find(|named_cluster| named_cluster.name == current_context.cluster)
37+
.find(|named_cluster| &named_cluster.name == cluster_name)
3138
.map(|named_cluster| &named_cluster.cluster)
3239
.ok_or(format_err!("Unable to load cluster of current context"))?;
40+
let user_name = user.as_ref().unwrap_or(&current_context.user);
3341
let user = config
3442
.auth_infos
3543
.iter()
36-
.find(|named_user| named_user.name == current_context.user)
44+
.find(|named_user| &named_user.name == user_name)
3745
.map(|named_user| {
3846
let mut user = named_user.auth_info.clone();
3947
match user.load_gcp() {

src/config/mod.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,33 @@ impl Configuration {
3535
/// .expect("failed to load kubeconfig");
3636
/// ```
3737
pub fn load_kube_config() -> Result<Configuration, Error> {
38+
load_kube_config_with(Default::default())
39+
}
40+
41+
/// ConfigOptions stores options used when loading kubeconfig file.
42+
#[derive(Default)]
43+
pub struct ConfigOptions {
44+
pub context: Option<String>,
45+
pub cluster: Option<String>,
46+
pub user: Option<String>,
47+
}
48+
49+
/// Returns a config includes authentication and cluster information from kubeconfig file.
50+
///
51+
/// # Example
52+
/// ```no_run
53+
/// use kubernetes::config;
54+
///
55+
/// let kubeconfig = config::load_kube_config()
56+
/// .expect("failed to load kubeconfig");
57+
/// ```
58+
pub fn load_kube_config_with(options: ConfigOptions) -> Result<Configuration, Error> {
3859
let kubeconfig = utils::kubeconfig_path()
3960
.or_else(utils::default_kube_path)
4061
.ok_or(format_err!("Unable to load kubeconfig"))?;
4162

42-
let loader = KubeConfigLoader::load(kubeconfig)?;
63+
let loader =
64+
KubeConfigLoader::load(kubeconfig, options.context, options.cluster, options.user)?;
4365
let token = match &loader.user.token {
4466
Some(token) => Some(token.clone()),
4567
None => {

0 commit comments

Comments
 (0)