Skip to content

Commit

Permalink
feat(kubegraph): begin implementation of basic simulation gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
HoKim98 committed May 16, 2024
1 parent 64d16ca commit f3c2db4
Show file tree
Hide file tree
Showing 34 changed files with 509 additions and 641 deletions.
5 changes: 3 additions & 2 deletions crates/kubegraph/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["full"]
full = ["connector-full", "df-full"]
default = ["minimal"]
minimal = ["connector-simulation", "df-polars", "function-dummy"]
full = ["connector-full", "df-full", "function-full"]

# Connectors
connector-full = ["connector-prometheus", "connector-simulation"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::{
path::PathBuf,
time::{Duration, Instant},
};
#[cfg(feature = "connector-prometheus")]
pub mod prometheus;
#[cfg(feature = "connector-simulation")]
pub mod simulation;

use std::time::{Duration, Instant};

use anyhow::Result;
use ark_core_k8s::data::Url;
use async_trait::async_trait;
use kube::CustomResource;
use schemars::JsonSchema;
Expand All @@ -16,14 +17,14 @@ use crate::db::NetworkGraphDB;

#[async_trait]
pub trait NetworkConnectors {
async fn add_connector(&self, namespace: String, name: String, spec: NetworkConnectorSpec);
async fn add_connector(&self, object: NetworkConnectorCrd);

async fn delete_connector(&self, namespace: String, name: String);

async fn get_connectors(
&self,
r#type: NetworkConnectorSourceRef,
) -> Option<Vec<NetworkConnectorSpec>>;
) -> Option<Vec<NetworkConnectorCrd>>;
}

#[async_trait]
Expand Down Expand Up @@ -64,6 +65,7 @@ pub trait NetworkConnector {
kind = "NetworkConnector",
root = "NetworkConnectorCrd",
shortname = "nc",
namespaced,
printcolumn = r#"{
"name": "created-at",
"type": "date",
Expand All @@ -78,11 +80,12 @@ pub trait NetworkConnector {
}"#
)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum NetworkConnectorSpec {
#[cfg(feature = "connector-prometheus")]
Prometheus(NetworkConnectorPrometheusSpec),
Prometheus(self::prometheus::NetworkConnectorPrometheusSpec),
#[cfg(feature = "connector-simulation")]
Simulation(NetworkConnectorSimulationSpec),
Simulation(self::simulation::NetworkConnectorSimulationSpec),
}

impl NetworkConnectorSpec {
Expand Down Expand Up @@ -119,6 +122,7 @@ impl PartialEq<NetworkConnectorSourceRef> for NetworkConnectorSpec {
Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum NetworkConnectorSourceRef {
#[cfg(feature = "connector-prometheus")]
Prometheus,
Expand All @@ -136,28 +140,3 @@ impl NetworkConnectorSourceRef {
}
}
}

#[cfg(feature = "connector-prometheus")]
#[derive(
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
)]
#[serde(rename_all = "camelCase")]
pub struct NetworkConnectorPrometheusSpec {
pub template: crate::query::NetworkQuery,
pub url: Url,
}

impl NetworkConnectorPrometheusSpec {
pub const fn name(&self) -> &'static str {
self.template.name()
}
}

#[cfg(feature = "connector-simulation")]
#[derive(
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
)]
#[serde(rename_all = "camelCase")]
pub struct NetworkConnectorSimulationSpec {
pub path: PathBuf,
}
20 changes: 20 additions & 0 deletions crates/kubegraph/api/src/connector/prometheus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use ark_core_k8s::data::Url;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::query::NetworkQuery;

#[derive(
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
)]
#[serde(rename_all = "camelCase")]
pub struct NetworkConnectorPrometheusSpec {
pub template: NetworkQuery,
pub url: Url,
}

impl NetworkConnectorPrometheusSpec {
pub const fn name(&self) -> &'static str {
self.template.name()
}
}
31 changes: 31 additions & 0 deletions crates/kubegraph/api/src/connector/simulation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::path::PathBuf;

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::graph::NetworkGraphMetadata;

#[derive(
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
)]
#[serde(rename_all = "camelCase")]
pub struct NetworkConnectorSimulationSpec {
#[serde(default)]
pub metadata: NetworkGraphMetadata,
pub path: PathBuf,

#[serde(default = "NetworkConnectorSimulationSpec::default_key_edges")]
pub key_edges: String,
#[serde(default = "NetworkConnectorSimulationSpec::default_key_nodes")]
pub key_nodes: String,
}

impl NetworkConnectorSimulationSpec {
fn default_key_edges() -> String {
"edges.csv".into()
}

fn default_key_nodes() -> String {
"nodes.csv".into()
}
}
15 changes: 8 additions & 7 deletions crates/kubegraph/api/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use pl::lazy::dsl;

use crate::{
function::FunctionMetadata,
graph::NetworkGraphMetadata,
ops::{And, Eq, Ge, Gt, Le, Lt, Ne, Or},
problem::{ProblemMetadata, ProblemSpec},
problem::ProblemSpec,
vm::{Feature, Number},
};

Expand Down Expand Up @@ -51,17 +52,17 @@ impl LazyFrame {
#[allow(unused_variables)]
let ProblemSpec {
metadata:
ProblemMetadata {
NetworkGraphMetadata {
capacity,
flow: _,
function: _,
name,
sink,
src,
verbose: _,
supply: _,
unit_cost: _,
},
capacity,
supply: _,
unit_cost: _,
verbose: _,
} = problem;

#[cfg(feature = "df-polars")]
Expand All @@ -85,7 +86,7 @@ impl LazyFrame {
Self::Polars(nodes) => Ok(Self::Polars(
select_polars_edge_side(&nodes, name, src)
.cross_join(select_polars_edge_side(&nodes, name, sink))
.with_column(dsl::lit(ProblemMetadata::MAX_CAPACITY).alias(capacity.as_ref())),
.with_column(dsl::lit(ProblemSpec::MAX_CAPACITY).alias(capacity.as_ref())),
)),
}
}
Expand Down
11 changes: 11 additions & 0 deletions crates/kubegraph/api/src/function/dummy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
)]
#[serde(rename_all = "camelCase")]
pub struct NetworkFunctionDummySpec<Filter = String, Script = String> {
pub filter: Filter,
pub script: Script,
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(feature = "function-dummy")]
pub mod dummy;

use kube::CustomResource;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
Expand All @@ -9,6 +12,7 @@ use serde::{Deserialize, Serialize};
kind = "NetworkFunction",
root = "NetworkFunctionCrd",
shortname = "nf",
namespaced,
printcolumn = r#"{
"name": "created-at",
"type": "date",
Expand All @@ -23,17 +27,12 @@ use serde::{Deserialize, Serialize};
}"#
)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum NetworkFunctionSpec {
#[cfg(feature = "function-dummy")]
Dummy(NetworkFunctionDummySpec),
Dummy(self::dummy::NetworkFunctionDummySpec),
}

#[derive(
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
)]
#[serde(rename_all = "camelCase")]
pub struct NetworkFunctionDummySpec {}

#[derive(
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
)]
Expand Down
Loading

0 comments on commit f3c2db4

Please sign in to comment.