Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: go into blocked status if MetalLB is not enabled and N2 relation is set #48

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,11 @@ def _configure_amf(self, event: EventBase) -> None: # noqa C901
return
self._generate_config_file()
self._configure_amf_workload()
self._set_n2_information()
try:
self._set_n2_information()
except ValueError:
self.unit.status = BlockedStatus("Waiting for MetalLB to be enabled")
return
self.unit.status = ActiveStatus()

def _on_certificates_relation_created(self, event: EventBase) -> None:
Expand Down Expand Up @@ -386,16 +390,20 @@ def _on_n2_relation_joined(self, event: RelationJoinedEvent) -> None:
Args:
event (RelationJoinedEvent): Juju event
"""
self._set_n2_information()
try:
self._set_n2_information()
except ValueError:
self.unit.status = BlockedStatus("Waiting for MetalLB to be enabled")
return

def _get_n2_amf_ip(self) -> str:
def _get_n2_amf_ip(self) -> Optional[str]:
"""Returns the IP to send for the N2 interface.

If a configuration is provided, it is returned, otherwise
returns the IP of the external LoadBalancer Service.

Returns:
str: IP address of the AMF
str/None: IP address of the AMF if available else None
"""
if configured_ip := self._get_external_amf_ip_config():
return configured_ip
Expand Down Expand Up @@ -634,45 +642,45 @@ def _amf_service_is_running(self) -> bool:
return False
return service.is_running()

def _amf_external_service_ip(self) -> str:
def _amf_external_service_ip(self) -> Optional[str]:
"""Returns the external service IP.

Returns:
str: External Service IP
str/None: External Service IP 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].ip # type: ignore[attr-defined]
except AttributeError:
except (AttributeError, TypeError):
logger.error(
"Service '%s-external' does not have an IP address:\n%s",
self.model.app.name,
service,
)
return ""
return None

def _amf_external_service_hostname(self) -> str:
def _amf_external_service_hostname(self) -> Optional[str]:
"""Returns the external service hostname.

Returns:
str: External Service hostname
str: External Service hostname 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


def _get_pod_ip() -> Optional[str]:
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,47 @@ def test_given_n2_information_and_service_is_running_and_lb_service_has_no_hostn
)
self.assertEqual(relation_data["amf_port"], "38412")

@patch("lightkube.core.client.GenericSyncClient", new=Mock)
@patch("lightkube.core.client.Client.get")
@patch("ops.model.Container.pull")
@patch("ops.model.Container.exists")
@patch("ops.model.Container.push")
@patch("charm.check_output")
@patch("charms.sdcore_nrf.v0.fiveg_nrf.NRFRequires.nrf_url", new_callable=PropertyMock)
@patch("charms.data_platform_libs.v0.data_interfaces.DatabaseRequires.is_resource_created")
gatici marked this conversation as resolved.
Show resolved Hide resolved
def test_given_n2_information_and_service_is_running_and_metallb_service_is_not_available_when_fiveg_n2_relation_joined_then_amf_goes_in_blocked_state( # noqa: E501
self,
patch_is_resource_created,
patch_nrf_url,
patch_check_output,
patch_push,
patch_exists,
patch_pull,
patch_get,
):
patch_pull.return_value = StringIO(
self._read_file("tests/unit/expected_config/config.conf").strip()
)
patch_exists.return_value = True
patch_check_output.return_value = b"1.1.1.1"
service = Mock(status=Mock(loadBalancer=Mock(ingress=None)))
patch_get.return_value = service
patch_exists.return_value = True
patch_is_resource_created.return_value = True
patch_nrf_url.return_value = "http://nrf:8081"
self.harness.set_can_connect(container="amf", val=True)
self.harness.add_relation(relation_name="fiveg-nrf", remote_app="nrf")
self.harness.add_relation(
relation_name="certificates", remote_app="tls-certificates-operator"
)
self._database_is_available()
self.harness.container_pebble_ready("amf")
relation_id = self.harness.add_relation(relation_name="fiveg-n2", remote_app="n2-requirer")
self.harness.add_relation_unit(relation_id=relation_id, remote_unit_name="n2-requirer/0")
self.assertEqual(
self.harness.charm.unit.status, BlockedStatus("Waiting for MetalLB to be enabled")
)

@patch("lightkube.core.client.GenericSyncClient", new=Mock)
@patch("lightkube.core.client.Client.get")
@patch("ops.model.Container.pull")
Expand Down
Loading