diff --git a/charms/istio-pilot/src/charm.py b/charms/istio-pilot/src/charm.py index 95923746..585ddf85 100755 --- a/charms/istio-pilot/src/charm.py +++ b/charms/istio-pilot/src/charm.py @@ -827,11 +827,10 @@ def _get_address_from_loadbalancer(svc): (str): The hostname or IP address of the LoadBalancer service """ ingresses = svc.status.loadBalancer.ingress - if len(ingresses) != 1: - if len(ingresses) == 0: - return None - else: - raise ValueError("Unknown situation - LoadBalancer service has more than one ingress") + if ingresses is None or len(ingresses) == 0: + return None + elif len(ingresses) != 1: + raise ValueError("Unknown situation - LoadBalancer service has more than one ingress") ingress = svc.status.loadBalancer.ingress[0] if getattr(ingress, "hostname", None) is not None: diff --git a/charms/istio-pilot/tests/unit/test_charm.py b/charms/istio-pilot/tests/unit/test_charm.py index 70109784..42be286c 100644 --- a/charms/istio-pilot/tests/unit/test_charm.py +++ b/charms/istio-pilot/tests/unit/test_charm.py @@ -513,6 +513,7 @@ def test_is_gateway_object_up( ("mock_loadbalancer_ip_service", True), ("mock_loadbalancer_hostname_service_not_ready", False), ("mock_loadbalancer_ip_service_not_ready", False), + ("mock_loadbalancer_service_not_ready_returning_none_for_ingresses", False), ], ) def test_is_gateway_service_up( @@ -542,6 +543,7 @@ def test_is_gateway_service_up( ("mock_loadbalancer_ip_service", "127.0.0.1"), ("mock_loadbalancer_hostname_service_not_ready", None), ("mock_loadbalancer_ip_service_not_ready", None), + ("mock_loadbalancer_service_not_ready_returning_none_for_ingresses", None), ], ) def test_get_gateway_address_from_svc( @@ -1435,6 +1437,18 @@ def mock_loadbalancer_hostname_service_not_ready(): ) return mock_nodeport_service +@pytest.fixture() +def mock_loadbalancer_service_not_ready_returning_none_for_ingresses(): + mock_nodeport_service = codecs.from_dict( + { + "apiVersion": "v1", + "kind": "Service", + "status": {"loadBalancer": {"ingress": None}}, + "spec": {"type": "LoadBalancer", "clusterIP": "10.10.10.10"}, + } + ) + return mock_nodeport_service + # autouse to ensure we don't accidentally call out, but # can also be used explicitly to get access to the mock.