Skip to content

Commit 0c4a2a5

Browse files
committed
refactor: merge hash and remote values management
Creates a `ConfigStatusManager` trait (with implementations for `onhost`/`k8s`) that is now in charge of managing both the hash repository and the yaml config. Previously, the management of these two items was split due to design decisions that are no longer in place Given both the hash and the remote config values come in a single OpAMP payload, I think we should read and persist this data with a unique service, and only split if absolutely needed.
1 parent e5cdc64 commit 0c4a2a5

File tree

35 files changed

+1319
-845
lines changed

35 files changed

+1319
-845
lines changed

agent-control/src/agent_control/agent_control.rs

Lines changed: 24 additions & 132 deletions
Large diffs are not rendered by default.

agent-control/src/agent_control/config_storer/loader_storer.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::agent_control::config::{
22
AgentControlConfig, AgentControlConfigError, AgentControlDynamicConfig,
33
};
4-
use crate::values::yaml_config::YAMLConfig;
4+
use crate::opamp::remote_config::status::AgentRemoteConfigStatus;
55

66
/// AgentControlConfigLoader loads a whole AgentControlConfig
77
#[cfg_attr(test, mockall::automock)]
@@ -11,7 +11,7 @@ pub trait AgentControlConfigLoader {
1111

1212
/// AgentControlDynamicConfigStorer stores the dynamic part of the AgentControlConfig
1313
pub trait AgentControlDynamicConfigStorer {
14-
fn store(&self, config: &YAMLConfig) -> Result<(), AgentControlConfigError>;
14+
fn store(&self, config: &AgentRemoteConfigStatus) -> Result<(), AgentControlConfigError>;
1515
}
1616

1717
/// AgentControlDynamicConfigLoader loads the dynamic part of the AgentControlConfig
@@ -33,14 +33,14 @@ pub(crate) mod tests {
3333
AgentControlDynamicConfigStorer,
3434
};
3535
use crate::agent_control::config::AgentControlDynamicConfig;
36-
use crate::values::yaml_config::YAMLConfig;
36+
use crate::opamp::remote_config::status::AgentRemoteConfigStatus;
3737
use mockall::{mock, predicate};
3838

3939
mock! {
4040
pub AgentControlDynamicConfigStore {}
4141

4242
impl AgentControlDynamicConfigStorer for AgentControlDynamicConfigStore {
43-
fn store(&self, config: &YAMLConfig) -> Result<(), AgentControlConfigError>;
43+
fn store(&self, config: &AgentRemoteConfigStatus) -> Result<(), AgentControlConfigError>;
4444
}
4545
impl AgentControlDynamicConfigLoader for AgentControlDynamicConfigStore {
4646
fn load(&self) -> Result<AgentControlDynamicConfig, AgentControlConfigError>;
@@ -58,11 +58,10 @@ pub(crate) mod tests {
5858
.returning(move || Ok(sub_agents_config.clone()));
5959
}
6060

61-
pub fn should_store(&mut self, sub_agents_config: &AgentControlDynamicConfig) {
62-
let sub_agents_config: YAMLConfig = sub_agents_config.try_into().unwrap();
61+
pub fn should_store(&mut self, sub_agents_config: &AgentRemoteConfigStatus) {
6362
self.expect_store()
6463
.once()
65-
.with(predicate::eq(sub_agents_config))
64+
.with(predicate::eq(sub_agents_config.clone()))
6665
.returning(move |_| Ok(()));
6766
}
6867
}

agent-control/src/agent_control/config_storer/store.rs

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,73 +6,75 @@ use crate::agent_control::config_storer::loader_storer::{
66
AgentControlDynamicConfigStorer,
77
};
88
use crate::agent_control::defaults::{default_capabilities, AGENT_CONTROL_CONFIG_ENV_VAR_PREFIX};
9-
use crate::values::yaml_config::{YAMLConfig, YAMLConfigError};
10-
use crate::values::yaml_config_repository::{YAMLConfigRepository, YAMLConfigRepositoryError};
9+
use crate::opamp::remote_config::status::AgentRemoteConfigStatus;
10+
use crate::opamp::remote_config::status_manager::error::ConfigStatusManagerError;
11+
use crate::opamp::remote_config::status_manager::ConfigStatusManager;
12+
use crate::values::yaml_config::YAMLConfigError;
1113
use config::builder::DefaultState;
1214
use config::{Config, ConfigBuilder, Environment, File, FileFormat};
1315
use opamp_client::operation::capabilities::Capabilities;
1416
use std::sync::Arc;
1517

16-
pub struct AgentControlConfigStore<Y>
18+
pub struct AgentControlConfigStore<M>
1719
where
18-
Y: YAMLConfigRepository,
20+
M: ConfigStatusManager,
1921
{
2022
config_builder: ConfigBuilder<DefaultState>,
21-
values_repository: Arc<Y>,
23+
config_manager: Arc<M>,
2224
agent_control_id: AgentID,
2325
agent_control_capabilities: Capabilities,
2426
}
2527

26-
impl<Y> AgentControlConfigLoader for AgentControlConfigStore<Y>
28+
impl<M> AgentControlConfigLoader for AgentControlConfigStore<M>
2729
where
28-
Y: YAMLConfigRepository,
30+
M: ConfigStatusManager,
2931
{
3032
fn load(&self) -> Result<AgentControlConfig, AgentControlConfigError> {
3133
self._load_config()
3234
}
3335
}
3436

35-
impl<Y> AgentControlDynamicConfigLoader for AgentControlConfigStore<Y>
37+
impl<M> AgentControlDynamicConfigLoader for AgentControlConfigStore<M>
3638
where
37-
Y: YAMLConfigRepository,
39+
M: ConfigStatusManager,
3840
{
3941
fn load(&self) -> Result<AgentControlDynamicConfig, AgentControlConfigError> {
4042
Ok(self._load_config()?.dynamic)
4143
}
4244
}
4345

44-
impl<Y> AgentControlDynamicConfigDeleter for AgentControlConfigStore<Y>
46+
impl<M> AgentControlDynamicConfigDeleter for AgentControlConfigStore<M>
4547
where
46-
Y: YAMLConfigRepository,
48+
M: ConfigStatusManager,
4749
{
4850
fn delete(&self) -> Result<(), AgentControlConfigError> {
49-
self.values_repository
50-
.delete_remote(&self.agent_control_id)?;
51+
self.config_manager
52+
.delete_remote_status(&self.agent_control_id)?;
5153
Ok(())
5254
}
5355
}
5456

55-
impl<Y> AgentControlDynamicConfigStorer for AgentControlConfigStore<Y>
57+
impl<M> AgentControlDynamicConfigStorer for AgentControlConfigStore<M>
5658
where
57-
Y: YAMLConfigRepository,
59+
M: ConfigStatusManager,
5860
{
59-
fn store(&self, yaml_config: &YAMLConfig) -> Result<(), AgentControlConfigError> {
60-
self.values_repository
61-
.store_remote(&self.agent_control_id, yaml_config)?;
61+
fn store(&self, yaml_config: &AgentRemoteConfigStatus) -> Result<(), AgentControlConfigError> {
62+
self.config_manager
63+
.store_remote_status(&self.agent_control_id, yaml_config)?;
6264
Ok(())
6365
}
6466
}
6567

66-
impl<V> AgentControlConfigStore<V>
68+
impl<M> AgentControlConfigStore<M>
6769
where
68-
V: YAMLConfigRepository,
70+
M: ConfigStatusManager,
6971
{
70-
pub fn new(values_repository: Arc<V>) -> Self {
72+
pub fn new(config_manager: Arc<M>) -> Self {
7173
let config_builder = Config::builder();
7274

7375
Self {
7476
config_builder,
75-
values_repository,
77+
config_manager,
7678
agent_control_id: AgentID::new_agent_control_id(),
7779
agent_control_capabilities: default_capabilities(),
7880
}
@@ -83,8 +85,8 @@ where
8385
/// on top of the local config
8486
fn _load_config(&self) -> Result<AgentControlConfig, AgentControlConfigError> {
8587
let local_config_string: String = self
86-
.values_repository
87-
.load_local(&self.agent_control_id)?
88+
.config_manager
89+
.retrieve_local_config(&self.agent_control_id)?
8890
.ok_or(AgentControlConfigError::Load(
8991
"missing local agent control config".to_string(),
9092
))?
@@ -110,8 +112,9 @@ where
110112
.try_deserialize::<AgentControlConfig>()?;
111113

112114
if let Some(remote_config) = self
113-
.values_repository
114-
.load_remote(&self.agent_control_id, &self.agent_control_capabilities)?
115+
.config_manager
116+
.retrieve_remote_status(&self.agent_control_id, &self.agent_control_capabilities)?
117+
.and_then(|status| status.remote_config)
115118
{
116119
let dynamic_config: AgentControlDynamicConfig = remote_config.try_into()?;
117120
config.dynamic = dynamic_config;
@@ -121,12 +124,12 @@ where
121124
}
122125
}
123126

124-
impl From<YAMLConfigRepositoryError> for AgentControlConfigError {
125-
fn from(e: YAMLConfigRepositoryError) -> Self {
127+
impl From<ConfigStatusManagerError> for AgentControlConfigError {
128+
fn from(e: ConfigStatusManagerError) -> Self {
126129
match e {
127-
YAMLConfigRepositoryError::LoadError(e) => AgentControlConfigError::Load(e),
128-
YAMLConfigRepositoryError::StoreError(e) => AgentControlConfigError::Store(e),
129-
YAMLConfigRepositoryError::DeleteError(e) => AgentControlConfigError::Delete(e),
130+
ConfigStatusManagerError::Retrieval(e) => AgentControlConfigError::Load(e),
131+
ConfigStatusManagerError::Store(e) => AgentControlConfigError::Store(e),
132+
ConfigStatusManagerError::Deletion(e) => AgentControlConfigError::Delete(e),
130133
}
131134
}
132135
}
@@ -138,9 +141,10 @@ pub(crate) mod tests {
138141
AgentControlConfig, AgentID, AgentTypeFQN, OpAMPClientConfig, SubAgentConfig,
139142
};
140143
use crate::agent_control::defaults::AGENT_CONTROL_CONFIG_FILENAME;
141-
use crate::values::file::YAMLConfigRepositoryFile;
144+
use crate::opamp::remote_config::status_manager::local_filesystem::FileSystemConfigStatusManager;
145+
// use crate::values::file::YAMLConfigRepositoryFile;
142146
use serial_test::serial;
143-
use std::path::PathBuf;
147+
144148
use std::{collections::HashMap, env};
145149
use url::Url;
146150

@@ -166,8 +170,8 @@ fleet_control:
166170
"#;
167171
std::fs::write(remote_file.as_path(), remote_config).unwrap();
168172

169-
let vr = YAMLConfigRepositoryFile::new(local_dir, remote_dir).with_remote();
170-
let store = AgentControlConfigStore::new(Arc::new(vr));
173+
let cm = FileSystemConfigStatusManager::new(local_dir).with_remote(remote_dir);
174+
let store = AgentControlConfigStore::new(Arc::new(cm));
171175
let actual = AgentControlConfigLoader::load(&store).unwrap();
172176

173177
let expected = AgentControlConfig {
@@ -212,8 +216,8 @@ fleet_control:
212216
let env_var_name = "NR_AC_AGENTS__ROLLDICE1__AGENT_TYPE";
213217
env::set_var(env_var_name, "namespace/com.newrelic.infrastructure:0.0.2");
214218

215-
let vr = YAMLConfigRepositoryFile::new(local_dir, PathBuf::new()).with_remote();
216-
let store = AgentControlConfigStore::new(Arc::new(vr));
219+
let cm = FileSystemConfigStatusManager::new(local_dir);
220+
let store = AgentControlConfigStore::new(Arc::new(cm));
217221
let actual = AgentControlConfigLoader::load(&store).unwrap();
218222

219223
let expected = AgentControlConfig {
@@ -260,8 +264,8 @@ agents:
260264
let env_var_name = "NR_AC_AGENTS__ROLLDICE2__AGENT_TYPE";
261265
env::set_var(env_var_name, "namespace/com.newrelic.infrastructure:0.0.2");
262266

263-
let vr = YAMLConfigRepositoryFile::new(local_dir, PathBuf::new()).with_remote();
264-
let store = AgentControlConfigStore::new(Arc::new(vr));
267+
let cm = FileSystemConfigStatusManager::new(local_dir);
268+
let store = AgentControlConfigStore::new(Arc::new(cm));
265269
let actual: AgentControlConfig = AgentControlConfigLoader::load(&store).unwrap();
266270

267271
let expected = AgentControlConfig {

agent-control/src/agent_control/defaults.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub const SUB_AGENT_DIR: &str = "fleet/agents.d";
4444
pub const AGENT_CONTROL_CONFIG_FILENAME: &str = "config.yaml";
4545
pub const DYNAMIC_AGENT_TYPE_FILENAME: &str = "dynamic-agent-type.yaml";
4646
pub const IDENTIFIERS_FILENAME: &str = "identifiers.yaml";
47+
pub const REMOTE_CONFIG_STATUS_FILENAME: &str = "config_status.yaml";
4748
pub const VALUES_DIR: &str = "values";
4849
pub const VALUES_FILENAME: &str = "values.yaml";
4950
pub const GENERATED_FOLDER_NAME: &str = "auto-generated";

agent-control/src/agent_control/error.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ use crate::agent_type::agent_type_registry::AgentRepositoryError;
33
use crate::agent_type::error::AgentTypeError;
44
use crate::event::channel::EventPublisherError;
55
use crate::opamp::client_builder::OpAMPClientBuilderError;
6-
use crate::opamp::hash_repository::repository::HashRepositoryError;
76
use crate::opamp::instance_id;
87
use crate::opamp::remote_config::RemoteConfigError;
98
use crate::sub_agent::effective_agents_assembler::EffectiveAgentsAssemblerError;
109
use crate::sub_agent::error::{SubAgentBuilderError, SubAgentCollectionError, SubAgentError};
1110
use crate::sub_agent::persister::config_persister::PersistError;
1211
use crate::values::yaml_config::YAMLConfigError;
13-
use crate::values::yaml_config_repository::YAMLConfigRepositoryError;
1412
use fs::file_reader::FileReaderError;
1513
use opamp_client::{ClientError, NotStartedClientError, StartedClientError};
1614
use std::fmt::Debug;
@@ -70,18 +68,12 @@ pub enum AgentError {
7068
#[error("system time error: `{0}`")]
7169
SystemTimeError(#[from] SystemTimeError),
7270

73-
#[error("remote config hash error: `{0}`")]
74-
RemoteConfigHashError(#[from] HashRepositoryError),
75-
7671
#[error("effective agents assembler error: `{0}`")]
7772
EffectiveAgentsAssemblerError(#[from] EffectiveAgentsAssemblerError),
7873

7974
#[error("remote config error: `{0}`")]
8075
RemoteConfigError(#[from] RemoteConfigError),
8176

82-
#[error("sub agent remote config error: `{0}`")]
83-
SubAgentRemoteConfigError(#[from] YAMLConfigRepositoryError),
84-
8577
#[error("External module error: `{0}`")]
8678
ExternalError(String),
8779

0 commit comments

Comments
 (0)