de-duplicate stuff

This commit is contained in:
2025-09-03 17:18:26 -04:00
parent fc4c18ccea
commit 01206f5db1
3 changed files with 43 additions and 8 deletions

View File

@@ -1,11 +1,8 @@
use std::{collections::HashSet, sync::Arc};
use log::warn;
use crate::{config::OPNsenseShell, Error};
use opnsense_config_xml::{
Frontend, HAProxy, HAProxyBackend, HAProxyHealthCheck, HAProxyServer, OPNsense,
};
use crate::{config::OPNsenseShell, Error};
use std::{collections::HashSet, sync::Arc};
pub struct LoadBalancerConfig<'a> {
opnsense: &'a mut OPNsense,
@@ -71,6 +68,7 @@ impl<'a> LoadBalancerConfig<'a> {
}
/// Adds the components of a new service to the HAProxy configuration.
/// This function de-duplicates servers by name to prevent configuration errors.
fn add_new_service(
&mut self,
frontend: Frontend,
@@ -82,7 +80,20 @@ impl<'a> LoadBalancerConfig<'a> {
if let Some(check) = healthcheck {
haproxy.healthchecks.healthchecks.push(check);
}
haproxy.servers.servers.extend(servers);
let mut existing_server_names: HashSet<_> = haproxy
.servers
.servers
.iter()
.map(|s| s.name.clone())
.collect();
for server in servers {
if existing_server_names.insert(server.name.clone()) {
haproxy.servers.servers.push(server);
}
}
haproxy.backends.backends.push(backend);
haproxy.frontends.frontend.push(frontend);
});