mirror of
https://codeberg.org/icewind/netnsd.git
synced 2026-06-05 01:54:06 +02:00
track which namespaces we control
This commit is contained in:
parent
d168b4bf4d
commit
32fc90debe
5 changed files with 59 additions and 14 deletions
|
|
@ -22,7 +22,7 @@ pub fn daemon(config: Config) -> MainResult {
|
|||
}
|
||||
|
||||
async fn daemon_async(mut config: Config) -> Result<(), DaemonError> {
|
||||
let mut state = State::default();
|
||||
let mut state = State::new()?;
|
||||
state.update(&config)?;
|
||||
|
||||
// now the namespaces are setup, we can tell systemd to start any service depending on them
|
||||
|
|
@ -104,12 +104,18 @@ enum Event {
|
|||
Info,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct State {
|
||||
namespaces: Vec<ActiveNamespace>,
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub fn new() -> Result<Self, DaemonError> {
|
||||
let namespaces = NetNs::existing()?.map(ActiveNamespace::new).collect::<Result<Vec<_>, _>>()?;
|
||||
Ok(State {
|
||||
namespaces
|
||||
})
|
||||
}
|
||||
|
||||
pub fn update(&mut self, config: &Config) -> Result<(), DaemonError> {
|
||||
for removed in self.namespaces.extract_if(.., |namespace| {
|
||||
config.get_namespace(namespace.name()).is_none()
|
||||
|
|
@ -119,7 +125,7 @@ impl State {
|
|||
|
||||
for new in &config.namespaces {
|
||||
if !self.has_namespace(&new.name) {
|
||||
self.namespaces.push(ActiveNamespace::new(new)?);
|
||||
self.namespaces.push(ActiveNamespace::new(new.name.clone())?);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -144,16 +150,13 @@ struct ActiveNamespace {
|
|||
}
|
||||
|
||||
impl ActiveNamespace {
|
||||
pub fn new(config: &NamespaceConfig) -> Result<Self, DaemonError> {
|
||||
let ns = NetNs::new(config.name.clone())?;
|
||||
pub fn new(name: NamespaceName) -> Result<Self, DaemonError> {
|
||||
let ns = NetNs::new(name)?;
|
||||
|
||||
let mut namespace = ActiveNamespace {
|
||||
Ok(ActiveNamespace {
|
||||
ns,
|
||||
proxies: Vec::default(),
|
||||
};
|
||||
namespace.update_proxies(config)?;
|
||||
|
||||
Ok(namespace)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn update_proxies(&mut self, config: &NamespaceConfig) -> Result<(), DaemonError> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue