runtime-rs: plug drop-in decoding into config-loading code

To plug drop-in support into existing config-loading code in a robust
way, more specifically to create a single point where this needs to be
handled, load_from_file() and load_raw_from_file() were refactored.
Seeing as the original implemenations of both functions were identical
apart from adjust_config() calls in load_from_file(), load_from_file()
was reimplemented in terms of load_raw_from_file().

Fixes  #4771

Signed-off-by: Pavel Mores <pmores@redhat.com>
This commit is contained in:
Pavel Mores 2022-07-29 13:42:08 +02:00
parent 87b97b6994
commit 57bd3f42d3
2 changed files with 15 additions and 16 deletions

View File

@ -3,6 +3,8 @@
// SPDX-License-Identifier: Apache-2.0
//
pub use drop_in_directory_handling::load;
mod toml_tree_ops {
// The following pair of functions implement toml::Value tree merging, with
// the second argument being merged into the first one and consumed in the

View File

@ -19,6 +19,7 @@ use crate::{eother, sl};
pub mod default;
mod agent;
mod drop_in;
pub mod hypervisor;
pub use self::agent::Agent;
@ -94,21 +95,15 @@ impl TomlConfig {
/// If `config_file` is valid, it will used, otherwise a built-in default path list will be
/// scanned.
pub fn load_from_file<P: AsRef<Path>>(config_file: P) -> Result<(TomlConfig, PathBuf)> {
let file_path = if !config_file.as_ref().as_os_str().is_empty() {
fs::canonicalize(config_file)?
} else {
Self::get_default_config_file()?
};
let mut result = Self::load_raw_from_file(config_file);
if let Ok((ref mut config, _)) = result {
Hypervisor::adjust_config(config)?;
Runtime::adjust_config(config)?;
Agent::adjust_config(config)?;
info!(sl!(), "get kata config: {:?}", config);
}
info!(
sl!(),
"load configuration from: {}",
file_path.to_string_lossy()
);
let content = fs::read_to_string(&file_path)?;
let config = Self::load(&content)?;
Ok((config, file_path))
result
}
/// Load raw Kata configuration information from configuration files.
@ -127,13 +122,15 @@ impl TomlConfig {
"load configuration from: {}",
file_path.to_string_lossy()
);
let content = fs::read_to_string(&file_path)?;
let config: TomlConfig = toml::from_str(&content)?;
let config = drop_in::load(&file_path)?;
Ok((config, file_path))
}
/// Load Kata configuration information from string.
///
/// This function only works with `configuration.toml` and does not handle
/// drop-in config file fragments in config.d/.
pub fn load(content: &str) -> Result<TomlConfig> {
let mut config: TomlConfig = toml::from_str(content)?;
Hypervisor::adjust_config(&mut config)?;