Skip to content

Commit

Permalink
wip: config remake
Browse files Browse the repository at this point in the history
  • Loading branch information
ElaBosak233 committed Jan 25, 2025
1 parent 3d6b083 commit f35f42b
Show file tree
Hide file tree
Showing 52 changed files with 167 additions and 429 deletions.
5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ resolver = "2"
cds-assets = { path = "crates/assets" }
cds-media = { path = "crates/media" }
cds-db = { path = "crates/db" }
cds-env = { path = "crates/env" }
cds-web = { path = "crates/web" }
cds-config = { path = "crates/config" }
cds-web = { path = "crates/web" }
cds-queue = { path = "crates/queue" }
cds-metric = { path = "crates/metric" }
cds-cache = { path = "crates/cache" }
Expand All @@ -20,7 +19,6 @@ async-trait = { version = "0.1" }
tokio = { version = "1.43", features = ["full"] }
tokio-util = { version = "0.7.13" }
futures = { version = "^0.3" }
futures-util = { version = "^0.3" }
tower = { version = "0.5" }
tower-http = { version = "0.6", features = ["cors", "fs", "trace"] }

Expand All @@ -36,7 +34,6 @@ axum = { version = "0.8", features = [
] }
rust-embed = { version = "8.5" }
mime = { version = "0.3" }
mime_guess = { version = "2.0" }
wsrx = { version = "0.3", features = ["server"] }
cookie = { version = "0.18.1" }
tower_governor = { version = "0.6" }
Expand Down
Binary file removed arts/favicon.webp
Binary file not shown.
10 changes: 0 additions & 10 deletions assets/banner.txt

This file was deleted.

