Skip to content

Commit f732edc

Browse files
committed
dix lazystatic to funcstion to build the ocnstants
1 parent c951b68 commit f732edc

File tree

18 files changed

+90
-76
lines changed

18 files changed

+90
-76
lines changed

agent-control/src/agent_control/defaults.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use crate::agent_type::agent_type_id::AgentTypeID;
22
use crate::k8s::store::StoreKey;
33
use crate::opamp::remote_config::signature::SIGNATURE_CUSTOM_CAPABILITY;
44
use crate::sub_agent::identity::AgentIdentity;
5-
use lazy_static::lazy_static;
65
use opamp_client::capabilities;
76
use opamp_client::opamp::proto::{AgentCapabilities, CustomCapabilities};
87
use opamp_client::operation::capabilities::Capabilities;
8+
use std::sync::OnceLock;
99

1010
pub const AGENT_CONTROL_ID: &str = "agent-control";
1111

@@ -55,19 +55,33 @@ pub const FOLDER_NAME_FLEET_DATA: &str = "fleet-data";
5555
pub const STORE_KEY_LOCAL_DATA_CONFIG: &StoreKey = "local_config";
5656
pub const STORE_KEY_OPAMP_DATA_CONFIG: &StoreKey = "remote_config";
5757

58-
lazy_static! {
59-
/// The prefixes for the ConfigMap name
60-
/// The cm having CM_NAME_LOCAL_DATA_PREFIX stores all the config that are "local",
61-
/// the SA treats those CM as read-only.
62-
pub static ref CM_NAME_LOCAL_DATA_PREFIX: String = format!("{}-", FOLDER_NAME_LOCAL_DATA);
63-
/// The cm having CM_NAME_OPAMP_DATA_PREFIX as prefix stores all the data related with opamp:
64-
/// Instance IDs, hashes, and remote configs. The Sa reads and writes those CMs.
65-
pub static ref CM_NAME_OPAMP_DATA_PREFIX: String = format!("{}-", FOLDER_NAME_FLEET_DATA);
66-
pub static ref STORE_KEY_LOCAL_DATA_CONFIG_YAML: &'static StoreKey =
67-
format!("{}.yaml", STORE_KEY_LOCAL_DATA_CONFIG).leak();
68-
69-
pub static ref STORE_KEY_OPAMP_DATA_CONFIG_YAML: &'static StoreKey =
70-
format!("{}.yaml", STORE_KEY_OPAMP_DATA_CONFIG).leak();
58+
/// The prefixes for the ConfigMap name
59+
/// The cm having cm_name_local_data_prefix stores all the config that are "local",
60+
/// the SA treats those CM as read-only.
61+
pub fn cm_name_local_data_prefix() -> &'static String {
62+
static LOCK: OnceLock<String> = OnceLock::new();
63+
LOCK.get_or_init(|| format!("{}-", FOLDER_NAME_LOCAL_DATA))
64+
}
65+
66+
/// The cm having cm_name_opamp_data_prefix as prefix stores all the data related with opamp:
67+
/// Instance IDs, hashes, and remote configs. The Sa reads and writes those CMs.
68+
pub fn cm_name_opamp_data_prefix() -> &'static String {
69+
static LOCK: OnceLock<String> = OnceLock::new();
70+
LOCK.get_or_init(|| format!("{}-", FOLDER_NAME_FLEET_DATA))
71+
}
72+
73+
/// The full key name (including .yaml) for the local config file.
74+
/// (Derived from `STORE_KEY_LOCAL_DATA_CONFIG`)
75+
pub fn store_key_local_data_config_yaml() -> &'static StoreKey {
76+
static LOCK: OnceLock<&'static StoreKey> = OnceLock::new();
77+
LOCK.get_or_init(|| format!("{}.yaml", STORE_KEY_LOCAL_DATA_CONFIG).leak())
78+
}
79+
80+
/// The full key name (including .yaml) for the remote OpAMP config file.
81+
/// (Derived from `STORE_KEY_OPAMP_DATA_CONFIG`)
82+
pub fn store_key_opamp_data_config_yaml() -> &'static StoreKey {
83+
static LOCK: OnceLock<&'static StoreKey> = OnceLock::new();
84+
LOCK.get_or_init(|| format!("{}.yaml", STORE_KEY_OPAMP_DATA_CONFIG).leak())
7185
}
7286
pub const NO_CONFIG: &str = "";
7387
pub const STORE_KEY_INSTANCE_ID: &StoreKey = "instance_id";

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

