[pve-devel] [PATCH network 2/4] fix #5900: add helper functions
Daniel Herzig
d.herzig at proxmox.com
Mon Dec 16 09:35:38 CET 2024
Hey Stefan,
thanks for the feedback!
Stefan Hanreich <s.hanreich at proxmox.com> writes:
>
> If we do it this way (see top-level discussion), I think we should
> abstract this into the IpamPlugins itself, since this implementation is
> specific to the PVE Plugin, but that's just one type of IPAM plugin.
> Something like:
>
> Add a abstract method in the base Ipam plugin
> (Network/SDN/Ipams/Plugin.pm), i.e.
>
> PVE::Network::SDN::Ipams::Plugin::vnet_has_free_ip($range, $ipversions)
>
> Then implement it for every IPAM Plugin separately.
>
> Add a helper method to the VNet that selects the correct plugin based on
> the zone setting and then iterates over all its subnets to check for
> free IPs - something like:
>
> PVE::Network::SDN::Vnets::has_free_ip($range, $ipversions)
>
I like this thought a lot, sounds like a much cleaner and modular
solution.
>
>
> The current implementation only works for the PVE plugin and would
> actually break on zones using Netbox / Phpipam (if my brain compiler is
> correct).
>
>
Thanks for the hint -- I need to do some research on that! The code
assumes the ~key: value~ of ~dhcp: dnsmasq~ to be exclusive for IPAM
PVE.
```
for my $zone (@$zone_ids) {
push(@$dhcp_dnsmasq_zones, $zone)
if (defined(${zones_cfg}->{'ids'}->{$zone}->{'dhcp'}) &&
(${zones_cfg}->{'ids'}->{$zone}->{'dhcp'} eq 'dnsmasq'))
}
```
If this is not the case, it will affect (and not ignore as intended)
zones with Netbox/Phpipam indeed. That would be bad.
A check for ~ipam: pve~ could however be easily implemented in the same section.
>>
>> +sub defined_dhcp_ip_count_in_zone {
>> + my $zone_id = shift;
>
> even with 1 argument I think we prefer `my ($arg) = @_;`, but I haven't
> actually found a definitive answer in our style guide.
Thanks, not having any feelings here.
>
>> + my $vnets_in_zone = PVE::Network::SDN::Zones::get_vnets($zone_id);
>> + my $range_count_array;
>> + my $res;
>> + for my $vnet_id (keys %$vnets_in_zone) {
>> + my $subnets_in_vnet = PVE::Network::SDN::Vnets::get_subnets($vnet_id);
>> + for my $subnet (keys %$subnets_in_vnet) {
>> + my $dhcp_ranges = PVE::Network::SDN::Subnets::get_dhcp_ranges(${subnets_in_vnet}->{$subnet});
>> + if (scalar @$dhcp_ranges) {
>> + for my $dhcp_range (@$dhcp_ranges) {
>
> You can just iterate over @$dhcp_ranges, get_dhcp_ranges() always
> returns an array reference. If it is empty, then there are just 0
> iterations of the loop, no need to check for existence.
>
Right, this is too timid indeed :)
>> +
>> +sub available_dhcp_ips_in_zone {
>> + my $zone_id = shift;
>> + my $available_ip_count = defined_dhcp_ip_count_in_zone($zone_id);
>> + my $used_ip_count = used_dhcp_ips_in_zone($zone_id);
>> + if (!defined($available_ip_count)) {
>> + $available_ip_count = 0;
>> + }
>> + if (!defined($used_ip_count)) {
>> + $used_ip_count = 0;
>> + }
>
> If you define $res to be 0 as suggested above, then those checks become
> unnecessary, since 0 becomes the default value.
>
Very cool, thank you, I did not see that.
>> + my $vnet_ids = [ PVE::Network::SDN::Vnets::sdn_vnets_ids($vnets_cfg) ];
>
> no need for selecting the ids, you can directly iterate over the VNets:
>
Thank you for your insights and the hints very much. I cannot claim that I'm exactly happy about
the current implementation of this patch from an aesthetical point of
view by now! I will act on it further, if we consider the
'ask-for-permission' approach in general.
More information about the pve-devel
mailing list