Skip to content

Commit

Permalink
Merge pull request #14 from avnik/avnik/refactor-types
Browse files Browse the repository at this point in the history
Refactor RegistryEntry and how agent's location keeped
  • Loading branch information
mbssrc authored Sep 5, 2024
2 parents 65e38b9 + f3ee433 commit d3291cc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
35 changes: 28 additions & 7 deletions src/admin/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,42 @@ use std::convert::{Into, TryFrom};
use givc_common::query::*;
use givc_common::types::*;

#[derive(Debug, Clone, PartialEq)]
pub enum Placement {
// Service is a `givc-agent` and could be directly connected
Endpoint(EndpointEntry),

// Service or application managed by specified agent
Managed(String),
}

#[derive(Debug, Clone, PartialEq)]
pub struct RegistryEntry {
pub name: String,
pub parent: String,
pub r#type: UnitType,
pub status: UnitStatus,
pub endpoint: EndpointEntry,
pub placement: Placement,
pub watch: bool,
}

impl RegistryEntry {
pub fn agent(self) -> anyhow::Result<EndpointEntry> {
match self.placement {
Placement::Endpoint(endpoint) => Ok(endpoint),
Placement::Managed(by) => Err(anyhow!(
"Agent endpoint {} is managed by {}!",
self.name,
by
)),
}
}
}

#[cfg(test)]
impl RegistryEntry {
pub fn dummy(n: String) -> Self {
Self {
name: n,
parent: "bogus".to_string(),
r#type: UnitType {
vm: VmType::AppVM,
service: ServiceType::App,
Expand All @@ -35,12 +55,12 @@ impl RegistryEntry {
sub_state: "bogus".to_string(),
path: "bogus".to_string(),
},
endpoint: EndpointEntry {
placement: Placement::Endpoint(EndpointEntry {
protocol: "bogus".to_string(),
address: "127.0.0.1".to_string(),
port: 42,
tls_name: "bogus".to_string(),
},
}),
watch: true,
}
}
Expand All @@ -59,13 +79,14 @@ impl TryFrom<pb::RegistryRequest> for RegistryEntry {
.ok_or(anyhow!("endpoint missing"))
.and_then(EndpointEntry::try_from)?;
let watch = ty.service == ServiceType::Mgr;
// FIXME: We currently ignore `req.parent`, what we should do if we got both parent and endpoint
// Protocol very inconsistent here
Ok(Self {
name: req.name,
parent: req.parent,
status: status,
watch: watch,
r#type: ty,
endpoint: endpoint,
placement: Placement::Endpoint(endpoint),
})
}
}
Expand Down
23 changes: 13 additions & 10 deletions src/admin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,19 @@ impl AdminServiceImpl {
vm: VmType::Host,
service: ServiceType::Mgr,
})?;
let endpoint = host_mgr
.agent()
.with_context(|| "Resolving host agent".to_string())?;
Ok(EndpointConfig {
transport: host_mgr.endpoint.into(),
transport: endpoint.into(),
tls: self.tls_config.clone(),
})
}

pub fn agent_endpoint(&self, name: &String) -> anyhow::Result<EndpointConfig> {
let agent = self.registry.by_name(&name)?;
let endpoint = self.registry.by_name(&name)?.agent()?;
Ok(EndpointConfig {
transport: agent.endpoint.into(),
transport: endpoint.into(),
tls: self.tls_config.clone(),
})
}
Expand All @@ -94,11 +97,12 @@ impl AdminServiceImpl {
&self,
entry: &RegistryEntry,
) -> anyhow::Result<crate::types::UnitStatus> {
let transport = if entry.endpoint.address.is_empty() {
let parent = self.registry.by_name(&entry.parent)?;
parent.endpoint.clone()
} else {
entry.endpoint.clone()
let transport = match &entry.placement {
Placement::Managed(parent) => {
let parent = self.registry.by_name(parent)?;
parent.agent()? // Fail, if parent also `Managed`
}
Placement::Endpoint(endpoint) => endpoint.clone(), // FIXME: avoid clone!
};
let tls_name = transport.tls_name.clone();
let endpoint = EndpointConfig {
Expand Down Expand Up @@ -265,14 +269,13 @@ impl AdminServiceImpl {

let app_entry = RegistryEntry {
name: app_name,
parent: systemd_agent,
status: status,
watch: true,
r#type: UnitType {
vm: VmType::AppVM,
service: ServiceType::App,
},
endpoint: endpoint.transport,
placement: Placement::Managed(systemd_agent),
};
self.registry.register(app_entry);
Ok(())
Expand Down

0 comments on commit d3291cc

Please sign in to comment.