[pve-devel] [PATCH qemu-server 6/6] drop get_pending_changes and simplify cloudinit_pending api call

Wolfgang Bumiller w.bumiller at proxmox.com
Wed Nov 16 18:14:08 CET 2022


- The forced-remove flag wasn't really used AFAICT and makes
  no sense IMO.
- Whether or not we care about non-MAC changes does not
  belong here, but should instead taken into account in the
  actual hotplug path recording the cloud-init state (iow.
  into $cloudinit_record_changed().)
  (This is not done here atm.)
- It seems much simpler to just have:
  * 'old' = the old value if it's not a new value
  * 'new' = the new value unless it's being deleted
  * If only one of them is set it's an addition or removal.

Signed-off-by: Wolfgang Bumiller <w.bumiller at proxmox.com>
---
 PVE/API2/Qemu.pm            | 44 +++++++++++----------
 PVE/QemuServer/Cloudinit.pm | 78 -------------------------------------
 2 files changed, 24 insertions(+), 98 deletions(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 30348e6..edb495b 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -1338,24 +1338,16 @@ __PACKAGE__->register_method({
 		    description => "Configuration option name.",
 		    type => 'string',
 		},
-		value => {
-		    description => "Current value.",
+		old => {
+		    description => "Value as it was used to generate the current cloudinit image.",
 		    type => 'string',
 		    optional => 1,
 		},
-		pending => {
-		    description => "Pending value.",
+		new => {
+		    description => "The new pending value.",
 		    type => 'string',
 		    optional => 1,
 		},
-		delete => {
-		    description => "Indicates a pending delete request if present and not 0. " .
-		                   "The value 2 indicates a force-delete request.",
-		    type => 'integer',
-		    minimum => 0,
-		    maximum => 2,
-		    optional => 1,
-		},
 	    },
 	},
     },
@@ -1365,17 +1357,29 @@ __PACKAGE__->register_method({
 	my $vmid = $param->{vmid};
 	my $conf = PVE::QemuConfig->load_config($vmid);
 
-	if (defined($conf->{cipassword}) &&
-	    defined($conf->{cloudinit}->{cipassword}) &&
-	    $conf->{cipassword} ne $conf->{cloudinit}->{cipassword}) {
-	    $conf->{cipassword} = '********** ';
-	} elsif (defined($conf->{cipassword})) {
-	    $conf->{cipassword} = '**********';
+	my $ci = $conf->{cloudinit};
+
+	my $res = {};
+	my $added = delete($ci->{added}) // '';
+	for my $key (PVE::Tools::split_list($added)) {
+	    $res->{$key} = { new => $conf->{$key} };
 	}
 
-	$conf->{cloudinit}->{cipassword} = '**********' if defined($conf->{cloudinit}->{cipassword});
+	for my $key (keys %$ci) {
+	    if (!exists($conf->{$key})) {
+		$res->{$key} = { old => $ci->{$key} };
+	    } else {
+		$res->{$key} = {
+		    old => $ci->{$key},
+		    new => $conf->{$key},
+		};
+	    }
+	}
 
-	my $res = PVE::QemuServer::Cloudinit::get_pending_config($conf, $vmid);
+	if (defined(my $pw = $res->{cipassword})) {
+	    $pw->{old} = '**********' if exists $pw->{old};
+	    $pw->{new} = '**********' if exists $pw->{new};
+	}
 
 	return $res;
    }});
diff --git a/PVE/QemuServer/Cloudinit.pm b/PVE/QemuServer/Cloudinit.pm
index 24725e7..a0c3d60 100644
--- a/PVE/QemuServer/Cloudinit.pm
+++ b/PVE/QemuServer/Cloudinit.pm
@@ -627,82 +627,4 @@ sub dump_cloudinit_config {
     }
 }
 
-sub get_pending_config {
-    my ($conf, $vmid) = @_;
-
-    my $newconf = dclone($conf);
-
-    my $cloudinit_current = $newconf->{cloudinit};
-    my @cloudinit_opts = keys %{PVE::QemuServer::cloudinit_config_properties()};
-    push @cloudinit_opts, 'name';
-
-    #add cloud-init drive
-    my $drives = {};
-    PVE::QemuConfig->foreach_volume($newconf, sub {
-	my ($ds, $drive) = @_;
-	$drives->{$ds} = 1 if PVE::QemuServer::drive_is_cloudinit($drive);
-    });
-
-    PVE::QemuConfig->foreach_volume($cloudinit_current, sub {
-	my ($ds, $drive) = @_;
-	$drives->{$ds} = 1 if PVE::QemuServer::drive_is_cloudinit($drive);
-    });
-    for my $ds (keys %{$drives}) {
-	push @cloudinit_opts, $ds;
-    }
-
-    $newconf->{name} = "VM$vmid" if !$newconf->{name};
-    $cloudinit_current->{name} = "VM$vmid" if !$cloudinit_current->{name};
-
-    #only mac-address is used in cloud-init config.
-    #We don't want to display other pending net changes.
-    my $print_cloudinit_net = sub {
-	my ($conf, $opt) = @_;
-
-	if (defined($conf->{$opt})) {
-	    my $net = PVE::QemuServer::parse_net($conf->{$opt});
-	    $conf->{$opt} = "macaddr=".$net->{macaddr} if $net->{macaddr};
-	}
-    };
-
-    my $cloudinit_options = {};
-    for my $opt (@cloudinit_opts) {
-	if ($opt =~ m/^ipconfig(\d+)/) {
-	    my $netid = "net$1";
-
-	    next if !defined($newconf->{$netid}) && !defined($cloudinit_current->{$netid}) &&
-		    !defined($newconf->{$opt}) && !defined($cloudinit_current->{$opt});
-
-	    &$print_cloudinit_net($newconf, $netid);
-	    &$print_cloudinit_net($cloudinit_current, $netid);
-	    $cloudinit_options->{$netid} = 1;
-	}
-	$cloudinit_options->{$opt} = 1;
-    }
-
-    my $res = [];
-
-    for my $opt (keys %{$cloudinit_options}) {
-
-	my $item = {
-	    key => $opt,
-	};
-	if ($cloudinit_current->{$opt}) {
-	    $item->{value} = $cloudinit_current->{$opt};
-	    if (defined($newconf->{$opt})) {
-		$item->{pending} = $newconf->{$opt} 
-		    if $newconf->{$opt} ne $cloudinit_current->{$opt};
-	    } else {
-		$item->{delete} = 1;
-	    }
-	} else {
-	    $item->{pending} = $newconf->{$opt} if $newconf->{$opt}
-	}
-
-	push @$res, $item;
-   }
-
-   return $res;
-}
-
 1;
-- 
2.30.2






More information about the pve-devel mailing list