Skip to content

Support '/' and '-' in braced template placeholders#2098

Open
zoispag wants to merge 2 commits into
robusta-dev:masterfrom
zoispag:template-braced-annotation-keys
Open

Support '/' and '-' in braced template placeholders#2098
zoispag wants to merge 2 commits into
robusta-dev:masterfrom
zoispag:template-braced-annotation-keys

Conversation

@zoispag

@zoispag zoispag commented Jun 10, 2026

Copy link
Copy Markdown

Problem

Kubernetes label and annotation keys routinely contain / (the prefix separator) and - — for example app.kubernetes.io/name or com.example/job-count. However, ExtendedTemplate.idpattern in src/robusta/utils/parsing.py only allows [_a-z0-9.], so a placeholder like:

customPlaybooks:
- triggers:
  - on_pod_oom_killed: {}
  actions:
  - create_finding:
      title: "Pod $name OOMKilled while running job ${annotations.com.example/job-count}"
      aggregation_key: PodOOMKilled

never matches and is left verbatim in the rendered output of format_event_templated_string (used by create_finding, customise_finding, kubectl_command, foreign_logs_enricher, warning_events_report, krr args, …). This makes it impossible to reference most real-world label/annotation keys, since any key with a prefix or a hyphen is unmatchable.

Solution

Extend only the braced form (${...}) via string.Template's braceidpattern to additionally allow / and -:

braceidpattern = r"(?a:[_a-z][_a-z0-9./-]*)"

The unbraced idpattern is intentionally left untouched so existing templates like $name-suffix (where -suffix is literal text after the variable) keep their current behavior. This is fully backwards compatible: braced placeholders that matched before still match and resolve identically; the only new behavior is that previously-unmatchable keys now resolve.

Testing

Added tests/test_parsing.py covering format_event_templated_string: subject fields (braced and unbraced), label/annotation lookups with plain and prefixed keys (${labels.app.kubernetes.io/name}, ${annotations.com.example/job-count}), <missing> fallback, $$ escaping, and the unchanged $name-suffix unbraced behavior. The new slash/hyphen cases fail without the fix and pass with it; all 9 cases pass.

🤖 Generated with Claude Code

Kubernetes label and annotation keys routinely contain '/' (prefix
separator) and '-', e.g. app.kubernetes.io/name or com.example/job-count.
ExtendedTemplate's idpattern only allows [_a-z0-9.], so a placeholder like
${annotations.com.example/job-count} never matches and is left verbatim
in the output of format_event_templated_string.

Extend only the braced form via braceidpattern so unbraced placeholders
(e.g. $name-suffix) keep their existing behavior.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@CLAassistant

CLAassistant commented Jun 10, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@coderabbitai

coderabbitai Bot commented Jun 10, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ca726204-e8f7-4dfa-89af-f8e98165dd3d

📥 Commits

Reviewing files that changed from the base of the PR and between a6005eb and 5d15b91.

📒 Files selected for processing (1)
  • tests/test_parsing.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/test_parsing.py

Walkthrough

The PR broadens braced template placeholder identifiers by adding ExtendedTemplate.braceidpattern (allowing / and -) and adds parametrized tests verifying substitution for braced/unbraced placeholders, label/annotation lookups (including keys with / and -), missing-variable fallback, and escaped dollar handling.

Changes

Template Placeholder Enhancement

Layer / File(s) Summary
Template placeholder pattern extension
src/robusta/utils/parsing.py
ExtendedTemplate.braceidpattern regex is added to permit / and - characters in braced placeholder identifiers, extending which keys can be safely substituted by safe_substitute.
Template substitution test coverage
tests/test_parsing.py
Parametrized test validates format_event_templated_string with subject field placeholders, braced and unbraced forms, label/annotation resolution via dot paths (including keys with / and -), fallback to "<missing>" for unknowns, and escaped dollar sign preservation.

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically summarizes the main change: extending braced template placeholders to support '/' and '-' characters in Kubernetes label/annotation keys.
Description check ✅ Passed The description is well-related to the changeset, explaining the problem with Kubernetes label/annotation keys, the solution via braceidpattern extension, and the testing approach.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants