2024-03-24 05:25:48 +00:00
|
|
|
use std::{collections::HashMap, time::Duration};
|
2024-03-23 02:10:30 +00:00
|
|
|
|
2024-03-15 16:11:35 +00:00
|
|
|
use anyhow::{anyhow, Result};
|
2024-03-24 05:52:25 +00:00
|
|
|
use krata::v1::{
|
2024-03-15 16:11:35 +00:00
|
|
|
common::{
|
2024-03-23 02:10:30 +00:00
|
|
|
guest_image_spec::Image, Guest, GuestErrorInfo, GuestExitInfo, GuestNetworkState,
|
|
|
|
GuestState, GuestStatus,
|
2024-03-15 16:11:35 +00:00
|
|
|
},
|
|
|
|
control::GuestChangedEvent,
|
|
|
|
};
|
2024-03-27 02:54:39 +00:00
|
|
|
use kratart::{launch::GuestLaunchRequest, GuestInfo, Runtime};
|
2024-03-23 02:10:30 +00:00
|
|
|
use log::{error, info, trace, warn};
|
|
|
|
use tokio::{select, sync::mpsc::Receiver, task::JoinHandle, time::sleep};
|
2024-03-15 16:11:35 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
db::GuestStore,
|
|
|
|
event::{DaemonEvent, DaemonEventContext},
|
|
|
|
};
|
|
|
|
|
|
|
|
pub struct GuestReconciler {
|
|
|
|
guests: GuestStore,
|
|
|
|
events: DaemonEventContext,
|
|
|
|
runtime: Runtime,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GuestReconciler {
|
|
|
|
pub fn new(guests: GuestStore, events: DaemonEventContext, runtime: Runtime) -> Result<Self> {
|
|
|
|
Ok(Self {
|
|
|
|
guests,
|
|
|
|
events,
|
|
|
|
runtime,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn launch(self, mut notify: Receiver<Uuid>) -> Result<JoinHandle<()>> {
|
|
|
|
Ok(tokio::task::spawn(async move {
|
2024-03-23 02:10:30 +00:00
|
|
|
if let Err(error) = self.reconcile_runtime(true).await {
|
2024-03-15 16:11:35 +00:00
|
|
|
error!("runtime reconciler failed: {}", error);
|
|
|
|
}
|
|
|
|
|
|
|
|
loop {
|
2024-03-23 02:10:30 +00:00
|
|
|
select! {
|
|
|
|
x = notify.recv() => match x {
|
|
|
|
None => {
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
|
|
|
|
Some(uuid) => {
|
|
|
|
if let Err(error) = self.reconcile(uuid).await {
|
|
|
|
error!("failed to reconcile guest {}: {}", uuid, error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-03-23 07:00:12 +00:00
|
|
|
_ = sleep(Duration::from_secs(5)) => {
|
2024-03-23 02:10:30 +00:00
|
|
|
if let Err(error) = self.reconcile_runtime(false).await {
|
|
|
|
error!("runtime reconciler failed: {}", error);
|
|
|
|
}
|
|
|
|
}
|
2024-03-15 16:11:35 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2024-03-23 02:10:30 +00:00
|
|
|
pub async fn reconcile_runtime(&self, initial: bool) -> Result<()> {
|
|
|
|
trace!("reconciling runtime");
|
2024-03-15 16:11:35 +00:00
|
|
|
let runtime_guests = self.runtime.list().await?;
|
|
|
|
let stored_guests = self.guests.list().await?;
|
2024-03-30 09:29:03 +00:00
|
|
|
for (uuid, mut stored_guest) in stored_guests {
|
2024-03-23 02:10:30 +00:00
|
|
|
let previous_guest = stored_guest.clone();
|
2024-03-15 16:11:35 +00:00
|
|
|
let runtime_guest = runtime_guests.iter().find(|x| x.uuid == uuid);
|
|
|
|
match runtime_guest {
|
|
|
|
None => {
|
|
|
|
let mut state = stored_guest.state.as_mut().cloned().unwrap_or_default();
|
|
|
|
if state.status() == GuestStatus::Started {
|
2024-03-23 07:00:12 +00:00
|
|
|
state.status = GuestStatus::Starting.into();
|
2024-03-15 16:11:35 +00:00
|
|
|
}
|
|
|
|
stored_guest.state = Some(state);
|
|
|
|
}
|
|
|
|
|
2024-03-23 02:10:30 +00:00
|
|
|
Some(runtime) => {
|
2024-03-15 16:11:35 +00:00
|
|
|
let mut state = stored_guest.state.as_mut().cloned().unwrap_or_default();
|
2024-03-23 02:10:30 +00:00
|
|
|
if let Some(code) = runtime.state.exit_code {
|
|
|
|
state.status = GuestStatus::Exited.into();
|
|
|
|
state.exit_info = Some(GuestExitInfo { code });
|
|
|
|
} else {
|
|
|
|
state.status = GuestStatus::Started.into();
|
2024-03-15 16:11:35 +00:00
|
|
|
}
|
2024-03-27 02:54:39 +00:00
|
|
|
state.network = Some(guestinfo_to_networkstate(runtime));
|
2024-03-23 07:00:12 +00:00
|
|
|
stored_guest.state = Some(state);
|
2024-03-23 02:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-30 09:29:03 +00:00
|
|
|
let changed = stored_guest != previous_guest;
|
2024-03-23 02:10:30 +00:00
|
|
|
|
|
|
|
if changed || initial {
|
2024-03-30 09:29:03 +00:00
|
|
|
self.guests.update(uuid, stored_guest).await?;
|
2024-03-23 02:10:30 +00:00
|
|
|
if let Err(error) = self.reconcile(uuid).await {
|
|
|
|
error!("failed to reconcile guest {}: {}", uuid, error);
|
2024-03-15 16:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn reconcile(&self, uuid: Uuid) -> Result<()> {
|
2024-03-30 09:29:03 +00:00
|
|
|
let Some(mut guest) = self.guests.read(uuid).await? else {
|
2024-03-15 16:11:35 +00:00
|
|
|
warn!(
|
|
|
|
"notified of reconcile for guest {} but it didn't exist",
|
|
|
|
uuid
|
|
|
|
);
|
|
|
|
return Ok(());
|
|
|
|
};
|
|
|
|
|
|
|
|
info!("reconciling guest {}", uuid);
|
|
|
|
|
|
|
|
self.events
|
|
|
|
.send(DaemonEvent::GuestChanged(GuestChangedEvent {
|
|
|
|
guest: Some(guest.clone()),
|
|
|
|
}))?;
|
|
|
|
|
|
|
|
let result = match guest.state.as_ref().map(|x| x.status()).unwrap_or_default() {
|
2024-03-30 09:29:03 +00:00
|
|
|
GuestStatus::Starting => self.start(uuid, &mut guest).await,
|
|
|
|
GuestStatus::Destroying | GuestStatus::Exited => self.destroy(uuid, &mut guest).await,
|
2024-03-15 16:11:35 +00:00
|
|
|
_ => Ok(false),
|
|
|
|
};
|
|
|
|
|
|
|
|
let changed = match result {
|
|
|
|
Ok(changed) => changed,
|
|
|
|
Err(error) => {
|
|
|
|
guest.state = Some(guest.state.as_mut().cloned().unwrap_or_default());
|
2024-03-23 07:00:12 +00:00
|
|
|
guest.state.as_mut().unwrap().status = GuestStatus::Failed.into();
|
2024-03-15 16:11:35 +00:00
|
|
|
guest.state.as_mut().unwrap().error_info = Some(GuestErrorInfo {
|
|
|
|
message: error.to_string(),
|
|
|
|
});
|
2024-03-31 01:44:28 +00:00
|
|
|
warn!("failed to start guest {}: {}", guest.id, error);
|
2024-03-15 16:11:35 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
info!("reconciled guest {}", uuid);
|
|
|
|
|
2024-03-23 07:00:12 +00:00
|
|
|
let status = guest.state.as_ref().map(|x| x.status()).unwrap_or_default();
|
|
|
|
let destroyed = status == GuestStatus::Destroyed || status == GuestStatus::Failed;
|
2024-03-15 16:11:35 +00:00
|
|
|
|
|
|
|
if changed {
|
|
|
|
let event = DaemonEvent::GuestChanged(GuestChangedEvent {
|
|
|
|
guest: Some(guest.clone()),
|
|
|
|
});
|
|
|
|
|
|
|
|
if destroyed {
|
|
|
|
self.guests.remove(uuid).await?;
|
|
|
|
} else {
|
2024-03-30 09:29:03 +00:00
|
|
|
self.guests.update(uuid, guest.clone()).await?;
|
2024-03-15 16:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.events.send(event)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn start(&self, uuid: Uuid, guest: &mut Guest) -> Result<bool> {
|
|
|
|
let Some(ref spec) = guest.spec else {
|
|
|
|
return Err(anyhow!("guest spec not specified"));
|
|
|
|
};
|
|
|
|
|
|
|
|
let Some(ref image) = spec.image else {
|
|
|
|
return Err(anyhow!("image spec not provided"));
|
|
|
|
};
|
|
|
|
let oci = match image.image {
|
|
|
|
Some(Image::Oci(ref oci)) => oci,
|
|
|
|
None => {
|
|
|
|
return Err(anyhow!("oci spec not specified"));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-03-27 02:54:39 +00:00
|
|
|
let task = spec.task.as_ref().cloned().unwrap_or_default();
|
|
|
|
|
2024-03-15 16:11:35 +00:00
|
|
|
let info = self
|
|
|
|
.runtime
|
|
|
|
.launch(GuestLaunchRequest {
|
|
|
|
uuid: Some(uuid),
|
|
|
|
name: if spec.name.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(&spec.name)
|
|
|
|
},
|
|
|
|
image: &oci.image,
|
|
|
|
vcpus: spec.vcpus,
|
|
|
|
mem: spec.mem,
|
2024-03-27 02:54:39 +00:00
|
|
|
env: task
|
|
|
|
.environment
|
2024-03-24 05:25:48 +00:00
|
|
|
.iter()
|
|
|
|
.map(|x| (x.key.clone(), x.value.clone()))
|
|
|
|
.collect::<HashMap<_, _>>(),
|
2024-03-27 02:54:39 +00:00
|
|
|
run: empty_vec_optional(task.command.clone()),
|
2024-03-15 16:11:35 +00:00
|
|
|
debug: false,
|
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
info!("started guest {}", uuid);
|
|
|
|
guest.state = Some(GuestState {
|
|
|
|
status: GuestStatus::Started.into(),
|
2024-03-27 02:54:39 +00:00
|
|
|
network: Some(guestinfo_to_networkstate(&info)),
|
2024-03-15 16:11:35 +00:00
|
|
|
exit_info: None,
|
|
|
|
error_info: None,
|
2024-03-27 02:54:39 +00:00
|
|
|
domid: info.domid,
|
2024-03-15 16:11:35 +00:00
|
|
|
});
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn destroy(&self, uuid: Uuid, guest: &mut Guest) -> Result<bool> {
|
2024-03-22 23:59:02 +00:00
|
|
|
if let Err(error) = self.runtime.destroy(uuid).await {
|
2024-03-23 07:00:12 +00:00
|
|
|
trace!("failed to destroy runtime guest {}: {}", uuid, error);
|
2024-03-22 23:59:02 +00:00
|
|
|
}
|
|
|
|
|
2024-03-15 16:11:35 +00:00
|
|
|
info!("destroyed guest {}", uuid);
|
|
|
|
guest.state = Some(GuestState {
|
|
|
|
status: GuestStatus::Destroyed.into(),
|
2024-03-23 07:00:12 +00:00
|
|
|
network: None,
|
2024-03-15 16:11:35 +00:00
|
|
|
exit_info: None,
|
|
|
|
error_info: None,
|
2024-03-27 02:54:39 +00:00
|
|
|
domid: guest.state.as_ref().map(|x| x.domid).unwrap_or(u32::MAX),
|
2024-03-15 16:11:35 +00:00
|
|
|
});
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn empty_vec_optional<T>(value: Vec<T>) -> Option<Vec<T>> {
|
|
|
|
if value.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(value)
|
|
|
|
}
|
|
|
|
}
|
2024-03-27 02:54:39 +00:00
|
|
|
|
|
|
|
fn guestinfo_to_networkstate(info: &GuestInfo) -> GuestNetworkState {
|
|
|
|
GuestNetworkState {
|
|
|
|
guest_ipv4: info.guest_ipv4.map(|x| x.to_string()).unwrap_or_default(),
|
|
|
|
guest_ipv6: info.guest_ipv6.map(|x| x.to_string()).unwrap_or_default(),
|
|
|
|
guest_mac: info.guest_mac.as_ref().cloned().unwrap_or_default(),
|
|
|
|
gateway_ipv4: info.gateway_ipv4.map(|x| x.to_string()).unwrap_or_default(),
|
|
|
|
gateway_ipv6: info.gateway_ipv6.map(|x| x.to_string()).unwrap_or_default(),
|
|
|
|
gateway_mac: info.gateway_mac.as_ref().cloned().unwrap_or_default(),
|
|
|
|
}
|
|
|
|
}
|