[pve-devel] [PATCH ha-manager v2 11/26] manager: migrate ha groups to location rules in-memory
Daniel Kral
d.kral at proxmox.com
Fri Jun 20 16:31:23 CEST 2025
Migrate the currently configured HA groups to HA Location rules
in-memory if the use-location-rules feature flag isn't set, so that they
can be applied as such in the next patches and therefore replace HA
groups internally.
Also ignore location rules written to the rules config if the
use-location-rules isn't set, because these should be mutually exclusive
so that users can only use HA groups or HA location rules.
Location rules in their initial implementation are designed to be as
restrictive as HA groups, i.e. only allow a service to be used in a
single location rule, to ease the migration between them.
HA groups map directly to location rules, except that the 'restricted'
property is renamed to 'strict', so the name is the same as for
colocation rules, and that the 'failback' property is moved to the
service config.
The 'nofailback' property is moved to the service config, because it
allows users to set it more granularly for individual services and
allows the location rules to be more extendible in the future, e.g.
multiple location rules for a single service.
Signed-off-by: Daniel Kral <d.kral at proxmox.com>
---
changes since v1:
- NEW!
src/PVE/HA/Config.pm | 3 ++-
src/PVE/HA/Groups.pm | 48 ++++++++++++++++++++++++++++++++++++
src/PVE/HA/Manager.pm | 24 ++++++++++++++++--
src/PVE/HA/Rules/Location.pm | 17 +++++++++++++
4 files changed, 89 insertions(+), 3 deletions(-)
diff --git a/src/PVE/HA/Config.pm b/src/PVE/HA/Config.pm
index 1b67443..2b3d726 100644
--- a/src/PVE/HA/Config.pm
+++ b/src/PVE/HA/Config.pm
@@ -131,7 +131,8 @@ sub read_and_check_resources_config {
}
}
- return $conf;
+ # TODO PVE 10: Remove digest when HA groups have been fully migrated to location rules
+ return wantarray ? ($conf, $res->{digest}) : $conf;
}
sub update_resources_config {
diff --git a/src/PVE/HA/Groups.pm b/src/PVE/HA/Groups.pm
index 821d969..d0f5721 100644
--- a/src/PVE/HA/Groups.pm
+++ b/src/PVE/HA/Groups.pm
@@ -107,4 +107,52 @@ sub parse_section_header {
__PACKAGE__->register();
__PACKAGE__->init();
+# Migrate nofailback from group config to service config
+sub migrate_groups_to_services {
+ my ($groups, $services) = @_;
+
+ for my $sid (keys %$services) {
+ my $groupid = $services->{$sid}->{group}
+ or next; # skip services without groups
+
+ # name it 'failback' to remove the double negation
+ $services->{$sid}->{failback} = !$groups->{ids}->{$groupid}->{nofailback};
+ }
+}
+
+# Migrate groups from group config to location rules in rules config
+sub migrate_groups_to_rules {
+ my ($rules, $groups, $services) = @_;
+
+ my $order = (sort { $a <=> $b } values %{ $rules->{order} })[0] || 0;
+
+ my $group_services = {};
+
+ for my $sid (keys %$services) {
+ my $groupid = $services->{$sid}->{group}
+ or next; # skip services without groups
+
+ $group_services->{$groupid}->{$sid} = 1;
+ }
+
+ while (my ($group, $services) = each %$group_services) {
+ # prefix generated rules with '_' as those are not allowed for user-provided ruleids
+ my $ruleid = "_group_$group";
+ my $nodes = {};
+ for my $entry (keys %{ $groups->{ids}->{$group}->{nodes} }) {
+ my ($node, $priority) = PVE::HA::Tools::parse_node_priority($entry);
+
+ $nodes->{$node} = { priority => $priority };
+ }
+
+ $rules->{order}->{$ruleid} = ++$order;
+ $rules->{ids}->{$ruleid} = {
+ type => 'location',
+ services => $services,
+ nodes => $nodes,
+ strict => $groups->{ids}->{$group}->{restricted},
+ };
+ }
+}
+
1;
diff --git a/src/PVE/HA/Manager.pm b/src/PVE/HA/Manager.pm
index 08c2fd3..5ae8da1 100644
--- a/src/PVE/HA/Manager.pm
+++ b/src/PVE/HA/Manager.pm
@@ -6,6 +6,7 @@ use warnings;
use Digest::MD5 qw(md5_base64);
use PVE::Tools;
+use PVE::HA::Groups;
use PVE::HA::Tools ':exit_codes';
use PVE::HA::NodeStatus;
use PVE::HA::Rules;
@@ -48,6 +49,8 @@ sub new {
haenv => $haenv,
crs => {},
last_rules_digest => '',
+ last_groups_digest => '',
+ last_services_digest => '',
}, $class;
my $old_ms = $haenv->read_manager_status();
@@ -530,7 +533,7 @@ sub manage {
$self->update_crs_scheduler_mode();
- my $sc = $haenv->read_service_config();
+ my ($sc, $services_digest) = $haenv->read_service_config();
$self->{groups} = $haenv->read_group_config(); # update
@@ -565,7 +568,22 @@ sub manage {
my $new_rules = $haenv->read_rules_config();
- if ($new_rules->{digest} ne $self->{last_rules_digest}) {
+ my $dc_cfg = $haenv->get_datacenter_settings();
+ my $use_location_rules = $dc_cfg->{ha}->{'use-location-rules'};
+
+ # TODO PVE 10: Remove group migration when HA groups have been fully migrated to location rules
+ PVE::HA::Groups::migrate_groups_to_services($self->{groups}, $sc) if !$use_location_rules;
+
+ if (
+ $new_rules->{digest} ne $self->{last_rules_digest}
+ || $self->{groups}->{digest} ne $self->{last_groups_digest}
+ || $services_digest && $services_digest ne $self->{last_services_digest}
+ ) {
+
+ if (!$use_location_rules) {
+ PVE::HA::Rules::Location::delete_location_rules($new_rules);
+ PVE::HA::Groups::migrate_groups_to_rules($new_rules, $self->{groups}, $sc);
+ }
my $messages = PVE::HA::Rules->canonicalize($new_rules);
$haenv->log('info', $_) for @$messages;
@@ -573,6 +591,8 @@ sub manage {
$self->{rules} = $new_rules;
$self->{last_rules_digest} = $self->{rules}->{digest};
+ $self->{last_groups_digest} = $self->{groups}->{digest};
+ $self->{last_services_digest} = $services_digest;
}
$self->update_crm_commands();
diff --git a/src/PVE/HA/Rules/Location.pm b/src/PVE/HA/Rules/Location.pm
index 67f0b32..b9f76c7 100644
--- a/src/PVE/HA/Rules/Location.pm
+++ b/src/PVE/HA/Rules/Location.pm
@@ -203,4 +203,21 @@ __PACKAGE__->register_check(
},
);
+=head1 LOCATION RULE HELPERS
+
+=cut
+
+# Remove location rules from rules config
+# TODO PVE 10: Can be removed if use-location-rules feature flag is not needed anymore
+sub delete_location_rules {
+ my ($rules) = @_;
+
+ for my $ruleid (keys %{ $rules->{ids} }) {
+ next if $rules->{ids}->{$ruleid}->{type} ne 'location';
+
+ delete $rules->{ids}->{$ruleid};
+ delete $rules->{order}->{$ruleid};
+ }
+}
+
1;
--
2.39.5
More information about the pve-devel
mailing list