Skip to content

Commit

Permalink
Prometheus plugin is NOT removed if the relation is gone
Browse files Browse the repository at this point in the history
  • Loading branch information
juditnovak committed Dec 3, 2023
1 parent cbcde7e commit 1a24325
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 67 deletions.
12 changes: 3 additions & 9 deletions lib/charms/opensearch/v0/opensearch_cos.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from charms.grafana_agent.v0.cos_agent import COSAgentProvider
from charms.opensearch.v0.opensearch_exceptions import OpenSearchError
from charms.opensearch.v0.opensearch_plugins import OpenSearchPluginError
from ops import EventBase, RelationBrokenEvent, RelationCreatedEvent
from ops import EventBase, RelationCreatedEvent
from ops.model import ActiveStatus, BlockedStatus, WaitingStatus

logger = logging.getLogger(__name__)
Expand All @@ -31,8 +31,6 @@ def __init__(self, charm, *args, **kwargs):

def _run_plugin_manager(self, event: EventBase):
"""Execute plugin manager and handle prossible errors."""
if not self._charm.unit.is_leader():
return
try:
if self._charm.plugin_manager.run():
self._charm.on[self._charm.service_manager.name].acquire_lock.emit(
Expand All @@ -52,11 +50,7 @@ def _run_plugin_manager(self, event: EventBase):

def _on_cos_agent_relation_created(self, event: RelationCreatedEvent):
"""COS workflow initialization happens when the COS relation is created."""
self._run_plugin_manager(event)

def _on_cos_agent_relation_broken(self, event: RelationBrokenEvent):
"""Re-run plugin configuration is the cos relation is not present anymore."""
if self._charm.model.get_relation("cos-agent"):
event.defer()
if not self._charm.unit.is_leader():
return

self._run_plugin_manager(event)
13 changes: 0 additions & 13 deletions lib/charms/opensearch/v0/opensearch_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,6 @@ def name(self) -> str:
class OpenSearchPrometheusExporter(OpenSearchPlugin):
"""Implements the opensearch-knn plugin."""

REMOVE_ON_DISABLE = True

@property
def name(self) -> str:
"""Returns the name of the plugin."""
Expand All @@ -378,14 +376,3 @@ def config(self) -> OpenSearchPluginConfig:
"prometheus.nodes.filter": "_all",
}
)

def disable(self) -> OpenSearchPluginConfig:
"""No config changes apply on disable."""
return OpenSearchPluginConfig(
config_entries_to_del={
"prometheus.metric_name.prefix": "opensearch_",
"prometheus.indices": False,
"prometheus.cluster.settings": False,
"prometheus.nodes.filter": "_all",
}
)
48 changes: 3 additions & 45 deletions tests/integration/plugins/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,35 +65,14 @@ async def test_build_and_deploy(ops_test: OpsTest) -> None:
await ops_test.model.wait_for_idle(
apps=[TLS_CERTIFICATES_APP_NAME, APP_NAME],
status="active",
timeout=1400,
timeout=3400,
idle_period=IDLE_PERIOD,
)
assert len(ops_test.model.applications[APP_NAME].units) == 3


async def test_prometheus_exporter_disabled_by_default(ops_test):
# Test that Prometheus Exporter is NOT running before the relation is there
leader_unit_ip = await get_leader_unit_ip(ops_test, app=APP_NAME)
endpoint = f"https://{leader_unit_ip}:9200/_prometheus/metrics"
response = await http_request(ops_test, "get", endpoint, app=APP_NAME)
assert response == {
"error": "no handler found for uri [/_prometheus/metrics] and method [GET]"
}


async def test_prometheus_exporter_enabled_by_cos_relation(ops_test):
await ops_test.model.deploy(COS_APP_NAME, channel="edge"),
await ops_test.model.relate(APP_NAME, COS_APP_NAME)
# NOTE: As for now, COS agent remains blocked, as we only have a partial config
await ops_test.model.wait_for_idle(
apps=[APP_NAME],
status="active",
timeout=1400,
idle_period=IDLE_PERIOD,
)
config = await ops_test.model.applications[APP_NAME].get_config()
assert config["prometheus_exporter_plugin_url"]["default"].startswith("https://")

async def test_prometheus_exporter_enabled_by_default(ops_test):
"""Test that Prometheus Exporter is running before the relation is there."""
leader_unit_ip = await get_leader_unit_ip(ops_test, app=APP_NAME)
endpoint = f"https://{leader_unit_ip}:9200/_prometheus/metrics"
response = await http_request(ops_test, "get", endpoint, app=APP_NAME, json_resp=False)
Expand All @@ -103,27 +82,6 @@ async def test_prometheus_exporter_enabled_by_cos_relation(ops_test):
assert len(response_str.split("\n")) > 500


async def test_prometheus_exporter_disabled_by_cos_relation_gone(ops_test):
await ops_test.juju("remove-relation", APP_NAME, COS_APP_NAME)

# Wait 90s as the update-status is supposed to happen every 1m
# If model config is updated, update this routine as well.
await asyncio.sleep(150)
await ops_test.model.wait_for_idle(
apps=[APP_NAME],
status="active",
timeout=1400,
idle_period=IDLE_PERIOD,
)

leader_unit_ip = await get_leader_unit_ip(ops_test, app=APP_NAME)
endpoint = f"https://{leader_unit_ip}:9200/_prometheus/metrics"
response = await http_request(ops_test, "get", endpoint, app=APP_NAME)
assert response == {
"error": "no handler found for uri [/_prometheus/metrics] and method [GET]"
}


async def test_knn_endabled_disabled(ops_test):
config = await ops_test.model.applications[APP_NAME].get_config()
assert config["plugin_opensearch_knn"]["default"] is False
Expand Down

0 comments on commit 1a24325

Please sign in to comment.