Merge "Add node replacement hook to LB policy"

This commit is contained in:
Zuul 2019-05-20 17:41:40 +00:00 committed by Gerrit Code Review
commit 2733879ccc
2 changed files with 68 additions and 4 deletions

View File

@ -60,10 +60,12 @@ class LoadBalancingPolicy(base.Policy):
('AFTER', consts.CLUSTER_RESIZE),
('AFTER', consts.NODE_RECOVER),
('AFTER', consts.NODE_CREATE),
('AFTER', consts.CLUSTER_REPLACE_NODES),
('BEFORE', consts.CLUSTER_DEL_NODES),
('BEFORE', consts.CLUSTER_SCALE_IN),
('BEFORE', consts.CLUSTER_RESIZE),
('BEFORE', consts.NODE_DELETE),
('BEFORE', consts.CLUSTER_REPLACE_NODES),
]
PROFILE_TYPE = [
@ -463,6 +465,9 @@ class LoadBalancingPolicy(base.Policy):
count = action.data['deletion']['count']
else: # action.action == consts.CLUSTER_SCALE_IN
count = 1
elif action.action == consts.CLUSTER_REPLACE_NODES:
candidates = list(action.inputs['candidates'].keys())
count = len(candidates)
else:
count = deletion.get('count', 0)
candidates = deletion.get('candidates', None)
@ -550,10 +555,11 @@ class LoadBalancingPolicy(base.Policy):
def _get_post_candidates(self, action):
# This method will parse action data passed from action layer
if (action.action == consts.NODE_CREATE or
action.action == consts.NODE_RECOVER):
candidates = [action.entity.id]
elif action.action == consts.CLUSTER_REPLACE_NODES:
candidates = list(action.inputs['candidates'].values())
else:
creation = action.data.get('creation', None)
candidates = creation.get('nodes', []) if creation else []
@ -645,7 +651,7 @@ class LoadBalancingPolicy(base.Policy):
# TODO(Yanyanhu): Need special handling for cross-az scenario
# which is supported by Neutron lbaas.
candidates = self._get_post_candidates(action)
if len(candidates) == 0:
if not candidates:
return
obj = action.entity
@ -666,5 +672,3 @@ class LoadBalancingPolicy(base.Policy):
error = _('Failed in adding nodes into lb pool: %s') % failed_nodes
action.data['status'] = base.CHECK_ERROR
action.data['reason'] = error
return

View File

@ -828,6 +828,66 @@ class TestLoadBalancingPolicyOperations(base.SenlinTestCase):
m_add.assert_called_once_with(ctx, ['NODE_ID'], cp, self.lb_driver)
self.assertFalse(m_remove.called)
@mock.patch.object(lb_policy.LoadBalancingPolicy, '_add_member')
@mock.patch.object(lb_policy.LoadBalancingPolicy, '_remove_member')
@mock.patch.object(lb_policy.LoadBalancingPolicy, '_get_delete_candidates')
def test_pre_op_node_replace(self, m_get, m_remove, m_add,
m_candidates, m_load):
ctx = mock.Mock()
cid = 'CLUSTER_ID'
cluster = mock.Mock(user='user1', project='project1')
action = mock.Mock(data={}, context=ctx,
action=consts.CLUSTER_REPLACE_NODES,
inputs={'candidates': {
'OLD_NODE_ID': 'NEW_NODE_ID'}})
action.entity = cluster
cp = mock.Mock()
m_load.return_value = cp
m_add.return_value = []
m_get.return_value = ['OLD_NODE_ID']
policy = lb_policy.LoadBalancingPolicy('test-policy', self.spec)
policy._lbaasclient = self.lb_driver
# do it
res = policy.pre_op(cid, action)
# assertion
self.assertIsNone(res)
m_get.assert_called_once_with(cid, action)
m_load.assert_called_once_with(ctx, cid, policy.id)
m_remove.assert_called_once_with(ctx, ['OLD_NODE_ID'],
cp, self.lb_driver)
@mock.patch.object(lb_policy.LoadBalancingPolicy, '_add_member')
@mock.patch.object(lb_policy.LoadBalancingPolicy, '_remove_member')
@mock.patch.object(lb_policy.LoadBalancingPolicy, '_get_post_candidates')
def test_post_op_node_replace(self, m_get, m_remove, m_add,
m_candidates, m_load):
ctx = mock.Mock()
cid = 'CLUSTER_ID'
cluster = mock.Mock(user='user1', project='project1')
action = mock.Mock(data={}, context=ctx,
action=consts.CLUSTER_REPLACE_NODES,
inputs={'candidates': {
'OLD_NODE_ID': 'NEW_NODE_ID'}})
action.entity = cluster
cp = mock.Mock()
m_load.return_value = cp
m_add.return_value = []
m_get.return_value = ['NEW_NODE_ID']
policy = lb_policy.LoadBalancingPolicy('test-policy', self.spec)
policy._lbaasclient = self.lb_driver
# do it
res = policy.post_op(cid, action)
# assertion
self.assertIsNone(res)
m_get.assert_called_once_with(action)
m_load.assert_called_once_with(ctx, cid, policy.id)
m_add.assert_called_once_with(ctx, ['NEW_NODE_ID'], cp, self.lb_driver)
self.assertFalse(m_remove.called)
@mock.patch.object(lb_policy.LoadBalancingPolicy, '_add_member')
@mock.patch.object(lb_policy.LoadBalancingPolicy, '_remove_member')
@mock.patch.object(lb_policy.LoadBalancingPolicy, '_get_post_candidates')