Skip to content
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
6 changes: 4 additions & 2 deletions charmcraft-22.04.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ description: |

parts:
charm:
charm-binary-python-packages:
- pydantic
plugin: uv
source: .
build-snaps:
- astral-uv

base: ubuntu@22.04
platforms:
Expand Down
6 changes: 4 additions & 2 deletions charmcraft-24.04.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ description: |

parts:
charm:
charm-binary-python-packages:
- pydantic
plugin: uv
source: .
build-snaps:
- astral-uv

base: ubuntu@24.04
platforms:
Expand Down
7 changes: 4 additions & 3 deletions lib/charms/consul_client/v0/consul_notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ def _on_consul_notify_ready(self, event):

This event happens when the relation is created or joined.
'''
# Set the socket information for the provider
# Set the socket information for the provider.
# unix_socket_filepath as the socket file name, not as an absolute path.
self.consul_notify.set_socket_info(
snap_name="my-service-snap",
unix_socket_filepath="/var/snap/my-service-snap/common/socket.sock"
unix_socket_filepath="socket.sock"
)
```
"""
Expand All @@ -113,7 +114,7 @@ def _on_consul_notify_ready(self, event):

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 2
LIBPATCH = 3

DEFAULT_RELATION_NAME = "consul-notify"

Expand Down
10 changes: 10 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
[project]
name = "consul-client"
version = "1.19"
requires-python = "~=3.12.0"
dependencies = [
"ops",
"pydantic",
]


# Testing tools configuration
[tool.coverage.run]
branch = true
Expand Down
6 changes: 5 additions & 1 deletion src/config_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class Ports(BaseModel):
class ConsulConfigBuilder:
"""Build the configuration file for consul."""

HYPERVISOR_SOCKET_PREFIX = "hypervisor"

def __init__(
self,
bind_address: str | None,
Expand Down Expand Up @@ -90,7 +92,9 @@ def build(self) -> dict:
args = ["python3", str(self.consul_health_check), *self.healthcheck_endpoints]
else:
args = ["python3", str(self.consul_health_check), *self.consul_servers]
args.extend(["--socket-path", self.unix_socket_filepath])
args.extend(
["--socket-path", f"{self.HYPERVISOR_SOCKET_PREFIX}/{self.unix_socket_filepath}"]
)
Comment thread
ahmad-can marked this conversation as resolved.
args.extend(["--monitoring-samples", str(self.monitoring_samples)])

config["services"] = [
Expand Down
21 changes: 17 additions & 4 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from ops.testing import Harness

from charm import ConsulCharm
from config_builder import ConsulConfigBuilder


@pytest.fixture()
Expand Down Expand Up @@ -190,7 +191,10 @@ def test_consul_notify_socket_available(
assert health_check_service["check"]["id"] == "tcp-check"
assert health_check_service["check"]["name"] == "TCP Health Check"
assert "--socket-path" in health_check_service["check"]["args"]
assert socket_path in health_check_service["check"]["args"]
assert (
f"{ConsulConfigBuilder.HYPERVISOR_SOCKET_PREFIX}/{socket_path}"
in health_check_service["check"]["args"]
)


def test_consul_notify_socket_gone(
Expand Down Expand Up @@ -371,7 +375,10 @@ def test_socket_config_persists_across_events(
assert len(initial_config_dict["services"]) == 1
assert initial_config_dict["services"][0]["name"] == "tcp-health-check"
assert "--socket-path" in initial_config_dict["services"][0]["check"]["args"]
assert socket_path in initial_config_dict["services"][0]["check"]["args"]
assert (
f"{ConsulConfigBuilder.HYPERVISOR_SOCKET_PREFIX}/{socket_path}"
in initial_config_dict["services"][0]["check"]["args"]
)

# Reset the mock to track subsequent calls
write_config.reset_mock()
Expand All @@ -389,7 +396,10 @@ def test_socket_config_persists_across_events(
assert len(post_event_config_dict["services"]) == 1
assert post_event_config_dict["services"][0]["name"] == "tcp-health-check"
assert "--socket-path" in post_event_config_dict["services"][0]["check"]["args"]
assert socket_path in post_event_config_dict["services"][0]["check"]["args"]
assert (
f"{ConsulConfigBuilder.HYPERVISOR_SOCKET_PREFIX}/{socket_path}"
in post_event_config_dict["services"][0]["check"]["args"]
)

# Verify relation data is still accessible
assert charm.consul_notify.is_ready
Expand All @@ -409,7 +419,10 @@ def test_socket_config_persists_across_events(
assert len(upgrade_config_dict["services"]) == 1
assert upgrade_config_dict["services"][0]["name"] == "tcp-health-check"
assert "--socket-path" in upgrade_config_dict["services"][0]["check"]["args"]
assert socket_path in upgrade_config_dict["services"][0]["check"]["args"]
assert (
f"{ConsulConfigBuilder.HYPERVISOR_SOCKET_PREFIX}/{socket_path}"
in upgrade_config_dict["services"][0]["check"]["args"]
)


def test_health_check_disabled_by_config(
Expand Down
Loading
Loading