Skip to content

Commit

Permalink
fix: use internal UPF hostname when MetalLB is not available (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
dariofaccin authored Nov 16, 2023
1 parent 770ef06 commit 0262507
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,25 +737,25 @@ def _get_gnb_subnet_config(self) -> Optional[str]:
def _get_external_upf_hostname_config(self) -> Optional[str]:
return self.model.config.get("external-upf-hostname")

def _upf_load_balancer_service_hostname(self) -> str:
def _upf_load_balancer_service_hostname(self) -> Optional[str]:
"""Returns the hostname of UPF's LoadBalancer service.
Returns:
str: Hostname of UPF's LoadBalancer service
str/None: Hostname of UPF's LoadBalancer service if available else None
"""
client = Client()
service = client.get(
Service, name=f"{self.model.app.name}-external", namespace=self.model.name
)
try:
return service.status.loadBalancer.ingress[0].hostname # type: ignore[attr-defined]
except AttributeError:
except (AttributeError, TypeError):
logger.error(
"Service '%s-external' does not have a hostname:\n%s",
self.model.app.name,
service,
)
return ""
return None

@property
def _upf_hostname(self) -> str:
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,26 @@ def test_given_external_upf_hostname_config_not_set_and_external_upf_service_hos
upf_n4_port=TEST_PFCP_PORT,
)

@patch("lightkube.core.client.GenericSyncClient", new=Mock)
@patch("lightkube.core.client.Client.get")
@patch("charms.sdcore_upf.v0.fiveg_n4.N4Provides.publish_upf_n4_information")
@patch("charm.PFCP_PORT", TEST_PFCP_PORT)
def test_given_external_upf_hostname_config_not_set_and_metallb_not_available_and_fiveg_n4_relation_created_when_fiveg_n4_request_then_upf_hostname_and_n4_port_is_published( # noqa: E501
self, patched_publish_upf_n4_information, patched_lightkube_client_get
):
service = Mock(status=Mock(loadBalancer=Mock(ingress=None)))
patched_lightkube_client_get.return_value = service

n4_relation_id = self.harness.add_relation("fiveg_n4", "n4_requirer_app")
self.harness.add_relation_unit(n4_relation_id, "n4_requirer_app/0")

patched_publish_upf_n4_information.assert_called_once_with(
relation_id=n4_relation_id,
upf_hostname=f"{self.harness.charm.app.name}-external.{self.namespace}"
".svc.cluster.local",
upf_n4_port=TEST_PFCP_PORT,
)

@patch("charms.sdcore_upf.v0.fiveg_n4.N4Provides.publish_upf_n4_information")
@patch(f"{MULTUS_LIBRARY_PATH}.KubernetesMultusCharmLib.is_ready")
@patch(f"{HUGEPAGES_LIBRARY_PATH}.KubernetesHugePagesPatchCharmLib.is_patched")
Expand Down

0 comments on commit 0262507

Please sign in to comment.