Skip to content

Commit

Permalink
src/loki_server.py removed (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
Abuelodelanada authored Jul 27, 2023
1 parent eecad4c commit f8601e7
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 324 deletions.
27 changes: 13 additions & 14 deletions lib/charms/loki_k8s/v0/loki_push_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,20 @@ class LokiOperatorCharm(CharmBase):
def __init__(self, *args):
super().__init__(*args)
...
self._loki_ready()
external_url = urlparse(self._external_url)
self.loki_provider = LokiPushApiProvider(
self,
address=external_url.hostname or self.hostname,
port=external_url.port or 80,
scheme=external_url.scheme,
path=f"{external_url.path}/loki/api/v1/push",
)
...
def _loki_ready(self):
try:
version = self._loki_server.version
self.loki_provider = LokiPushApiProvider(self)
logger.debug("Loki Provider is available. Loki version: %s", version)
except LokiServerNotReadyError as e:
self.unit.status = MaintenanceStatus(str(e))
except LokiServerError as e:
self.unit.status = BlockedStatus(str(e))
- `port`: Loki Push Api endpoint port. Default value: 3100.
- `rules_dir`: Directory to store alert rules. Default value: "/loki/rules".
- `port`: Loki Push Api endpoint port. Default value: `3100`.
- `scheme`: Loki Push Api endpoint scheme (`HTTP` or `HTTPS`). Default value: `HTTP`
- `address`: Loki Push Api endpoint address. Default value: `localhost`
- `path`: Loki Push Api endpoint path. Default value: `loki/api/v1/push`
The `LokiPushApiProvider` object has several responsibilities:
Expand Down Expand Up @@ -481,7 +480,7 @@ def _alert_rules_error(self, event):

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

logger = logging.getLogger(__name__)

Expand Down
15 changes: 0 additions & 15 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
RULES_DIR,
ConfigBuilder,
)
from loki_server import LokiServer, LokiServerError, LokiServerNotReadyError
from ops.charm import CharmBase
from ops.main import main
from ops.model import ActiveStatus, BlockedStatus, WaitingStatus
Expand Down Expand Up @@ -130,7 +129,6 @@ def __init__(self, *args):
],
)

self._loki_server = LokiServer()
external_url = urlparse(self._external_url)
self.loki_provider = LokiPushApiProvider(
self,
Expand Down Expand Up @@ -359,19 +357,6 @@ def _push_certs(self):
if self.server_cert.key:
self._container.push(KEY_FILE, self.server_cert.key, make_dirs=True)

def _loki_ready(self) -> bool:
"""Gets LokiPushApiProvider instance into `self.loki_provider`."""
try:
version = self._loki_server.version
logger.debug("Loki Provider is available. Loki version: %s", version)
return True
except LokiServerNotReadyError as e:
self.unit.status = WaitingStatus(str(e))
return False
except LokiServerError as e:
self.unit.status = BlockedStatus(str(e))
return False

def _alerting_config(self) -> str:
"""Construct Loki altering configuration.
Expand Down
90 changes: 0 additions & 90 deletions src/loki_server.py

This file was deleted.

64 changes: 6 additions & 58 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from charm import LOKI_CONFIG as LOKI_CONFIG_PATH
from charm import LokiOperatorCharm
from helpers import FakeProcessVersionCheck, k8s_resource_multipatch
from loki_server import LokiServerError, LokiServerNotReadyError
from ops.model import ActiveStatus, BlockedStatus, Container, WaitingStatus
from ops.testing import Harness

Expand Down Expand Up @@ -115,7 +114,9 @@ class TestCharm(unittest.TestCase):
def setUp(self, *_):
self.container_name: str = "loki"
version_patcher = patch(
"loki_server.LokiServer.version", new_callable=PropertyMock, return_value="3.14159"
"charm.LokiOperatorCharm._loki_version",
new_callable=PropertyMock,
return_value="3.14159",
)
self.mock_version = version_patcher.start()
self.harness = Harness(LokiOperatorCharm)
Expand Down Expand Up @@ -165,61 +166,6 @@ def test__on_config_can_connect(self, mock_loki_config):
self.harness.charm.on.config_changed.emit()
self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)

def test__loki_ready(self):
with self.assertLogs(level="DEBUG") as logger:
self.harness.charm._loki_ready()
self.assertEqual(
sorted(logger.output),
["DEBUG:charm:Loki Provider is available. Loki version: 3.14159"],
)

def test__loki_ready_not_ready(self):
self.mock_version.side_effect = LokiServerNotReadyError
self.harness.charm._loki_ready()
self.assertIsInstance(self.harness.charm.unit.status, WaitingStatus)

def test__loki_ready_server_error(self):
self.mock_version.side_effect = LokiServerError
self.harness.charm._loki_ready()
self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)

def test__loki_config(self):
with self.assertLogs(level="DEBUG") as logger:
self.harness.charm._loki_ready()
self.assertEqual(
sorted(logger.output),
["DEBUG:charm:Loki Provider is available. Loki version: 3.14159"],
)


class TestWorkloadUnavailable(unittest.TestCase):
@patch("charm.KubernetesServicePatch", lambda x, y: None)
@k8s_resource_multipatch
@patch("lightkube.core.client.GenericSyncClient")
@patch.object(Container, "exec", new=FakeProcessVersionCheck)
def setUp(self, *_):
self.container_name: str = "loki"
version_patcher = patch(
"loki_server.LokiServer.version", new_callable=PropertyMock, return_value="3.14159"
)
self.mock_version = version_patcher.start()
self.harness = Harness(LokiOperatorCharm)
self.addCleanup(self.harness.cleanup)
self.addCleanup(version_patcher.stop)
self.harness.set_leader(True)
self.harness.begin_with_initial_hooks()
self.harness.container_pebble_ready("loki")

def test_loki_not_ready(self):
self.mock_version.side_effect = LokiServerNotReadyError
self.harness.charm._loki_ready()
self.assertIsInstance(self.harness.charm.unit.status, WaitingStatus)

def test_loki_server_error(self):
self.mock_version.side_effect = LokiServerError
self.harness.charm._loki_ready()
self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)


class TestConfigFile(unittest.TestCase):
"""Feature: Loki config file in the workload container is rendered by the charm."""
Expand Down Expand Up @@ -365,7 +311,9 @@ def setUp(self, *_):
)
self.check_alert_rules_patcher.start()
self.version_patcher = patch(
"loki_server.LokiServer.version", new_callable=PropertyMock, return_value="3.14159"
"charm.LokiOperatorCharm._loki_version",
new_callable=PropertyMock,
return_value="3.14159",
)
self.version_patcher.start()
self.harness = Harness(LokiOperatorCharm)
Expand Down
Loading

0 comments on commit f8601e7

Please sign in to comment.