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: Allows deletion to proceed even if there is no external service #95

Merged
merged 3 commits into from
Feb 16, 2024
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
16 changes: 10 additions & 6 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
)
from charms.sdcore_upf_k8s.v0.fiveg_n3 import N3Provides # type: ignore[import]
from charms.sdcore_upf_k8s.v0.fiveg_n4 import N4Provides # type: ignore[import]
from httpx import HTTPStatusError
from jinja2 import Environment, FileSystemLoader
from lightkube.core.client import Client
from lightkube.models.core_v1 import ServicePort, ServiceSpec
Expand Down Expand Up @@ -189,12 +190,15 @@ def _delete_external_upf_service(self) -> None:
# dirty state in k8s, but it will be cleaned up when the juju model is
# destroyed. It will be re-used if the charm is re-deployed.
client = Client()
client.delete(
Service,
name=f"{self.app.name}-external",
namespace=self._namespace,
)
logger.info("Deleted external UPF service")
try:
client.delete(
Service,
name=f"{self.app.name}-external",
namespace=self._namespace,
)
logger.info("Deleted external UPF service")
except HTTPStatusError as status:
logger.info(f"Could not delete {self.app.name}-external due to: {status}")

@property
def _namespace(self) -> str:
Expand Down
6 changes: 3 additions & 3 deletions terraform/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ output "app_name" {

output "metrics_endpoint" {
description = "Exposes the Prometheus metrics endpoint providing telemetry about the UPF instance."
value = "metrics-endpoint"
value = "metrics-endpoint"
}

output "fiveg_n3_endpoint" {
description = "Name of the endpoint used to provide information on connectivity to the N3 plane."
value = "fiveg_n3"
value = "fiveg_n3"
}

output "fiveg_n4_endpoint" {
description = "Name of the endpoint used to provide information on connectivity to the N4 plane."
value = "fiveg_n4"
value = "fiveg_n4"
}
20 changes: 19 additions & 1 deletion tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
NetworkAnnotation,
NetworkAttachmentDefinition,
)
from httpx import HTTPStatusError
from lightkube.models.core_v1 import Node, NodeStatus, ServicePort, ServiceSpec
from lightkube.models.meta_v1 import ObjectMeta
from lightkube.resources.core_v1 import Service
Expand Down Expand Up @@ -1560,7 +1561,24 @@ def test_when_install_then_external_service_is_created(self, patch_client):
)

@patch("charm.Client")
def test_when_remove_then_external_service_is_deleted(self, patch_client):
def test_given_service_exists_on_remove_then_external_service_is_deleted(self, patch_client):
self.harness.charm.on.remove.emit()

patch_client.return_value.delete.assert_called_once_with(
Service,
name=f"{self.harness.charm.app.name}-external",
namespace=self.namespace,
)

@patch("charm.Client")
def test_given_service_does_not_exist_on_remove_then_no_exception_is_thrown(
self, patch_client
):
patch_client.return_value.delete.side_effect = HTTPStatusError(
message='services "upf-external" not found',
request=None,
response=None,
)
self.harness.charm.on.remove.emit()

patch_client.return_value.delete.assert_called_once_with(
Expand Down
Loading