Skip to content

Commit cdb65f9

Browse files
committed
feat: restructure onhost folders and naming
1 parent 0e195ea commit cdb65f9

File tree

40 files changed

+435
-270
lines changed

40 files changed

+435
-270
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: 24 additions & 5 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,13 +48,26 @@ 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
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+
/// The name for on-host directories storing local, read-only configurations.
59+
pub const FOLDER_NAME_LOCAL_DATA: &str = "local-data";
60+
/// The name for on-host directories storing read-write data
61+
pub const FOLDER_NAME_FLEET_DATA: &str = "fleet-data";
5162
pub const AGENT_CONTROL_CONFIG_FILENAME: &str = "config.yaml";
63+
pub const STORE_KEY_LOCAL_DATA_CONFIG: &StoreKey = "local_config";
64+
pub const STORE_KEY_OPAMP_DATA_CONFIG: &StoreKey = "remote_config";
65+
66+
pub const STORE_KEY_LOCAL_DATA_CONFIG_YAML: &StoreKey = "local_config.yaml";
67+
pub const STORE_KEY_OPAMP_DATA_CONFIG_YAML: &StoreKey = "remote_config.yaml";
68+
pub const STORE_KEY_INSTANCE_ID: &StoreKey = "instance_id";
5269
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";
55-
pub const VALUES_FILENAME: &str = "values.yaml";
70+
pub const INSTANCE_ID_FILENAME: &str = "instance_id.yaml";
5671
pub const GENERATED_FOLDER_NAME: &str = "auto-generated";
5772
pub const AGENT_CONTROL_LOG_FILENAME: &str = "newrelic-agent-control.log";
5873
pub const STDOUT_LOG_PREFIX: &str = "stdout.log";
@@ -75,6 +90,10 @@ pub fn default_sub_agent_custom_capabilities() -> CustomCapabilities {
7590
}
7691
}
7792

93+
pub fn build_folder_name(agent_id: &AgentID, prefix: &str) -> String {
94+
format!("{prefix}{agent_id}")
95+
}
96+
7897
pub(crate) fn get_custom_capabilities(agent_type_id: &AgentTypeID) -> Option<CustomCapabilities> {
7998
if agent_type_id.eq(&AgentIdentity::new_agent_control_identity().agent_type_id) {
8099
// 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
@@ -4,7 +4,7 @@ use crate::agent_control::config_repository::store::AgentControlConfigStore;
44
use crate::agent_control::config_validator::RegistryDynamicConfigValidator;
55
use crate::agent_control::defaults::{
66
AGENT_CONTROL_VERSION, FLEET_ID_ATTRIBUTE_KEY, HOST_ID_ATTRIBUTE_KEY, HOST_NAME_ATTRIBUTE_KEY,
7-
OPAMP_AGENT_VERSION_ATTRIBUTE_KEY, SUB_AGENT_DIR,
7+
OPAMP_AGENT_VERSION_ATTRIBUTE_KEY,
88
};
99
use crate::agent_control::http_server::runner::Runner;
1010
use crate::agent_control::resource_cleaner::no_op::NoOpResourceCleaner;
@@ -100,7 +100,6 @@ impl AgentControlRunner {
100100
LocalFile,
101101
DirectoryManagerFs,
102102
self.base_paths.remote_dir.clone(),
103-
self.base_paths.remote_dir.join(SUB_AGENT_DIR),
104103
);
105104
let instance_id_getter =
106105
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: 2 additions & 6 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::STORE_KEY_LOCAL_DATA_CONFIG_YAML;
33
use fs::LocalFile;
44
use fs::directory_manager::{DirectoryManagementError, DirectoryManager, DirectoryManagerFs};
55
use fs::writer_file::{FileWriter, WriteError};
@@ -51,11 +51,7 @@ where
5151
if !path.exists() {
5252
self.create_directory(&path)?;
5353
}
54-
path.push(VALUES_DIR);
55-
if !path.exists() {
56-
self.create_directory(&path)?;
57-
}
58-
path.push(VALUES_FILENAME);
54+
path.push(STORE_KEY_LOCAL_DATA_CONFIG_YAML);
5955

6056
debug!("writing to file {:?}", path.as_path());
6157

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: 31 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::{FOLDER_NAME_FLEET_DATA, 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
}
@@ -129,19 +122,17 @@ where
129122
}
130123

131124
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-
}
125+
self.agent_control_remote_dir
126+
.join(FOLDER_NAME_FLEET_DATA)
127+
.join(agent_id)
128+
.join(INSTANCE_ID_FILENAME)
139129
}
140130
}
141131

