[pve-devel] [PATCH pve-ha-manager 6/8] add resources group

Alexandre Derumier aderumier at odiso.com
Wed Apr 27 17:33:49 CEST 2022


---
 debian/pve-ha-manager.install |  1 +
 src/PVE/HA/Config.pm          | 22 +++++++++
 src/PVE/HA/Env.pm             |  6 +++
 src/PVE/HA/Env/PVE2.pm        |  6 +++
 src/PVE/HA/Makefile           |  2 +-
 src/PVE/HA/Manager.pm         |  1 +
 src/PVE/HA/ResourcesGroups.pm | 90 +++++++++++++++++++++++++++++++++++
 src/PVE/HA/Sim/Env.pm         |  8 ++++
 src/PVE/HA/Sim/Hardware.pm    | 15 ++++++
 src/PVE/HA/Tools.pm           |  6 +++
 10 files changed, 156 insertions(+), 1 deletion(-)
 create mode 100644 src/PVE/HA/ResourcesGroups.pm

diff --git a/debian/pve-ha-manager.install b/debian/pve-ha-manager.install
index 3bc7cc8..a4486eb 100644
--- a/debian/pve-ha-manager.install
+++ b/debian/pve-ha-manager.install
@@ -35,6 +35,7 @@
 /usr/share/perl5/PVE/HA/Manager.pm
 /usr/share/perl5/PVE/HA/NodeStatus.pm
 /usr/share/perl5/PVE/HA/Resources.pm
+/usr/share/perl5/PVE/HA/ResourcesGroups.pm
 /usr/share/perl5/PVE/HA/Resources/PVECT.pm
 /usr/share/perl5/PVE/HA/Resources/PVEVM.pm
 /usr/share/perl5/PVE/HA/Tools.pm
diff --git a/src/PVE/HA/Config.pm b/src/PVE/HA/Config.pm
index 993b456..f9805d2 100644
--- a/src/PVE/HA/Config.pm
+++ b/src/PVE/HA/Config.pm
@@ -6,11 +6,13 @@ use JSON;
 
 use PVE::HA::Tools;
 use PVE::HA::Groups;
+use PVE::HA::ResourcesGroups;
 use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
 use PVE::HA::Resources;
 
 my $manager_status_filename = "ha/manager_status";
 my $ha_groups_config = "ha/groups.cfg";
+my $ha_resources_groups_config = "ha/resources_groups.cfg";
 my $ha_resources_config = "ha/resources.cfg";
 my $crm_commands_filename = "ha/crm_commands";
 my $ha_fence_config = "ha/fence.cfg";
