Skip to content

Commit 5482ef7

Browse files
committed
feat: restructure onhost folders and naming
1 parent 4023850 commit 5482ef7

File tree

31 files changed

+190
-166
lines changed

31 files changed

+190
-166
lines changed

.goreleaser.yml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,31 +72,23 @@ nfpms:
7272
file_info:
7373
mode: 0700
7474
- src: build/package/config.yaml
75-
dst: /etc/newrelic-agent-control/config.yaml
75+
dst: /etc/newrelic-agent-control/local-data-agent-control/config.yaml
7676
type: config
7777
file_info:
7878
mode: 0600
7979
- src: build/package/newrelic-agent-control.service
8080
dst: /lib/systemd/system/newrelic-agent-control.service
8181
- src: build/package/newrelic-agent-control.conf
82-
dst: /etc/newrelic-agent-control/newrelic-agent-control.conf
82+
dst: /etc/newrelic-agent-control/systemd-env.conf
8383
type: config|noreplace
8484
file_info:
8585
mode: 0600
86-
- dst: /etc/newrelic-agent-control/fleet/agents.d
87-
type: dir
88-
file_info:
89-
mode: 0700
9086
- src: LICENSE.md
9187
dst: /usr/share/doc/newrelic/newrelic-agent-control/LICENSE.md
9288
- dst: /var/lib/newrelic-agent-control
9389
type: dir
9490
file_info:
9591
mode: 0700
96-
- dst: /var/lib/newrelic-agent-control/fleet/agents.d
97-
type: dir
98-
file_info:
99-
mode: 0700
10092
- dst: /var/lib/newrelic-agent-control/auto-generated
10193
type: dir
10294
file_info:

agent-control/src/agent_control/defaults.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
use crate::agent_control::agent_id::AgentID;
12
use crate::agent_type::agent_type_id::AgentTypeID;
3+
use crate::k8s::store::StoreKey;
24
use crate::opamp::remote_config::signature::SIGNATURE_CUSTOM_CAPABILITY;
35
use crate::sub_agent::identity::AgentIdentity;
46
use opamp_client::capabilities;
@@ -46,12 +48,19 @@ cfg_if::cfg_if! {
4648
pub const AGENT_CONTROL_LOG_DIR: &str = "/var/log/newrelic-agent-control";
4749
}
4850
}
49-
50-
pub const SUB_AGENT_DIR: &str = "fleet/agents.d";
51+
/// The prefixes for the ConfigMap name and for on host directories.
52+
/// The cm having CM_NAME_LOCAL_DATA_PREFIX stores all the config that are "local",
53+
/// the SA treats those CM as read-only.
54+
pub const CM_NAME_LOCAL_DATA_PREFIX: &str = "local-data-";
55+
/// The cm having CM_NAME_OPAMP_DATA_PREFIX as prefix stores all the data related with opamp:
56+
/// Instance IDs, hashes, and remote configs. The Sa reads and writes those CMs.
57+
pub const CM_NAME_OPAMP_DATA_PREFIX: &str = "fleet-data-";
58+
pub const STORE_KEY_LOCAL_DATA_CONFIG: &StoreKey = "local_config";
59+
pub const STORE_KEY_OPAMP_DATA_CONFIG: &StoreKey = "remote_config";
60+
pub const STORE_KEY_INSTANCE_ID: &StoreKey = "instance_id";
5161
pub const AGENT_CONTROL_CONFIG_FILENAME: &str = "config.yaml";
5262
pub const DYNAMIC_AGENT_TYPE_DIR: &str = "dynamic-agent-types";
53-
pub const IDENTIFIERS_FILENAME: &str = "identifiers.yaml";
54-
pub const VALUES_DIR: &str = "values";
63+
pub const INSTANCE_ID_FILENAME: &str = "instance_id.yaml";
5564
pub const VALUES_FILENAME: &str = "values.yaml";
5665
pub const GENERATED_FOLDER_NAME: &str = "auto-generated";
5766
pub const AGENT_CONTROL_LOG_FILENAME: &str = "newrelic-agent-control.log";
@@ -75,6 +84,10 @@ pub fn default_sub_agent_custom_capabilities() -> CustomCapabilities {
7584
}
7685
}
7786

