Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Runtime abstraction #47

Merged
merged 16 commits into from
Mar 30, 2023
Merged
593 changes: 353 additions & 240 deletions Cargo.lock

Large diffs are not rendered by default.

57 changes: 1 addition & 56 deletions controller/src/core/instance.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,8 @@
use crate::api::ApiChannel;
use definition::workload::{Spec, WorkloadKind};
use definition::InstanceStatus;
use names::{Generator, Name};
use proto::common::ResourceStatus;
use serde::{Deserialize, Serialize};
use std::fmt::Display;

#[derive(Serialize, Deserialize, Clone)]
pub enum InstanceStatus {
Unknown(String),
Pending,
Running,
Failed,
Terminated,
Creating,
Destroying,
}

impl Display for InstanceStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
InstanceStatus::Unknown(_) => write!(f, "Unknown"),
InstanceStatus::Pending => write!(f, "Pending"),
InstanceStatus::Running => write!(f, "Running"),
InstanceStatus::Failed => write!(f, "Failed"),
InstanceStatus::Terminated => write!(f, "Terminated"),
InstanceStatus::Creating => write!(f, "Creating"),
InstanceStatus::Destroying => write!(f, "Destroying"),
}
}
}

#[derive(Serialize, Deserialize, Clone)]
pub struct Instance {
Expand All @@ -47,35 +21,6 @@ pub struct Instance {
pub spec: Spec,
}

impl From<ResourceStatus> for InstanceStatus {
fn from(value: ResourceStatus) -> Self {
match value {
ResourceStatus::Unknown => InstanceStatus::Unknown(String::from("")),
ResourceStatus::Pending => InstanceStatus::Pending,
ResourceStatus::Running => InstanceStatus::Running,
ResourceStatus::Failed => InstanceStatus::Failed,
ResourceStatus::Terminated => InstanceStatus::Terminated,
ResourceStatus::Creating => InstanceStatus::Creating,
ResourceStatus::Destroying => InstanceStatus::Destroying,
}
}
}

impl From<i32> for InstanceStatus {
fn from(value: i32) -> Self {
match value {
0 => InstanceStatus::Unknown(String::from("")),
1 => InstanceStatus::Pending,
2 => InstanceStatus::Running,
3 => InstanceStatus::Failed,
4 => InstanceStatus::Terminated,
5 => InstanceStatus::Creating,
6 => InstanceStatus::Destroying,
_ => InstanceStatus::Unknown(String::from("")),
}
}
}

impl From<ApiChannel> for Instance {
fn from(value: ApiChannel) -> Self {
let workload_definition = value.workload_definition.unwrap();
Expand Down
3 changes: 2 additions & 1 deletion controller/src/core/instance_service.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::api::{Crud, RikError};
use crate::core::core::CoreInternalEvent;
use crate::core::instance::{Instance, InstanceStatus};
use crate::core::instance::Instance;
use crate::core::instance_repository::InstanceRepositoryImpl;
use crate::core::{with_backoff, InstanceRepository, InstanceService, Listener};
use async_trait::async_trait;
use definition::workload::{WorkloadDefinition, WorkloadKind};
use definition::InstanceStatus;
use dotenv::dotenv;
use proto::common::worker_status::Status;
use proto::common::InstanceMetric;
Expand Down
58 changes: 58 additions & 0 deletions crates/definition/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use std::fmt::Display;

use serde::{Deserialize, Serialize};

pub mod workload {
use serde::{Deserialize, Serialize};
use std::fmt::Display;
Expand Down Expand Up @@ -129,3 +133,57 @@ pub mod workload {
}
}
}

#[derive(Serialize, Deserialize, Clone)]
pub enum InstanceStatus {
Unknown(String),
Pending,
Running,
Failed,
Terminated,
Creating,
Destroying,
}

impl Display for InstanceStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
InstanceStatus::Unknown(_) => write!(f, "Unknown"),
InstanceStatus::Pending => write!(f, "Pending"),
InstanceStatus::Running => write!(f, "Running"),
InstanceStatus::Failed => write!(f, "Failed"),
InstanceStatus::Terminated => write!(f, "Terminated"),
InstanceStatus::Creating => write!(f, "Creating"),
InstanceStatus::Destroying => write!(f, "Destroying"),
}
}
}

impl From<InstanceStatus> for i32 {
fn from(value: InstanceStatus) -> Self {
match value {
InstanceStatus::Unknown(_) => 0,
InstanceStatus::Pending => 1,
InstanceStatus::Running => 2,
InstanceStatus::Failed => 3,
InstanceStatus::Terminated => 4,
InstanceStatus::Creating => 5,
InstanceStatus::Destroying => 6,
}
}
}

impl From<i32> for InstanceStatus {
fn from(value: i32) -> Self {
match value {
0 => InstanceStatus::Unknown(String::from("")),
1 => InstanceStatus::Pending,
2 => InstanceStatus::Running,
3 => InstanceStatus::Failed,
4 => InstanceStatus::Terminated,
5 => InstanceStatus::Creating,
6 => InstanceStatus::Destroying,
_ => InstanceStatus::Unknown(String::from("")),
}
}
}
5 changes: 5 additions & 0 deletions proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ authors = ["Alexandre Burgoni <[email protected]>"]
edition = "2021"

[dependencies]
serde = { version = "1.0.126", features = ["derive"] }
prost.workspace = true
tonic.workspace = true
protobuf.workspace = true

[dependencies.definition]
path = "../crates/definition"

[build-dependencies]
tonic-build.workspace = true

57 changes: 55 additions & 2 deletions proto/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use common::{ResourceStatus, WorkloadRequestKind};

use common::{worker_status::Status, InstanceMetric, ResourceStatus, WorkloadRequestKind};
use definition::InstanceStatus;
use std::ops::Deref;
pub mod common {
tonic::include_proto!("common");
}
Expand Down Expand Up @@ -35,4 +36,56 @@ impl From<i32> for ResourceStatus {
}
}

impl From<ResourceStatus> for InstanceStatus {
fn from(value: ResourceStatus) -> Self {
match value {
ResourceStatus::Unknown => InstanceStatus::Unknown(String::from("")),
ResourceStatus::Pending => InstanceStatus::Pending,
ResourceStatus::Running => InstanceStatus::Running,
ResourceStatus::Failed => InstanceStatus::Failed,
ResourceStatus::Terminated => InstanceStatus::Terminated,
ResourceStatus::Creating => InstanceStatus::Creating,
ResourceStatus::Destroying => InstanceStatus::Destroying,
}
}
}

pub extern crate protobuf;

pub enum WorkloadAction {
CREATE,
DELETE,
}

pub struct WorkerStatus(pub common::WorkerStatus);
impl WorkerStatus {
pub fn new(identifier: String, instance_id: String, status: InstanceStatus) -> Self {
Self(common::WorkerStatus {
identifier,
host_address: None,
status: Some(Status::Instance(InstanceMetric {
instance_id,
status: status.into(),
metrics: "".to_string(),
})),
})
}
}

impl Deref for WorkerStatus {
type Target = common::WorkerStatus;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl From<i32> for WorkloadAction {
fn from(value: i32) -> Self {
match value {
0 => WorkloadAction::CREATE,
1 => WorkloadAction::DELETE,
_ => panic!("Unknown workload action"),
}
}
}
Loading