diff --git a/sunbeam-python/sunbeam/provider/maas/commands.py b/sunbeam-python/sunbeam/provider/maas/commands.py index 09827bf71..bf5f7a44c 100644 --- a/sunbeam-python/sunbeam/provider/maas/commands.py +++ b/sunbeam-python/sunbeam/provider/maas/commands.py @@ -115,6 +115,7 @@ MachineComputeNicCheck, MachineNetworkCheck, MachineRequirementsCheck, + MachineRoleCombinationCheck, MachineRolesCheck, MachineRootDiskCheck, MachineStorageCheck, @@ -1616,6 +1617,7 @@ def validate_machine_cmd(ctx: click.Context, machine: str): sys.exit(1) validation_checks = [ MachineRolesCheck(machine_obj), + MachineRoleCombinationCheck(machine_obj), MachineNetworkCheck(deployment, machine_obj), MachineStorageCheck(machine_obj), MachineComputeNicCheck(machine_obj), diff --git a/sunbeam-python/sunbeam/provider/maas/steps.py b/sunbeam-python/sunbeam/provider/maas/steps.py index 5d8525673..89f63b4bc 100644 --- a/sunbeam-python/sunbeam/provider/maas/steps.py +++ b/sunbeam-python/sunbeam/provider/maas/steps.py @@ -56,7 +56,7 @@ from sunbeam.core.manifest import Manifest from sunbeam.core.steps import CreateLoadBalancerIPPoolsStep from sunbeam.core.terraform import TerraformHelper -from sunbeam.feature_gates import split_roles_enabled +from sunbeam.feature_gates import FEATURE_GATES, split_roles_enabled from sunbeam.lazy import LazyImport from sunbeam.provider.common import nic_utils from sunbeam.steps import clusterd @@ -94,6 +94,53 @@ MACHINE_DEPLOY_TIMEOUT = 3600 +SPLIT_ROLES_GATE = "feature.split-roles" + + +def _compute_network_roles_conflict_error() -> str: + """Explain why compute and network roles conflict and how to fix it. + + The split-roles gate cannot be enabled on its own (it is rejected unless + the gates it requires, e.g. feature.microovn-sdn, are enabled too), so the + gates to enable are derived from FEATURE_GATES rather than hard-coded. + """ + required = FEATURE_GATES.get(SPLIT_ROLES_GATE, {}).get("requires", []) + required_gates = list(required) if isinstance(required, list) else [] + commands = "\n".join( + f" sudo snap set openstack {gate}=true" + for gate in [*required_gates, SPLIT_ROLES_GATE] + ) + compute = maas_deployment.RoleTags.COMPUTE.value + network = maas_deployment.RoleTags.NETWORK.value + requires_note = ( + f" Split roles requires {', '.join(required_gates)}, so enable" + f" {'it' if len(required_gates) == 1 else 'them'} first:" + if required_gates + else "" + ) + return ( + f"The {compute} and {network} roles are mutually exclusive unless" + f" {SPLIT_ROLES_GATE} is enabled.\n" + f"Remove the {compute} or {network} tag from the machine in MAAS.\n" + f"To keep both roles on the machine, enable split roles" + f" instead.{requires_note}\n" + f"{commands}\n" + ) + + +def has_conflicting_compute_network_roles(roles: Sequence[str]) -> bool: + """Return True if roles combine compute and network without split roles. + + Without the split-roles feature gate, compute and network roles are + mutually exclusive, matching the local provider's bootstrap/join checks. + """ + return ( + maas_deployment.RoleTags.COMPUTE.value in roles + and maas_deployment.RoleTags.NETWORK.value in roles + and not split_roles_enabled() + ) + + class AddMaasDeployment(BaseStep): """Perform various checks and add MAAS-backed deployment.""" @@ -400,6 +447,32 @@ def run(self) -> DiagnosticsResult: ) +class MachineRoleCombinationCheck(DiagnosticsCheck): + """Check machine is not assigned roles that cannot be combined.""" + + def __init__(self, machine: dict): + super().__init__( + "Role combination check", + "Checking role combination", + ) + self.machine = machine + + def run(self) -> DiagnosticsResult: + """Fail if compute and network roles are combined without split roles.""" + assigned_roles = self.machine["roles"] + if has_conflicting_compute_network_roles(assigned_roles): + return DiagnosticsResult.fail( + self.name, + "machine has both compute and network roles.", + diagnostics=_compute_network_roles_conflict_error(), + machine=self.machine["hostname"], + ) + return DiagnosticsResult.success( + self.name, + machine=self.machine["hostname"], + ) + + class MachineComputeNicCheck(DiagnosticsCheck): """Check machine has a neutron-tagged nic assigned if required.""" @@ -660,6 +733,7 @@ def run(self) -> list[DiagnosticsResult]: checks: list[DiagnosticsCheck] = [] for machine in self.machines: checks.append(MachineRolesCheck(machine)) + checks.append(MachineRoleCombinationCheck(machine)) checks.append(MachineNetworkCheck(self.deployment, machine)) checks.append(MachineStorageCheck(machine)) checks.append(MachineComputeNicCheck(machine)) @@ -1440,6 +1514,18 @@ def is_skip(self, context: StepContext) -> Result: LOG.debug("Machines containing worker roles: %s", filtered_machines) if filtered_machines is None or len(filtered_machines) == 0: return Result(ResultType.FAILED, "Maas deployment has no machines.") + conflicting = sorted( + machine["hostname"] + for machine in filtered_machines + if has_conflicting_compute_network_roles(machine["roles"]) + ) + if conflicting: + return Result( + ResultType.FAILED, + f"Machines with both compute and network roles: " + f"{', '.join(conflicting)}.\n" + f"{_compute_network_roles_conflict_error()}", + ) clusterd_nodes = self.client.cluster.list_nodes() nodes_to_update = [] for node in clusterd_nodes: diff --git a/sunbeam-python/tests/unit/sunbeam/provider/maas/test_maas.py b/sunbeam-python/tests/unit/sunbeam/provider/maas/test_maas.py index 7c146580b..6a97382e1 100644 --- a/sunbeam-python/tests/unit/sunbeam/provider/maas/test_maas.py +++ b/sunbeam-python/tests/unit/sunbeam/provider/maas/test_maas.py @@ -32,6 +32,7 @@ from sunbeam.provider.maas.steps import ( ActionFailedException, AddMaasDeployment, + DeploymentMachinesCheck, DeploymentRolesCheck, IpRangesCheck, MaasAddMachinesToClusterdStep, @@ -47,6 +48,7 @@ MachineComputeNicCheck, MachineNetworkCheck, MachineRequirementsCheck, + MachineRoleCombinationCheck, MachineRolesCheck, MachineRootDiskCheck, MachineStorageCheck, @@ -303,6 +305,87 @@ def test_run_with_no_assigned_roles(self): assert result.message and "machine has no role assigned" in result.message +class TestMachineRoleCombinationCheck: + def test_compute_and_network_split_roles_disabled(self): + machine = { + "hostname": "test_machine", + "roles": [RoleTags.COMPUTE.value, RoleTags.NETWORK.value], + } + result = MachineRoleCombinationCheck(machine).run() + assert result.passed is DiagnosticResultType.FAILURE + assert result.details["machine"] == "test_machine" + assert result.message == "machine has both compute and network roles." + + def test_diagnostics_enable_required_gates_before_split_roles(self): + machine = { + "hostname": "test_machine", + "roles": [RoleTags.COMPUTE.value, RoleTags.NETWORK.value], + } + result = MachineRoleCombinationCheck(machine).run() + diagnostics = result.diagnostics + assert diagnostics is not None + assert "Remove the compute or network tag" in diagnostics + microovn = "sudo snap set openstack feature.microovn-sdn=true" + split_roles = "sudo snap set openstack feature.split-roles=true" + assert microovn in diagnostics + assert split_roles in diagnostics + assert diagnostics.index(microovn) < diagnostics.index(split_roles) + + @patch("sunbeam.provider.maas.steps.split_roles_enabled", return_value=True) + def test_compute_and_network_split_roles_enabled(self, _): + machine = { + "hostname": "test_machine", + "roles": [RoleTags.COMPUTE.value, RoleTags.NETWORK.value], + } + result = MachineRoleCombinationCheck(machine).run() + assert result.passed is DiagnosticResultType.SUCCESS + + @pytest.mark.parametrize( + "roles", + [ + [], + [RoleTags.COMPUTE.value, RoleTags.STORAGE.value], + [RoleTags.CONTROL.value, RoleTags.NETWORK.value], + ], + ) + def test_non_conflicting_roles(self, roles): + machine = {"hostname": "test_machine", "roles": roles} + result = MachineRoleCombinationCheck(machine).run() + assert result.passed is DiagnosticResultType.SUCCESS + + def test_machine_roles_check_unaffected(self): + """The generic roles check keeps reporting the assigned roles.""" + machine = { + "hostname": "test_machine", + "roles": [RoleTags.COMPUTE.value, RoleTags.NETWORK.value], + } + result = MachineRolesCheck(machine).run() + assert result.passed is DiagnosticResultType.SUCCESS + + +class TestDeploymentMachinesCheckRoleCombination: + def test_runs_role_combination_check_per_machine(self, mocker): + run_check_list = mocker.patch( + "sunbeam.provider.maas.steps._run_check_list", return_value=[] + ) + machines = [ + {"hostname": "node1", "roles": [RoleTags.CONTROL.value]}, + { + "hostname": "node2", + "roles": [RoleTags.COMPUTE.value, RoleTags.NETWORK.value], + }, + ] + DeploymentMachinesCheck(Mock(), machines).run() + checks = run_check_list.call_args.args[0] + combination_checks = [ + check for check in checks if isinstance(check, MachineRoleCombinationCheck) + ] + assert [check.machine["hostname"] for check in combination_checks] == [ + "node1", + "node2", + ] + + class TestValidateDeploymentCommand: def test_exit_nonzero_on_failed_checks(self, mocker): runner = CliRunner() @@ -410,6 +493,33 @@ def test_exit_zero_on_successful_checks(self, mocker): assert result.exit_code == 0 + def test_runs_role_combination_check(self, mocker): + runner = CliRunner() + deployment = MaasDeployment(name="test", token="token", url="url") + + mocker.patch("sunbeam.provider.maas.commands.run_preflight_checks") + mocker.patch( + "sunbeam.provider.maas.commands.MaasClient.from_deployment", + return_value=Mock(), + ) + mocker.patch( + "sunbeam.provider.maas.commands.get_machine", + return_value={"hostname": "node1"}, + ) + run_checks = mocker.patch( + "sunbeam.provider.maas.commands._run_maas_checks", + return_value=[{"passed": DiagnosticResultType.SUCCESS.value}], + ) + mocker.patch( + "sunbeam.provider.maas.commands._save_report", + return_value="/tmp/report.yaml", + ) + + runner.invoke(validate_machine_cmd, ["node1"], obj=deployment) + + checks = run_checks.call_args.args[0] + assert any(isinstance(c, MachineRoleCombinationCheck) for c in checks) + def test_run_with_not_storage_node(self): machine = { "hostname": "test_machine", @@ -1118,6 +1228,50 @@ def test_is_skip_with_filtered_machines( result = maas_add_machines_to_clusterd_step.is_skip(step_context) assert result.result_type == ResultType.COMPLETED + def test_is_skip_with_compute_and_network_roles_split_roles_disabled( + self, + mocker, + maas_add_machines_to_clusterd_step, + step_context, + ): + mocker.patch( + "sunbeam.provider.maas.client.list_machines", + return_value=[ + {"hostname": "machine1", "roles": [RoleTags.CONTROL.value]}, + { + "hostname": "machine2", + "roles": [RoleTags.COMPUTE.value, RoleTags.NETWORK.value], + }, + ], + ) + maas_add_machines_to_clusterd_step.client.cluster.list_nodes.return_value = [] + result = maas_add_machines_to_clusterd_step.is_skip(step_context) + assert result.result_type == ResultType.FAILED + assert "machine2" in result.message + assert "machine1" not in result.message + maas_add_machines_to_clusterd_step.client.cluster.list_nodes.assert_not_called() + + @patch("sunbeam.provider.maas.steps.split_roles_enabled", return_value=True) + def test_is_skip_with_compute_and_network_roles_split_roles_enabled( + self, + _, + mocker, + maas_add_machines_to_clusterd_step, + step_context, + ): + mocker.patch( + "sunbeam.provider.maas.client.list_machines", + return_value=[ + { + "hostname": "machine1", + "roles": [RoleTags.COMPUTE.value, RoleTags.NETWORK.value], + }, + ], + ) + maas_add_machines_to_clusterd_step.client.cluster.list_nodes.return_value = [] + result = maas_add_machines_to_clusterd_step.is_skip(step_context) + assert result.result_type == ResultType.COMPLETED + def test_run_with_no_machines_and_nodes( self, maas_add_machines_to_clusterd_step, step_context ):