87+
pub fn build_folder_name(agent_id: &AgentID, prefix: &str) -> String {
88+
format!("{prefix}{agent_id}")
89+
}
90+
7891
pub(crate) fn get_custom_capabilities(agent_type_id: &AgentTypeID) -> Option<CustomCapabilities> {
7992
if agent_type_id.eq(&AgentIdentity::new_agent_control_identity().agent_type_id) {
8093
// Agent_Control does not have custom capabilities for now

agent-control/src/agent_control/run/on_host.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::agent_control::config_repository::store::AgentControlConfigStore;
55
use crate::agent_control::config_validator::RegistryDynamicConfigValidator;
66
use crate::agent_control::defaults::{
77
AGENT_CONTROL_VERSION, FLEET_ID_ATTRIBUTE_KEY, HOST_ID_ATTRIBUTE_KEY, HOST_NAME_ATTRIBUTE_KEY,
8-
OPAMP_AGENT_VERSION_ATTRIBUTE_KEY, SUB_AGENT_DIR,
8+
OPAMP_AGENT_VERSION_ATTRIBUTE_KEY,
99
};
1010
use crate::agent_control::http_server::runner::Runner;
1111
use crate::agent_control::resource_cleaner::no_op::NoOpResourceCleaner;
@@ -99,7 +99,6 @@ impl AgentControlRunner {
9999
LocalFile,
100100
DirectoryManagerFs,
101101
self.base_paths.remote_dir.clone(),
102-
self.base_paths.remote_dir.join(SUB_AGENT_DIR),
103102
);
104103
let instance_id_getter =
105104
InstanceIDWithIdentifiersGetter::new(instance_id_storer, identifiers);
@@ -154,8 +153,7 @@ impl AgentControlRunner {
154153
&self.base_paths.remote_dir,
155154
));
156155

157-
let supervisor_builder =
158-
SupervisortBuilderOnHost::new(self.base_paths.log_dir.join(SUB_AGENT_DIR));
156+
let supervisor_builder = SupervisortBuilderOnHost::new(self.base_paths.log_dir);
159157

160158
let signature_validator = Arc::new(self.signature_validator);
161159
let remote_config_validators = vec![

agent-control/src/bin/main_config_migrate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn main() -> Result<(), Box<dyn Error>> {
2626
let config_migrator = ConfigMigrator::new(
2727
ConfigConverter::default(),
2828
AgentConfigGetter::new(sa_local_config_loader),
29-
ValuesPersisterFile::new(cli.local_sub_agent_data_dir()),
29+
ValuesPersisterFile::new(cli.local_data_dir()),
3030
);
3131

3232
let legacy_config_renamer = LegacyConfigRenamer::default();

agent-control/src/config_migrate/cli.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
agent_control::defaults::{
3-
AGENT_CONTROL_DATA_DIR, AGENT_CONTROL_LOCAL_DATA_DIR, AGENT_CONTROL_LOG_DIR, SUB_AGENT_DIR,
3+
AGENT_CONTROL_DATA_DIR, AGENT_CONTROL_LOCAL_DATA_DIR, AGENT_CONTROL_LOG_DIR,
44
},
55
config_migrate::migration::defaults::NEWRELIC_INFRA_AGENT_TYPE_CONFIG_MAPPING,
66
};
@@ -38,10 +38,6 @@ impl Cli {
3838
self.local_dir.to_path_buf()
3939
}
4040

41-
pub fn local_sub_agent_data_dir(&self) -> PathBuf {
42-
self.local_dir.join(SUB_AGENT_DIR)
43-
}
44-
4541
pub fn remote_data_dir(&self) -> PathBuf {
4642
self.remote_dir.to_path_buf()
4743
}

agent-control/src/config_migrate/migration/persister/values_persister_file.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::agent_control::agent_id::AgentID;
2-
use crate::agent_control::defaults::{VALUES_DIR, VALUES_FILENAME};
2+
use crate::agent_control::defaults::VALUES_FILENAME;
33
use fs::LocalFile;
44
use fs::directory_manager::{DirectoryManagementError, DirectoryManager, DirectoryManagerFs};
55
use fs::writer_file::{FileWriter, WriteError};
@@ -51,7 +51,6 @@ where
5151
if !path.exists() {
5252
self.create_directory(&path)?;
5353
}
54-
path.push(VALUES_DIR);
5554
if !path.exists() {
5655
self.create_directory(&path)?;
5756
}

agent-control/src/k8s/store.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,12 @@ use super::Error;
33
use super::client::SyncK8sClient;
44
use super::labels::Labels;
55
use crate::agent_control::agent_id::AgentID;
6+
use crate::agent_control::defaults::{CM_NAME_LOCAL_DATA_PREFIX, CM_NAME_OPAMP_DATA_PREFIX};
67
use std::sync::{Arc, RwLock};
78

8-
/// The prefixes for the ConfigMap name.
9-
/// The cm having CM_NAME_LOCAL_DATA_PREFIX stores all the config that are "local",
10-
/// the SA treats those CM as read-only.
11-
pub const CM_NAME_LOCAL_DATA_PREFIX: &str = "local-data-";
12-
/// The cm having CM_NAME_OPAMP_DATA_PREFIX as prefix stores all the data related with opamp:
13-
/// Instance IDs, hashes, and remote configs. The Sa reads and writes those CMs.
14-
pub const CM_NAME_OPAMP_DATA_PREFIX: &str = "fleet-data-";
15-
169
/// The key used to identify the data in the Store.
1710
pub type StoreKey = str;
1811

19-
pub const STORE_KEY_LOCAL_DATA_CONFIG: &StoreKey = "local_config";
20-
pub const STORE_KEY_OPAMP_DATA_CONFIG: &StoreKey = "remote_config";
21-
pub const STORE_KEY_INSTANCE_ID: &StoreKey = "instance_id";
22-
2312
/// Represents a Kubernetes persistent store of Agents data such as instance id and configs.
2413
/// The store is implemented using one ConfigMap per Agent with all the data.
2514
pub struct K8sStore {

agent-control/src/opamp/instance_id/k8s/storer.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
use crate::agent_control::agent_id::AgentID;
22

3-
use crate::k8s::{
4-
self,
5-
store::{K8sStore, STORE_KEY_INSTANCE_ID},
6-
};
3+
use super::getter::Identifiers;
4+
use crate::agent_control::defaults::STORE_KEY_INSTANCE_ID;
5+
use crate::k8s::{self, store::K8sStore};
76
use crate::opamp::instance_id::getter::DataStored;
87
use crate::opamp::instance_id::storer::InstanceIDStorer;
98
use std::sync::Arc;
109
use tracing::debug;
1110

12-
use super::getter::Identifiers;
13-
1411
pub struct Storer {
1512
k8s_store: Arc<K8sStore>,
1613
}

agent-control/src/opamp/instance_id/on_host/storer.rs

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::getter::Identifiers;
22
use crate::agent_control::agent_id::AgentID;
3-
use crate::agent_control::defaults::IDENTIFIERS_FILENAME;
3+
use crate::agent_control::defaults::{CM_NAME_OPAMP_DATA_PREFIX, INSTANCE_ID_FILENAME};
44
use crate::opamp::instance_id::getter::DataStored;
55
use crate::opamp::instance_id::storer::InstanceIDStorer;
66
use fs::LocalFile;
@@ -20,7 +20,6 @@ where
2020
file_rw: F,
2121
dir_manager: D,
2222
agent_control_remote_dir: PathBuf,
23-
agent_remote_dir: PathBuf,
2423
}
2524

2625
#[derive(thiserror::Error, Debug)]
@@ -67,17 +66,11 @@ where
6766
D: DirectoryManager,
6867
F: FileWriter + FileReader,
6968
{
70-
pub fn new(
71-
file_rw: F,
72-
dir_manager: D,
73-
agent_control_remote_dir: PathBuf,
74-
agent_remote_dir: PathBuf,
75-
) -> Self {
69+
pub fn new(file_rw: F, dir_manager: D, agent_control_remote_dir: PathBuf) -> Self {
7670
Self {
7771
file_rw,
7872
dir_manager,
7973
agent_control_remote_dir,
80-
agent_remote_dir,
8174
}
8275
}
8376
}
@@ -128,14 +121,14 @@ where
128121
}
129122
}
130123

124+
pub fn build_folder_name(&self, agent_id: &AgentID, prefix: &str) -> String {
125+
format!("{prefix}{agent_id}")
126+
}
127+
131128
fn get_instance_id_path(&self, agent_id: &AgentID) -> PathBuf {
132-
if agent_id == &AgentID::AgentControl {
133-
self.agent_control_remote_dir.join(IDENTIFIERS_FILENAME)
134-
} else {
135-
self.agent_remote_dir
136-
.join(agent_id)
137-
.join("identifiers.yaml")
138-
}
129+
self.agent_control_remote_dir
130+
.join(self.build_folder_name(agent_id, CM_NAME_OPAMP_DATA_PREFIX))
131+
.join(INSTANCE_ID_FILENAME)
139132
}
140133
}
141134