142132
#[cfg(test)]
143133
mod tests {
144134
use crate::agent_control::agent_id::AgentID;
135+
use crate::agent_control::defaults::{FOLDER_NAME_FLEET_DATA, INSTANCE_ID_FILENAME};
145136
use crate::opamp::instance_id::InstanceID;
146137
use crate::opamp::instance_id::getter::DataStored;
147138
use crate::opamp::instance_id::on_host::getter::Identifiers;
@@ -156,26 +147,35 @@ mod tests {
156147
#[test]
157148
fn basic_get_uild_path() {
158149
let sa_dir = PathBuf::from("/super");
159-
let sub_agent_dir = PathBuf::from("/sub");
160150
let storer = Storer::new(
161151
MockLocalFile::default(),
162152
MockDirectoryManager::default(),
163153
sa_dir.clone(),
164-
sub_agent_dir.clone(),
165154
);
166155
let agent_id = AgentID::try_from("test").unwrap();
167156
let path = storer.get_instance_id_path(&agent_id);
168-
assert_eq!(path, sub_agent_dir.join("test").join("identifiers.yaml"));
157+
assert_eq!(
158+
path,
159+
sa_dir
160+
.join(FOLDER_NAME_FLEET_DATA)
161+
.join("test")
162+
.join(INSTANCE_ID_FILENAME)
163+
);
169164

170165
let agent_control_id = AgentID::AgentControl;
171166
let path = storer.get_instance_id_path(&agent_control_id);
172-
assert_eq!(path, sa_dir.join("identifiers.yaml"));
167+
assert_eq!(
168+
path,
169+
sa_dir
170+
.join("fleet-data/agent-control")
171+
.join(INSTANCE_ID_FILENAME)
172+
);
173173
}
174174

175175
#[test]
176176
fn test_successful_write() {
177177
// Data
178-
let (agent_id, sa_path, sub_agent_path, instance_id_path) = test_data();
178+
let (agent_id, sa_path, instance_id_path) = test_data();
179179
let mut file_rw = MockLocalFile::default();
180180
let mut dir_manager = MockDirectoryManager::default();
181181
let instance_id = InstanceID::create();
@@ -188,14 +188,14 @@ mod tests {
188188
dir_manager.should_create(instance_id_path.parent().unwrap());
189189
file_rw.should_write(&instance_id_path, expected_file(instance_id));
190190

191-
let storer = Storer::new(file_rw, dir_manager, sa_path, sub_agent_path);
191+
let storer = Storer::new(file_rw, dir_manager, sa_path);
192192
assert!(storer.set(&agent_id, &ds).is_ok());
193193
}
194194

195195
#[test]
196196
fn test_unsuccessful_write() {
197197
// Data
198-
let (agent_id, sa_path, sub_agent_path, instance_id_path) = test_data();
198+
let (agent_id, sa_path, instance_id_path) = test_data();
199199
let mut file_rw = MockLocalFile::default();
200200
let mut dir_manager = MockDirectoryManager::default();
201201
let instance_id = InstanceID::create();
@@ -208,14 +208,14 @@ mod tests {
208208
file_rw.should_not_write(&instance_id_path, expected_file(instance_id));
209209
dir_manager.should_create(instance_id_path.parent().unwrap());
210210

211-
let storer = Storer::new(file_rw, dir_manager, sa_path, sub_agent_path);
211+
let storer = Storer::new(file_rw, dir_manager, sa_path);
212212
assert!(storer.set(&agent_id, &ds).is_err());
213213
}
214214

215215
#[test]
216216
fn test_successful_read() {
217217
// Data
218-
let (agent_id, sa_path, sub_agent_path, instance_id_path) = test_data();
218+
let (agent_id, sa_path, instance_id_path) = test_data();
219219
let mut file_rw = MockLocalFile::default();
220220
let dir_manager = MockDirectoryManager::default();
221221
let instance_id = InstanceID::create();
@@ -234,15 +234,15 @@ mod tests {
234234
.once()
235235
.return_once(|_| Ok(expected_file(instance_id)));
236236

237-
let storer = Storer::new(file_rw, dir_manager, sa_path, sub_agent_path);
237+
let storer = Storer::new(file_rw, dir_manager, sa_path);
238238
let actual = storer.get(&agent_id);
239239
assert!(actual.is_ok());
240240
assert_eq!(expected, actual.unwrap());
241241
}
242242

243243
#[test]
244244
fn test_unsuccessful_read() {
245-
let (agent_id, sa_path, sub_agent_path, instance_id_path) = test_data();
245+
let (agent_id, sa_path, instance_id_path) = test_data();
246246
let mut file_rw = MockLocalFile::default();
247247
let dir_manager = MockDirectoryManager::default();
248248

@@ -254,7 +254,7 @@ mod tests {
254254
.once()
255255
.return_once(|_| Err(io::Error::other("some error message").into()));
256256

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

260260
// As said above, we are not generating the error variant here
@@ -270,12 +270,11 @@ mod tests {
270270
const HOST_ID: &str = "test-host-id";
271271
const FLEET_ID: &str = "test-fleet-id";
272272

273-
fn test_data() -> (AgentID, PathBuf, PathBuf, PathBuf) {
273+
fn test_data() -> (AgentID, PathBuf, PathBuf) {
274274
let agent_id = AgentID::try_from("test").unwrap();
275275
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)
276+
let instance_id_path = PathBuf::from("/super/fleet-data/test/instance_id.yaml");
277+
(agent_id, sa_path, instance_id_path)
279278
}
280279

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

0 commit comments

Comments
 (0)