Remove hosts if can't find hosts in host agrregate

Change-Id: Ie2dc20d8068f6d945a487238a0b6e5bc2f72fa3b
Closes-Bug: #1789366
This commit is contained in:
licanwei 2018-08-29 13:18:55 +08:00
parent f93225b75c
commit d822a0f37e
3 changed files with 28 additions and 0 deletions

View File

@ -167,6 +167,7 @@ class ComputeScope(base.BaseScope):
instance_metadata = []
projects_to_exclude = []
compute_scope = []
found_nothing_flag = False
model_hosts = list(cluster_model.get_all_compute_nodes().keys())
if not self.scope:
@ -182,9 +183,13 @@ class ComputeScope(base.BaseScope):
if 'host_aggregates' in rule:
self._collect_aggregates(rule['host_aggregates'],
allowed_nodes)
if not allowed_nodes:
found_nothing_flag = True
elif 'availability_zones' in rule:
self._collect_zones(rule['availability_zones'],
allowed_nodes)
if not allowed_nodes:
found_nothing_flag = True
elif 'exclude' in rule:
self.exclude_resources(
rule['exclude'], instances=instances_to_exclude,
@ -195,6 +200,10 @@ class ComputeScope(base.BaseScope):
instances_to_exclude = set(instances_to_exclude)
if allowed_nodes:
nodes_to_remove = set(model_hosts) - set(allowed_nodes)
# This branch means user set host_aggregates and/or availability_zones
# but can't find any nodes, so we should remove all nodes.
elif found_nothing_flag:
nodes_to_remove = set(model_hosts)
nodes_to_remove.update(nodes_to_exclude)
self.remove_nodes_from_model(nodes_to_remove, cluster_model)

View File

@ -47,3 +47,9 @@ fake_scope_2 = [{'storage': [{'availability_zones': [{'name': 'zone_0'}]},
]}]
}
]
fake_scope_3 = [{'compute': [{'host_aggregates': [{'id': '1'}]},
{'exclude': []
}]
}
]

View File

@ -281,3 +281,16 @@ class TestComputeScope(base.TestCase):
model.get_instance_by_uuid('INSTANCE_0').watcher_exclude)
self.assertTrue(
model.get_instance_by_uuid('INSTANCE_1').watcher_exclude)
@mock.patch.object(nova_helper.NovaHelper, 'get_aggregate_detail')
@mock.patch.object(nova_helper.NovaHelper, 'get_aggregate_list')
def test_get_scoped_model_with_hostaggregate_null(
self, mock_list, mock_detail):
cluster = self.fake_cluster.generate_scenario_1()
audit_scope = fake_scopes.fake_scope_3
mock_list.return_value = [mock.Mock(id=i,
name="HA_{0}".format(i))
for i in range(2)]
model = compute.ComputeScope(audit_scope, mock.Mock(),
osc=mock.Mock()).get_scoped_model(cluster)
self.assertEqual(0, len(model.edges()))