diff --git a/bin/nanocld/src/utils/container/cargo.rs b/bin/nanocld/src/utils/container/cargo.rs index 831bd294..a0b9f2e5 100644 --- a/bin/nanocld/src/utils/container/cargo.rs +++ b/bin/nanocld/src/utils/container/cargo.rs @@ -23,7 +23,7 @@ use crate::{ utils, }; -/// Function that execute the init container before the main cargo container +/// Function that start and wait the status of the init container before the main cargo container /// async fn start_init_container( process: &Process, @@ -66,6 +66,8 @@ async fn start_init_container( Ok(()) } +/// Function that create the init container of the cargo +/// async fn create_init_container( cargo: &Cargo, init_container: &Config, @@ -124,11 +126,14 @@ pub async fn create( number: usize, state: &SystemState, ) -> IoResult> { + let data = serde_json::to_string(&cargo)?; + let new_data = super::generic::inject_data(&data, state).await?; + let cargo = serde_json::from_str::(&new_data)?; super::image::download( &cargo.spec.container.image.clone().unwrap_or_default(), cargo.spec.image_pull_secret.clone(), cargo.spec.image_pull_policy.clone().unwrap_or_default(), - cargo, + &cargo, state, ) .await?; @@ -152,6 +157,7 @@ pub async fn create( .collect::>() .into_iter() .map(move |current| { + let cargo = cargo.clone(); let secret_envs = secret_envs.clone(); async move { let ordinal_index = if current > 0 { diff --git a/bin/nanocld/src/utils/container/generic.rs b/bin/nanocld/src/utils/container/generic.rs index a294652b..3278606e 100644 --- a/bin/nanocld/src/utils/container/generic.rs +++ b/bin/nanocld/src/utils/container/generic.rs @@ -1,4 +1,5 @@ -use nanocl_error::io::IoResult; +use bollard_next::network::InspectNetworkOptions; +use nanocl_error::io::{IoError, IoResult}; use nanocl_stubs::{ process::{Process, ProcessKind}, system::{NativeEventAction, ObjPsStatusKind}, @@ -142,3 +143,30 @@ pub async fn emit_stopping( emit(kind_key, kind, NativeEventAction::Stopping, state).await?; Ok(()) } + +/// Inject data into the payload +/// eg: $$INTERNAL_GATEWAY +pub async fn inject_data(data: &str, state: &SystemState) -> IoResult { + let network_gateway = state + .inner + .docker_api + .inspect_network("nanoclbr0", None::>) + .await + .map_err(|err| { + IoError::interrupted( + "Network", + &format!("Unable to inspect network nanoclbr0 {err}"), + ) + })?; + let ipam = network_gateway.ipam.unwrap_or_default(); + let ipam_config = ipam.config.unwrap_or_default(); + let Some(network) = ipam_config.first() else { + return Err(IoError::invalid_data( + "Network", + "No network found for nanoclbr0", + )); + }; + let gateway_addr = network.gateway.clone().unwrap_or_default(); + let new_data = data.replace("$$INTERNAL_GATEWAY", &gateway_addr); + Ok(new_data) +} diff --git a/bin/nanocld/src/utils/system.rs b/bin/nanocld/src/utils/system.rs index b976a423..700926f9 100644 --- a/bin/nanocld/src/utils/system.rs +++ b/bin/nanocld/src/utils/system.rs @@ -110,6 +110,8 @@ pub async fn register_namespace( Ok(()) } +/// Sync the cargo status with the container status. +/// We use it at startup to be sure that the cargo status is up to date. async fn sync_cargo_status( cargo: &Cargo, container: &ContainerInspectResponse,