Skip to content

Commit

Permalink
add TypedDict
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldmitry committed Aug 30, 2024
1 parent 9df07f7 commit ea5e791
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/cosl/coordinated_workers/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import yaml

import cosl
from cosl.coordinated_workers.interface import ClusterProvider
from cosl.coordinated_workers.interface import ClusterProvider, RemoteWriteEndpoint
from cosl.coordinated_workers.nginx import (
Nginx,
NginxMappingOverrides,
Expand Down Expand Up @@ -175,7 +175,7 @@ def __init__(
resources_limit_options: Optional[_ResourceLimitOptionsMapping] = None,
resources_requests: Optional[Callable[["Coordinator"], Dict[str, str]]] = None,
container_name: Optional[str] = None,
remote_write_endpoints: Optional[Callable[[], List[Dict[str, str]]]] = None,
remote_write_endpoints: Optional[Callable[[], List[RemoteWriteEndpoint]]] = None,
):
"""Constructor for a Coordinator object.
Expand Down
13 changes: 10 additions & 3 deletions src/cosl/coordinated_workers/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import yaml
from ops import EventSource, Object, ObjectEvents, RelationCreatedEvent
from pydantic import ConfigDict
from typing_extensions import TypedDict

import cosl

Expand Down Expand Up @@ -122,6 +123,12 @@ def dump(self, databag: Optional[_RawDatabag] = None, clear: bool = True) -> _Ra
# =============


class RemoteWriteEndpoint(TypedDict):
"""Type of the remote write endpoints to be passed to the worker through cluster relation data."""

url: str


class ConfigReceivedEvent(ops.EventBase):
"""Event emitted when the "-cluster" provider has shared a new config."""

Expand Down Expand Up @@ -188,7 +195,7 @@ class ClusterProviderAppData(DatabagModel):
"""Endpoints to which the workload (and the worker charm) can push logs to."""
tracing_receivers: Optional[Dict[str, str]] = None
"""Endpoints to which the workload (and the worker charm) can push traces to."""
remote_write_endpoints: Optional[List[Dict[str, str]]] = None
remote_write_endpoints: Optional[List[RemoteWriteEndpoint]] = None
"""Endpoints to which the workload (and the worker charm) can push metrics to."""

### TLS stuff
Expand Down Expand Up @@ -277,7 +284,7 @@ def publish_data(
privkey_secret_id: Optional[str] = None,
loki_endpoints: Optional[Dict[str, str]] = None,
tracing_receivers: Optional[Dict[str, str]] = None,
remote_write_endpoints: Optional[List[Dict[str, str]]] = None,
remote_write_endpoints: Optional[List[RemoteWriteEndpoint]] = None,
) -> None:
"""Publish the config to all related worker clusters."""
for relation in self._relations:
Expand Down Expand Up @@ -545,7 +552,7 @@ def get_tracing_receivers(self) -> Optional[Dict[str, str]]:
return data.tracing_receivers or {}
return {}

def get_remote_write_endpoints(self) -> List[Dict[str, str]]:
def get_remote_write_endpoints(self) -> List[RemoteWriteEndpoint]:
"""Fetch the remote write endpoints from the coordinator databag."""
data = self._get_data_from_coordinator()
if data:
Expand Down
6 changes: 3 additions & 3 deletions src/cosl/coordinated_workers/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ def status(self) -> ServiceEndpointStatus:
return ServiceEndpointStatus.down

def _on_collect_status(self, e: ops.CollectStatusEvent):
if self.resources_patch and self.resources_patch.get_status().name != "active":
e.add_status(self.resources_patch.get_status())

if not self._container.can_connect():
e.add_status(WaitingStatus(f"Waiting for `{self._name}` container"))
if not self.model.get_relation(self._endpoints["cluster"]):
Expand Down Expand Up @@ -317,9 +320,6 @@ def _on_collect_status(self, e: ops.CollectStatusEvent):
)
)

if self.resources_patch:
e.add_status(self.resources_patch.get_status())

# Utility functions
@property
def roles(self) -> List[str]:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_coordinated_workers/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,15 @@ def raise_change_error(*args):
[{"url": "test-url.com"}],
),
({"remote_write_endpoints": json.dumps(None), "worker_config": json.dumps("test")}, []),
(
{
"remote_write_endpoints": json.dumps(
[{"url": "test-url.com"}, {"url": "test2-url.com"}]
),
"worker_config": json.dumps("test"),
},
[{"url": "test-url.com"}, {"url": "test2-url.com"}],
),
),
)
def test_get_remote_write_endpoints(remote_databag, expected):
Expand Down

0 comments on commit ea5e791

Please sign in to comment.