Skip to content

Commit 5931ac3

Browse files
committed
feat: Unify AgentControlDynamicConfig traits into one AgentControlDynamicConfigRepository
1 parent 6ebaae2 commit 5931ac3

File tree

7 files changed

+60
-93
lines changed

7 files changed

+60
-93
lines changed

agent-control/src/agent_control/agent_control.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
use super::config::{
22
AgentControlConfig, AgentControlDynamicConfig, SubAgentsMap, sub_agents_difference,
33
};
4-
use super::config_storer::loader_storer::{
5-
AgentControlDynamicConfigLoader, AgentControlRemoteConfigDeleter,
6-
AgentControlRemoteConfigHashGetter, AgentControlRemoteConfigHashStateUpdater,
7-
AgentControlRemoteConfigStorer,
8-
};
4+
use super::config_storer::loader_storer::AgentControlDynamicConfigRepository;
95
use super::resource_cleaner::ResourceCleaner;
106
use super::version_updater::VersionUpdater;
117
use crate::agent_control::config_validator::DynamicConfigValidator;
@@ -35,11 +31,7 @@ use tracing::{debug, error, info, instrument, trace, warn};
3531
pub struct AgentControl<S, O, SL, DV, RC, VU>
3632
where
3733
O: StartedClient,
38-
SL: AgentControlRemoteConfigStorer
39-
+ AgentControlDynamicConfigLoader
40-
+ AgentControlRemoteConfigDeleter
41-
+ AgentControlRemoteConfigHashStateUpdater
42-
+ AgentControlRemoteConfigHashGetter,
34+
SL: AgentControlDynamicConfigRepository,
4335
S: SubAgentBuilder,
4436
DV: DynamicConfigValidator,
4537
RC: ResourceCleaner,
@@ -63,11 +55,7 @@ impl<S, O, SL, DV, RC, VU> AgentControl<S, O, SL, DV, RC, VU>
6355
where
6456
O: StartedClient,
6557
S: SubAgentBuilder,
66-
SL: AgentControlRemoteConfigStorer
67-
+ AgentControlDynamicConfigLoader
68-
+ AgentControlRemoteConfigDeleter
69-
+ AgentControlRemoteConfigHashStateUpdater
70-
+ AgentControlRemoteConfigHashGetter,
58+
SL: AgentControlDynamicConfigRepository,
7159
DV: DynamicConfigValidator,
7260
RC: ResourceCleaner,
7361
VU: VersionUpdater,

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

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,28 @@ pub trait AgentControlConfigLoader {
1010
fn load(&self) -> Result<AgentControlConfig, AgentControlConfigError>;
1111
}
1212