@@ -21,6 +23,9 @@ cfs_register_file($crm_commands_filename,
 cfs_register_file($ha_groups_config,
 		  sub { PVE::HA::Groups->parse_config(@_); },
 		  sub { PVE::HA::Groups->write_config(@_); });
+cfs_register_file($ha_resources_groups_config,
+		  sub { PVE::HA::ResourceGroups->parse_config(@_); },
+		  sub { PVE::HA::ResourceGroups->write_config(@_); });
 cfs_register_file($ha_resources_config,
 		  sub { PVE::HA::Resources->parse_config(@_); },
 		  sub { PVE::HA::Resources->write_config(@_); });
@@ -78,6 +83,12 @@ sub parse_groups_config {
     return PVE::HA::Groups->parse_config($filename, $raw);
 }
 
+sub parse_resources_roups_config {
+    my ($filename, $raw) = @_;
+
+    return PVE::HA::Resources_Groups->parse_config($filename, $raw);
+}
+
 sub parse_resources_config {
     my ($filename, $raw) = @_;
 
@@ -200,6 +211,17 @@ sub write_group_config {
     cfs_write_file($ha_groups_config, $cfg);
 }
 
+sub read_resources_group_config {
+
+    return cfs_read_file($ha_resources_groups_config);
+}
+
+sub write_resources_group_config {
+    my ($cfg) = @_;
+
+    cfs_write_file($ha_resources_groups_config, $cfg);
+}
+
 sub write_resources_config {
     my ($cfg) = @_;
 
diff --git a/src/PVE/HA/Env.pm b/src/PVE/HA/Env.pm
index 757c5e0..0c917e5 100644
--- a/src/PVE/HA/Env.pm
+++ b/src/PVE/HA/Env.pm
@@ -130,6 +130,12 @@ sub read_group_config {
     return $self->{plug}->read_group_config();
 }
 
+sub read_resources_groups_config {
+    my ($self) = @_;
+
+    return $self->{plug}->read_resources_groups_config();
+}
+
 # this should return a hash containing info
 # what nodes are members and online.
 sub get_node_info {
diff --git a/src/PVE/HA/Env/PVE2.pm b/src/PVE/HA/Env/PVE2.pm
index ee97559..6e0a55f 100644
--- a/src/PVE/HA/Env/PVE2.pm
+++ b/src/PVE/HA/Env/PVE2.pm
@@ -189,6 +189,12 @@ sub read_group_config {
     return PVE::HA::Config::read_group_config();
 }
 
+sub read_resources_group_config {
+    my ($self) = @_;
+
+    return PVE::HA::Config::read_resources_groups_config();
+}
+
 # this should return a hash containing info
 # what nodes are members and online.
 sub get_node_info {
diff --git a/src/PVE/HA/Makefile b/src/PVE/HA/Makefile
index a548c86..bafc7c0 100644
--- a/src/PVE/HA/Makefile
+++ b/src/PVE/HA/Makefile
@@ -1,5 +1,5 @@
 SIM_SOURCES=CRM.pm Env.pm Groups.pm Resources.pm LRM.pm Manager.pm \
-	NodeStatus.pm Tools.pm FenceConfig.pm Fence.pm
+	NodeStatus.pm Tools.pm FenceConfig.pm Fence.pm ResourcesGroups.pm
 
 SOURCES=${SIM_SOURCES} Config.pm
 
diff --git a/src/PVE/HA/Manager.pm b/src/PVE/HA/Manager.pm
index 6fa866a..4e318bd 100644
--- a/src/PVE/HA/Manager.pm
+++ b/src/PVE/HA/Manager.pm
@@ -371,6 +371,7 @@ sub manage {
     my $sc = $haenv->read_service_config();
 
     $self->{groups} = $haenv->read_group_config(); # update
+    $self->{vmgroups} = $haenv->read_vmgroup_config();
 
     # compute new service status
 
diff --git a/src/PVE/HA/ResourcesGroups.pm b/src/PVE/HA/ResourcesGroups.pm
new file mode 100644
index 0000000..10f97a6
--- /dev/null
+++ b/src/PVE/HA/ResourcesGroups.pm
@@ -0,0 +1,90 @@
+package PVE::HA::ResourcesGroups;
+
+use strict;
+use warnings;
+
+use PVE::JSONSchema qw(get_standard_option);
+use PVE::SectionConfig;
+use PVE::HA::Tools;
+
+use base qw(PVE::SectionConfig);
+
+my $defaultData = {
+    propertyList => {
+	type => {
+	    description => "Group type.",
+	    optional => 1,
+	},
+	group => get_standard_option('pve-ha-group-id',
+				    { completion => \&PVE::HA::Tools::complete_group }),
+	resources => get_standard_option('pve-ha-resource-id-list'),
+	affinity => {
+	    description => "group or separate vms on same host",
+	    type => 'string',
+	    optional => 1,
+	    enum => ['group', 'separate'],
+	},
+    },
+};
+
+sub type {
+    return 'resourcegroup';
+}
+
+sub options {
+    return {
+	resources => { optional => 0 },
+	affinity => { optional => 1 },
+    };
+}
+
+sub private {
+    return $defaultData;
+}
+
+sub decode_value {
+    my ($class, $type, $key, $value) = @_;
+
+    if ($key eq 'resources') {
+	my $res = {};
+
+	foreach my $resource (PVE::Tools::split_list($value)) {
+	    if (PVE::HA::Tools::pve_verify_ha_resource_id($resource)) {
+		$res->{$resource} = 1;
+	    }
+	}
+
+	return $res;
+    }
+
+   return $value;
+}
+
+sub encode_value {
+    my ($class, $type, $key, $value) = @_;
+
+    if ($key eq 'resources') {
+        return join(',', keys(%$value));
+    }
+
+    return $value;
+}
+
+sub parse_section_header {
+    my ($class, $line) = @_;
+
+    if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
+	my ($type, $group) = (lc($1), $2);
+	my $errmsg = undef; # set if you want to skip whole section
+	eval { PVE::JSONSchema::pve_verify_configid($group); };
+	$errmsg = $@ if $@;
+	my $config = {}; # to return additional attributes
+	return ($type, $group, $errmsg, $config);
+    }
+    return undef;
+}
+
+__PACKAGE__->register();
+__PACKAGE__->init();
+
+1;
diff --git a/src/PVE/HA/Sim/Env.pm b/src/PVE/HA/Sim/Env.pm
index b286708..c3c541b 100644
--- a/src/PVE/HA/Sim/Env.pm
+++ b/src/PVE/HA/Sim/Env.pm
@@ -253,6 +253,14 @@ sub read_group_config {
     return $self->{hardware}->read_group_config();
 }
 
+sub read_resources_groups_config {
+    my ($self) = @_;
+
+    $assert_cfs_can_rw->($self);
+
+    return $self->{hardware}->read_resources_groups_config();
+}
+
 # this is normally only allowed by the master to recover a _fenced_ service
 sub steal_service {
     my ($self, $sid, $current_node, $new_node) = @_;
diff --git a/src/PVE/HA/Sim/Hardware.pm b/src/PVE/HA/Sim/Hardware.pm
index 3c3622b..70c2031 100644
--- a/src/PVE/HA/Sim/Hardware.pm
+++ b/src/PVE/HA/Sim/Hardware.pm
@@ -18,6 +18,7 @@ use POSIX qw(strftime EINTR);
 
 use PVE::HA::FenceConfig;
 use PVE::HA::Groups;
+use PVE::HA::ResourcesGroups;
 
 my $watchdog_timeout = 60;
 
@@ -397,6 +398,16 @@ sub read_group_config {
     return PVE::HA::Groups->parse_config($filename, $raw);
 }
 
+sub read_resources_groups_config {
+    my ($self) = @_;
+
+    my $filename = "$self->{statusdir}/resources_groups";
+    my $raw = '';
+    $raw = PVE::Tools::file_get_contents($filename) if -f $filename;
+
+    return PVE::HA::ResourcesGroups->parse_config($filename, $raw);
+}
+
 sub read_service_status {
     my ($self, $node) = @_;
 
@@ -455,6 +466,10 @@ sub new {
 	PVE::Tools::file_set_contents("$statusdir/groups", $default_group_config);
     }
 
+    if (-f "$testdir/resources_groups") {
+	copy("$testdir/resources_groups", "$statusdir/resources_groups");
+    }
+
     if (-f "$testdir/service_config") {
 	copy("$testdir/service_config", "$statusdir/service_config");
     } else {
diff --git a/src/PVE/HA/Tools.pm b/src/PVE/HA/Tools.pm
index 1a88351..5b9823a 100644
--- a/src/PVE/HA/Tools.pm
+++ b/src/PVE/HA/Tools.pm
@@ -44,6 +44,12 @@ PVE::JSONSchema::register_standard_option('pve-ha-resource-id', {
     type => 'string', format => 'pve-ha-resource-id',
 });
 
+PVE::JSONSchema::register_standard_option('pve-ha-resource-id-list', {
+    description => "List of resources",
+    type => 'string', format => 'pve-ha-resource-id-list',
+    typetext => '<resource>{,<resource>}*',
+});
+
 PVE::JSONSchema::register_format('pve-ha-resource-or-vm-id', \&pve_verify_ha_resource_or_vm_id);
 sub pve_verify_ha_resource_or_vm_id {
     my ($sid, $noerr) = @_;
-- 
2.30.2





More information about the pve-devel mailing list