-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a webserver to perform a synchronous analysis of the cluster. The idea is to allow perform an ad-hoc analysis on each request to the `/evaluate` endopint. On the future, we may emit kubernetes events for each (new) found issues on the cluster.
- Loading branch information
Showing
17 changed files
with
645 additions
and
193 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ members = [ | |
"korrecte-autogen", | ||
"korrecte-cli", | ||
"korrecte-lib", | ||
"korrecte-web", | ||
] |
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
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
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,98 @@ | ||
use crate::config::Config; | ||
use crate::error::KorrecteError; | ||
use crate::kube::api::{ApiObjectRepository, FrozenObjectRepository}; | ||
use crate::kube::file::FileObjectRepository; | ||
use crate::kube::ObjectRepository; | ||
use crate::linters::evaluator::{Evaluator, SingleEvaluator}; | ||
use crate::linters::LintCollection; | ||
use crate::reporting::{Reporter, SingleThreadedReporter}; | ||
use std::fs::File; | ||
use std::io::Read; | ||
use std::path::Path; | ||
|
||
#[derive(Debug)] | ||
pub enum ConfigError { | ||
CouldNotLoadError, | ||
CouldNotParseError, | ||
} | ||
|
||
pub enum ExecutionMode<'a> { | ||
Api, | ||
FileSystem(&'a Path), | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct ExecutionContextBuilder<'a> { | ||
mode: Option<ExecutionMode<'a>>, | ||
configuration: Option<Config>, | ||
} | ||
|
||
impl<'a> ExecutionContextBuilder<'a> { | ||
pub fn configuration_from_path( | ||
mut self, | ||
path: &Path, | ||
) -> Result<ExecutionContextBuilder<'a>, ConfigError> { | ||
let config = Self::load_config_from_filesystem(path)?; | ||
self.configuration = Some(config); | ||
|
||
Ok(self) | ||
} | ||
|
||
pub fn execution_mode(mut self, mode: ExecutionMode<'a>) -> ExecutionContextBuilder<'a> { | ||
self.mode = Some(mode); | ||
|
||
self | ||
} | ||
|
||
pub fn build(self) -> ExecutionContext<'a> { | ||
ExecutionContext { | ||
mode: self.mode.unwrap_or(ExecutionMode::Api), | ||
configuration: self.configuration.unwrap_or_default(), | ||
} | ||
} | ||
|
||
fn load_config_from_filesystem(path: &Path) -> Result<Config, ConfigError> { | ||
let mut file = File::open(path).map_err(|_| ConfigError::CouldNotLoadError)?; | ||
let mut buffer = String::new(); | ||
file.read_to_string(&mut buffer) | ||
.map_err(|_| ConfigError::CouldNotLoadError)?; | ||
|
||
Ok(toml::from_str(&buffer).map_err(|_| ConfigError::CouldNotParseError)?) | ||
} | ||
} | ||
|
||
pub struct ExecutionContext<'a> { | ||
mode: ExecutionMode<'a>, | ||
configuration: Config, | ||
} | ||
|
||
pub struct Executor<'a> { | ||
context: ExecutionContext<'a>, | ||
} | ||
|
||
impl<'a> Executor<'a> { | ||
pub fn with_context(context: ExecutionContext<'a>) -> Executor<'a> { | ||
Executor { context } | ||
} | ||
|
||
pub fn execute(self) -> Result<impl Reporter, KorrecteError> { | ||
let reporter = SingleThreadedReporter::default(); | ||
let object_repository = self.load_object_repository()?; | ||
let lints = LintCollection::all(self.context.configuration, &*object_repository); | ||
let evaluator = SingleEvaluator; | ||
evaluator.evaluate(&reporter, &lints, &*object_repository); | ||
|
||
Ok(reporter) | ||
} | ||
|
||
fn load_object_repository(&self) -> Result<Box<dyn ObjectRepository>, KorrecteError> { | ||
match self.context.mode { | ||
ExecutionMode::Api => Ok(Box::new(FrozenObjectRepository::from( | ||
ApiObjectRepository::new()?, | ||
))), | ||
ExecutionMode::FileSystem(path) => { | ||
Ok(Box::new(FileObjectRepository::new(Path::new(path))?)) | ||
} | ||
} | ||
} | ||
} |
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
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "korrecte-web" | ||
version = "0.1.0" | ||
authors = ["Guillem Nieto <[email protected]>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
korrecte = { path="../korrecte-lib" } | ||
gotham = "*" | ||
hyper = "*" | ||
mime = "*" | ||
serde_json = "*" |
Oops, something went wrong.