all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pmg-devel@lists.proxmox.com
Subject: [pmg-devel] [PATCH pmg-api] config: add ips/nets uniquely to the template variables
Date: Tue, 31 May 2022 13:52:53 +0200	[thread overview]
Message-ID: <20220531115253.3398902-1-d.csapak@proxmox.com> (raw)

otherwise a config with many entries such as:
domain1: ip1
domain2: ip1
domain3: ip1
etc.

unnecessarily adds 'ip1' multiple times to the 'mynetworks' variable

to keep the output sorted (so it's stable) move the sort to the 'join'

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/PMG/Config.pm | 40 +++++++++++++++++++++-------------------
 1 file changed, 21 insertions(+), 19 deletions(-)

diff --git a/src/PMG/Config.pm b/src/PMG/Config.pm
index 31f9c6f..9ba5c76 100755
--- a/src/PMG/Config.pm
+++ b/src/PMG/Config.pm
@@ -1297,63 +1297,65 @@ sub get_template_vars {
     my $int_ip = PMG::Cluster::remote_node_ip($dnsinfo->{hostname});
     $vars->{ipconfig}->{int_ip} = $int_ip;
 
-    my $transportnets = [];
+    my $transportnets = {};
+    my $mynetworks = {
+	'127.0.0.0/8' => 1,
+	'[::1]/128', => 1,
+    };
 
     if (my $tmap = PVE::INotify::read_file('transport')) {
-	foreach my $domain (sort keys %$tmap) {
+	foreach my $domain (keys %$tmap) {
 	    my $data = $tmap->{$domain};
 	    my $host = $data->{host};
 	    if ($host =~ m/^$IPV4RE$/) {
-		push @$transportnets, "$host/32";
+		$transportnets->{"$host/32"} = 1;
+		$mynetworks->{"$host/32"} = 1;
 	    } elsif ($host =~ m/^(?:ipv6:)?($IPV6RE)$/i) {
-		push @$transportnets, "[$1]/128";
+		$transportnets->{"[$1]/128"} = 1;
+		$mynetworks->{"[$1]/128"} = 1;
 	    }
 	}
     }
 
-    $vars->{postfix}->{transportnets} = join(' ', @$transportnets);
-
-    my $mynetworks = [ '127.0.0.0/8', '[::1]/128' ];
+    $vars->{postfix}->{transportnets} = join(' ', sort keys %$transportnets);
 
     if (defined($int_ip)) { # we cannot really do anything and the loopback nets are already added
 	if (my $int_net_cidr = PMG::Utils::find_local_network_for_ip($int_ip, 1)) {
 	    if ($int_net_cidr =~ m/^($IPV6RE)\/(\d+)$/) {
-		push @$mynetworks, "[$1]/$2";
+		$mynetworks->{"[$1]/$2"} = 1;
 	    } else {
-		push @$mynetworks, $int_net_cidr;
+		$mynetworks->{$int_net_cidr} = 1;
 	    }
 	} else {
 	    if ($int_ip =~ m/^$IPV6RE$/) {
-		push @$mynetworks, "[$int_ip]/128";
+		$mynetworks->{"[$int_ip]/128"} = 1;
 	    } else {
-		push @$mynetworks, "$int_ip/32";
+		$mynetworks->{"$int_ip/32"} = 1;
 	    }
 	}
     }
 
     my $netlist = PVE::INotify::read_file('mynetworks');
-    foreach my $cidr (sort keys %$netlist) {
+    foreach my $cidr (keys %$netlist) {
 	if ($cidr =~ m/^($IPV6RE)\/(\d+)$/) {
-	    push @$mynetworks, "[$1]/$2";
+	    $mynetworks->{"[$1]/$2"} = 1;
 	} else {
-	    push @$mynetworks, $cidr;
+	    $mynetworks->{$cidr} = 1;
 	}
     }
 
-    push @$mynetworks, @$transportnets;
-
     # add default relay to mynetworks
     if (my $relay = $self->get('mail', 'relay')) {
 	if ($relay =~ m/^$IPV4RE$/) {
-	    push @$mynetworks, "$relay/32";
+	    $mynetworks->{"$relay/32"} = 1;
 	} elsif ($relay =~ m/^$IPV6RE$/) {
-	    push @$mynetworks, "[$relay]/128";
+	    $mynetworks->{"[$relay]/128"} = 1;
 	} else {
 	    # DNS name - do nothing ?
 	}
     }
 
-    $vars->{postfix}->{mynetworks} = join(' ', @$mynetworks);
+    $vars->{postfix}->{mynetworks} = join(' ', sort keys %$mynetworks);
 
     # normalize dnsbl_sites
     my @dnsbl_sites = PVE::Tools::split_list($vars->{pmg}->{mail}->{dnsbl_sites});
-- 
2.30.2





             reply	other threads:[~2022-05-31 11:52 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-31 11:52 Dominik Csapak [this message]
2022-06-14  7:56 ` [pmg-devel] applied: " Wolfgang Bumiller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220531115253.3398902-1-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pmg-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal