From 2df14cb44ed547b2d53c3477184aa6f87351b9e6 Mon Sep 17 00:00:00 2001 From: Florian Paul Azim Hoberg Date: Thu, 25 Jul 2024 08:42:07 +0200 Subject: [PATCH] feature: Add option to select best node by free resources in percent. Fixes: #29 --- README.md | 2 +- proxlb | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 045b41d..a892eae 100644 --- a/README.md +++ b/README.md @@ -102,7 +102,7 @@ The following options can be set in the `proxlb.conf` file: | api_pass | FooBar | Password for the API. | | verify_ssl | 1 | Validate SSL certificates (1) or ignore (0). (default: 1) | | method | memory | Defines the balancing method (default: memory) where you can use `memory`, `disk` or `cpu`. | -| mode | used | Rebalance by `used` resources (efficiency) or `assigned` (avoid overprovisioning) resources. (default: used)| +| mode | used | Rebalance by `used` resources (efficiency) or `assigned` (avoid over provisioning) resources. (default: used)| | type | vm | Rebalance only `vm` (virtual machines), `ct` (containers) or `all` (virtual machines & containers). (default: vm)| | balanciness | 10 | Value of the percentage of lowest and highest resource consumption on nodes may differ before rebalancing. (default: 10) | | ignore_nodes | dummynode01,dummynode02,test* | Defines a comma separated list of nodes to exclude. | diff --git a/proxlb b/proxlb index ef3db12..0b46ad2 100755 --- a/proxlb +++ b/proxlb @@ -495,11 +495,11 @@ def __validate_balancing_mode(balancing_mode): error_prefix = 'Error: [balancing-mode-validation]:' info_prefix = 'Info: [balancing-mode-validation]]:' - if balancing_mode not in ['used', 'assigned']: - logging.error(f'{error_prefix} Invalid balancing method: {balancing_mode}') + if balancing_mode not in ['used', 'free_percent', 'assigned']: + logging.error(f'{error_prefix} Invalid balancing mode: {balancing_mode}') sys.exit(2) else: - logging.info(f'{info_prefix} Valid balancing method: {balancing_mode}') + logging.info(f'{info_prefix} Valid balancing mode: {balancing_mode}') def __validate_balanciness(balanciness, balancing_method, balancing_mode, node_statistics): @@ -511,6 +511,8 @@ def __validate_balanciness(balanciness, balancing_method, balancing_mode, node_s # Remap balancing mode to get the related values from nodes dict. if balancing_mode == 'used': node_resource_selector = 'free' + if balancing_mode == 'free_percent': + node_resource_selector = 'free' if balancing_mode == 'assigned': node_resource_selector = 'assigned' @@ -556,6 +558,8 @@ def __get_most_used_resources_vm(balancing_method, balancing_mode, vm_statistics # Remap balancing mode to get the related values from nodes dict. if balancing_mode == 'used': vm_resource_selector = 'used' + if balancing_mode == 'free_percent': + vm_resource_selector = 'used' if balancing_mode == 'assigned': vm_resource_selector = 'total' @@ -573,6 +577,8 @@ def __get_most_free_resources_node(balancing_method, balancing_mode, node_statis # Return the node information based on the balancing mode. if balancing_mode == 'used': node = max(node_statistics.items(), key=lambda item: item[1][f'{balancing_method}_free']) + if balancing_mode == 'free_percent': + node = max(node_statistics.items(), key=lambda item: item[1][f'{balancing_method}_free_percent']) if balancing_mode == 'assigned': node = min(node_statistics.items(), key=lambda item: item[1][f'{balancing_method}_assigned'] if item[1][f'{balancing_method}_assigned_percent'] > 0 or item[1][f'{balancing_method}_assigned_percent'] < 100 else -float('inf'))