public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: POYEN Alexis <alexis.poyen@groupe-cyllene.com>
To: pve-devel@lists.proxmox.com
Cc: POYEN Alexis <alexis.poyen@groupe-cyllene.com>
Subject: [RFC PATCH manager 1/3] close #4186: vzdump: select guests by tag
Date: Thu,  3 Dec 2026 10:27:08 +0100	[thread overview]
Message-ID: <20261203092717.1863218-3-alexis.poyen@groupe-cyllene.com> (raw)
In-Reply-To: <20261203092717.1863218-1-alexis.poyen@groupe-cyllene.com>

Allow vzdump backup jobs to select the guests to back up by tag: all
guests having at least one of the given tags are included. The new
'tags' option is mutually exclusive with the existing 'vmid', 'all',
'exclude' and 'pool' selection modes.

Add unit tests covering tag selection, tag selection combined with a
node limit, multiple tags, and a tag without any match.

Signed-off-by: POYEN Alexis <alexis.poyen@groupe-cyllene.com>
---
 PVE/VZDump.pm                      | 31 ++++++++++++-
 test/vzdump_guest_included_test.pl | 71 +++++++++++++++++++++++++++++-
 2 files changed, 100 insertions(+), 2 deletions(-)

diff --git a/PVE/VZDump.pm b/PVE/VZDump.pm
index 5b9397cd..c4675449 100644
--- a/PVE/VZDump.pm
+++ b/PVE/VZDump.pm
@@ -1533,6 +1533,18 @@ sub verify_vzdump_parameters {
     raise_param_exc({ pool => "option conflicts with option 'vmid'" })
         if $param->{pool} && $param->{vmid};
 
+    raise_param_exc({ tags => "option conflicts with option 'vmid'" })
+        if $param->{tags} && $param->{vmid};
+
+    raise_param_exc({ tags => "option conflicts with option 'all'" })
+        if $param->{tags} && $param->{all};
+
+    raise_param_exc({ tags => "option conflicts with option 'exclude'" })
+        if $param->{tags} && $param->{exclude};
+
+    raise_param_exc({ tags => "option conflicts with option 'pool'" })
+        if $param->{tags} && $param->{pool};
+
     $parse_prune_backups->($param, 'CLI parameters');
     parse_fleecing($param);
     parse_performance($param);
@@ -1547,7 +1559,8 @@ sub verify_vzdump_parameters {
     return if !$check_missing;
 
     raise_param_exc({ vmid => "property is missing" })
-        if !($param->{all} || $param->{stop} || $param->{pool}) && !$param->{vmid};
+        if !($param->{all} || $param->{stop} || $param->{pool} || $param->{tags})
+        && !$param->{vmid};
 
 }
 
@@ -1584,6 +1597,22 @@ sub get_included_guests {
 
     if ($job->{pool}) {
         $vmids = PVE::API2Tools::get_resource_pool_guest_members($job->{pool});
+    } elsif ($job->{tags}) {
+        # select guests by tags
+        my $tags = { map { $_ => 1 } split_list($job->{tags}) };
+        my $props = PVE::Cluster::get_guest_config_properties(['tags']);
+
+        for my $id (keys %{ $vmlist->{ids} }) {
+            my $guest_tags = $props->{$id}->{tags} // '';
+            my $has_tag = 0;
+            for my $tag (split_list($guest_tags)) {
+                if ($tags->{$tag}) {
+                    $has_tag = 1;
+                    last;
+                }
+            }
+            push @$vmids, $id if $has_tag;
+        }
     } elsif ($job->{vmid}) {
         $vmids = [split_list($job->{vmid})];
     } elsif ($job->{all}) {
diff --git a/test/vzdump_guest_included_test.pl b/test/vzdump_guest_included_test.pl
index be42e9ab..de9db6e5 100755
--- a/test/vzdump_guest_included_test.pl
+++ b/test/vzdump_guest_included_test.pl
@@ -5,7 +5,7 @@ use warnings;
 
 use lib '..';
 
-use Test::More tests => 10;
+use Test::More tests => 14;
 use Test::MockModule;
 
 use PVE::VZDump;
@@ -53,12 +53,32 @@ my $pools = {
     testpool => [100, 101, 200, 201],
 };
 
+my $guest_tags = {
+    100 => 'prod,web',
+    101 => 'prod',
+    112 => 'dev',
+    200 => 'prod,db',
+    201 => 'dev',
+    212 => 'db',
+    213 => 'prod',
+};
+
 my $pve_cluster_module = Test::MockModule->new('PVE::Cluster');
 $pve_cluster_module->mock(
     get_vmlist => sub {
         return $vmlist;
     },
 );
+$pve_cluster_module->mock(
+    get_guest_config_properties => sub {
+        my ($prop_list) = @_;
+        my $res = {};
+        for my $id (keys %{ $vmlist->{ids} }) {
+            $res->{$id}->{tags} = $guest_tags->{$id} // '';
+        }
+        return $res;
+    },
+);
 
 my $pve_inotify = Test::MockModule->new('PVE::INotify');
 $pve_inotify->mock(
@@ -215,6 +235,55 @@ $addtest->(
     },
 );
 
+$addtest->(
+    'Test guests by tag',
+    {
+        expected => {
+            node1 => [100, 101],
+            node2 => [200, 213],
+        },
+        param => {
+            tags => 'prod',
+        },
+    },
+);
+
+$addtest->(
+    'Test guests by tag with node limit',
+    {
+        expected => {
+            node2 => [200, 213],
+        },
+        param => {
+            tags => 'prod',
+            node => 'node2',
+        },
+    },
+);
+
+$addtest->(
+    'Test guests by multiple tags',
+    {
+        expected => {
+            node1 => [100, 101, 112],
+            node2 => [200, 201, 213],
+        },
+        param => {
+            tags => 'prod,dev',
+        },
+    },
+);
+
+$addtest->(
+    'Test guests by tag with no matches',
+    {
+        expected => {},
+        param => {
+            tags => 'nonexistent',
+        },
+    },
+);
+
 for my $test (@{$tests}) {
     my $testname = $test->{name};
     my $testdata = $test->{test};
-- 
2.53.0




  parent reply	other threads:[~2026-12-03  9:58 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  9:27 [RFC PATCH guest-common/manager 0/4] vzdump: support tag based guest selection in backup jobs POYEN Alexis
2026-12-03  9:27 ` [RFC PATCH guest-common 1/1] close #4186: vzdump: add 'tags' option to select guests by tag POYEN Alexis
2026-12-03  9:27 ` POYEN Alexis [this message]
2026-12-03  9:27 ` [RFC PATCH manager 2/3] close #4186: api: backup: support tag based guest selection in jobs POYEN Alexis
2026-12-03  9:27 ` [RFC PATCH manager 3/3] close #4186: ui: backup: add tag based selection mode POYEN Alexis

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=20261203092717.1863218-3-alexis.poyen@groupe-cyllene.com \
    --to=alexis.poyen@groupe-cyllene.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal