Created
October 1, 2023 02:10
-
-
Save STashakkori/0da279eeee46b106162634575b9d6810 to your computer and use it in GitHub Desktop.
Kube Rust for slm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use kube::{ | |
api::{Api, ListParams, ResourceExt}, | |
Client, Config, error::Error, | |
}; | |
use tokio::sync::watch; | |
struct SalvumPod { | |
} | |
pub struct SalvumController { | |
client: Client, | |
namespace: String, | |
} | |
impl SalvumController { | |
pub async fn new(namespace: &str) -> Result<Self, Error> { | |
let config = Config::infer().await?; | |
let client = Client::new(config); | |
Ok(SalvumController { | |
client, | |
namespace: namespace.to_string(), | |
}) | |
} | |
pub async fn run(&self, stop_signal: watch::Receiver<bool>) -> Result<(), Error> { | |
let pods_api: Api<MyPod> = Api::namespaced(self.client.clone(), &self.namespace); | |
let lp = ListParams::default(); | |
let mut stream = pods_api.watch(&lp, "0").await?.boxed(); | |
while !*stop_signal.borrow() { | |
match self.handle_events(&mut stream).await { | |
Ok(_) => (), | |
Err(err) => { | |
eprintln!("Error handling events: {:?}", err); | |
} | |
} | |
} | |
Ok(()) | |
} | |
async fn handle_events(&self, stream: &mut kube::watcher::EventStream<SalvumPod>) -> Result<(), Error> { | |
while let Some(event) = stream.try_next().await? { | |
match event { | |
kube::watcher::Event::Applied(pod) => { | |
self.handle_added_pod(&pod).await?; | |
} | |
kube::watcher::Event::Modified(pod) => { | |
self.handle_modified_pod(&pod).await?; | |
} | |
kube::watcher::Event::Deleted(pod) => { | |
self.handle_deleted_pod(&pod).await?; | |
} | |
} | |
} | |
Ok(()) | |
} | |
async fn handle_added_pod(&self, pod: &SalvumPod) -> Result<(), Error> { | |
if let Some(pod_name) = &pod.metadata.name { | |
println!("Added Pod: {}", pod_name); | |
} else { | |
println!("Added Pod with an unknown name"); | |
} | |
Ok(()) | |
} | |
async fn handle_modified_pod(&self, pod: &SalvumPod) -> Result<(), Error> { | |
if let Some(annotations) = &pod.metadata.annotations { | |
if let Some(value) = annotations.get("custom-annotation") { | |
println!("Custom annotation modified: {}", value); | |
} | |
} | |
Ok(()) | |
} | |
async fn handle_deleted_pod(&self, pod: &SalvumPod) -> Result<(), Error> { | |
if let Some(pod_name) = &pod.metadata.name { | |
println!("Deleted Pod: {}", pod_name); | |
} | |
Ok(()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment