Skip to content

Commit

Permalink
Protect against load balancer service existing
Browse files Browse the repository at this point in the history
In case something happens to cause our charm to go down without properly
cleaning up the service, use `client.apply()` instead of
client.create()`.

`apply()` will ensure the service configuration matches what is expected,
but is OK if it already exists, unlike `create()` which will throw an
error if the service already exists.

This will also prevent errors when scaling up (even though scaling isn't
supported at this time).
  • Loading branch information
DanielArndt committed Oct 27, 2023
1 parent 4326796 commit daaf1d5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
PFCP_PORT = 8805
REQUIRED_CPU_EXTENSIONS = ["avx2", "rdrand"]

# The default field manager set when using kubectl to create resources
DEFAULT_FIELD_MANAGER = "controller"


class IncompatibleCPUError(Exception):
"""Custom error to be raised when CPU doesn't support required instructions."""
Expand Down Expand Up @@ -168,8 +171,8 @@ def _create_external_upf_service(self) -> None:
),
)

client.create(service)
logger.info("Created external UPF service")
client.apply(service, field_manager=DEFAULT_FIELD_MANAGER)
logger.info("Created/asserted existence of the external UPF service")

def _on_remove(self, event: RemoveEvent) -> None:
self._delete_external_upf_service()
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,9 @@ def test_when_install_then_external_service_is_created(self, patch_client):
),
)

patch_client.return_value.create.assert_called_once_with(expected_service)
patch_client.return_value.apply.assert_called_once_with(
expected_service, field_manager="controller"
)

@patch("charm.Client")
def test_when_remove_then_external_service_is_deleted(self, patch_client):
Expand Down

0 comments on commit daaf1d5

Please sign in to comment.