From 3d652cb710b9ff56c08190cea20e106ddcbb2dac Mon Sep 17 00:00:00 2001 From: Abdullah <5238256+motjuste@users.noreply.github.com> Date: Fri, 24 Nov 2023 17:31:34 +0100 Subject: [PATCH 1/2] Handle None being returned for ingresses --- charms/istio-pilot/src/charm.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) 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: From a761e5b1e29b664dd24510b5ecc53306fc4bd303 Mon Sep 17 00:00:00 2001 From: Abdullah <5238256+motjuste@users.noreply.github.com> Date: Mon, 27 Nov 2023 19:21:45 +0100 Subject: [PATCH 2/2] Add test for istio-pilot charm for loadbalancer returning None for ingresses --- charms/istio-pilot/tests/unit/test_charm.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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.