Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.

Add rendering of template variables to environment variables #30

Merged
merged 5 commits into from
Dec 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions src/provider/states/starting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,48 @@ impl State<PodState> 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::<Vec<_>>()
.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
Expand Down