Clarify _is_pending function use

VIFHandler._is_pending's name is currently misleading, since the
function only returns when 1) the node is in PENDING state; AND 2) the
node has a node assigned.
This commit renames the function to better match implementation, adds a
docstring and a unit-test, that tests for a pod pending without a node
assigned.

Change-Id: I880a579354f1f9bf939ca22fbdcd33d0fc442fcd
This commit is contained in:
Kirill Zaitsev 2017-06-27 11:28:49 +03:00
parent 330985932e
commit ba1f9f3cba
2 changed files with 15 additions and 9 deletions

View File

@ -52,7 +52,7 @@ class VIFHandler(k8s_base.ResourceEventHandler):
self._drv_vif_pool.set_vif_driver(self._drv_vif)
def on_present(self, pod):
if self._is_host_network(pod) or not self._is_pending(pod):
if self._is_host_network(pod) or not self._is_pending_node(pod):
# REVISIT(ivc): consider an additional configurable check that
# would allow skipping pods to enable heterogeneous environments
# where certain pods/namespaces/nodes can be managed by other
@ -96,7 +96,8 @@ class VIFHandler(k8s_base.ResourceEventHandler):
return pod['spec'].get('hostNetwork', False)
@staticmethod
def _is_pending(pod):
def _is_pending_node(pod):
"""Checks if Pod is in PENDGING status and has node assigned."""
try:
return (pod['spec']['nodeName'] and
pod['status']['phase'] == constants.K8S_POD_STATUS_PENDING)

View File

@ -62,12 +62,12 @@ class TestVIFHandler(test_base.TestCase):
self._get_vif = self._handler._get_vif
self._set_vif = self._handler._set_vif
self._is_host_network = self._handler._is_host_network
self._is_pending = self._handler._is_pending
self._is_pending_node = self._handler._is_pending_node
self._request_vif.return_value = self._vif
self._get_vif.return_value = self._vif
self._is_host_network.return_value = False
self._is_pending.return_value = True
self._is_pending_node.return_value = True
self._get_project.return_value = self._project_id
self._get_subnets.return_value = self._subnets
self._get_security_groups.return_value = self._security_groups
@ -113,15 +113,20 @@ class TestVIFHandler(test_base.TestCase):
del pod['spec']['hostNetwork']
self.assertFalse(h_vif.VIFHandler._is_host_network(pod))
def test_is_pending(self):
self.assertTrue(h_vif.VIFHandler._is_pending(self._pod))
def test_is_pending_node(self):
self.assertTrue(h_vif.VIFHandler._is_pending_node(self._pod))
def test_is_not_pending(self):
self._pod['status']['phase'] = 'Unknown'
self.assertFalse(h_vif.VIFHandler._is_pending(self._pod))
self.assertFalse(h_vif.VIFHandler._is_pending_node(self._pod))
def test_is_pending_no_node(self):
self._pod['spec']['nodeName'] = None
self.assertFalse(h_vif.VIFHandler._is_pending_node(self._pod))
def test_unset_pending(self):
self.assertFalse(h_vif.VIFHandler._is_pending({}))
self.assertFalse(h_vif.VIFHandler._is_pending_node({'spec': {},
'status': {}}))
def test_on_present(self):
h_vif.VIFHandler.on_present(self._handler, self._pod)
@ -142,7 +147,7 @@ class TestVIFHandler(test_base.TestCase):
self._set_vif.assert_not_called()
def test_on_present_not_pending(self):
self._is_pending.return_value = False
self._is_pending_node.return_value = False
h_vif.VIFHandler.on_present(self._handler, self._pod)