Skip to content

Commit

Permalink
Performance Improvments
Browse files Browse the repository at this point in the history
This commit ensures that there are less requests to the Kubernetes API,
improving the performance. Additionally, it fixes some additional TODO
comments.
  • Loading branch information
schrieveslaach committed Jan 15, 2024
1 parent a5d3f07 commit 5cf9924
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 145 deletions.
4 changes: 2 additions & 2 deletions api/src/apps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ impl AppsService {
pub async fn get_logs(
&self,
app_name: &AppName,
service_name: &String,
service_name: &str,
since: &Option<DateTime<FixedOffset>>,
limit: usize,
) -> Result<Option<LogChunk>, AppsServiceError> {
Expand All @@ -370,7 +370,7 @@ impl AppsService {
pub async fn change_status(
&self,
app_name: &AppName,
service_name: &String,
service_name: &str,
status: ServiceStatus,
) -> Result<Option<Service>, AppsServiceError> {
Ok(self
Expand Down
35 changes: 21 additions & 14 deletions api/src/config/companion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use crate::config::AppSelector;
use crate::models::service::ContainerType;
use crate::models::{AppName, Environment, Image, Router, ServiceConfig};
use handlebars::Handlebars;
use handlebars::{Handlebars, RenderError};
use secstr::SecUtf8;
use serde_value::Value;
use std::collections::BTreeMap;
Expand Down Expand Up @@ -204,7 +204,11 @@ impl BootstrappingContainer {
&self.image
}

pub fn templated_args(&self, app_name: &AppName, base_url: &Option<Url>) -> Vec<String> {
pub fn templated_args(
&self,
app_name: &AppName,
base_url: &Option<Url>,
) -> Result<Vec<String>, RenderError> {
let handlebars = Handlebars::new();

#[derive(Serialize)]
Expand All @@ -222,16 +226,17 @@ impl BootstrappingContainer {

let data = Data {
application: AppData {
name: &app_name,
name: app_name,
base_url,
},
};

self.args
.iter()
// TODO: handle result
.map(|arg| handlebars.render_template(&arg, &data).unwrap())
.collect()
let mut args = Vec::with_capacity(self.args.len());
for arg in &self.args {
args.push(handlebars.render_template(arg, &data)?);
}

Ok(args)
}
}

Expand Down Expand Up @@ -287,7 +292,7 @@ mod tests {

assert_eq!(container.image, Image::from_str("busybox").unwrap());
assert_eq!(
container.templated_args(&AppName::master(), &None),
container.templated_args(&AppName::master(), &None).unwrap(),
Vec::<String>::new()
);
}
Expand All @@ -306,7 +311,7 @@ mod tests {

assert_eq!(container.image, Image::from_str("busybox").unwrap());
assert_eq!(
container.templated_args(&AppName::master(), &None),
container.templated_args(&AppName::master(), &None).unwrap(),
vec![String::from("echo"), String::from("Hello master")]
);
}
Expand All @@ -325,10 +330,12 @@ mod tests {

assert_eq!(container.image, Image::from_str("busybox").unwrap());
assert_eq!(
container.templated_args(
&AppName::master(),
&Some(Url::parse("http://example.com").unwrap())
),
container
.templated_args(
&AppName::master(),
&Some(Url::parse("http://example.com").unwrap())
)
.unwrap(),
vec![
String::from("echo"),
String::from("Hello http://example.com/")
Expand Down
2 changes: 1 addition & 1 deletion api/src/deployment/deployment_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ impl DeploymentUnitBuilder<WithResolvedImages> {
self.stage.service_companions.iter()
{
let templated_companion = service_companion
.apply_templating_for_service_companion(&self.stage.app_name, &service)?;
.apply_templating_for_service_companion(&self.stage.app_name, service)?;

service_companions.push(ServiceCompanion {
templated_companion,
Expand Down
7 changes: 2 additions & 5 deletions api/src/deployment/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,15 @@ impl<'a> Hooks<'a> {
}
}

fn register_configs_as_global_property(
mut context: &mut Context,
services: &[DeployableService],
) {
fn register_configs_as_global_property(context: &mut Context, services: &[DeployableService]) {
let js_configs = services
.iter()
.map(JsServiceConfig::from)
.collect::<Vec<_>>();

let js_configs = serde_json::to_value(js_configs).expect("Should be serializable");
let js_configs =
JsValue::from_json(&js_configs, &mut context).expect("Unable to read JSON value");
JsValue::from_json(&js_configs, context).expect("Unable to read JSON value");

context
.register_global_property("serviceConfigs", js_configs, Attribute::READONLY)
Expand Down
17 changes: 10 additions & 7 deletions api/src/infrastructure/kubernetes/deployment_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
};
use failure::Error;
use futures::{AsyncBufReadExt, AsyncReadExt, StreamExt, TryStreamExt};
use handlebars::RenderError;
use k8s_openapi::{
api::{
apps::v1::{Deployment, StatefulSet},
Expand Down Expand Up @@ -78,14 +79,16 @@ impl K8sDeploymentUnit {
let containers = bootstrapping_containers
.iter()
.enumerate()
.map(|(i, bc)| Container {
name: format!("bootstrap-{i}"),
image: Some(bc.image().to_string()),
image_pull_policy: Some(String::from("Always")),
args: Some(bc.templated_args(app_name, &base_url)),
..Default::default()
.map(|(i, bc)| {
Ok(Container {
name: format!("bootstrap-{i}"),
image: Some(bc.image().to_string()),
image_pull_policy: Some(String::from("Always")),
args: Some(bc.templated_args(app_name, &base_url)?),
..Default::default()
})
})
.collect::<Vec<_>>();
.collect::<Result<Vec<_>, RenderError>>()?;

let pod_name = format!(
"{}-bootstrap-{}",
Expand Down
Loading

0 comments on commit 5cf9924

Please sign in to comment.