Skip to content

Commit

Permalink
Keep vm/agent names in registry
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander V. Nikolaev <[email protected]>
  • Loading branch information
avnik committed Nov 15, 2024
1 parent f45e6f8 commit 799930e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 19 deletions.
48 changes: 37 additions & 11 deletions src/admin/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ use givc_common::types::*;
#[derive(Debug, Clone, PartialEq)]
pub enum Placement {
// Service is a `givc-agent` and could be directly connected
Endpoint(EndpointEntry),
Endpoint { endpoint: EndpointEntry, vm: String },

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

// Running on host
Host,
}

#[derive(Debug, Clone, PartialEq)]
Expand All @@ -26,14 +29,31 @@ pub struct RegistryEntry {
}

impl RegistryEntry {
pub fn agent_name(&self) -> Option<&str> {
match &self.placement {
Placement::Endpoint { .. } => Some(&self.name),
Placement::Managed { by, .. } => Some(by),
Placement::Host => None,
}
}

pub fn vm_name(&self) -> Option<&str> {
match &self.placement {
Placement::Endpoint { vm, .. } => Some(vm),
Placement::Managed { vm, .. } => Some(vm),
Placement::Host => None,
}
}

pub fn agent(&self) -> anyhow::Result<&EndpointEntry> {
match &self.placement {
Placement::Endpoint(endpoint) => Ok(endpoint),
Placement::Managed(by) => Err(anyhow!(
Placement::Endpoint { endpoint, .. } => Ok(endpoint),
Placement::Managed { by, .. } => Err(anyhow!(
"Agent endpoint {} is managed by {}!",
self.name,
by
)),
Placement::Host => Err(anyhow!("Its a host!")),
}
}
}
Expand All @@ -57,13 +77,16 @@ impl RegistryEntry {
path: "bogus".to_string(),
freezer_state: "bogus".to_string(),
},
placement: Placement::Endpoint(EndpointEntry {
address: EndpointAddress::Tcp {
addr: "127.0.0.1".to_string(),
port: 42,
placement: Placement::Endpoint {
endpoint: EndpointEntry {
address: EndpointAddress::Tcp {
addr: "127.0.0.1".to_string(),
port: 42,
},
tls_name: "bogus".to_string(),
},
tls_name: "bogus".to_string(),
}),
vm: "bogus".into(),
},
watch: true,
}
}
Expand All @@ -89,7 +112,10 @@ impl TryFrom<pb::RegistryRequest> for RegistryEntry {
status,
watch,
r#type: ty,
placement: Placement::Endpoint(endpoint),
placement: Placement::Endpoint {
endpoint,
vm: "bogus".into(),
},
})
}
}
Expand Down
19 changes: 14 additions & 5 deletions src/admin/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ impl Registry {
Some(entry) => {
let cascade: Vec<String> = state
.values()
.filter_map(|re| match &re.placement {
Placement::Managed(within) if within == name => Some(re.name.clone()),
_ => None,
.filter_map(|re| {
if re.agent_name() == Some(name) || re.vm_name() == Some(name) {
Some(re.name.clone())
} else {
None
}
})
.collect();
for each in cascade {
Expand Down Expand Up @@ -204,11 +207,17 @@ mod tests {
let r = Registry::new();
let foo = RegistryEntry::dummy("foo".to_string());
let bar = RegistryEntry {
placement: Placement::Managed("foo".into()),
placement: Placement::Managed {
by: "foo".into(),
vm: "foo-vm".into(),
},
..RegistryEntry::dummy("bar".to_string())
};
let baz = RegistryEntry {
placement: Placement::Managed("foo".into()),
placement: Placement::Managed {
by: "foo".into(),
vm: "foo-vm".into(),
},
..RegistryEntry::dummy("baz".to_string())
};

Expand Down
10 changes: 7 additions & 3 deletions src/admin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,15 @@ impl AdminServiceImpl {

pub fn endpoint(&self, entry: &RegistryEntry) -> anyhow::Result<EndpointConfig> {
let transport = match &entry.placement {
Placement::Managed(parent) => {
Placement::Managed { by: parent, .. } => {
let parent = self.registry.by_name(parent)?;
parent
.agent()
.with_context(|| "When get_remote_status()")?
.to_owned() // Fail, if parent also `Managed`
}
Placement::Endpoint(endpoint) => endpoint.clone(), // FIXME: avoid clone!
Placement::Endpoint { endpoint, .. } => endpoint.clone(), // FIXME: avoid clone!
Placement::Host => bail!("endpoint() called for Host"), // impossible, FIXME: should never happens atm
};
let tls_name = transport.tls_name.clone();
Ok(EndpointConfig {
Expand Down Expand Up @@ -332,7 +333,10 @@ impl AdminServiceImpl {
vm: VmType::AppVM,
service: ServiceType::App,
},
placement: Placement::Managed(systemd_agent),
placement: Placement::Managed {
by: systemd_agent,
vm: vm_name,
},
};
self.registry.register(app_entry);
Ok(())
Expand Down

0 comments on commit 799930e

Please sign in to comment.