diff --git a/src/provider/states/starting.rs b/src/provider/states/starting.rs index 0b95fe9..6c81541 100644 --- a/src/provider/states/starting.rs +++ b/src/provider/states/starting.rs @@ -56,22 +56,48 @@ impl State for Starting { "Starting command: {:?} with arguments {:?}", binary, os_args ); + + // Check if environment variables are set on the container - if some are present + // we render all values as templates to replace configroot, packageroot and logroot + // directories in case they are referenced in the values + // + // If even one of these renderings fails the entire pod will be failed and + // transitioned to a complete state with the error that occurred. + // If all renderings work, the vec<(String,String)> is returned as value and used + // later when starting the process + // This works because Result implements + // (FromIterator)[https://doc.rust-lang.org/std/result/enum.Result.html#method.from_iter] + // which returns a Result that is Ok(..) if none of the internal results contained + // an Error. If any error occurred, iteration stops on the first error and returns + // that in the outer result. let env_variables = if let Some(vars) = container.env() { debug!( "Got environment vars: {:?} service {}", vars, pod_state.service_name ); - vars.iter() + let render_result = vars + .iter() .map(|env_var| { - ( - String::from(&env_var.name), - String::from( - &env_var.value.clone().unwrap_or_else(|| String::from("")), - ), + // Replace variables in value + CreatingConfig::render_config_template( + &template_data, + &env_var.value.as_deref().unwrap_or_default(), ) + .map(|value| (&env_var.name, value)) }) - .collect::>() + .collect(); + + // If any single rendering failed, the overall result for the map will have + // collected the Err which we can check for here + match render_result { + Ok(rendered_values) => rendered_values, + Err(error) => { + error!("Failed to render value for env var due to: {:?}", error); + return Transition::Complete(Err(anyhow::Error::from(error))); + } + } } else { + // No environment variables present for this container -> empty vec debug!( "No environment vars set for service {}", pod_state.service_name