Skip to content

Commit

Permalink
feat: allow configuring k8s context with env var (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
MicaiahReid authored Sep 25, 2023
1 parent cb57c32 commit f7e6bc3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
53 changes: 46 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ use k8s_openapi::{
};
use kube::{
api::{Api, DeleteParams, PostParams},
Client,
config::KubeConfigOptions,
Client, Config,
};
use resources::{
pvc::StacksDevnetPvc,
service::{get_service_port, ServicePort},
StacksDevnetResource,
};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::thread::sleep;
use std::{collections::BTreeMap, str::FromStr, time::Duration};
use std::{env, thread::sleep};
use strum::IntoEnumIterator;
use tower::BoxError;

Expand Down Expand Up @@ -99,17 +100,55 @@ pub struct StacksDevnetApiK8sManager {
}

impl StacksDevnetApiK8sManager {
pub async fn default(ctx: &Context) -> StacksDevnetApiK8sManager {
let client = Client::try_default()
.await
.expect("could not create kube client");
pub async fn new(ctx: &Context) -> StacksDevnetApiK8sManager {
let context = match env::var("KUBE_CONTEXT") {
Ok(context) => Some(context),
Err(_) => {
if cfg!(test) {
let is_ci = match env::var("GITHUB_ACTIONS") {
Ok(is_ci) => is_ci == format!("true"),
Err(_) => false,
};
if is_ci {
None
} else {
// ensures that if no context is supplied and we're running
// tests locally, we deploy to the local kind cluster
Some(format!("kind-kind"))
}
} else {
None
}
}
};
let client = match context {
Some(context) => {
let kube_config = KubeConfigOptions {
context: Some(context.clone()),
cluster: Some(context),
user: None,
};
let client_config =
Config::from_kubeconfig(&kube_config)
.await
.unwrap_or_else(|e| {
panic!("could not create kube client config: {}", e.to_string())
});
Client::try_from(client_config)
.unwrap_or_else(|e| panic!("could not create kube client: {}", e.to_string()))
}
None => Client::try_default()
.await
.expect("could not create kube client"),
};

StacksDevnetApiK8sManager {
client,
ctx: ctx.to_owned(),
}
}

pub async fn new<S, B, T>(
pub async fn from_service<S, B, T>(
service: S,
default_namespace: T,
ctx: &Context,
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async fn main() {
logger: Some(logger),
tracer: false,
};
let k8s_manager = StacksDevnetApiK8sManager::default(&ctx).await;
let k8s_manager = StacksDevnetApiK8sManager::new(&ctx).await;
let config_path = match env::var("CONFIG_PATH") {
Ok(path) => path,
Err(_) => {
Expand Down
4 changes: 2 additions & 2 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async fn get_k8s_manager() -> (StacksDevnetApiK8sManager, Context) {
let logger = hiro_system_kit::log::setup_logger();
let _guard = hiro_system_kit::log::setup_global_logger(logger.clone());
let ctx = Context::empty();
let k8s_manager = StacksDevnetApiK8sManager::default(&ctx).await;
let k8s_manager = StacksDevnetApiK8sManager::new(&ctx).await;
(k8s_manager, ctx)
}

Expand Down Expand Up @@ -269,7 +269,7 @@ async fn get_mock_k8s_manager() -> (StacksDevnetApiK8sManager, Context) {
let logger = hiro_system_kit::log::setup_logger();
let _guard = hiro_system_kit::log::setup_global_logger(logger.clone());
let ctx = Context::empty();
let k8s_manager = StacksDevnetApiK8sManager::new(mock_service, "default", &ctx).await;
let k8s_manager = StacksDevnetApiK8sManager::from_service(mock_service, "default", &ctx).await;
(k8s_manager, ctx)
}

Expand Down

0 comments on commit f7e6bc3

Please sign in to comment.