@@ -156,26 +149,32 @@ mod tests {
156149
#[test]
157150
fn basic_get_uild_path() {
158151
let sa_dir = PathBuf::from("/super");
159-
let sub_agent_dir = PathBuf::from("/sub");
160152
let storer = Storer::new(
161153
MockLocalFile::default(),
162154
MockDirectoryManager::default(),
163155
sa_dir.clone(),
164-
sub_agent_dir.clone(),
165156
);
166157
let agent_id = AgentID::try_from("test").unwrap();
167158
let path = storer.get_instance_id_path(&agent_id);
168-
assert_eq!(path, sub_agent_dir.join("test").join("identifiers.yaml"));
159+
assert_eq!(
160+
path,
161+
sa_dir.join("fleet-data-test").join("instance_id.yaml")
162+
);
169163

170164
let agent_control_id = AgentID::AgentControl;
171165
let path = storer.get_instance_id_path(&agent_control_id);
172-
assert_eq!(path, sa_dir.join("identifiers.yaml"));
166+
assert_eq!(
167+
path,
168+
sa_dir
169+
.join("fleet-data-agent-control")
170+
.join("instance_id.yaml")
171+
);
173172
}
174173

175174
#[test]
176175
fn test_successful_write() {
177176
// Data
178-
let (agent_id, sa_path, sub_agent_path, instance_id_path) = test_data();
177+
let (agent_id, sa_path, instance_id_path) = test_data();
179178
let mut file_rw = MockLocalFile::default();
180179
let mut dir_manager = MockDirectoryManager::default();
181180
let instance_id = InstanceID::create();
@@ -188,14 +187,14 @@ mod tests {
188187
dir_manager.should_create(instance_id_path.parent().unwrap());
189188
file_rw.should_write(&instance_id_path, expected_file(instance_id));
190189

191-
let storer = Storer::new(file_rw, dir_manager, sa_path, sub_agent_path);
190+
let storer = Storer::new(file_rw, dir_manager, sa_path);
192191
assert!(storer.set(&agent_id, &ds).is_ok());
193192
}
194193

195194
#[test]
196195
fn test_unsuccessful_write() {
197196
// Data
198-
let (agent_id, sa_path, sub_agent_path, instance_id_path) = test_data();
197+
let (agent_id, sa_path, instance_id_path) = test_data();
199198
let mut file_rw = MockLocalFile::default();
200199
let mut dir_manager = MockDirectoryManager::default();
201200
let instance_id = InstanceID::create();
@@ -208,14 +207,14 @@ mod tests {
208207
file_rw.should_not_write(&instance_id_path, expected_file(instance_id));
209208
dir_manager.should_create(instance_id_path.parent().unwrap());
210209

211-
let storer = Storer::new(file_rw, dir_manager, sa_path, sub_agent_path);
210+
let storer = Storer::new(file_rw, dir_manager, sa_path);
212211
assert!(storer.set(&agent_id, &ds).is_err());
213212
}
214213

215214
#[test]
216215
fn test_successful_read() {
217216
// Data
218-
let (agent_id, sa_path, sub_agent_path, instance_id_path) = test_data();
217+
let (agent_id, sa_path, instance_id_path) = test_data();
219218
let mut file_rw = MockLocalFile::default();
220219
let dir_manager = MockDirectoryManager::default();
221220
let instance_id = InstanceID::create();
@@ -234,15 +233,15 @@ mod tests {
234233
.once()
235234
.return_once(|_| Ok(expected_file(instance_id)));
236235

237-
let storer = Storer::new(file_rw, dir_manager, sa_path, sub_agent_path);
236+
let storer = Storer::new(file_rw, dir_manager, sa_path);
238237
let actual = storer.get(&agent_id);
239238
assert!(actual.is_ok());
240239
assert_eq!(expected, actual.unwrap());
241240
}
242241

243242
#[test]
244243
fn test_unsuccessful_read() {
245-
let (agent_id, sa_path, sub_agent_path, instance_id_path) = test_data();
244+
let (agent_id, sa_path, instance_id_path) = test_data();
246245
let mut file_rw = MockLocalFile::default();
247246
let dir_manager = MockDirectoryManager::default();
248247

@@ -254,7 +253,7 @@ mod tests {
254253
.once()
255254
.return_once(|_| Err(io::Error::other("some error message").into()));
256255

257-
let storer = Storer::new(file_rw, dir_manager, sa_path, sub_agent_path);
256+
let storer = Storer::new(file_rw, dir_manager, sa_path);
258257
let expected = storer.get(&agent_id);
259258

260259
// As said above, we are not generating the error variant here
@@ -270,12 +269,11 @@ mod tests {
270269
const HOST_ID: &str = "test-host-id";
271270
const FLEET_ID: &str = "test-fleet-id";
272271

273-
fn test_data() -> (AgentID, PathBuf, PathBuf, PathBuf) {
272+
fn test_data() -> (AgentID, PathBuf, PathBuf) {
274273
let agent_id = AgentID::try_from("test").unwrap();
275274
let sa_path = PathBuf::from("/super");
276-
let sub_agent_path = PathBuf::from("/sub");
277-
let instance_id_path = PathBuf::from("/sub/test/identifiers.yaml");
278-
(agent_id, sa_path, sub_agent_path, instance_id_path)
275+
let instance_id_path = PathBuf::from("/super/fleet-data-test/instance_id.yaml");
276+
(agent_id, sa_path, instance_id_path)
279277
}
280278

281279
fn expected_file(instance_id: InstanceID) -> String {

0 commit comments

Comments
 (0)