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

feat: allow configuring k8s context with env var #71

Merged
merged 6 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ jobs:
run: |
cargo install cargo-tarpaulin
cargo --version
cargo tarpaulin --out lcov --features k8s_tests
KUBE_CONTEXT="k3d-kube" cargo tarpaulin --out lcov --features k8s_tests

- name: Upload to codecov.io
uses: codecov/codecov-action@v3
Expand Down
42 changes: 35 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,44 @@ 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 no context is supplied and we're running a test,
// specify a local context so we don't deploy a bunch of
// test assets
if cfg!(test) {
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
.expect("could not create kube client config");
Client::try_from(client_config).expect("could not create kube client")
}
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
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::{convert::Infallible, net::SocketAddr};
#[tokio::main]
async fn main() {
const HOST: &str = "0.0.0.0";
const PORT: &str = "8477";
const PORT: &str = "8478";
MicaiahReid marked this conversation as resolved.
Show resolved Hide resolved
let endpoint: String = HOST.to_owned() + ":" + PORT;
let addr: SocketAddr = endpoint.parse().expect("Could not parse ip:port.");

Expand All @@ -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
Loading