feat: replace local CosTool fork with cosl implementation - #607
Conversation
- Replace local CosTool class with cosl's implementation - Remove unused helper functions _is_official_alert_rule_format and _is_single_alert_rule_format - Remove now-unused subprocess and tempfile imports - Bump LIBPATCH to 24 - Update pyproject.toml to require cosl>=1.9.1 - Regenerate uv.lock Agent-Logs-Url: https://github.com/canonical/loki-k8s-operator/sessions/c05ad25f-2364-4e26-ba4e-e72a980f28f4 Co-authored-by: lucabello <36242061+lucabello@users.noreply.github.com>
The cosl CosTool properly captures stderr from cos-tool validation, meaning invalid alert rules are now correctly intercepted before being pushed to Loki. Previously, the old local CosTool had a bug where stderr was not captured, causing validation errors to be silently ignored and bad rules to flow through to Loki's HTTP API. This fix ensures the charm goes blocked when the provider detects validation errors in relation data, maintaining the expected behavior that invalid rules from consumers result in blocked status. Also fixes unit test ALERT_RULES to use valid LogQL instead of the %%juju_topology%% placeholder (which is only substituted when topology labels are present). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
@copilot Can you figure out why those integration tests are failing and provide a fix? |
…units Non-leader units running pebble-ready hooks cannot access their own app's relation data (permission denied in Juju). Fix both the charm's _has_alert_rule_errors and the library's alerts property to only read/write app-level relation data when the unit is the leader. Agent-Logs-Url: https://github.com/canonical/loki-k8s-operator/sessions/4e9aa569-5770-42e9-a35f-4d6ff98120f8 Co-authored-by: lucabello <36242061+lucabello@users.noreply.github.com>
The failures were caused by two related bugs introduced in the commit that added
Commit: e554754 |
Diff:
|
Co-authored-by: Sina P <55766091+sinapah@users.noreply.github.com> Signed-off-by: Luca Bello <36242061+lucabello@users.noreply.github.com>
Co-authored-by: Sina P <55766091+sinapah@users.noreply.github.com> Signed-off-by: Luca Bello <36242061+lucabello@users.noreply.github.com>
Issue
v1/loki_push_api.pymaintained a local fork ofCosToolthat diverged from the upstreamcoslimplementation, contributing to bugs from the deviation.AlertRuleshad already been migrated tocosl.rulesin a prior change;CosToolremained local.Solution
CosToolfromcoslinstead of defining it locally:from cosl import CosTool, JujuTopologyLokiPushApiProvider.__init__:CosTool("logql")— cosl'sCosTooltakes adefault_query_typeinstead of a charm reference;"logql"is correct for all Loki query expressionsCosToolclass (~120 lines of duplicated/drifted code)_is_official_alert_rule_formatand_is_single_alert_rule_format— unused in v1 sinceAlertRuleswas moved to coslsubprocessandtempfileimports — only needed by the removed local classLIBPATCH23 → 25cosl>=1.9.1inpyproject.toml; regenerateuv.lockLokiPushApiProvider.alertswith a leader check — only the leader may write to the app data bag; non-leaders attempting this write would fail with "permission denied"_has_alert_rule_errors()incharm.pywith an earlyif not self.unit.is_leader(): return False— non-leader units running non-relation hooks (e.g.pebble-ready) cannot read their own app's relation data and would crash with "permission denied"InvalidAlertRulePathErrorand_resolve_dir_against_charm_pathare retained — they are charm-specific (resolve paths againstcharm.charm_dir) with no cosl equivalent.Per the issue guidance,
v0/loki_push_api.pyis unchanged.Context
The cosl
CosTooldiffers from the previous local fork in two key ways:default_query_type: Optional[QueryType]instead of a charm object; query type flows to all method calls automaticallyplatform.machine()(correct) instead ofplatform.processor()(unreliable in containers/CI) to locate thecos-toolbinaryIn Juju, non-leader units running non-relation hooks (like
pebble-ready) get "permission denied" when callingrelation-get --appto access their own application's relation data bag. Any read or write ofrelation.data[self.app]outside a relation hook must be guarded byself.unit.is_leader().Testing Instructions
Note: tests that validate topology label injection into LogQL expressions (
test_rules_have_correct_labels) require thecos-toolbinary, whichtox -e unitdownloads automatically via curl. These tests pass in CI; they fail locally without the binary — this is pre-existing behaviour unrelated to this change (those tests target v0, which is unchanged).Upgrade Notes
Users of
LokiPushApiProviderfromcharms.loki_k8s.v1.loki_push_apishould ensurecosl>=1.9.1is available in their charm's environment. No API-level changes to the public interface.