Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] - Kubeconfig merge needs user and cluster #59

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ pub fn selectable_list(input: Vec<String>) -> 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()})
Expand Down
28 changes: 23 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}
Expand All @@ -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());
Expand All @@ -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);
}
}
}
Expand All @@ -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()
Expand Down
39 changes: 39 additions & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<String, Value>,
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using hashmap here because we aren't interested in the structure and they can vary from providers (AWS,k3s,GKE)

#[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)]
Expand All @@ -28,4 +63,8 @@ pub struct KubeConfig {
pub current_context: String,
#[serde(default)]
pub contexts: Vec<Contexts>,
#[serde(default)]
pub users: Vec<Users>,
#[serde(default)]
pub clusters: Vec<Clusters>,
}
Loading