Skip to content

Commit 3ff41a0

Browse files
feat(version): align k8s and onhost approach (#989)
* feat(version): align k8s and onhost approach
1 parent ddd077e commit 3ff41a0

File tree

22 files changed

+417
-274
lines changed

22 files changed

+417
-274
lines changed

agent-control/src/agent_control/defaults.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@ use crate::opamp::remote_config::signature::SIGNATURE_CUSTOM_CAPABILITY;
22
use opamp_client::capabilities;
33
use opamp_client::opamp::proto::{AgentCapabilities, CustomCapabilities};
44
use opamp_client::operation::capabilities::Capabilities;
5-
use opamp_client::operation::settings::DescriptionValueType;
65

76
pub const AGENT_CONTROL_ID: &str = "agent-control";
87
pub const AGENT_CONTROL_TYPE: &str = "com.newrelic.agent_control";
98
pub const AGENT_CONTROL_NAMESPACE: &str = "newrelic";
109
pub const AGENT_CONTROL_VERSION: &str = env!("CARGO_PKG_VERSION");
11-
pub const NEWRELIC_INFRA_AGENT_VERSION: &str =
12-
konst::option::unwrap_or!(option_env!("NEWRELIC_INFRA_AGENT_VERSION"), "0.0.0");
13-
pub const NR_OTEL_COLLECTOR_VERSION: &str =
14-
konst::option::unwrap_or!(option_env!("NR_OTEL_COLLECTOR_VERSION"), "0.0.0");
1510

1611
// Keys identifying attributes
1712
pub const OPAMP_CHART_VERSION_ATTRIBUTE_KEY: &str = "chart.version";
@@ -75,15 +70,3 @@ pub fn default_sub_agent_custom_capabilities() -> CustomCapabilities {
7570

7671
pub const FQN_NAME_INFRA_AGENT: &str = "com.newrelic.infrastructure";
7772
pub const FQN_NAME_NRDOT: &str = "io.opentelemetry.collector";
78-
79-
pub fn sub_agent_version(agent_type: &str) -> Option<DescriptionValueType> {
80-
match agent_type {
81-
FQN_NAME_INFRA_AGENT => Some(DescriptionValueType::String(
82-
NEWRELIC_INFRA_AGENT_VERSION.to_string(),
83-
)),
84-
FQN_NAME_NRDOT => Some(DescriptionValueType::String(
85-
NR_OTEL_COLLECTOR_VERSION.to_string(),
86-
)),
87-
_ => None,
88-
}
89-
}

agent-control/src/cli/one_shot_operation.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use crate::utils::binary_metadata::binary_metadata;
2-
31
use super::Cli;
2+
use crate::utils::binary_metadata::binary_metadata;
43

54
pub enum OneShotCommand {
65
PrintVersion,
@@ -14,7 +13,10 @@ impl OneShotCommand {
1413
OneShotCommand::PrintVersion => {
1514
println!("{}", binary_metadata());
1615
#[cfg(feature = "onhost")]
17-
println!("{}", crate::utils::binary_metadata::sub_agent_versions());
16+
println!(
17+
"{}",
18+
crate::sub_agent::version::onhost::onhost_sub_agent_versions()
19+
);
1820
}
1921
OneShotCommand::PrintDebugInfo(cli) => {
2022
println!("Printing debug info");

agent-control/src/event.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
pub mod cancellation;
22
pub mod channel;
3-
43
use crate::agent_control::config::AgentTypeFQN;
5-
/// EVENTS
64
use crate::opamp::{LastErrorCode, LastErrorMessage};
75
use crate::sub_agent::health::health_checker::{Healthy, Unhealthy};
86
use crate::sub_agent::health::with_start_time::HealthWithStartTime;
7+
use crate::sub_agent::version::version_checker::AgentVersion;
98
use crate::{agent_control::config::AgentID, opamp::remote_config::RemoteConfig};
109

1110
#[derive(Clone, Debug, PartialEq)]
@@ -45,8 +44,7 @@ impl SubAgentEvent {
4544
pub enum SubAgentInternalEvent {
4645
StopRequested,
4746
AgentHealthInfo(HealthWithStartTime),
48-
#[cfg(feature = "k8s")]
49-
AgentVersionInfo(crate::sub_agent::version::k8s::version_checker::AgentVersion),
47+
AgentVersionInfo(AgentVersion),
5048
}
5149

5250
impl From<HealthWithStartTime> for SubAgentInternalEvent {

agent-control/src/k8s/garbage_collector.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ where
8787

8888
let handle = thread::spawn(move || {
8989
loop {
90-
if stop_rx.is_cancelled(interval) {
91-
break;
92-
}
9390
let _ = self
9491
.collect()
9592
.inspect_err(|err| warn!("executing garbage collection: {err}"));
93+
if stop_rx.is_cancelled(interval) {
94+
break;
95+
}
9696
}
9797
info!("k8s garbage collector stopped");
9898
});

agent-control/src/sub_agent/effective_agents_assembler.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,13 @@ impl EffectiveAgent {
8080
)
8181
}
8282

83-
#[allow(dead_code)]
8483
pub(crate) fn get_agent_id(&self) -> &AgentID {
8584
&self.agent_id
8685
}
86+
87+
pub(crate) fn get_agent_type(&self) -> &AgentTypeFQN {
88+
&self.agent_fqn
89+
}
8790
}
8891

8992
pub trait EffectiveAgentsAssembler {
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
pub mod on_health;
2-
#[cfg(feature = "k8s")]
32
pub mod on_version;
43
pub mod opamp;

agent-control/src/sub_agent/event_handler/on_version.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::sub_agent::error::SubAgentError;
2-
use crate::sub_agent::version::k8s::version_checker::AgentVersion;
2+
use crate::sub_agent::version::version_checker::AgentVersion;
33
use opamp_client::opamp::proto::{any_value, AgentDescription, AnyValue, KeyValue};
44
use opamp_client::operation::callbacks::Callbacks;
55
use opamp_client::StartedClient;

agent-control/src/sub_agent/health/health_checker.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,6 @@ pub(crate) fn spawn_health_checker<H>(
221221
H: HealthChecker + Send + 'static,
222222
{
223223
thread::spawn(move || loop {
224-
if cancel_signal.is_cancelled(interval.into()) {
225-
break;
226-
}
227224
debug!(%agent_id, "starting to check health with the configured checker");
228225

229226
let health = health_checker.check_health().unwrap_or_else(|err| {
@@ -235,6 +232,11 @@ pub(crate) fn spawn_health_checker<H>(
235232
&sub_agent_internal_publisher,
236233
SubAgentInternalEvent::AgentHealthInfo(health),
237234
);
235+
236+
// Check the cancellation signal
237+
if cancel_signal.is_cancelled(interval.into()) {
238+
break;
239+
}
238240
});
239241
}
240242

agent-control/src/sub_agent/k8s/builder.rs

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,8 @@ where
130130
.map(|(client, consumer)| (Some(client), Some(consumer)))
131131
.unwrap_or_default();
132132

133-
let supervisor_builder = SupervisorBuilderK8s::new(
134-
agent_id.clone(),
135-
sub_agent_config.clone(),
136-
self.k8s_client.clone(),
137-
self.k8s_config.clone(),
138-
);
133+
let supervisor_builder =
134+
SupervisorBuilderK8s::new(self.k8s_client.clone(), self.k8s_config.clone());
139135

140136
let remote_config_handler = RemoteConfigHandler::new(
141137
agent_id.clone(),
@@ -168,22 +164,13 @@ where
168164
}
169165

170166
pub struct SupervisorBuilderK8s {
171-
agent_id: AgentID,
172-
agent_cfg: SubAgentConfig,
173167
k8s_client: Arc<SyncK8sClient>,
174168
k8s_config: K8sConfig,
175169
}
176170

177171
impl SupervisorBuilderK8s {
178-
pub fn new(
179-
agent_id: AgentID,
180-
agent_cfg: SubAgentConfig,
181-
k8s_client: Arc<SyncK8sClient>,
182-
k8s_config: K8sConfig,
183-
) -> Self {
172+
pub fn new(k8s_client: Arc<SyncK8sClient>, k8s_config: K8sConfig) -> Self {
184173
Self {
185-
agent_id,
186-
agent_cfg,
187174
k8s_client,
188175
k8s_config,
189176
}
@@ -197,7 +184,9 @@ impl SupervisorBuilder for SupervisorBuilderK8s {
197184
&self,
198185
effective_agent: EffectiveAgent,
199186
) -> Result<Self::SupervisorStarter, SubAgentBuilderError> {
200-
debug!("Building CR supervisors {}", &self.agent_id);
187+
let agent_id = effective_agent.get_agent_id().clone();
188+
let agent_type = effective_agent.get_agent_type().clone();
189+
debug!("Building supervisors {}:{}", agent_type, agent_id);
201190

202191
let k8s_objects = effective_agent.get_k8s_config()?;
203192

@@ -221,8 +210,8 @@ impl SupervisorBuilder for SupervisorBuilderK8s {
221210

222211
// Clone the k8s_client on each build.
223212
Ok(NotStartedSupervisorK8s::new(
224-
self.agent_id.clone(),
225-
self.agent_cfg.agent_type.clone(),
213+
agent_id,
214+
agent_type,
226215
self.k8s_client.clone(),
227216
k8s_objects.clone(),
228217
))
@@ -352,8 +341,8 @@ pub mod tests {
352341
};
353342

354343
let effective_agent = EffectiveAgent::new(
355-
agent_id.clone(),
356-
sub_agent_config.agent_type.clone(),
344+
agent_id,
345+
sub_agent_config.agent_type,
357346
Runtime {
358347
deployment: Deployment {
359348
on_host: None,
@@ -362,8 +351,7 @@ pub mod tests {
362351
},
363352
);
364353

365-
let supervisor_builder =
366-
testing_supervisor_builder(agent_id.clone(), sub_agent_config.clone());
354+
let supervisor_builder = testing_supervisor_builder();
367355

368356
let result = supervisor_builder.build_supervisor(effective_agent);
369357
assert!(
@@ -381,8 +369,8 @@ pub mod tests {
381369
};
382370

383371
let effective_agent = EffectiveAgent::new(
384-
agent_id.clone(),
385-
sub_agent_config.agent_type.clone(),
372+
agent_id,
373+
sub_agent_config.agent_type,
386374
Runtime {
387375
deployment: Deployment {
388376
on_host: None,
@@ -391,8 +379,7 @@ pub mod tests {
391379
},
392380
);
393381

394-
let supervisor_builder =
395-
testing_supervisor_builder(agent_id.clone(), sub_agent_config.clone());
382+
let supervisor_builder = testing_supervisor_builder();
396383

397384
let result = supervisor_builder.build_supervisor(effective_agent);
398385
assert_matches!(
@@ -483,10 +470,7 @@ pub mod tests {
483470
(opamp_builder, instance_id_getter, hash_repository_mock)
484471
}
485472

486-
fn testing_supervisor_builder(
487-
agent_id: AgentID,
488-
sub_agent_config: SubAgentConfig,
489-
) -> SupervisorBuilderK8s {
473+
fn testing_supervisor_builder() -> SupervisorBuilderK8s {
490474
let mut mock_client = MockSyncK8sClient::default();
491475
mock_client
492476
.expect_default_namespace()
@@ -498,11 +482,6 @@ pub mod tests {
498482
cr_type_meta: K8sConfig::default().cr_type_meta,
499483
..Default::default()
500484
};
501-
SupervisorBuilderK8s::new(
502-
agent_id,
503-
sub_agent_config,
504-
Arc::new(mock_client),
505-
k8s_config,
506-
)
485+
SupervisorBuilderK8s::new(Arc::new(mock_client), k8s_config)
507486
}
508487
}

agent-control/src/sub_agent/k8s/supervisor.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use crate::sub_agent::health::k8s::health_checker::SubAgentHealthChecker;
1313
use crate::sub_agent::health::with_start_time::StartTime;
1414
use crate::sub_agent::supervisor::starter::{SupervisorStarter, SupervisorStarterError};
1515
use crate::sub_agent::supervisor::stopper::SupervisorStopper;
16-
use crate::sub_agent::version::k8s::version_checker::{spawn_version_checker, AgentVersionChecker};
16+
use crate::sub_agent::version::k8s::checkers::K8sAgentVersionChecker;
17+
use crate::sub_agent::version::version_checker::spawn_version_checker;
1718
use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;
1819
use k8s_openapi::serde_json;
1920
use kube::{api::DynamicObject, core::TypeMeta};
@@ -183,7 +184,7 @@ impl NotStartedSupervisorK8s {
183184
) -> Option<EventPublisher<()>> {
184185
let (stop_version_publisher, stop_version_consumer) = pub_sub();
185186

186-
let k8s_version_checker = AgentVersionChecker::checked_new(
187+
let k8s_version_checker = K8sAgentVersionChecker::checked_new(
187188
self.k8s_client.clone(),
188189
self.agent_id.to_string(),
189190
resources,

0 commit comments

Comments
 (0)