13-
/// AgentControlRemoteConfigStorer stores a remote_config containing
14-
/// the dynamic part of the AgentControlConfig and the remote config hash and status
15-
pub trait AgentControlRemoteConfigStorer {
13+
/// AgentControlDynamicConfigRepository loads, stores, deletes or updates agent_control's remote_configs
14+
#[cfg_attr(test, mockall::automock)]
15+
pub trait AgentControlDynamicConfigRepository {
16+
/// load the dynamic part of the AgentControlConfig
17+
fn load(&self) -> Result<AgentControlDynamicConfig, AgentControlConfigError>;
18+
/// store a remote_config containing
19+
/// the dynamic part of the AgentControlConfig and the remote config hash and status
1620
fn store(&self, config: &RemoteConfig) -> Result<(), AgentControlConfigError>;
17-
}
1821

19-
/// AgentControlRemoteConfigHashUpdater stores the hash and status for a remote_config
20-
pub trait AgentControlRemoteConfigHashStateUpdater {
22+
/// update the state of a remote_config
2123
fn update_hash_state(&self, state: &ConfigState) -> Result<(), AgentControlConfigError>;
22-
}
2324

24-
/// AgentControlRemoteConfigHashGetter retrieves the hash and status
25-
/// from the stored remote_config if exists
26-
pub trait AgentControlRemoteConfigHashGetter {
25+
/// retrieves the hash and status from the stored remote_config if exists
2726
fn get_hash(&self) -> Result<Option<Hash>, AgentControlConfigError>;
28-
}
2927

30-
/// AgentControlRemoteConfigDeleter deletes the dynamic part of the AgentControlConfig
31-
pub trait AgentControlRemoteConfigDeleter {
28+
/// delete the dynamic part of the AgentControlConfig
3229
fn delete(&self) -> Result<(), AgentControlConfigError>;
3330
}
3431

35-
/// AgentControlDynamicConfigLoader loads the dynamic part of the AgentControlConfig
36-
#[cfg_attr(test, mockall::automock)]
37-
pub trait AgentControlDynamicConfigLoader {
38-
fn load(&self) -> Result<AgentControlDynamicConfig, AgentControlConfigError>;
39-
}
40-
4132
#[cfg(test)]
4233
pub(crate) mod tests {
43-
use super::{
44-
AgentControlConfigError, AgentControlDynamicConfigLoader, AgentControlRemoteConfigDeleter,
45-
AgentControlRemoteConfigHashGetter, AgentControlRemoteConfigHashStateUpdater,
46-
AgentControlRemoteConfigStorer,
47-
};
34+
use super::{AgentControlConfigError, AgentControlDynamicConfigRepository};
4835
use crate::agent_control::config::AgentControlDynamicConfig;
4936
use crate::opamp::remote_config::hash::{ConfigState, Hash};
5037
use crate::values::config::RemoteConfig;
@@ -53,20 +40,16 @@ pub(crate) mod tests {
5340
mock! {
5441
pub AgentControlDynamicConfigStore {}
5542

56-
impl AgentControlRemoteConfigStorer for AgentControlDynamicConfigStore {
57-
fn store(&self, config: &RemoteConfig) -> Result<(), AgentControlConfigError>;
58-
}
59-
impl AgentControlDynamicConfigLoader for AgentControlDynamicConfigStore {
43+
impl AgentControlDynamicConfigRepository for AgentControlDynamicConfigStore {
6044
fn load(&self) -> Result<AgentControlDynamicConfig, AgentControlConfigError>;
61-
}
62-
impl AgentControlRemoteConfigDeleter for AgentControlDynamicConfigStore {
63-
fn delete(&self) -> Result<(), AgentControlConfigError>;
64-
}
65-
impl AgentControlRemoteConfigHashStateUpdater for AgentControlDynamicConfigStore {
45+
46+
fn store(&self, config: &RemoteConfig) -> Result<(), AgentControlConfigError>;
47+
6648
fn update_hash_state(&self, state: &ConfigState) -> Result<(), AgentControlConfigError>;
67-
}
68-
impl AgentControlRemoteConfigHashGetter for AgentControlDynamicConfigStore {
49+
6950
fn get_hash(&self) -> Result<Option<Hash>, AgentControlConfigError>;
51+
52+
fn delete(&self) -> Result<(), AgentControlConfigError>;
7053
}
7154
}
7255

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

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ use crate::agent_control::config::{
33
AgentControlConfig, AgentControlConfigError, AgentControlDynamicConfig,
44
};
55
use crate::agent_control::config_storer::loader_storer::{
6-
AgentControlConfigLoader, AgentControlDynamicConfigLoader, AgentControlRemoteConfigDeleter,
7-
AgentControlRemoteConfigHashGetter, AgentControlRemoteConfigHashStateUpdater,
8-
AgentControlRemoteConfigStorer,
6+
AgentControlConfigLoader, AgentControlDynamicConfigRepository,
97
};
108
use crate::agent_control::defaults::{AGENT_CONTROL_CONFIG_ENV_VAR_PREFIX, default_capabilities};
119
use crate::opamp::remote_config::hash::{ConfigState, Hash};
@@ -36,55 +34,35 @@ where
3634
}
3735
}
3836

39-
impl<Y> AgentControlDynamicConfigLoader for AgentControlConfigStore<Y>
37+
impl<Y> AgentControlDynamicConfigRepository for AgentControlConfigStore<Y>
4038
where
4139
Y: ConfigRepository,
4240
{
4341
fn load(&self) -> Result<AgentControlDynamicConfig, AgentControlConfigError> {
4442
Ok(self._load_config()?.dynamic)
4543
}
46-
}
47-
48-
impl<Y> AgentControlRemoteConfigDeleter for AgentControlConfigStore<Y>
49-
where
50-
Y: ConfigRepository,
51-
{
52-
fn delete(&self) -> Result<(), AgentControlConfigError> {
53-
self.values_repository
54-
.delete_remote(&self.agent_control_id)?;
55-
Ok(())
56-
}
57-
}
5844

59-
impl<Y> AgentControlRemoteConfigStorer for AgentControlConfigStore<Y>
60-
where
61-
Y: ConfigRepository,
62-
{
6345
fn store(&self, config: &RemoteConfig) -> Result<(), AgentControlConfigError> {
6446
self.values_repository
6547
.store_remote(&self.agent_control_id, config)?;
6648
Ok(())
6749
}
68-
}
6950

70-
impl<Y> AgentControlRemoteConfigHashStateUpdater for AgentControlConfigStore<Y>
71-
where
72-
Y: ConfigRepository,
73-
{
7451
fn update_hash_state(&self, state: &ConfigState) -> Result<(), AgentControlConfigError> {
7552
self.values_repository
7653
.update_hash_state(&self.agent_control_id, state)?;
7754
Ok(())
7855
}
79-
}
8056

81-
impl<Y> AgentControlRemoteConfigHashGetter for AgentControlConfigStore<Y>
82-
where
83-
Y: ConfigRepository,
84-
{
8557
fn get_hash(&self) -> Result<Option<Hash>, AgentControlConfigError> {
8658
Ok(self.values_repository.get_hash(&self.agent_control_id)?)
8759
}
60+
61+
fn delete(&self) -> Result<(), AgentControlConfigError> {
62+
self.values_repository
63+
.delete_remote(&self.agent_control_id)?;
64+
Ok(())
65+
}
8866
}
8967

9068
impl<V> AgentControlConfigStore<V>

agent-control/src/config_migrate/migration/agent_config_getter.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::agent_control::config::{AgentControlConfigError, AgentControlDynamicConfig};
2-
use crate::agent_control::config_storer::loader_storer::AgentControlDynamicConfigLoader;
2+
use crate::agent_control::config_storer::loader_storer::AgentControlDynamicConfigRepository;
33
use crate::agent_type::agent_type_id::AgentTypeID;
44
use semver::VersionReq;
55
use thiserror::Error;
@@ -16,15 +16,15 @@ pub enum ConversionError {
1616

1717
pub struct AgentConfigGetter<SL>
1818
where
19-
SL: AgentControlDynamicConfigLoader,
19+
SL: AgentControlDynamicConfigRepository,
2020
{
2121
pub(super) sub_agents_config_loader: SL,
2222
}
2323

2424
#[cfg_attr(test, mockall::automock)]
2525
impl<SL> AgentConfigGetter<SL>
2626
where
27-
SL: AgentControlDynamicConfigLoader + 'static,
27+
SL: AgentControlDynamicConfigRepository + 'static,
2828
{
2929
pub fn new(sub_agents_config_loader: SL) -> Self {
3030
Self {
@@ -69,13 +69,23 @@ pub(crate) mod tests {
6969
use super::*;
7070
use crate::agent_control::agent_id::AgentID;
7171
use crate::agent_control::config::{AgentControlDynamicConfig, SubAgentConfig};
72+
use crate::opamp::remote_config::hash::{ConfigState, Hash};
73+
use crate::values::config::RemoteConfig;
7274
use mockall::mock;
7375
use std::collections::HashMap;
7476

7577
mock! {
7678
pub AgentControlDynamicConfigLoader {}
77-
impl AgentControlDynamicConfigLoader for AgentControlDynamicConfigLoader {
79+
impl AgentControlDynamicConfigRepository for AgentControlDynamicConfigLoader {
7880
fn load(&self) -> Result<AgentControlDynamicConfig, AgentControlConfigError>;
81+
82+
fn store(&self, config: &RemoteConfig) -> Result<(), AgentControlConfigError>;
83+
84+
fn update_hash_state(&self, state: &ConfigState) -> Result<(), AgentControlConfigError>;
85+
86+
fn get_hash(&self) -> Result<Option<Hash>, AgentControlConfigError>;
87+
88+
fn delete(&self) -> Result<(), AgentControlConfigError>;
7989
}
8090
}
8191

agent-control/src/config_migrate/migration/migrator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::agent_control::config::AgentControlConfigError;
2-
use crate::agent_control::config_storer::loader_storer::AgentControlDynamicConfigLoader;
2+
use crate::agent_control::config_storer::loader_storer::AgentControlDynamicConfigRepository;
33
use crate::agent_control::config_storer::store::AgentControlConfigStore;
44
use crate::agent_type::agent_type_registry::AgentRegistry;
55
use crate::agent_type::embedded_registry::EmbeddedRegistry;
@@ -39,7 +39,7 @@ pub enum MigratorError {
3939

4040
pub struct ConfigMigrator<
4141
R: AgentRegistry,
42-
SL: AgentControlDynamicConfigLoader + 'static,
42+
SL: AgentControlDynamicConfigRepository + 'static,
4343
C: DirectoryManager,
4444
F: FileReader,
4545
> {

agent-control/tests/k8s/garbage_collector.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use super::tools::{
1010
use k8s_openapi::api::core::v1::Secret;
1111
use kube::{api::Api, core::TypeMeta};
1212
use mockall::mock;
13+
use newrelic_agent_control::agent_control::config_storer::loader_storer::AgentControlDynamicConfigRepository;
14+
use newrelic_agent_control::opamp::remote_config::hash::{ConfigState, Hash};
1315
use newrelic_agent_control::sub_agent::k8s::supervisor::NotStartedSupervisorK8s;
16+
use newrelic_agent_control::values::config::RemoteConfig;
1417
use newrelic_agent_control::{
1518
agent_control::config::default_group_version_kinds, agent_type::agent_type_id::AgentTypeID,
1619
};
@@ -26,7 +29,7 @@ use newrelic_agent_control::{
2629
use newrelic_agent_control::{
2730
agent_control::{
2831
config::{AgentControlConfig, AgentControlConfigError, AgentControlDynamicConfig},
29-
config_storer::loader_storer::{AgentControlConfigLoader, AgentControlDynamicConfigLoader},
32+
config_storer::loader_storer::AgentControlConfigLoader,
3033
},
3134
k8s::labels::Labels,
3235
};
@@ -49,10 +52,18 @@ mock! {
4952

5053
// Setup AgentControlDynamicConfigLoader mock
5154
mock! {
52-
pub AgentControlDynamicConfigLoader{}
55+
pub AgentControlDynamicConfigRepository{}
5356

54-
impl AgentControlDynamicConfigLoader for AgentControlDynamicConfigLoader {
57+
impl AgentControlDynamicConfigRepository for AgentControlDynamicConfigRepository {
5558
fn load(&self) -> Result<AgentControlDynamicConfig, AgentControlConfigError>;
59+
60+
fn store(&self, config: &RemoteConfig) -> Result<(), AgentControlConfigError>;
61+
62+
fn update_hash_state(&self, state: &ConfigState) -> Result<(), AgentControlConfigError>;
63+
64+
fn get_hash(&self) -> Result<Option<Hash>, AgentControlConfigError>;
65+
66+
fn delete(&self) -> Result<(), AgentControlConfigError>;
5667
}
5768
}
5869

agent-control/tests/k8s/store.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ use crate::k8s::tools::k8s_env::K8sEnv;
44
use k8s_openapi::api::core::v1::ConfigMap;
55
use kube::Api;
66
use newrelic_agent_control::agent_control::agent_id::AgentID;
7-
use newrelic_agent_control::agent_control::config_storer::loader_storer::{
8-
AgentControlDynamicConfigLoader, AgentControlRemoteConfigDeleter,
9-
AgentControlRemoteConfigStorer,
10-
};
7+
use newrelic_agent_control::agent_control::config_storer::loader_storer::AgentControlDynamicConfigRepository;
118
use newrelic_agent_control::agent_control::config_storer::store::AgentControlConfigStore;
129
use newrelic_agent_control::agent_control::defaults::default_capabilities;
1310
use newrelic_agent_control::k8s::client::SyncK8sClient;

0 commit comments

Comments
 (0)