unifying kubernetes and file provider configs

This commit is contained in:
Ara Sadoyan
2025-10-15 19:13:33 +02:00
parent 25693a7058
commit e87c60cf4f
4 changed files with 29 additions and 25 deletions

View File

@@ -1,4 +1,4 @@
// use crate::utils::dnsclient::DnsClientPool;
use crate::utils::parceyaml::build_headers;
use crate::utils::structs::{Configuration, InnerMap, ServiceMapping, UpstreamsDashMap};
use crate::utils::tools::{clone_dashmap_into, compare_dashmaps, print_upstreams};
use dashmap::DashMap;
@@ -14,9 +14,6 @@ use std::time::Duration;
use tokio::fs::File;
use tokio::io::AsyncReadExt;
// static KUBERNETES_SERVICE_HOST: &str = "IP_ADDRESS";
// static TOKEN: &str = "TOKEN";
#[derive(Debug, serde::Deserialize)]
struct Endpoints {
subsets: Option<Vec<Subset>>,
@@ -35,12 +32,10 @@ struct Address {
#[derive(Debug, serde::Deserialize)]
struct Port {
// name: String,
port: u16,
}
pub async fn start(mut toreturn: Sender<Configuration>, config: Arc<Configuration>) {
println!("{:?}", config);
let upstreams = UpstreamsDashMap::new();
let prev_upstreams = UpstreamsDashMap::new();
loop {
@@ -62,22 +57,21 @@ pub async fn start(mut toreturn: Sender<Configuration>, config: Arc<Configuratio
if let Some(svc) = kuber.services {
for i in svc {
let header_list = DashMap::new();
let mut hl = Vec::new();
if let Some(headers) = &i.headers {
for header in headers {
if let Some((key, val)) = header.split_once(':') {
hl.push((key.trim().to_string(), val.trim().to_string()));
build_headers(&i.headers, config.as_ref(), &mut hl);
if hl.len() > 0 {
header_list.insert(i.path.clone().unwrap_or("/".to_string()), hl);
config.headers.insert(i.hostname.clone(), header_list);
}
}
}
header_list.insert(path.clone(), hl);
let url = format!("https://{}/api/v1/namespaces/staging/endpoints/{}", server, i.hostname);
let list = get_by_http(&*url, &*token, &i).await;
if let Some(list) = list {
match upstreams.get(&i.upstream.clone()) {
Some(foo) => {
Some(upstr) => {
for (k, v) in list {
foo.value().insert(k, v);
upstr.value().insert(k, v);
}
}
None => {

View File

@@ -109,15 +109,8 @@ async fn populate_file_upstreams(config: &mut Configuration, parsed: &Config) {
}
let mut server_list = Vec::new();
let mut hl = Vec::new();
if let Some(headers) = &path_config.headers {
for header in headers {
if let Some((key, val)) = header.split_once(':') {
hl.push((key.trim().to_string(), val.trim().to_string()));
}
}
}
let mut hl: Vec<(String, String)> = Vec::new();
build_headers(&path_config.headers, config, &mut hl);
header_list.insert(path.clone(), hl);
for server in &path_config.servers {
@@ -224,3 +217,20 @@ fn log_builder(conf: &AppConfig) {
}
env_logger::builder().init();
}
pub fn build_headers(path_config: &Option<Vec<String>>, config: &Configuration, hl: &mut Vec<(String, String)>) {
if let Some(headers) = &path_config {
for header in headers {
if let Some((key, val)) = header.split_once(':') {
hl.push((key.trim().to_string(), val.trim().to_string()));
}
}
if let Some(push) = config.headers.get("GLOBAL_HEADERS") {
for k in push.iter() {
for x in k.value() {
hl.push(x.to_owned());
}
}
}
}
}

View File

@@ -52,7 +52,6 @@ impl GetHost for LB {
let host_entry = self.headers.get(peer)?;
let mut current_path = path.to_string();
let mut best_match: Option<Vec<(String, String)>> = None;
loop {
if let Some(entry) = host_entry.get(&current_path) {
if !entry.value().is_empty() {

View File

@@ -215,6 +215,7 @@ impl ProxyHttp for LB {
let path = session.req_header().uri.path();
let host_header = host;
let split_header = host_header.split_once(':');
match split_header {
Some(sh) => {
let yoyo = self.get_header(sh.0, path);