feat: basic foundations

This commit is contained in:
Tamipes 2025-11-25 16:18:38 +01:00
parent 5874d06016
commit e7d06a45f6
5 changed files with 112 additions and 125 deletions

41
src/KubeCache.rs Normal file
View file

@ -0,0 +1,41 @@
use k8s_openapi::api::apps::v1::Deployment;
use kube::{
api::{ListParams, ObjectList},
runtime::reflector::Lookup,
Api, Client, ResourceExt,
};
pub struct Cache {
deployments: Api<Deployment>,
}
impl Cache {
pub fn create() -> Option<Cache> {
let kubeconfig = kube::config::Kubeconfig::read().unwrap();
let client = Client::try_from(kubeconfig).unwrap();
let deployments: Api<Deployment> = Api::default_namespaced(client);
return Some(Cache { deployments });
}
pub async fn get_deploys(&self) -> ObjectList<Deployment> {
// let lp: ListParams = ListParams::default();
let lp: ListParams = ListParams::default().labels("tami.moe/minecraft");
self.deployments.list(&lp).await.unwrap()
}
pub async fn query_addr(&self, addr: String) -> Option<String> {
let deploys = self.get_deploys().await;
let result = deploys
.iter()
.find(|x| filter_label_value(x, addr.clone()))?;
Some(result.name()?.to_string())
}
}
fn filter_label_value(dep: &Deployment, str: String) -> bool {
dep.labels()
.values()
.filter(|x| x.as_str() == str.as_str())
.count()
> 0
}

View file

@ -1,37 +1,46 @@
//! This is a simple imitation of the basic functionality of kubectl:
//! kubectl {get, delete, apply, watch, edit} <resource> [name]
//! with labels and namespace selectors supported.
use anyhow::{bail, Context, Result};
use futures::{StreamExt, TryStreamExt};
use k8s_openapi::{
api::apps::v1::Deployment, apimachinery::pkg::apis::meta::v1::Time, chrono::Utc,
};
use kube::{
api::{Api, DynamicObject, ListParams, Patch, PatchParams, ResourceExt},
core::GroupVersionKind,
discovery::{ApiCapabilities, ApiResource, Discovery, Scope},
runtime::{
wait::{await_condition, conditions::is_deleted},
watcher, WatchStreamExt,
},
Client,
};
use tracing::*;
use std::sync::Arc;
use tokio::net::{TcpListener, TcpStream};
mod KubeCache;
mod packets;
mod types;
#[tokio::main]
async fn main() -> Result<()> {
async fn main() {
tracing_subscriber::fmt::init();
let cache = KubeCache::Cache::create().unwrap();
let arcCache = Arc::new(cache);
let kubeconfig = kube::config::Kubeconfig::read()?;
let client = Client::try_from(kubeconfig)?;
let mut listener = TcpListener::bind("0.0.0.0:25565").await.unwrap();
let deployments: Api<Deployment> = Api::default_namespaced(client);
loop {
let (socket, _) = listener.accept().await.unwrap();
let acc = arcCache.clone();
let lp: ListParams = ListParams::default();
println!("{:?}", deployments.list(&lp).await?);
Ok(())
tokio::spawn(async move {
if let Err(e) = process_socket(socket, acc).await {
eprintln!("ERR: {e:?}");
}
});
}
}
async fn process_socket(stream: TcpStream, cache: Arc<KubeCache::Cache>) -> Result<(), ()> {
todo!()
}
// ----- Debug tools -----
// let mc_deployments = cache.get_deploys().await;
// for dep in mc_deployments.iter() {
// println!("{:?}", dep.labels().values())
// }
// // println!("{:?}", mc_deployments);
// println!("count: {}", mc_deployments.iter().count());
// println!(
// "{:?}",
// cache.query_addr("ferret.tami.moe".to_string()).await
// );

View file

@ -1,8 +1,9 @@
use mc_proxy::{types::*, ProtocolState};
use std::{
io::{self, Read, Write},
net::TcpStream,
};
use crate::types::VarInt;
pub mod clientbound;
pub mod serverbound;
@ -103,18 +104,18 @@ impl Packet {
all.append(&mut vec);
return Some(all);
}
pub fn proto_name(&self, state: &ProtocolState) -> String {
match state {
ProtocolState::Handshaking => match self.id.get_int() {
0 => "Handshake".to_owned(),
_ => "error".to_owned(),
},
ProtocolState::Status => match self.id.get_int() {
0 => "StatusRequest".to_owned(),
1 => "PingRequest".to_owned(),
_ => "error".to_owned(),
},
_ => "Dont care state".to_owned(),
}
}
// pub fn proto_name(&self, state: &Type) -> String {
// match state {
// ProtocolState::Handshaking => match self.id.get_int() {
// 0 => "Handshake".to_owned(),
// _ => "error".to_owned(),
// },
// ProtocolState::Status => match self.id.get_int() {
// 0 => "StatusRequest".to_owned(),
// 1 => "PingRequest".to_owned(),
// _ => "error".to_owned(),
// },
// _ => "Dont care state".to_owned(),
// }
// }
}