From f3ee43324d4c339fb8a086b5ca3bfedece411221 Mon Sep 17 00:00:00 2001 From: "Alexander V. Nikolaev" Date: Wed, 4 Sep 2024 16:54:23 +0300 Subject: [PATCH] Refactor RegistryEntry and how agent's location keeped Reflect our cases in inner type system, partially remove magic of empty address/parent values. Signed-off-by: Alexander V. Nikolaev --- src/admin/entry.rs | 35 ++++++++++++++++++++++++++++------- src/admin/server.rs | 23 +++++++++++++---------- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/src/admin/entry.rs b/src/admin/entry.rs index a6bc3b4..e8dcb70 100644 --- a/src/admin/entry.rs +++ b/src/admin/entry.rs @@ -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 { + 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, @@ -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, } } @@ -59,13 +79,14 @@ impl TryFrom 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), }) } } diff --git a/src/admin/server.rs b/src/admin/server.rs index 94fd39a..6ccd5ee 100644 --- a/src/admin/server.rs +++ b/src/admin/server.rs @@ -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 { - 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(), }) } @@ -94,11 +97,12 @@ impl AdminServiceImpl { &self, entry: &RegistryEntry, ) -> anyhow::Result { - 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 { @@ -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(())