Skip to content

Commit 4479861

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

File tree

38 files changed

+265
-195
lines changed

38 files changed

+265
-195
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: 21 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,23 @@ 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+
/// 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+
pub const STORE_KEY_INSTANCE_ID: &StoreKey = "instance_id";
5266
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";
67+
pub const INSTANCE_ID_FILENAME: &str = "instance_id.yaml";
5668
pub const GENERATED_FOLDER_NAME: &str = "auto-generated";
5769
pub const AGENT_CONTROL_LOG_FILENAME: &str = "newrelic-agent-control.log";
5870
pub const STDOUT_LOG_PREFIX: &str = "stdout.log";
@@ -75,6 +87,10 @@ pub fn default_sub_agent_custom_capabilities() -> CustomCapabilities {
7587
}
7688
}
7789

90+
pub fn build_folder_name(agent_id: &AgentID, prefix: &str) -> String {
91+
format!("{prefix}{agent_id}")
92+
}
93+
7894
pub(crate) fn get_custom_capabilities(agent_type_id: &AgentTypeID) -> Option<CustomCapabilities> {
7995
if agent_type_id.eq(&AgentIdentity::new_agent_control_identity().agent_type_id) {
8096
// 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: 2 additions & 3 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;
33
use fs::LocalFile;
44
use fs::directory_manager::{DirectoryManagementError, DirectoryManager, DirectoryManagerFs};
55
use fs::writer_file::{FileWriter, WriteError};
@@ -51,11 +51,10 @@ 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
}
58-
path.push(VALUES_FILENAME);
57+
path.push(STORE_KEY_LOCAL_DATA_CONFIG);
5958

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

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: 26 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::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,13 +122,9 @@ 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(agent_id)
127+
.join(INSTANCE_ID_FILENAME)
139128
}
140129
}
141130

@@ -156,26 +145,32 @@ mod tests {
156145
#[test]
157146
fn basic_get_uild_path() {
158147
let sa_dir = PathBuf::from("/super");
159-
let sub_agent_dir = PathBuf::from("/sub");
160148
let storer = Storer::new(
161149
MockLocalFile::default(),
162150
MockDirectoryManager::default(),
163151
sa_dir.clone(),
164-
sub_agent_dir.clone(),
165152
);
166153
let agent_id = AgentID::try_from("test").unwrap();
167154
let path = storer.get_instance_id_path(&agent_id);
168-
assert_eq!(path, sub_agent_dir.join("test").join("identifiers.yaml"));
155+
assert_eq!(
156+
path,
157+
sa_dir.join("fleet-data-test").join("instance_id.yaml")
158+
);
169159

170160
let agent_control_id = AgentID::AgentControl;
171161
let path = storer.get_instance_id_path(&agent_control_id);
172-
assert_eq!(path, sa_dir.join("identifiers.yaml"));
162+
assert_eq!(
163+
path,
164+
sa_dir
165+
.join("fleet-data-agent-control")
166+
.join("instance_id.yaml")
167+
);
173168
}
174169

175170
#[test]
176171
fn test_successful_write() {
177172
// Data
178-
let (agent_id, sa_path, sub_agent_path, instance_id_path) = test_data();
173+
let (agent_id, sa_path, instance_id_path) = test_data();
179174
let mut file_rw = MockLocalFile::default();
180175
let mut dir_manager = MockDirectoryManager::default();
181176
let instance_id = InstanceID::create();
@@ -188,14 +183,14 @@ mod tests {
188183
dir_manager.should_create(instance_id_path.parent().unwrap());
189184
file_rw.should_write(&instance_id_path, expected_file(instance_id));
190185

191-
let storer = Storer::new(file_rw, dir_manager, sa_path, sub_agent_path);
186+
let storer = Storer::new(file_rw, dir_manager, sa_path);
192187
assert!(storer.set(&agent_id, &ds).is_ok());
193188
}
194189

195190
#[test]
196191
fn test_unsuccessful_write() {
197192
// Data
198-
let (agent_id, sa_path, sub_agent_path, instance_id_path) = test_data();
193+
let (agent_id, sa_path, instance_id_path) = test_data();
199194
let mut file_rw = MockLocalFile::default();
200195
let mut dir_manager = MockDirectoryManager::default();
201196
let instance_id = InstanceID::create();
@@ -208,14 +203,14 @@ mod tests {
208203
file_rw.should_not_write(&instance_id_path, expected_file(instance_id));
209204
dir_manager.should_create(instance_id_path.parent().unwrap());
210205

211-
let storer = Storer::new(file_rw, dir_manager, sa_path, sub_agent_path);
206+
let storer = Storer::new(file_rw, dir_manager, sa_path);
212207
assert!(storer.set(&agent_id, &ds).is_err());
213208
}
214209

215210
#[test]
216211
fn test_successful_read() {
217212
// Data
218-
let (agent_id, sa_path, sub_agent_path, instance_id_path) = test_data();
213+
let (agent_id, sa_path, instance_id_path) = test_data();
219214
let mut file_rw = MockLocalFile::default();
220215
let dir_manager = MockDirectoryManager::default();
221216
let instance_id = InstanceID::create();
@@ -234,15 +229,15 @@ mod tests {
234229
.once()
235230
.return_once(|_| Ok(expected_file(instance_id)));
236231

237-
let storer = Storer::new(file_rw, dir_manager, sa_path, sub_agent_path);
232+
let storer = Storer::new(file_rw, dir_manager, sa_path);
238233
let actual = storer.get(&agent_id);
239234
assert!(actual.is_ok());
240235
assert_eq!(expected, actual.unwrap());
241236
}
242237

243238
#[test]
244239
fn test_unsuccessful_read() {
245-
let (agent_id, sa_path, sub_agent_path, instance_id_path) = test_data();
240+
let (agent_id, sa_path, instance_id_path) = test_data();
246241
let mut file_rw = MockLocalFile::default();
247242
let dir_manager = MockDirectoryManager::default();
248243

@@ -254,7 +249,7 @@ mod tests {
254249
.once()
255250
.return_once(|_| Err(io::Error::other("some error message").into()));
256251

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

260255
// As said above, we are not generating the error variant here
@@ -270,12 +265,11 @@ mod tests {
270265
const HOST_ID: &str = "test-host-id";
271266
const FLEET_ID: &str = "test-fleet-id";
272267

273-
fn test_data() -> (AgentID, PathBuf, PathBuf, PathBuf) {
268+
fn test_data() -> (AgentID, PathBuf, PathBuf) {
274269
let agent_id = AgentID::try_from("test").unwrap();
275270
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)
271+
let instance_id_path = PathBuf::from("/super/fleet-data-test/instance_id.yaml");
272+
(agent_id, sa_path, instance_id_path)
279273
}
280274

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

0 commit comments

Comments
 (0)