Skip to content

Commit

Permalink
WIP: fixes #125
Browse files Browse the repository at this point in the history
  • Loading branch information
schrieveslaach committed Jan 8, 2024
1 parent 40c46a5 commit 77a0e67
Show file tree
Hide file tree
Showing 8 changed files with 344 additions and 395 deletions.
422 changes: 202 additions & 220 deletions api/Cargo.lock

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ kube = { version = "0.84", default-features = false, features = ["client", "deri
lazy_static = "1.4"
log = "0.4"
multimap = "0.9"
oci-distribution = "0.10"
pest = "2.6"
pest_derive = "2.6"
regex = "1.9"
Expand All @@ -51,10 +52,6 @@ uuid = { version = "1.3", features = ["serde", "v4"] }
yansi = "0.5"


[dependencies.dkregistry]
git = "https://github.com/camallo/dkregistry-rs.git"
rev = "0642b1c"

[dependencies.shiplift]
git = "https://github.com/softprops/shiplift.git"
rev = "3a7c1dc3ae388b6a9f0a8f724fabff30953bcc5b"
Expand Down
36 changes: 20 additions & 16 deletions api/src/apps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ use crate::deployment::deployment_unit::DeploymentUnitBuilder;
use crate::infrastructure::Infrastructure;
use crate::models::service::{ContainerType, Service, ServiceStatus};
use crate::models::{AppName, AppStatusChangeId, LogChunk, ServiceConfig};
use crate::registry::ImagesServiceError;
use crate::registry::Registry;
use crate::registry::RegistryError;
use chrono::{DateTime, FixedOffset};
use handlebars::RenderError;
pub use host_meta_cache::new as host_meta_crawling;
pub use host_meta_cache::HostMetaCache;
pub use host_meta_cache::HostMetaCrawler;
use multimap::MultiMap;
pub use routes::{apps_routes, delete_app_sync};
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -275,9 +275,13 @@ impl AppsService {

let deployment_unit_builder = DeploymentUnitBuilder::init(app_name.clone(), configs)
.extend_with_config(&self.config)
.extend_with_templating_only_service_configs(configs_for_templating)
.resolve_image_manifest(&self.config)
.await?
.extend_with_templating_only_service_configs(configs_for_templating);

let images = deployment_unit_builder.images();
let image_infos = Registry::new(&self.config).resolve_image_infos(&images).await?;

let deployment_unit_builder = deployment_unit_builder
.extend_with_image_infos(image_infos)
.apply_templating()?
.apply_hooks(&self.config)
.await?;
Expand Down Expand Up @@ -393,7 +397,7 @@ pub enum AppsServiceError {
#[fail(display = "Invalid configuration (invalid template): {}", error)]
InvalidTemplateFormat { error: Arc<RenderError> },
#[fail(display = "Unable to resolve information about image: {}", error)]
UnableToResolveImage { error: ImagesServiceError },
UnableToResolveImage { error: RegistryError },
#[fail(display = "Invalid deployment hook.")]
InvalidDeploymentHook,
}
Expand Down Expand Up @@ -422,8 +426,8 @@ impl From<RenderError> for AppsServiceError {
}
}

impl From<ImagesServiceError> for AppsServiceError {
fn from(error: ImagesServiceError) -> Self {
impl From<RegistryError> for AppsServiceError {
fn from(error: RegistryError) -> Self {
AppsServiceError::UnableToResolveImage { error }
}
}
Expand Down Expand Up @@ -704,12 +708,12 @@ Log msg 3 of service-a of app master
[companions.openid]
serviceName = 'openid'
type = 'application'
image = 'private.example.com/library/openid:latest'
image = 'keycloak/keycloak:23.0'
[companions.db]
serviceName = 'db'
type = 'service'
image = 'private.example.com/library/db:latest'
image = 'postgres:16.1'
"#
);
let infrastructure = Box::new(Dummy::new());
Expand Down Expand Up @@ -742,12 +746,12 @@ Log msg 3 of service-a of app master
[companions.openid]
serviceName = 'openid'
type = 'application'
image = 'private.example.com/library/openid:latest'
image = 'keycloak/keycloak:23.0'
[companions.db]
serviceName = 'db'
type = 'service'
image = 'private.example.com/library/db:latest'
image = 'postgres:16.1'
"#
);
let infrastructure = Box::new(Dummy::new());
Expand Down Expand Up @@ -785,7 +789,7 @@ Log msg 3 of service-a of app master
[companions.openid]
serviceName = 'openid'
type = 'application'
image = 'private.example.com/library/openid:latest'
image = 'keycloak/keycloak:23.0'
env = [ "VAR_1=abcd", "VAR_2=1234" ]
[companions.openid.labels]
Expand Down Expand Up @@ -850,7 +854,7 @@ Log msg 3 of service-a of app master
[companions.openid]
serviceName = 'openid'
type = 'application'
image = 'private.example.com/library/openid:latest'
image = 'keycloak/keycloak:23.0'
env = [ """SERVICES={{~#each services~}}{{name}},{{~/each~}}""" ]
"#
);
Expand Down Expand Up @@ -980,15 +984,15 @@ Log msg 3 of service-a of app master
[companions.db1]
serviceName = 'db1'
type = 'service'
image = 'private.example.com/library/db:latest'
image = 'postgres:16.1'
[companions.db1.volumes]
'/etc/mysql1/my.cnf' = 'EFGH'
[companions.db2]
serviceName = 'db2'
type = 'service'
image = 'private.example.com/library/db:latest'
image = 'postgres:16.1'
[companions.db2.files]
'/etc/mysql2/my.cnf' = 'ABCD'
Expand Down
1 change: 1 addition & 0 deletions api/src/apps/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ impl From<AppsError> for HttpApiError {
AppsError::InfrastructureError { .. }
| AppsError::InvalidServerConfiguration { .. }
| AppsError::InvalidTemplateFormat { .. }
// TODO change status
| AppsError::UnableToResolveImage { .. }
| AppsError::InvalidDeploymentHook => {
error!("Internal server error: {}", error);
Expand Down
58 changes: 20 additions & 38 deletions api/src/deployment/deployment_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::config::{Config, StorageStrategy};
use crate::deployment::hooks::Hooks;
use crate::infrastructure::TraefikIngressRoute;
use crate::models::{AppName, ContainerType, Image, ServiceConfig};
use crate::registry::{ImageInfo, ImagesService, ImagesServiceError};
use crate::registry::ImageInfo;
use std::collections::{HashMap, HashSet};

pub struct Initialized {
Expand Down Expand Up @@ -225,7 +225,7 @@ impl DeploymentUnitBuilder<WithCompanions> {
}

impl DeploymentUnitBuilder<WithTemplatedConfigs> {
fn images(&self) -> HashSet<Image> {
pub fn images(&self) -> HashSet<Image> {
let mut images = HashSet::new();

images.extend(
Expand Down Expand Up @@ -256,15 +256,10 @@ impl DeploymentUnitBuilder<WithTemplatedConfigs> {
images
}

pub async fn resolve_image_manifest(
pub fn extend_with_image_infos(
mut self,
config: &Config,
) -> Result<DeploymentUnitBuilder<WithResolvedImages>, ImagesServiceError> {
let images = self.images();
let image_infos = ImagesService::new(config)
.resolve_image_infos(&images)
.await?;

image_infos: HashMap<Image, ImageInfo>,
) -> DeploymentUnitBuilder<WithResolvedImages> {
Self::assign_port_mappings_impl(self.stage.configs.iter_mut(), &image_infos);
Self::assign_port_mappings_impl(
self.stage
Expand All @@ -285,7 +280,7 @@ impl DeploymentUnitBuilder<WithTemplatedConfigs> {
&image_infos,
);

Ok(DeploymentUnitBuilder {
DeploymentUnitBuilder {
stage: WithResolvedImages {
app_name: self.stage.app_name,
configs: self.stage.configs,
Expand All @@ -294,7 +289,7 @@ impl DeploymentUnitBuilder<WithTemplatedConfigs> {
templating_only_service_configs: self.stage.templating_only_service_configs,
image_infos,
},
})
}
}

fn assign_port_mappings_impl<'a, Iter>(configs: Iter, image_infos: &HashMap<Image, ImageInfo>)
Expand Down Expand Up @@ -628,8 +623,7 @@ mod tests {
let unit = DeploymentUnitBuilder::init(app_name, service_configs)
.extend_with_config(&config)
.extend_with_templating_only_service_configs(Vec::new())
.resolve_image_manifest(&config)
.await?
.extend_with_image_infos(HashMap::new())
.apply_templating()?
.apply_hooks(&config)
.await?
Expand Down Expand Up @@ -667,8 +661,7 @@ mod tests {
let unit = DeploymentUnitBuilder::init(app_name, service_configs)
.extend_with_config(&config)
.extend_with_templating_only_service_configs(Vec::new())
.resolve_image_manifest(&config)
.await?
.extend_with_image_infos(HashMap::new())
.apply_templating()?
.apply_hooks(&config)
.await?
Expand Down Expand Up @@ -723,8 +716,7 @@ mod tests {
let unit = DeploymentUnitBuilder::init(app_name, service_configs)
.extend_with_config(&config)
.extend_with_templating_only_service_configs(Vec::new())
.resolve_image_manifest(&config)
.await?
.extend_with_image_infos(HashMap::new())
.apply_templating()?
.apply_hooks(&config)
.await?
Expand Down Expand Up @@ -771,8 +763,7 @@ mod tests {
let unit = DeploymentUnitBuilder::init(app_name, service_configs)
.extend_with_config(&config)
.extend_with_templating_only_service_configs(Vec::new())
.resolve_image_manifest(&config)
.await?
.extend_with_image_infos(HashMap::new())
.apply_templating()?
.apply_hooks(&config)
.await?
Expand Down Expand Up @@ -816,8 +807,7 @@ mod tests {
let unit = DeploymentUnitBuilder::init(app_name, vec![service_config])
.extend_with_config(&config)
.extend_with_templating_only_service_configs(Vec::new())
.resolve_image_manifest(&config)
.await?
.extend_with_image_infos(HashMap::new())
.apply_templating()?
.apply_hooks(&config)
.await?
Expand Down Expand Up @@ -852,8 +842,7 @@ mod tests {
let unit = DeploymentUnitBuilder::init(app_name, vec![service_configs])
.extend_with_config(&config)
.extend_with_templating_only_service_configs(Vec::new())
.resolve_image_manifest(&config)
.await?
.extend_with_image_infos(HashMap::new())
.apply_templating()?
.apply_hooks(&config)
.await?
Expand Down Expand Up @@ -892,8 +881,7 @@ mod tests {
let unit = DeploymentUnitBuilder::init(app_name, service_configs)
.extend_with_config(&config)
.extend_with_templating_only_service_configs(Vec::new())
.resolve_image_manifest(&config)
.await?
.extend_with_image_infos(HashMap::new())
.apply_templating()?
.apply_hooks(&config)
.await?
Expand Down Expand Up @@ -947,8 +935,7 @@ mod tests {
let unit = DeploymentUnitBuilder::init(app_name, service_configs)
.extend_with_config(&config)
.extend_with_templating_only_service_configs(vec![sc!("postgres", "postgres:alpine")])
.resolve_image_manifest(&config)
.await?
.extend_with_image_infos(HashMap::new())
.apply_templating()?
.apply_hooks(&config)
.await?
Expand Down Expand Up @@ -1002,8 +989,7 @@ mod tests {
let unit = DeploymentUnitBuilder::init(app_name, service_configs)
.extend_with_config(&config)
.extend_with_templating_only_service_configs(Vec::new())
.resolve_image_manifest(&config)
.await?
.extend_with_image_infos(HashMap::new())
.apply_templating()?
.apply_hooks(&config)
.await?
Expand Down Expand Up @@ -1055,8 +1041,7 @@ mod tests {
let unit = DeploymentUnitBuilder::init(app_name, service_configs)
.extend_with_config(&config)
.extend_with_templating_only_service_configs(Vec::new())
.resolve_image_manifest(&config)
.await?
.extend_with_image_infos(HashMap::new())
.apply_templating()?
.apply_hooks(&config)
.await?
Expand Down Expand Up @@ -1107,8 +1092,7 @@ mod tests {
let unit = DeploymentUnitBuilder::init(app_name, service_configs)
.extend_with_config(&config)
.extend_with_templating_only_service_configs(Vec::new())
.resolve_image_manifest(&config)
.await?
.extend_with_image_infos(HashMap::new())
.apply_templating()?
.apply_hooks(&config)
.await?
Expand Down Expand Up @@ -1153,8 +1137,7 @@ mod tests {
let unit = DeploymentUnitBuilder::init(app_name, service_configs)
.extend_with_config(&config)
.extend_with_templating_only_service_configs(Vec::new())
.resolve_image_manifest(&config)
.await?
.extend_with_image_infos(HashMap::new())
.apply_templating()?
.apply_hooks(&config)
.await?
Expand Down Expand Up @@ -1184,8 +1167,7 @@ mod tests {
let unit = DeploymentUnitBuilder::init(app_name, service_configs)
.extend_with_config(&config)
.extend_with_templating_only_service_configs(Vec::new())
.resolve_image_manifest(&config)
.await?
.extend_with_image_infos(HashMap::new())
.apply_templating()?
.apply_hooks(&config)
.await?
Expand Down
Loading

0 comments on commit 77a0e67

Please sign in to comment.