Skip to content

fix(k8s): devops k8s logs sends the user's LogQL to Loki unchanged, and the kubectl fallback refuses what it cannot evaluate #1431

Description

@dan-petty

Problem

Lines cited at 05e9388 (release/v0.2.29).

  • The query sent to Loki is rewritten from a partial parse.
    • parse_logql_query (k8s/logql.py:152-182) reads stream selectors with _SELECTOR_REGEX (:35), which matches only label="value". It drops =~, != and !~ matchers.
    • When --namespace is given and the query names no namespace, execute_logql_query rebuilds raw_query from that parse (:431-438). Line-filter patterns are re-quoted without escaping (:434).
    • Example: {app=~"api.*"} |= "error" | json | level="error" with --namespace prod reaches Loki as {namespace="prod"} |= "error" | json. The matcher and the label filter are gone.
  • The kubectl fallback emulates LogQL.
    • execute_kubectl_logql (:287-342) lists pods with only the = selectors and reads at most 5 pods (:317).
    • It applies the parsed filters in Python (evaluate_log_lines, :223-254).
    • Everything the parser skipped is silently ignored, so a regex selector reads logs from unrelated pods.
  • Durations are parsed by hand.
    • _parse_duration_to_seconds (:269-284) returns 3600 for anything it cannot read, 1h30m included.
    • The code then computes start and end itself (:354-362), although Loki's query_range takes since directly.
    • The fallback passes the same string to kubectl logs --since, which parses it differently.
  • The request bypasses the egress factory. It uses module-level httpx2.get (:365).

Proposed remedy

  • Send the query as written. The user's query goes to Loki byte for byte, with since, limit and direction as Loki parameters. _parse_duration_to_seconds is deleted.
  • --namespace no longer rewrites the query. A query whose selector lacks the namespace fails with an error that prints the selector to use. A conflicting namespace already fails (:425-430).
  • The kubectl fallback does only what kubectl can.
    • It accepts a selector of label="value" matchers and |= substring filters.
    • It refuses anything else with an error naming loki.url.
    • The emulation of LogQL pipelines (| json, | logfmt, regex filters) is deleted.
  • Egress. The Loki request goes through the egress factory (refactor(cli): Unify HTTP and DNS seams with shared fakes #1314).

Acceptance criteria

  • The query recorded at the HTTP fake equals the input byte for byte, with and without --namespace.
  • since=1h30m reaches Loki as since=1h30m.
  • In fallback mode, these fail with the error naming loki.url: {app=~"api.*"}, != matchers, label filters and | json.
  • In fallback mode, {app="api"} |= "error" runs kubectl logs with -l app=api (process fake).
  • A missing namespace fails before any request and prints the selector to use.
  • Tests run offline with fakes only at process and HTTP edges; each new test runs under 1 s.
  • uv run devops ci passes.

Related: #1322, #1314, #1165 (Loki's localhost default), #691, #1357.

Found by the 2026-10-08 client-library audit.

Opened as a borrow (#1153, budget/borrowed): the agent filing quota is spent for this cycle, and this is a defect or security finding of the owner-requested 2026-10-08 client-library audit.

Activity

  1. added
    type/bug
    scope/k8s
    source/agent
    on Oct 8, 2026
  2. dan-petty-bot commented on Oct 8, 2026

    @dan-petty-bot
    Collaborator

    Intake placed this item to the backlog: at intake only a critical fix goes into a release; a release start pulls Ready items in, P0 features first.

    • Priority: P2-Medium. Operators relying on 'devops k8s logs' for debugging receive silently incomplete or wrong log sets, but the tool still functions and no data is exposed or corrupted.
    • Value: High. Correct log querying is the core purpose of the command, and silent loss of matchers or filters undermines every diagnostic workflow built on it.
    • Effort: Medium. The fix is well-scoped (pass query through unchanged, simplify fallback to kubectl-native selectors, delete hand-rolled duration parsing) but touches multiple code paths and requires new offline tests.
  3. added a commit that references this issue on Oct 25, 2026
    29743af
  4. added a commit that references this issue on Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    scope/k8ssource/agenttype/bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions