[pve-devel] [RFC pve-network 2/9] dhcp : add|del_ip_mapping: only add|del dhcp reservervation

Alexandre Derumier aderumier at odiso.com
Mon Nov 13 11:04:08 CET 2023


don't try to add|del ip from ipam here

Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 src/PVE/Network/SDN/Dhcp.pm | 75 +++++++++----------------------------
 1 file changed, 18 insertions(+), 57 deletions(-)

diff --git a/src/PVE/Network/SDN/Dhcp.pm b/src/PVE/Network/SDN/Dhcp.pm
index e4c4078..1c32fec 100644
--- a/src/PVE/Network/SDN/Dhcp.pm
+++ b/src/PVE/Network/SDN/Dhcp.pm
@@ -21,72 +21,33 @@ PVE::Network::SDN::Dhcp::Dnsmasq->register();
 PVE::Network::SDN::Dhcp::Dnsmasq->init();
 
 sub add_mapping {
-    my ($vmid, $vnet, $mac) = @_;
+    my ($vmid, $vnetid, $mac, $ip) = @_;
 
-    my $vnet_config = PVE::Network::SDN::Vnets::get_vnet($vnet, 1);
-    return if !$vnet_config;
+    my $subnets = PVE::Network::SDN::Vnets::get_subnets($vnetid, 1);
+    my $vnet = PVE::Network::SDN::Vnets::get_vnet($vnetid);
+    my $zoneid = $vnet->{zone};
+    my $zone = PVE::Network::SDN::Zones::get_zone($zoneid);
 
-    my $subnets = PVE::Network::SDN::Vnets::get_subnets($vnet, 1);
+    my $dhcptype = $zone->{dhcp};
+    return if !$dhcptype;
 
-    for my $subnet_id (keys %{$subnets}) {
-	my $subnet_config = $subnets->{$subnet_id};
-
-	next if !$subnet_config->{'dhcp-range'};
-
-	foreach my $dhcp_range (@{$subnet_config->{'dhcp-range'}}) {
-	    my $dhcp_config = PVE::Network::SDN::Dhcp::get_dhcp($dhcp_range->{server});
-
-	    if (!$dhcp_config) {
-		warn "Cannot find configuration for DHCP server $dhcp_range->{server}";
-		next;
-	    }
-
-	    my $ipam_plugin = PVE::Network::SDN::Ipams::Plugin->lookup('pve');
-
-	    my $data = {
-		vmid => $vmid,
-		mac => $mac,
-	    };
-
-	    my $ip = $ipam_plugin->add_dhcp_ip($subnet_config, $dhcp_range, $data);
-
-	    next if !$ip;
-
-	    my $dhcp_plugin = PVE::Network::SDN::Dhcp::Plugin->lookup($dhcp_config->{type});
-	    $dhcp_plugin->add_ip_mapping($dhcp_config, $mac, $ip);
-
-	    return $ip;
-	}
-    }
+    my $dhcp_plugin = PVE::Network::SDN::Dhcp::Plugin->lookup($dhcptype);
+    $dhcp_plugin->add_ip_mapping($zoneid, $mac, $ip);
 }
 
 sub remove_mapping {
-    my ($vnet, $mac) = @_;
-
-    my $vnet_config = PVE::Network::SDN::Vnets::get_vnet($vnet, 1);
-    return if !$vnet_config;
-
-    my $subnets = PVE::Network::SDN::Vnets::get_subnets($vnet, 1);
-
-    for my $subnet_id (keys %{$subnets}) {
-	my $subnet_config = $subnets->{$subnet_id};
-	next if !$subnet_config->{'dhcp-range'};
+    my ($vnetid, $mac) = @_;
 
-	my $ipam_plugin = PVE::Network::SDN::Ipams::Plugin->lookup('pve');
-	$ipam_plugin->del_dhcp_ip($subnet_config, $mac);
+    my $subnets = PVE::Network::SDN::Vnets::get_subnets($vnetid, 1);
+    my $vnet = PVE::Network::SDN::Vnets::get_vnet($vnetid);
+    my $zoneid = $vnet->{zone};
+    my $zone = PVE::Network::SDN::Zones::get_zone($zoneid);
 
-	foreach my $dhcp_range (@{$subnet_config->{'dhcp-range'}}) {
-	    my $dhcp_config = PVE::Network::SDN::Dhcp::get_dhcp($dhcp_range->{server});
+    my $dhcptype = $zone->{dhcp};
+    return if !$dhcptype;
 
-	    if (!$dhcp_config) {
-		warn "Cannot find configuration for DHCP server $dhcp_range->{server}";
-		next;
-	    }
-
-	    my $dhcp_plugin = PVE::Network::SDN::Dhcp::Plugin->lookup($dhcp_config->{type});
-	    $dhcp_plugin->del_ip_mapping($dhcp_config, $mac);
-	}
-    }
+    my $dhcp_plugin = PVE::Network::SDN::Dhcp::Plugin->lookup($dhcptype);
+    $dhcp_plugin->del_ip_mapping($zoneid, $mac);
 }
 
 sub regenerate_config {
-- 
2.39.2





More information about the pve-devel mailing list