From: Alexandre Derumier <aderumier@odiso.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH v2 pve-container] POC : add/del/update ip from vnet-subnet-ipam
Date: Mon, 24 Aug 2020 18:49:23 +0200 [thread overview]
Message-ID: <20200824164923.12652-1-aderumier@odiso.com> (raw)
This is a POC to call ip to retreive ip address from ipam.
(it's really just a poc && buggt , it need to be improve for vnet changes, pending config apply/revert,...)
---
src/PVE/LXC/Config.pm | 107 +++++++++++++++++++++++++++++++++++++++++-
1 file changed, 106 insertions(+), 1 deletion(-)
diff --git a/src/PVE/LXC/Config.pm b/src/PVE/LXC/Config.pm
index 5bf12d5..05498e2 100644
--- a/src/PVE/LXC/Config.pm
+++ b/src/PVE/LXC/Config.pm
@@ -11,6 +11,14 @@ use PVE::INotify;
use PVE::JSONSchema qw(get_standard_option);
use PVE::Tools;
+my $have_sdn;
+eval {
+ require PVE::Network::SDN::Vnets;
+ require PVE::Network::SDN::Subnets;
+ $have_sdn = 1;
+};
+
+
use base qw(PVE::AbstractConfig);
my $nodename = PVE::INotify::nodename();
@@ -1145,7 +1153,6 @@ sub parse_lxc_network {
my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg');
$res->{hwaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix});
}
-
return $res;
}
@@ -1225,6 +1232,8 @@ sub vmconfig_hotplug_pending {
$cgroup->change_cpu_shares(undef, $confdesc->{cpuunits}->{default});
} elsif ($opt =~ m/^net(\d)$/) {
my $netid = $1;
+ my $net = $class->parse_lxc_network($conf->{$opt});
+ delete_net_ip($conf->{hostname}, $net);
PVE::Network::veth_delete("veth${vmid}i$netid");
} else {
die "skip\n"; # skip non-hotpluggable opts
@@ -1251,6 +1260,7 @@ sub vmconfig_hotplug_pending {
} elsif ($opt =~ m/^net(\d+)$/) {
my $netid = $1;
my $net = $class->parse_lxc_network($value);
+ update_net_ip($conf->{hostname}, $net);
$value = $class->print_lxc_network($net);
PVE::LXC::update_net($vmid, $conf, $opt, $net, $netid, $rootdir);
} elsif ($opt eq 'memory' || $opt eq 'swap') {
@@ -1327,6 +1337,8 @@ sub vmconfig_apply_pending {
} elsif ($opt =~ m/^net(\d+)$/) {
my $netid = $1;
my $net = $class->parse_lxc_network($conf->{pending}->{$opt});
+ my $oldnet = $class->parse_lxc_network($conf->{$opt});
+ update_net_ip($conf->{hostname}, $net, $oldnet);
$conf->{pending}->{$opt} = $class->print_lxc_network($net);
}
};
@@ -1590,4 +1602,97 @@ sub get_backup_volumes {
return $return_volumes;
}
+sub update_net_ip {
+ my ($hostname, $net, $oldnet) = @_;
+
+ return if !$have_sdn;
+
+ my $oldbridge = $oldnet->{bridge};
+ my $bridge = $net->{bridge};
+
+ my $subnets = PVE::Network::SDN::Vnets::get_subnets($bridge);
+
+ return if !keys %{$subnets};
+
+ eval {
+ if (!$net->{ip}) {
+ $net->{ip} = PVE::Network::SDN::Vnets::get_next_free_ip($bridge, $hostname);
+ #writeconfig ?
+ } elsif ($net->{ip} ne 'dhcp' && $net->{ip} ne 'manual') {
+ PVE::Network::SDN::Vnets::add_ip($bridge, $net->{ip}, $hostname) if $net->{ip} ne $oldnet->{ip};
+ #writeconfig ?
+ }
+ };
+ if ($@) {
+ #keep old config if error
+ $net->{ip} = $oldnet->{ip};
+ die $@;
+ }
+
+ #delete old ip after adding new
+ if ($oldnet->{ip} && $oldnet->{ip} ne 'dhcp' && $oldnet->{ip} ne 'manual') {
+ my $deletebridge = $oldbridge ne $bridge ? $oldbridge : $bridge;
+ eval {
+ PVE::Network::SDN::Vnets::del_ip($deletebridge, $oldnet->{ip}, $hostname) if !$net->{ip} || $net->{ip} ne $oldnet->{ip};
+ #writeconfig ?
+ };
+ warn $@ if $@;
+ }
+
+
+ eval {
+ if (!$net->{ip6}) {
+ $net->{ip6} = PVE::Network::SDN::Vnets::get_next_free_ip($bridge, $hostname, 6);
+ #writeconfig ?
+ } elsif ($net->{ip6} ne 'dhcp' && $net->{ip6} ne 'manual' && $net->{ip6} ne 'auto') {
+ PVE::Network::SDN::Vnets::add_ip($bridge, $net->{ip6}, $hostname) if $net->{ip6} ne $oldnet->{ip6};
+ #writeconfig ?
+ }
+ };
+ if ($@) {
+ #keep old config if error
+ $net->{ip6} = $oldnet->{ip6};
+ die $@;
+ }
+
+ #delete old ip after adding new
+ if ($oldnet->{ip6} && $oldnet->{ip6} ne 'dhcp' && $oldnet->{ip6} ne 'manual' && $net->{ip6} ne 'auto') {
+ my $deletebridge = $oldbridge ne $bridge ? $oldbridge : $bridge;
+ eval {
+ PVE::Network::SDN::Vnets::del_ip($deletebridge, $oldnet->{ip6}, $hostname) if !$net->{ip6} || $net->{ip6} ne $oldnet->{ip6};
+ #writeconfig ?
+ };
+ warn $@ if $@;
+ }
+
+ if ($net->{ip}) {
+ my ($ip, undef) = split(/\//, $net->{ip});
+ my ($subnetidv4, $subnetv4) = PVE::Network::SDN::Subnets::find_ip_subnet($ip, $subnets);
+ $net->{gw} = $subnetv4->{gateway};
+ }
+
+ if ($net->{ip6}) {
+ my ($ip6, undef) = split(/\//, $net->{ip6});
+ my ($subnetidv6, $subnetv6) = PVE::Network::SDN::Subnets::find_ip_subnet($ip6, $subnets);
+ $net->{gw6} = $subnetv6->{gateway} if !$net->{gw6};
+ }
+
+}
+
+sub delete_net_ip {
+ my ($hostname, $net) = @_;
+
+ return if !$have_sdn;
+
+ my $bridge = $net->{bridge};
+ my $subnets = PVE::Network::SDN::Vnets::get_subnets($bridge);
+ return if !keys %{$subnets};
+
+ if ($net->{ip6} && ($net->{ip6} eq 'auto' || $net->{ip6} eq 'manual' || $net->{ip6} eq 'dhcp')) {
+ PVE::Network::SDN::Vnets::del_ip($hostname, $bridge, $net->{ip6});
+ } elsif ($net->{ip} && $net->{ip} ne 'dhcp' && $net->{ip} ne 'manual') {
+ PVE::Network::SDN::Vnets::del_ip($hostname, $bridge, $net->{ip});
+ }
+}
+
1;
--
2.20.1
next reply other threads:[~2020-08-24 16:50 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-08-24 16:49 Alexandre Derumier [this message]
2020-09-07 16:40 ` Thomas Lamprecht
2020-09-08 3:52 ` Alexandre DERUMIER
2020-09-08 7:44 ` Thomas Lamprecht
2020-09-08 8:58 ` Alexandre DERUMIER
2020-09-11 2:27 ` Alexandre DERUMIER
2020-09-24 8:58 ` Alexandre DERUMIER
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=20200824164923.12652-1-aderumier@odiso.com \
--to=aderumier@odiso.com \
--cc=pve-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