diff --git a/src/commands.rs b/src/commands.rs index dd34a64..3741923 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -89,12 +89,12 @@ pub fn selectable_list(input: Vec) -> String { pub fn set_namespace(ctx: &str, selection: &str, temp_dir: &str, config: &KubeConfig) { let choice = config.contexts.iter().find(|x| x.name == ctx); - config::write(choice.unwrap(), Some(selection), temp_dir) + config::write(choice.unwrap(), config, Some(selection), temp_dir) } pub fn set_context(ctx: &str, temp_dir: &str, config: &KubeConfig) -> Result<(), SetContextError> { if let Some(choice) = config.contexts.iter().find(|x| x.name == ctx) { - config::write(choice, None, temp_dir); + config::write(choice, config, None, temp_dir); Ok(()) } else { Err(SetContextError::ContextNotFound{ctx: ctx.to_owned()}) diff --git a/src/config.rs b/src/config.rs index 6285b40..8ebd57f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,10 +1,10 @@ -use crate::model::{KubeConfig, Context, Contexts}; +use crate::model::{Clusters, Context, Contexts, KubeConfig, Users}; use crate::{KUBECONFIG, KUBESESSCONFIG}; use std::fs::{self, File}; use std::io::{BufReader, BufWriter, Read}; use std::path::Path; -fn build(ctx: &Contexts, ns: Option<&str>, strbuf: &str) -> KubeConfig { +fn build(ctx: &Contexts, kube_config: &KubeConfig, ns: Option<&str>, strbuf: &str) -> KubeConfig { let mut config: KubeConfig = serde_yaml::from_str(strbuf).unwrap(); config.api_version = "v1".to_string(); config.kind = "Config".to_string(); @@ -32,6 +32,20 @@ fn build(ctx: &Contexts, ns: Option<&str>, strbuf: &str) -> KubeConfig { name: ctx.name.to_string(), }]; + if let Some(user) = kube_config.users.iter().find(|x| x.name == ctx.context.user) { + config.users = vec![Users { + name: user.name.clone(), + user: user.user.clone(), + }]; + } + + if let Some(cluster) = kube_config.clusters.iter().find(|x| x.name == ctx.context.cluster) { + config.clusters = vec![Clusters { + name: cluster.name.clone(), + cluster: cluster.cluster.clone(), + }]; + } + config } @@ -59,7 +73,7 @@ fn get_path(ctx: &Contexts, dest: &str) -> String { path.display().to_string() } -pub fn write(ctx: &Contexts, namespace: Option<&str>, dest: &str) { +pub fn write(ctx: &Contexts, config: &KubeConfig, namespace: Option<&str>, dest: &str) { let path = get_path(ctx, dest); let strbuf = match fs::read_to_string(&path) { @@ -69,7 +83,7 @@ pub fn write(ctx: &Contexts, namespace: Option<&str>, dest: &str) { let options = get_file(&path); let writer = BufWriter::new(&options); - let config = build(ctx, namespace, &strbuf); + let config = build(ctx, config, namespace, &strbuf); serde_yaml::to_writer(writer, &config).unwrap(); } @@ -87,6 +101,8 @@ pub fn get() -> KubeConfig { configs.api_version = config.api_version; configs.kind = config.kind; configs.contexts.extend(config.contexts); + configs.users.extend(config.users); + configs.clusters.extend(config.clusters); } let dir = format!("{}/.kube", dirs::home_dir().unwrap().display()); @@ -97,6 +113,8 @@ pub fn get() -> KubeConfig { let config: KubeConfig = get_config(path.to_str().unwrap()); configs.contexts.extend(config.contexts); + configs.users.extend(config.users); + configs.clusters.extend(config.clusters); } } } @@ -119,7 +137,7 @@ fn get_config(path: &str) -> KubeConfig { } pub fn get_current_session() -> KubeConfig { - let current= if KUBESESSCONFIG.is_empty() { + let current = if KUBESESSCONFIG.is_empty() { KUBECONFIG.split(':').next().unwrap() } else { KUBESESSCONFIG.as_str() diff --git a/src/model.rs b/src/model.rs index ed75048..0d4ef3c 100644 --- a/src/model.rs +++ b/src/model.rs @@ -1,4 +1,7 @@ +use std::collections::HashMap; + use serde::{Deserialize, Serialize}; +use serde_yaml::Value; #[derive(Default, PartialEq, Debug, Serialize, Deserialize)] pub struct Contexts { @@ -8,6 +11,38 @@ pub struct Contexts { pub name: String, } +#[derive(Default, Clone, PartialEq, Debug, Serialize, Deserialize)] +pub struct Clusters { + #[serde(default)] + pub cluster: Cluster, + #[serde(default)] + pub name: String, +} +#[derive(Default, Clone, PartialEq, Debug, Serialize, Deserialize)] +pub struct Cluster { + #[serde(rename = "certificate-authority-data")] + pub certificate_authority_data: String, + pub server: String, +} + +#[derive(Default, Clone, PartialEq, Debug, Serialize, Deserialize)] +pub struct Users { + #[serde(default)] + pub user: HashMap, + #[serde(default)] + pub name: String, +} + +#[derive(Default, PartialEq, Debug, Serialize, Deserialize)] +pub struct User { + #[serde(skip_serializing_if = "String::is_empty", default)] + #[serde(rename = "client-certificate-data")] + pub client_certificate_data: String, + #[serde(skip_serializing_if = "String::is_empty", default)] + #[serde(rename = "client-key-data")] + pub client_key_data: String, +} + #[derive(Default, PartialEq, Debug, Serialize, Deserialize)] pub struct Context { #[serde(skip_serializing_if = "String::is_empty", default)] @@ -28,4 +63,8 @@ pub struct KubeConfig { pub current_context: String, #[serde(default)] pub contexts: Vec, + #[serde(default)] + pub users: Vec, + #[serde(default)] + pub clusters: Vec, }