all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Wolfgang Bumiller <w.bumiller@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH qemu-server 4/6] record cloud-init changes in the cloudinit section
Date: Wed, 16 Nov 2022 18:14:06 +0100	[thread overview]
Message-ID: <20221116171408.216775-5-w.bumiller@proxmox.com> (raw)
In-Reply-To: <20221116171408.216775-1-w.bumiller@proxmox.com>

introducing an 'added' value in the cloudinit section for
values which have not been present when the cloudinit image
has been generated

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
---
 PVE/QemuServer.pm | 67 ++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 64 insertions(+), 3 deletions(-)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index d8bcfff..6053d6c 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -2290,6 +2290,15 @@ sub cloudinit_config_properties {
     return dclone($confdesc_cloudinit);
 }
 
+sub cloudinit_pending_properties {
+    my $p = {
+	map { $_ => 1 } keys $confdesc_cloudinit->%*,
+	name => 1,
+    };
+    $p->{"net$_"} = 1 for 0..($MAX_NETS-1);
+    return $p;
+}
+
 sub check_type {
     my ($key, $value) = @_;
 
@@ -4892,11 +4901,61 @@ sub vmconfig_hotplug_pending {
 	$errors->{$opt} = "hotplug problem - $msg";
     };
 
+    my $cloudinit_pending_properties = PVE::QemuServer::cloudinit_pending_properties();
+
+    my $cloudinit_record_changed = sub {
+	my ($conf, $opt, $old, $new) = @_;
+	return if !$cloudinit_pending_properties->{$opt};
+
+	my $ci = ($conf->{cloudinit} //= {});
+
+	my $recorded = $ci->{$opt};
+
+	my $remove_added = sub {
+	    my @added = grep { $_ ne $opt } PVE::Tools::split_list(delete($ci->{added}) // '');
+	    my $added = join(',', @added);
+	    $ci->{added} = $added if length($added);
+	};
+
+	my $record_added = sub {
+	    my %added = map { $_ => 1 } PVE::Tools::split_list(delete($ci->{added}) // '');
+	    $added{$opt} = 1;
+	    $ci->{added} = join(',', keys %added);
+	};
+
+	if (defined($recorded)) {
+	    # cloud-init already had a version of this key
+	    if (!defined($new)) {
+		# the option existed temporarily and was now removed:
+		$remove_added->();
+	    } elsif ($new eq $recorded) {
+		# it was reverted to the state in cloud-init
+		delete $ci->{$opt};
+	    } else {
+		# $old was newer than $recorded, do nothing
+	    }
+	} else {
+	    # the key was not present the last time we generated the cloud-init image:
+	    if (!defined($new)) {
+		# it is being deleted, which means we're back to the cloud-init sate:
+		delete $ci->{$opt};
+		$remove_added->();
+	    } elsif (defined($old)) {
+		# the key was modified
+		$ci->{$opt} = $old;
+	    } else {
+		# the key was added, no old value exists
+		$record_added->();
+	    }
+	}
+    };
+
     my $changes = 0;
     foreach my $opt (keys %{$conf->{pending}}) { # add/change
 	if ($fast_plug_option->{$opt}) {
-	    $conf->{$opt} = $conf->{pending}->{$opt};
-	    delete $conf->{pending}->{$opt};
+	    my $new = delete $conf->{pending}->{$opt};
+	    $cloudinit_record_changed->($conf, $opt, $conf->{$opt}, $new);
+	    $conf->{$opt} = $new;
 	    $changes = 1;
 	}
     }
@@ -4914,6 +4973,7 @@ sub vmconfig_hotplug_pending {
 
     my $cgroup = PVE::QemuServer::CGroup->new($vmid);
     my $pending_delete_hash = PVE::QemuConfig->parse_pending_delete($conf->{pending}->{delete});
+
     foreach my $opt (sort keys %$pending_delete_hash) {
 	next if $selection && !$selection->{$opt};
 	my $force = $pending_delete_hash->{$opt}->{force};
@@ -4967,7 +5027,8 @@ sub vmconfig_hotplug_pending {
 	if (my $err = $@) {
 	    &$add_error($opt, $err) if $err ne "skip\n";
 	} else {
-	    delete $conf->{$opt};
+	    my $old = delete $conf->{$opt};
+	    $cloudinit_record_changed->($conf, $opt, $old, undef);
 	    PVE::QemuConfig->remove_from_pending_delete($conf, $opt);
 	}
     }
-- 
2.30.2





  parent reply	other threads:[~2022-11-16 17:14 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-16 17:14 [pve-devel] [PATCH qemu-server 0/6] Manage [special:cloudinit] in hotplug code Wolfgang Bumiller
2022-11-16 17:14 ` [pve-devel] [PATCH qemu-server 1/6] Revert "cloudinit: avoid unsafe write of VM config" Wolfgang Bumiller
2022-11-16 17:14 ` [pve-devel] [PATCH qemu-server 2/6] Partially-revert "cloudinit: add cloudinit section for current generated config" Wolfgang Bumiller
2022-11-16 17:14 ` [pve-devel] [PATCH qemu-server 3/6] delay cloudinit generation in hotplug Wolfgang Bumiller
2022-11-16 17:14 ` Wolfgang Bumiller [this message]
2022-11-16 17:14 ` [pve-devel] [PATCH qemu-server 5/6] don't call 'cleanup_config' the cloudinit section Wolfgang Bumiller
2022-11-16 17:14 ` [pve-devel] [PATCH qemu-server 6/6] drop get_pending_changes and simplify cloudinit_pending api call Wolfgang Bumiller
2022-11-16 17:41 ` [pve-devel] applied-series: [PATCH qemu-server 0/6] Manage [special:cloudinit] in hotplug code Thomas Lamprecht

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=20221116171408.216775-5-w.bumiller@proxmox.com \
    --to=w.bumiller@proxmox.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