Skip to content

Commit

Permalink
fix conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
MaloPolese committed Mar 21, 2023
1 parent 50921db commit da25de0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 34 deletions.
15 changes: 6 additions & 9 deletions riklet/src/runtime/function_runtime.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
cli::{config::Configuration, function_config::FnConfiguration},
runtime::{RuntimeError, RuntimeManagerError},
runtime::{network::RuntimeNetwork, RuntimeError, RuntimeManagerError},
structs::WorkloadDefinition,
};
use async_trait::async_trait;
Expand All @@ -20,10 +20,7 @@ use std::{
};
use tracing::{event, Level};

use super::{
network::{FunctionRuntimeNetwork, RuntimeNetwork},
Runtime, RuntimeManager,
};
use super::{network::FunctionRuntimeNetwork, Runtime, RuntimeManager};

#[derive(Debug)]
struct FunctionRuntime {
Expand All @@ -36,16 +33,16 @@ struct FunctionRuntime {
#[async_trait]
impl Runtime for FunctionRuntime {
async fn run(&mut self) -> super::RuntimeResult<()> {
self.network.init().map_err(RuntimeError::Network)?;
self.network.init().await.map_err(RuntimeError::Network)?;

event!(Level::INFO, "Function workload detected");

event!(Level::INFO, "Define network");

let firecracker = Firecracker::new(Some(firepilot::FirecrackerOptions {
let firecracker = Firecracker::new(firepilot::FirecrackerOptions {
command: Some(self.function_config.firecracker_location.clone()),
..Default::default()
}))
})
.map_err(RuntimeError::Firecracker)?;

event!(Level::DEBUG, "Creating a new MicroVM");
Expand Down Expand Up @@ -192,7 +189,7 @@ impl RuntimeManager for FunctionRuntimeManager {
Ok(Box::new(FunctionRuntime {
function_config: FnConfiguration::load(),
file_path: self.create_fs(&workload_definition)?,
network: FunctionRuntimeNetwork::new(&workload_definition)
network: FunctionRuntimeNetwork::new(&workload)
.map_err(RuntimeManagerError::Network)?,
workload_definition,
}))
Expand Down
53 changes: 29 additions & 24 deletions riklet/src/runtime/network/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::{net::Ipv4Addr, process::Command};

use async_trait::async_trait;
use proto::worker::InstanceScheduling;
use std::fmt::Debug;
use thiserror::Error;
use tracing::{event, Level};
use tracing::{debug, event, Level};

#[derive(Debug, Error)]
pub enum NetworkError {
Expand All @@ -14,21 +16,26 @@ pub enum NetworkError {

#[error("Iptables error: {0}")]
Iptables(IptablesError),

#[error("Json error: {0}")]
JsonError(serde_json::Error),
}

type Result<T> = std::result::Result<T, NetworkError>;

use crate::network::net::{Net, NetworkInterfaceConfig};
use crate::{
cli::function_config::FnConfiguration,
iptables::{rule::Rule, Chain, Iptables, IptablesError, MutateIptables, Table},
structs::WorkloadDefinition,
IP_ALLOCATOR,
};

#[async_trait]
pub trait RuntimeNetwork: Send + Sync + Debug {
fn init(&self) -> Result<()>;
async fn init(&self) -> Result<()>;

fn destroy(&self) -> Result<()>;
async fn destroy(&self) -> Result<()>;
}

#[derive(Debug, Clone)]
Expand All @@ -39,12 +46,15 @@ pub struct FunctionRuntimeNetwork {
pub function_config: FnConfiguration,
pub default_agent_port: u16,
pub workload_definition: WorkloadDefinition,
pub workload: InstanceScheduling,
}

impl FunctionRuntimeNetwork {
pub fn new(workload_definition: &WorkloadDefinition) -> Result<Self> {
pub fn new(workload: &InstanceScheduling) -> Result<Self> {
let default_agent_port: u16 = 8080;
let mask_long: &str = "255.255.255.252";
let workload_definition: WorkloadDefinition =
serde_json::from_str(workload.definition.as_str()).map_err(NetworkError::JsonError)?;

// Alocate ip range for tap interface and firecracker micro VM
let subnet = IP_ALLOCATOR
Expand All @@ -70,31 +80,25 @@ impl FunctionRuntimeNetwork {
function_config: FnConfiguration::load(),
tap_ip,
default_agent_port,
workload: workload.clone(),
workload_definition: workload_definition.clone(),
})
}
}

#[async_trait]
impl RuntimeNetwork for FunctionRuntimeNetwork {
fn init(&self) -> Result<()> {
async fn init(&self) -> Result<()> {
println!("Function network initialized");

// Get port to expose function

let output = Command::new("/bin/sh")
.arg(self.function_config.script_path.clone())
.arg(&self.workload_definition.name)
.arg(self.tap_ip.to_string())
.output()
.map_err(NetworkError::IoError)?;

if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
if !stderr.is_empty() {
event!(Level::ERROR, "stderr: {}", stderr);
}
// return Err(stderr.into());
}
let config = NetworkInterfaceConfig::new(
self.workload.instance_id.clone(),
self.workload_definition.name.clone(),
self.tap_ip,
)
.unwrap();
let tap = Net::new_with_tap(config).await.unwrap(); // TODO Error;
debug!("Waiting for the microvm to start");

// Create a new IPTables object
let mut ipt = Iptables::new(false).map_err(NetworkError::Iptables)?;
Expand Down Expand Up @@ -142,7 +146,7 @@ impl RuntimeNetwork for FunctionRuntimeNetwork {
Ok(())
}

fn destroy(&self) -> Result<()> {
async fn destroy(&self) -> Result<()> {
todo!()
}
}
Expand All @@ -156,12 +160,13 @@ impl PodRuntimeNetwork {
}
}

#[async_trait]
impl RuntimeNetwork for PodRuntimeNetwork {
fn init(&self) -> Result<()> {
async fn init(&self) -> Result<()> {
todo!()
}

fn destroy(&self) -> Result<()> {
async fn destroy(&self) -> Result<()> {
todo!()
}
}
2 changes: 1 addition & 1 deletion riklet/src/runtime/pod_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct PodRuntime {
#[async_trait]
impl Runtime for PodRuntime {
async fn run(&mut self) -> super::RuntimeResult<()> {
self.network.init().map_err(RuntimeError::Network)?;
self.network.init().await.map_err(RuntimeError::Network)?;

event!(Level::INFO, "Container workload detected");

Expand Down

0 comments on commit da25de0

Please sign in to comment.