Skip to content

Commit

Permalink
include in cached files
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramilito committed Feb 10, 2024
1 parent 522af07 commit 284b025
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 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,21 @@ fn build(ctx: &Contexts, ns: Option<&str>, strbuf: &str) -> KubeConfig {
name: ctx.name.to_string(),
}];

if let Some(u) = kube_config.users.iter().find(|x| x.name == ctx.context.user) {
let usr = u.clone();
config.users = vec![Users {
name: usr.name,
user: usr.user,
}];
}

if let Some(c) = kube_config.clusters.iter().find(|x| x.name == ctx.context.cluster) {
let cluster = c.clone();
config.clusters = vec![Clusters {
name: cluster.name,
cluster: cluster.cluster,
}];
}
config
}

Expand Down Expand Up @@ -59,7 +74,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 +84,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 +102,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 +114,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 +138,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

0 comments on commit 284b025

Please sign in to comment.