10 changes: 10 additions & 0 deletions crates/assets/embed/banner.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
____ _ ____ _____ _____
/ ___|__| |___ / ___|_ _| ___|
| | / _` / __| | | | | |_
| |__| (_| \__ \ |___ | | | _|
\____\__,_|___/\____| |_| |_|
Version {{version}}

Commit: {{git_commit}}
Build At: {{build_at}}
GitHub: https://github.com/elabosak233/cdsctf
18 changes: 18 additions & 0 deletions crates/assets/embed/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions crates/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use std::fs;
use rust_embed::Embed;

#[derive(Embed)]
#[folder = "../assets/"]
pub struct Assets;
#[folder = "./embed/"]
pub struct Embeds;

pub fn get(path: &str) -> Option<Vec<u8>> {
if let Ok(file) = fs::read(format!("assets/{}", path)) {
return Some(file);
}
Assets::get(path).map(|e| e.data.into_owned())
Embeds::get(path).map(|e| e.data.into_owned())
}
2 changes: 1 addition & 1 deletion crates/cache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ publish = false
resolver = "2"

[dependencies]
cds-env = {workspace = true}
cds-config = {workspace = true}

fred = { workspace = true }
once_cell = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/cache/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub async fn flush() -> Result<(), CacheError> {
}

pub async fn init() {
let config = Config::from_url(&cds_env::get_env().cache.url).unwrap();
let config = Config::from_url(&cds_config::get_config().cache.url).unwrap();
let client = Client::new(config, None, None, None);
client.init().await.unwrap();

Expand Down
1 change: 0 additions & 1 deletion crates/cluster/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ publish = false
resolver = "2"

[dependencies]
cds-env = { workspace = true }
cds-config = { workspace = true }
cds-db = { workspace = true }

Expand Down
39 changes: 21 additions & 18 deletions crates/cluster/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
pub mod traits;

use std::{collections::BTreeMap, process};
use std::path::Path;
use anyhow::anyhow;
use std::{collections::BTreeMap, path::Path, process};

use axum::extract::ws::WebSocket;
use k8s_openapi::{
api::core::v1::{
Expand All @@ -14,9 +13,9 @@ use k8s_openapi::{
use kube::{
Client as K8sClient, Config,
api::{Api, DeleteParams, ListParams, PostParams},
config::Kubeconfig,
runtime::wait::conditions,
};
use kube::config::Kubeconfig;
use once_cell::sync::OnceCell;
use tokio_util::codec::Framed;
use tracing::{error, info};
Expand All @@ -30,9 +29,13 @@ pub fn get_k8s_client() -> K8sClient {
}

pub async fn init() -> Result<(), ClusterError> {
let result = Config::from_custom_kubeconfig(Kubeconfig::read_from(
Path::new(cds_env::get_env().cluster.kube_config_path.as_str())
)?, &Default::default()).await;
let result = Config::from_custom_kubeconfig(
Kubeconfig::read_from(Path::new(
cds_config::get_config().cluster.kube_config_path.as_str(),
))?,
&Default::default(),
)
.await;
if let Err(e) = result {
error!(
"Failed to create Kubernetes client from custom config: {:?}",
Expand All @@ -52,11 +55,11 @@ pub async fn init() -> Result<(), ClusterError> {
let namespace_api: Api<Namespace> = Api::all(get_k8s_client().clone());
let namespaces = namespace_api.list(&ListParams::default()).await?;
if !namespaces.items.iter().any(|namespace| {
namespace.metadata.name == Some(cds_env::get_env().clone().cluster.namespace)
namespace.metadata.name == Some(cds_config::get_config().clone().cluster.namespace)
}) {
let namespace = Namespace {
metadata: ObjectMeta {
name: Some(cds_env::get_env().clone().cluster.namespace),
name: Some(cds_config::get_config().clone().cluster.namespace),
..Default::default()
},
..Default::default()
Expand Down Expand Up @@ -85,7 +88,7 @@ pub async fn create(

let pod_api: Api<Pod> = Api::namespaced(
get_k8s_client(),
cds_env::get_env().cluster.namespace.as_str(),
cds_config::get_config().cluster.namespace.as_str(),
);

let mut env_vars: Vec<EnvVar> = env
Expand Down Expand Up @@ -157,21 +160,21 @@ pub async fn create(

let mut nats: Vec<cds_db::entity::pod::Nat> = Vec::new();

match cds_env::get_env().cluster.proxy.is_enabled {
match cds_config::get_config().cluster.proxy.is_enabled {
true => {
for port in env.ports {
nats.push(cds_db::entity::pod::Nat {
src: format!("{}", port),
dst: None,
proxy: cds_env::get_env().cluster.proxy.is_enabled,
proxy: cds_config::get_config().cluster.proxy.is_enabled,
entry: None,
});
}
}
false => {
let service_api: Api<Service> = Api::namespaced(
get_k8s_client(),
cds_env::get_env().cluster.namespace.as_str(),
cds_config::get_config().cluster.namespace.as_str(),
);
let service_ports: Vec<ServicePort> = env
.ports
Expand Down Expand Up @@ -208,10 +211,10 @@ pub async fn create(
nats.push(cds_db::entity::pod::Nat {
src: format!("{}", port.port),
dst: Some(format!("{}", node_port)),
proxy: cds_env::get_env().cluster.proxy.is_enabled,
proxy: cds_config::get_config().cluster.proxy.is_enabled,
entry: Some(format!(
"{}:{}",
cds_config::get_config().await.cluster.entry,
cds_config::get_config().cluster.entry_host,
node_port
)),
});
Expand All @@ -228,20 +231,20 @@ pub async fn create(
pub async fn delete(name: String) {
let pod_api: Api<Pod> = Api::namespaced(
get_k8s_client(),
cds_env::get_env().cluster.namespace.as_str(),
cds_config::get_config().cluster.namespace.as_str(),
);
let _ = pod_api.delete(&name, &DeleteParams::default()).await;
let service_api: Api<Service> = Api::namespaced(
get_k8s_client(),
cds_env::get_env().cluster.namespace.as_str(),
cds_config::get_config().cluster.namespace.as_str(),
);
let _ = service_api.delete(&name, &DeleteParams::default()).await;
}

pub async fn wsrx(name: String, port: u16, ws: WebSocket) -> Result<(), ClusterError> {
let pod_api: Api<Pod> = Api::namespaced(
get_k8s_client(),
cds_env::get_env().cluster.namespace.as_str(),
cds_config::get_config().cluster.namespace.as_str(),
);
let mut pf = pod_api.portforward(&name, &[port]).await?;
let pfw = pf.take_stream(port);
Expand Down
12 changes: 7 additions & 5 deletions crates/config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ publish = false
resolver = "2"

[dependencies]
cds-db = { workspace = true }
cds-cache = { workspace = true }

sea-orm = { workspace = true }
once_cell = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
toml = { workspace = true }

[lib]
path = "src/lib.rs"
8 changes: 3 additions & 5 deletions crates/config/src/auth/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
pub mod registration;

use sea_orm::FromJsonQueryResult;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize, FromJsonQueryResult, PartialEq, Eq, Default)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Config {
pub registration: registration::Config,
pub secret: String,
pub expiration: i64,
}
7 changes: 0 additions & 7 deletions crates/config/src/auth/registration/email.rs

This file was deleted.

10 changes: 0 additions & 10 deletions crates/config/src/auth/registration/mod.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Env {
pub struct Config {
pub url: String,
}
12 changes: 6 additions & 6 deletions crates/config/src/cluster/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
pub mod proxy;

pub mod strategy;

use sea_orm::FromJsonQueryResult;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize, FromJsonQueryResult, PartialEq, Eq, Default)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Config {
pub entry: String,
pub strategy: strategy::Config,
pub namespace: String,
pub kube_config_path: String,
pub proxy: proxy::Config,
pub entry_host: String,
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Env {
pub struct Config {
pub is_enabled: bool,
pub traffic_capture: bool,
}
Loading

0 comments on commit f35f42b

Please sign in to comment.