use std::collections::HashMap;
use std::pin::Pin;
use std::time::Duration;
use async_trait::async_trait;
use tokio::io::AsyncRead;
use crate::error::Result;
use crate::types::{ResourceStats, WorkloadSpec, WorkloadStatus};
#[derive(Debug, Clone)]
pub struct WorkloadHandle {
pub runtime_id: String,
pub name: String,
pub metadata: HashMap<String, String>,
}
#[derive(Debug, Clone, Default)]
pub struct LogOpts {
pub follow: bool,
pub tail: Option<u64>,
pub since: Option<chrono::DateTime<chrono::Utc>>,
pub timestamps: bool,
}
#[derive(Debug)]
pub struct ExecResult {
pub exit_code: i32,
pub stdout: Vec<u8>,
pub stderr: Vec<u8>,
}
pub type LogStream = Pin<Box<dyn AsyncRead + Send>>;
#[async_trait]
pub trait Runtime: Send + Sync + 'static {
fn name(&self) -> &str;
async fn create(&self, spec: &WorkloadSpec) -> Result<WorkloadHandle>;
async fn start(&self, handle: &WorkloadHandle) -> Result<()>;
async fn stop(&self, handle: &WorkloadHandle, timeout: Duration) -> Result<()>;
async fn remove(&self, handle: &WorkloadHandle) -> Result<()>;
async fn status(&self, handle: &WorkloadHandle) -> Result<WorkloadStatus>;
async fn logs(&self, handle: &WorkloadHandle, opts: &LogOpts) -> Result<LogStream>;
async fn exec(&self, handle: &WorkloadHandle, cmd: &[String]) -> Result<ExecResult>;
async fn stats(&self, handle: &WorkloadHandle) -> Result<ResourceStats>;
async fn resolve_host_port(
&self,
handle: &WorkloadHandle,
_container_port: u16,
) -> Result<Option<u16>> {
Ok(handle
.metadata
.get("host_port")
.and_then(|p| p.parse().ok()))
}
async fn resolve_container_address(
&self,
_handle: &WorkloadHandle,
_container_port: u16,
_network: &str,
) -> Result<Option<String>> {
Ok(None)
}
}