Lines changed: 2 additions & 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::STORE_KEY_OPAMP_DATA_CONFIG_YAML;
2+
use crate::agent_control::defaults::store_key_opamp_data_config_yaml;
33
use fs::LocalFile;
44
use fs::directory_manager::{DirectoryManagementError, DirectoryManager, DirectoryManagerFs};
55
use fs::writer_file::{FileWriter, WriteError};
@@ -51,7 +51,7 @@ where
5151
if !path.exists() {
5252
self.create_directory(&path)?;
5353
}
54-
path.push(*STORE_KEY_OPAMP_DATA_CONFIG_YAML);
54+
path.push(store_key_opamp_data_config_yaml());
5555

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

agent-control/src/k8s/store.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ 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};
6+
use crate::agent_control::defaults::{cm_name_local_data_prefix, cm_name_opamp_data_prefix};
77
use std::sync::{Arc, RwLock};
88

99
/// The key used to identify the data in the Store.
@@ -33,7 +33,7 @@ impl K8sStore {
3333
where
3434
T: serde::de::DeserializeOwned,
3535
{
36-
self.get(agent_id, &CM_NAME_OPAMP_DATA_PREFIX, key)
36+
self.get(agent_id, cm_name_opamp_data_prefix(), key)
3737
}
3838

3939
/// get_local_data is used to get data from CMs storing local configurations. I.e. all the CMs
@@ -42,7 +42,7 @@ impl K8sStore {
4242
where
4343
T: serde::de::DeserializeOwned,
4444
{
45-
self.get(agent_id, &CM_NAME_LOCAL_DATA_PREFIX, key)
45+
self.get(agent_id, cm_name_local_data_prefix(), key)
4646
}
4747

4848
/// Retrieves data from an Agent store.
@@ -80,7 +80,7 @@ impl K8sStore {
8080
let _write_guard = self.rw_lock.write().unwrap();
8181

8282
let data_as_string = serde_yaml::to_string(data)?;
83-
let configmap_name = K8sStore::build_cm_name(agent_id, &CM_NAME_OPAMP_DATA_PREFIX);
83+
let configmap_name = K8sStore::build_cm_name(agent_id, cm_name_opamp_data_prefix());
8484
self.k8s_client.set_configmap_key(
8585
&configmap_name,
8686
self.namespace.as_str(),
@@ -95,7 +95,7 @@ impl K8sStore {
9595
#[allow(clippy::readonly_write_lock)]
9696
let _write_guard = self.rw_lock.write().unwrap();
9797

98-
let configmap_name = K8sStore::build_cm_name(agent_id, &CM_NAME_OPAMP_DATA_PREFIX);
98+
let configmap_name = K8sStore::build_cm_name(agent_id, cm_name_opamp_data_prefix());
9999
self.k8s_client
100100
.delete_configmap_key(&configmap_name, self.namespace.as_str(), key)
101101
}
@@ -107,9 +107,9 @@ impl K8sStore {
107107

108108
#[cfg(test)]
109109
pub mod tests {
110-
use super::{CM_NAME_LOCAL_DATA_PREFIX, CM_NAME_OPAMP_DATA_PREFIX};
111110
use super::{K8sStore, StoreKey};
112111
use crate::agent_control::agent_id::AgentID;
112+
use crate::agent_control::defaults::{cm_name_local_data_prefix, cm_name_opamp_data_prefix};
113113
use crate::k8s::client::MockSyncK8sClient;
114114
use crate::k8s::error::K8sError;
115115
use crate::k8s::labels::Labels;
@@ -141,7 +141,7 @@ pub mod tests {
141141
.with(
142142
predicate::eq(K8sStore::build_cm_name(
143143
&agent_id,
144-
&CM_NAME_OPAMP_DATA_PREFIX,
144+
cm_name_opamp_data_prefix(),
145145
)),
146146
predicate::eq(TEST_NAMESPACE),
147147
predicate::eq(Labels::new(&AgentID::try_from(AGENT_NAME).unwrap()).get()),
@@ -155,7 +155,7 @@ pub mod tests {
155155
.with(
156156
predicate::eq(K8sStore::build_cm_name(
157157
&agent_id,
158-
&CM_NAME_OPAMP_DATA_PREFIX,
158+
cm_name_opamp_data_prefix(),
159159
)),
160160
predicate::eq(TEST_NAMESPACE),
161161
predicate::eq(STORE_KEY_TEST),
@@ -186,7 +186,7 @@ pub mod tests {
186186
.with(
187187
predicate::eq(K8sStore::build_cm_name(
188188
agent_id,
189-
&CM_NAME_OPAMP_DATA_PREFIX,
189+
cm_name_opamp_data_prefix(),
190190
)),
191191
predicate::eq(TEST_NAMESPACE),
192192
predicate::eq(STORE_KEY_TEST),
@@ -203,7 +203,7 @@ pub mod tests {
203203
.with(
204204
predicate::eq(K8sStore::build_cm_name(
205205
agent_id,
206-
&CM_NAME_LOCAL_DATA_PREFIX,
206+
cm_name_local_data_prefix(),
207207
)),
208208
predicate::eq(TEST_NAMESPACE),
209209
predicate::always(),

agent-control/src/values/file.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::agent_control::agent_id::AgentID;
22
use crate::agent_control::defaults::{
3-
FOLDER_NAME_FLEET_DATA, FOLDER_NAME_LOCAL_DATA, STORE_KEY_LOCAL_DATA_CONFIG_YAML,
4-
STORE_KEY_OPAMP_DATA_CONFIG_YAML,
3+
FOLDER_NAME_FLEET_DATA, FOLDER_NAME_LOCAL_DATA, store_key_local_data_config_yaml,
4+
store_key_opamp_data_config_yaml,
55
};
66
use crate::opamp::remote_config::hash::ConfigState;
77
use crate::values::config::{Config, RemoteConfig};
@@ -75,14 +75,14 @@ where
7575
self.local_conf_path
7676
.join(FOLDER_NAME_LOCAL_DATA)
7777
.join(agent_id)
78-
.join(*STORE_KEY_LOCAL_DATA_CONFIG_YAML)
78+
.join(store_key_local_data_config_yaml())
7979
}
8080

8181
pub fn get_remote_values_file_path(&self, agent_id: &AgentID) -> PathBuf {
8282
self.remote_conf_path
8383
.join(FOLDER_NAME_FLEET_DATA)
8484
.join(agent_id)
85-
.join(*STORE_KEY_OPAMP_DATA_CONFIG_YAML)
85+
.join(store_key_opamp_data_config_yaml())
8686
}
8787

8888
// Load a file contents only if the file is present.

agent-control/tests/k8s/store.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use newrelic_agent_control::agent_control::agent_id::AgentID;
77
use newrelic_agent_control::agent_control::config_repository::repository::AgentControlDynamicConfigRepository;
88
use newrelic_agent_control::agent_control::config_repository::store::AgentControlConfigStore;
99
use newrelic_agent_control::agent_control::defaults::{
10-
CM_NAME_LOCAL_DATA_PREFIX, default_capabilities,
10+
STORE_KEY_INSTANCE_ID, STORE_KEY_OPAMP_DATA_CONFIG,
1111
};
1212
use newrelic_agent_control::agent_control::defaults::{
13-
CM_NAME_OPAMP_DATA_PREFIX, STORE_KEY_INSTANCE_ID, STORE_KEY_OPAMP_DATA_CONFIG,
13+
cm_name_local_data_prefix, cm_name_opamp_data_prefix, default_capabilities,
1414
};
1515
use newrelic_agent_control::k8s::client::SyncK8sClient;
1616
use newrelic_agent_control::k8s::labels::Labels;
@@ -252,7 +252,7 @@ agents:
252252
block_on(create_config_map(
253253
test.client.clone(),
254254
test_ns.as_str(),
255-
K8sStore::build_cm_name(&AgentID::AgentControl, &CM_NAME_LOCAL_DATA_PREFIX).as_str(),
255+
K8sStore::build_cm_name(&AgentID::AgentControl, cm_name_local_data_prefix()).as_str(),
256256
agents_cfg_local,
257257
));
258258

@@ -340,7 +340,7 @@ fn k8s_multiple_store_entries() {
340340
}
341341

342342
fn assert_agent_cm(cm_client: &Api<ConfigMap>, agent_id: &AgentID, store_key: &StoreKey) {
343-
let cm_name = format!("{}{}", *CM_NAME_OPAMP_DATA_PREFIX, agent_id);
343+
let cm_name = format!("{}{}", cm_name_opamp_data_prefix(), agent_id);
344344
let cm = block_on(cm_client.get(&cm_name));
345345
assert!(cm.is_ok());
346346
let cm_un = cm.unwrap();

agent-control/tests/k8s/tools/agent_control.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use kube::{
1111
api::{Api, DeleteParams, PostParams},
1212
};
1313
use newrelic_agent_control::agent_control::defaults::{
14-
AGENT_CONTROL_ID, CM_NAME_LOCAL_DATA_PREFIX, FOLDER_NAME_LOCAL_DATA,
15-
STORE_KEY_LOCAL_DATA_CONFIG, STORE_KEY_LOCAL_DATA_CONFIG_YAML,
14+
AGENT_CONTROL_ID, FOLDER_NAME_LOCAL_DATA, STORE_KEY_LOCAL_DATA_CONFIG,
15+
cm_name_local_data_prefix, store_key_local_data_config_yaml,
1616
};
1717
use newrelic_agent_control::agent_control::{agent_id::AgentID, run::Environment};
1818
use newrelic_agent_control::{agent_control::run::BasePaths, k8s::store::K8sStore};
@@ -161,14 +161,14 @@ pub fn create_local_agent_control_config(
161161
block_on(create_config_map(
162162
client,
163163
ac_ns,
164-
K8sStore::build_cm_name(&AgentID::AgentControl, &CM_NAME_LOCAL_DATA_PREFIX).as_str(),
164+
K8sStore::build_cm_name(&AgentID::AgentControl, cm_name_local_data_prefix()).as_str(),
165165
content.clone(),
166166
));
167167

168168
let local = tmp_dir.join(FOLDER_NAME_LOCAL_DATA).join(AGENT_CONTROL_ID);
169169
std::fs::create_dir_all(&local).unwrap();
170170

171-
File::create(local.join(*STORE_KEY_LOCAL_DATA_CONFIG_YAML))
171+
File::create(local.join(store_key_local_data_config_yaml()))
172172
.unwrap()
173173
.write_all(content.as_bytes())
174174
.unwrap();

agent-control/tests/k8s/tools/instance_id.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use k8s_openapi::api::core::v1::ConfigMap;
44
use kube::{Api, Client};
55
use newrelic_agent_control::agent_control::agent_id::AgentID;
66
use newrelic_agent_control::agent_control::defaults::{
7-
CM_NAME_OPAMP_DATA_PREFIX, STORE_KEY_INSTANCE_ID,
7+
STORE_KEY_INSTANCE_ID, cm_name_opamp_data_prefix,
88
};
99
use newrelic_agent_control::k8s::store::K8sStore;
1010
use newrelic_agent_control::opamp::instance_id::InstanceID;
@@ -15,7 +15,7 @@ use std::time::Duration;
1515
pub fn get_instance_id(k8s_client: Client, namespace: &str, agent_id: &AgentID) -> InstanceID {
1616
let cm_client: Api<ConfigMap> = Api::<ConfigMap>::namespaced(k8s_client, namespace);
1717

18-
let cm_name = K8sStore::build_cm_name(agent_id, &CM_NAME_OPAMP_DATA_PREFIX);
18+
let cm_name = K8sStore::build_cm_name(agent_id, cm_name_opamp_data_prefix());
1919

2020
let mut id = InstanceID::create();
2121

agent-control/tests/on_host/cli.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::common::retry::retry;
22
use assert_cmd::Command;
33
use newrelic_agent_control::agent_control::defaults::{
4-
AGENT_CONTROL_ID, FOLDER_NAME_LOCAL_DATA, STORE_KEY_LOCAL_DATA_CONFIG_YAML,
4+
AGENT_CONTROL_ID, FOLDER_NAME_LOCAL_DATA, store_key_local_data_config_yaml,
55
};
66
use predicates::prelude::predicate;
77
use std::error::Error;
@@ -41,7 +41,7 @@ fn print_debug_info() -> Result<(), Box<dyn std::error::Error>> {
4141
&dir.path()
4242
.join(FOLDER_NAME_LOCAL_DATA)
4343
.join(AGENT_CONTROL_ID),
44-
*STORE_KEY_LOCAL_DATA_CONFIG_YAML,
44+
store_key_local_data_config_yaml(),
4545
r"agents: {}",
4646
)?;
4747
let mut cmd = Command::cargo_bin("newrelic-agent-control")?;
@@ -60,7 +60,7 @@ fn does_not_run_if_no_root() -> Result<(), Box<dyn std::error::Error>> {
6060
&dir.path()
6161
.join(FOLDER_NAME_LOCAL_DATA)
6262
.join(AGENT_CONTROL_ID),
63-
*STORE_KEY_LOCAL_DATA_CONFIG_YAML,
63+
store_key_local_data_config_yaml(),
6464
r"agents: {}",
6565
)?;
6666
let mut cmd = Command::cargo_bin("newrelic-agent-control")?;
@@ -83,7 +83,7 @@ fn basic_startup() -> Result<(), Box<dyn std::error::Error>> {
8383
&dir.path()
8484
.join(FOLDER_NAME_LOCAL_DATA)
8585
.join(AGENT_CONTROL_ID),
86-
*STORE_KEY_LOCAL_DATA_CONFIG_YAML,
86+
store_key_local_data_config_yaml(),
8787
r#"
8888
agents: {}
8989
server:
@@ -133,7 +133,7 @@ fn custom_logging_format() -> Result<(), Box<dyn std::error::Error>> {
133133
&dir.path()
134134
.join(FOLDER_NAME_LOCAL_DATA)
135135
.join(AGENT_CONTROL_ID),
136-
*STORE_KEY_LOCAL_DATA_CONFIG_YAML,
136+
store_key_local_data_config_yaml(),
137137
r#"
138138
agents: {}
139139
log:
@@ -196,7 +196,7 @@ fn custom_directory_overrides_as_root() -> Result<(), Box<dyn std::error::Error>
196196
&dir.path()
197197
.join(FOLDER_NAME_LOCAL_DATA)
198198
.join(AGENT_CONTROL_ID),
199-
*STORE_KEY_LOCAL_DATA_CONFIG_YAML,
199+
store_key_local_data_config_yaml(),
200200
format!(
201201
r#"
202202
fleet_control:

agent-control/tests/on_host/config_repository.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use fs::directory_manager::{DirectoryManager, DirectoryManagerFs};
22
use newrelic_agent_control::agent_control::agent_id::AgentID;
33
use newrelic_agent_control::agent_control::defaults::{
4-
FOLDER_NAME_FLEET_DATA, STORE_KEY_OPAMP_DATA_CONFIG_YAML,
4+
FOLDER_NAME_FLEET_DATA, store_key_opamp_data_config_yaml,
55
};
66
use newrelic_agent_control::opamp::remote_config::hash::{ConfigState, Hash};
77
use newrelic_agent_control::values::config::RemoteConfig;
@@ -47,7 +47,7 @@ fn test_store_remote_no_mocks() {
4747
remote_dir
4848
.join(FOLDER_NAME_FLEET_DATA)
4949
.join(agent_id)
50-
.join(*STORE_KEY_OPAMP_DATA_CONFIG_YAML)
50+
.join(store_key_opamp_data_config_yaml())
5151
)
5252
.expect("Failed to read the config file")
5353
);

agent-control/tests/on_host/id.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use assert_cmd::Command;
77
use httpmock::Method::GET;
88
use httpmock::MockServer;
99
use newrelic_agent_control::agent_control::defaults::{
10-
AGENT_CONTROL_ID, FOLDER_NAME_LOCAL_DATA, NO_CONFIG, STORE_KEY_LOCAL_DATA_CONFIG_YAML,
10+
AGENT_CONTROL_ID, FOLDER_NAME_LOCAL_DATA, NO_CONFIG, store_key_local_data_config_yaml,
1111
};
1212
use newrelic_agent_control::agent_control::run::BasePaths;
1313
use newrelic_agent_control::http::client::HttpClient;
@@ -202,7 +202,7 @@ deployment:
202202
.path()
203203
.join(FOLDER_NAME_LOCAL_DATA)
204204
.join(AGENT_CONTROL_ID)
205-
.join(*STORE_KEY_LOCAL_DATA_CONFIG_YAML);
205+
.join(store_key_local_data_config_yaml());
206206
create_file(
207207
r#"
208208
host_id: fixed-host-id

0 commit comments

Comments
 (0)