Skip to content

Commit

Permalink
feature/nanocld: inject data to cargo (#1092)
Browse files Browse the repository at this point in the history
  • Loading branch information
leon3s authored Oct 21, 2024
1 parent fff136d commit 196e3e2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
10 changes: 8 additions & 2 deletions bin/nanocld/src/utils/container/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -124,11 +126,14 @@ pub async fn create(
number: usize,
state: &SystemState,
) -> IoResult<Vec<Process>> {
let data = serde_json::to_string(&cargo)?;
let new_data = super::generic::inject_data(&data, state).await?;
let cargo = serde_json::from_str::<Cargo>(&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?;
Expand All @@ -152,6 +157,7 @@ pub async fn create(
.collect::<Vec<usize>>()
.into_iter()
.map(move |current| {
let cargo = cargo.clone();
let secret_envs = secret_envs.clone();
async move {
let ordinal_index = if current > 0 {
Expand Down
30 changes: 29 additions & 1 deletion bin/nanocld/src/utils/container/generic.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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<String> {
let network_gateway = state
.inner
.docker_api
.inspect_network("nanoclbr0", None::<InspectNetworkOptions<String>>)
.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)
}
2 changes: 2 additions & 0 deletions bin/nanocld/src/utils/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 196e3e2

Please sign in to comment.