-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap controller in Actix server (#67)
- Loading branch information
1 parent
25bcf23
commit b926228
Showing
8 changed files
with
898 additions
and
20 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use failure::_core::time::Duration; | ||
use gordo_controller::{ | ||
crd::gordo::{load_gordo_resource, Gordo}, | ||
crd::model::{load_model_resource, Model}, | ||
load_kube_config, | ||
}; | ||
use kube::api::{DeleteParams, PostParams}; | ||
use kube::client::APIClient; | ||
use serde_json::Value; | ||
|
||
#[tokio::main] | ||
#[test] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
// Calling /gordos /models /gordos/<name> and /models/<name> will give back nothing before submitting | ||
let resp: Vec<Gordo> = reqwest::get("http://0.0.0.0:8888/gordos").await?.json().await?; | ||
assert!(resp.is_empty()); | ||
|
||
let resp: Vec<Model> = reqwest::get("http://0.0.0.0:8888/models").await?.json().await?; | ||
assert!(resp.is_empty()); | ||
|
||
let resp: Option<Gordo> = reqwest::get("http://0.0.0.0:8888/gordos/test-project-name") | ||
.await? | ||
.json() | ||
.await?; | ||
assert!(resp.is_none()); | ||
|
||
let resp: Vec<Model> = reqwest::get("http://0.0.0.0:8888/models/test-project-name") | ||
.await? | ||
.json() | ||
.await?; | ||
assert!(resp.is_empty()); | ||
|
||
// Apply a Gordo and Model | ||
let gordo: Value = read_manifest("example-gordo.yaml"); | ||
let model: Value = read_manifest("example-model.yaml"); | ||
|
||
let client = APIClient::new(load_kube_config().await); | ||
let gordo_api = load_gordo_resource(&client, "default"); | ||
let model_api = load_model_resource(&client, "default"); | ||
|
||
// Create the Gordo and Model | ||
gordo_api | ||
.create(&PostParams::default(), serde_json::to_vec(&gordo).unwrap()) | ||
.await | ||
.unwrap(); | ||
model_api | ||
.create(&PostParams::default(), serde_json::to_vec(&model).unwrap()) | ||
.await | ||
.unwrap(); | ||
|
||
// Wait for controller to pick up changes | ||
std::thread::sleep(Duration::from_secs(20)); | ||
|
||
// Calling /gordos /models /gordos/<name> and /models/<name> will now give back stuff | ||
let resp: Vec<Gordo> = reqwest::get("http://0.0.0.0:8888/gordos").await?.json().await?; | ||
assert_eq!(resp.len(), 1); | ||
|
||
let resp: Vec<Model> = reqwest::get("http://0.0.0.0:8888/models").await?.json().await?; | ||
assert_eq!(resp.len(), 1); | ||
|
||
let resp: Option<Gordo> = reqwest::get("http://0.0.0.0:8888/gordos/test-project-name") | ||
.await? | ||
.json() | ||
.await?; | ||
assert!(resp.is_some()); | ||
|
||
let resp: Vec<Model> = reqwest::get("http://0.0.0.0:8888/models/test-project-name") | ||
.await? | ||
.json() | ||
.await?; | ||
assert_eq!(resp.len(), 1); | ||
|
||
// Clean up | ||
gordo_api | ||
.delete("test-project-name", &DeleteParams::default()) | ||
.await | ||
.unwrap(); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn read_manifest(name: &str) -> Value { | ||
let raw = std::fs::read_to_string(format!("{}/{}", env!("CARGO_MANIFEST_DIR"), name)).unwrap(); | ||
serde_yaml::from_str(&raw).unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use crate::crd::model::Model; | ||
use crate::{Controller, Gordo}; | ||
use actix_web::{http::StatusCode, web, HttpRequest, HttpResponse}; | ||
|
||
// Simple health check endpoint | ||
pub async fn health(_req: HttpRequest) -> HttpResponse { | ||
HttpResponse::new(StatusCode::OK) | ||
} | ||
|
||
// List current gordos | ||
pub async fn gordos(data: web::Data<Controller>, _req: HttpRequest) -> web::Json<Vec<Gordo>> { | ||
web::Json(data.gordo_state().await) | ||
} | ||
|
||
// Get a gordo by name | ||
pub async fn get_gordo(data: web::Data<Controller>, name: web::Path<String>) -> web::Json<Option<Gordo>> { | ||
let gordo = data | ||
.gordo_state() | ||
.await | ||
.into_iter() | ||
.filter(|gordo| gordo.metadata.name == name.as_str()) | ||
.nth(0); | ||
web::Json(gordo) | ||
} | ||
|
||
// List current models | ||
pub async fn models(data: web::Data<Controller>, _req: HttpRequest) -> web::Json<Vec<Model>> { | ||
web::Json(data.model_state().await) | ||
} | ||
|
||
// List current models belonging to a specific Gordo | ||
pub async fn models_by_gordo(data: web::Data<Controller>, gordo_name: web::Path<String>) -> web::Json<Vec<Model>> { | ||
let models = data | ||
.model_state() | ||
.await | ||
.into_iter() | ||
.filter(|model| { | ||
model | ||
.metadata | ||
.ownerReferences | ||
.iter() | ||
.any(|owner_ref| owner_ref.name == gordo_name.as_str()) | ||
}) | ||
.collect(); | ||
web::Json(models) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use gordo_controller::{controller_init, load_kube_config, views, Controller, Gordo, GordoEnvironmentConfig}; | ||
use tokio_test::block_on; | ||
|
||
use actix_web::web::Json; | ||
use actix_web::{http::StatusCode, test, web}; | ||
use gordo_controller::crd::model::Model; | ||
|
||
#[test] | ||
fn test_view_health() { | ||
block_on(async { | ||
let req = test::TestRequest::default().to_http_request(); | ||
let resp = views::health(req).await; | ||
assert_eq!(resp.status(), StatusCode::OK); | ||
}) | ||
} | ||
|
||
#[test] | ||
fn test_view_gordos() { | ||
block_on(async { | ||
let data = app_state().await; | ||
|
||
let req = test::TestRequest::default().to_http_request(); | ||
let resp: Json<Vec<Gordo>> = views::gordos(data, req).await; | ||
assert_eq!(resp.0.len(), 0); | ||
}) | ||
} | ||
|
||
#[test] | ||
fn test_view_models() { | ||
block_on(async { | ||
let data = app_state().await; | ||
|
||
let req = test::TestRequest::default().to_http_request(); | ||
let resp: Json<Vec<Model>> = views::models(data, req).await; | ||
assert_eq!(resp.0.len(), 0); | ||
}) | ||
} | ||
|
||
// Helper for just this module: loading app state for testing | ||
async fn app_state() -> web::Data<Controller> { | ||
let kube_config = load_kube_config().await; | ||
let controller = controller_init(kube_config, GordoEnvironmentConfig::default()) | ||
.await | ||
.unwrap(); | ||
web::Data::new(controller) | ||
} |