k8s: Fix logic of when a cluster API is accessible

At present, if floating_ip_enabled is true and master_lb_enabled is also
true but master_lb_fip_enabled is false, a cluster still resolves to
being accessible therefore resolves to an UNHEALTHY status when it
should be UNKNOWN. This patch fixes this edge case and also adds a unit
test to capture this issue.

Story: 2007242
Task: 39140

Change-Id: I74f7455e3caa920032080747a315470878ba5500
(cherry picked from commit fbaba6e001)
This commit is contained in:
Bharat Kunwar 2020-03-24 09:28:47 +00:00 committed by Bharat Kunwar
parent 6ae483846d
commit 5d74dfa161
2 changed files with 25 additions and 5 deletions

View File

@ -61,11 +61,12 @@ class K8sMonitor(monitors.MonitorBase):
self.data['health_status_reason'] = reason
def _is_cluster_accessible(self):
lb_fip = self.cluster.labels.get("master_lb_floating_ip_enabled")
lb_fip_enabled = strutils.bool_from_string(lb_fip)
return (self.cluster.floating_ip_enabled or
(lb_fip_enabled and
self.cluster.cluster_template.master_lb_enabled))
if self.cluster.cluster_template.master_lb_enabled:
lb_fip = self.cluster.labels.get("master_lb_floating_ip_enabled",
self.cluster.floating_ip_enabled)
return strutils.bool_from_string(lb_fip)
else:
return self.cluster.floating_ip_enabled
def _compute_res_util(self, res):
res_total = 0

View File

@ -52,6 +52,10 @@ class MonitorsTestCase(base.TestCase):
master_addresses=['10.0.0.6'],
labels={})
self.cluster = objects.Cluster(self.context, **cluster)
cluster_template = (
utils.get_test_cluster_template(master_lb_enabled=False))
self.cluster.cluster_template = (
objects.ClusterTemplate(self.context, **cluster_template))
nodegroups = utils.get_nodegroups_for_cluster(
node_addresses=['1.2.3.4'], master_addresses=['10.0.0.6'])
self.nodegroups = [
@ -523,3 +527,18 @@ class MonitorsTestCase(base.TestCase):
self.k8s_monitor.poll_health_status()
self.assertEqual(self.k8s_monitor.data['health_status'],
m_fields.ClusterHealthStatus.UNKNOWN)
@mock.patch('magnum.conductor.k8s_api.create_k8s_api')
def test_k8s_monitor_health_unreachable_with_master_lb(self, mock_k8s_api):
mock_nodes = mock.MagicMock()
mock_node = mock.MagicMock()
mock_node.status = mock.MagicMock()
mock_nodes.items = [mock_node]
cluster = self.k8s_monitor.cluster
cluster.floating_ip_enabled = True
cluster.cluster_template.master_lb_enabled = True
cluster.labels['master_lb_floating_ip_enabled'] = False
self.k8s_monitor.poll_health_status()
self.assertEqual(self.k8s_monitor.data['health_status'],
m_fields.ClusterHealthStatus.UNKNOWN)