Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions playbooks/robusta_playbooks/dmesg_enricher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from robusta.api import *


class DmesgParams(ActionParams):
"""
:var tail_lines: Number of lines to show from the end of the dmesg output.
"""

tail_lines: int = 100


@action
def dmesg_enricher(event: NodeEvent, params: DmesgParams):
"""
Run dmesg on the node and display the output.
"""
node = event.get_node()
if not node:
logging.error("dmesg_enricher was called on event without node")
return

node_name = node.metadata.name
exec_result = RobustaPod.exec_in_debugger_pod(
"dmesg",
node_name,
override_container_name="debug",
command_timeout=60,
)

if exec_result.return_code != 0:
logging.error(f"dmesg failed on node {node_name}: {exec_result.stderr}")
return

output = exec_result.stdout
Comment on lines +23 to +34
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Search for exec_in_debugger_pod definition and usage patterns
ast-grep --pattern $'def exec_in_debugger_pod($$$) {
  $$$
}'

echo "---"
echo "Checking existing usages in playbooks:"
rg -n "exec_in_debugger_pod" --type py -C 3

Repository: robusta-dev/robusta

Length of output: 6151


🏁 Script executed:

sed -n '318,345p' src/robusta/integrations/kubernetes/custom_models.py

Repository: robusta-dev/robusta

Length of output: 1014


Critical: exec_in_debugger_pod API mismatch causes runtime failure.

The method signature exec_in_debugger_pod(pod_name: str, node_name: str, cmd, ...) -> str confirms three critical issues:

  1. Wrong argument order: "dmesg" is passed as pod_name (1st arg), but should be the cmd (3rd arg).
  2. Missing pod_name argument: The first positional argument should be a pod identifier string.
  3. Invalid kwargs: override_container_name and command_timeout are not valid parameters; only debug_image and custom_annotations are supported.
  4. Return type mismatch: The method returns str (command output), not an object with .return_code, .stderr, .stdout attributes. Lines 30-34 will fail when accessing these attributes.
🐛 Proposed fix aligning with the actual API
-    exec_result = RobustaPod.exec_in_debugger_pod(
-        "dmesg",
-        node_name,
-        override_container_name="debug",
-        command_timeout=60,
-    )
-
-    if exec_result.return_code != 0:
-        logging.error(f"dmesg failed on node {node_name}: {exec_result.stderr}")
-        return
-
-    output = exec_result.stdout
+    try:
+        output = RobustaPod.exec_in_debugger_pod(
+            pod_name="dmesg-debugger",
+            node_name=node_name,
+            cmd="dmesg",
+        )
+    except Exception as e:
+        logging.error(f"dmesg failed on node {node_name}: {e}")
+        return
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
exec_result = RobustaPod.exec_in_debugger_pod(
"dmesg",
node_name,
override_container_name="debug",
command_timeout=60,
)
if exec_result.return_code != 0:
logging.error(f"dmesg failed on node {node_name}: {exec_result.stderr}")
return
output = exec_result.stdout
try:
output = RobustaPod.exec_in_debugger_pod(
pod_name="dmesg-debugger",
node_name=node_name,
cmd="dmesg",
)
except Exception as e:
logging.error(f"dmesg failed on node {node_name}: {e}")
return
🧰 Tools
🪛 Flake8 (7.3.0)

[error] 23-23: 'RobustaPod' may be undefined, or defined from star imports: robusta.api

(F405)


[error] 31-31: 'logging' may be undefined, or defined from star imports: robusta.api

(F405)

🪛 Ruff (0.15.7)

[error] 23-23: RobustaPod may be undefined, or defined from star imports

(F405)


[error] 31-31: logging may be undefined, or defined from star imports

(F405)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@playbooks/robusta_playbooks/dmesg_enricher.py` around lines 23 - 34, The call
to RobustaPod.exec_in_debugger_pod is using the wrong signature and handling its
return incorrectly: pass a valid pod_name string as the first arg, then
node_name as the second, and put "dmesg" (the command) as the third arg to match
exec_in_debugger_pod(pod_name, node_name, cmd, ...); remove invalid kwargs
override_container_name and command_timeout and if needed use supported kwargs
like debug_image or custom_annotations; treat the function's return as a plain
str (e.g., assign exec_output = RobustaPod.exec_in_debugger_pod(...)) and
replace the return_code/.stderr/.stdout checks with a simple emptiness check and
error log (use logging.error with node_name and exec_output), then set output =
exec_output for downstream use (references: RobustaPod.exec_in_debugger_pod,
exec_result/exec_output, node_name, output, logging.error).

if params.tail_lines:
output = "\n".join(output.splitlines()[-params.tail_lines:])

event.add_enrichment([
FileBlock("dmesg.txt", output.encode()),
])


@action
def dmesg_enricher_on_pod(event: PodEvent, params: DmesgParams):
"""
Run dmesg on the node where the pod is running.
"""
pod = event.get_pod()
if not pod:
logging.error("dmesg_enricher_on_pod was called on event without pod")
return

if not pod.spec.nodeName:
logging.error(f"pod {pod.metadata.name} has no nodeName")
return

node_event = NodeEvent(
metadata=pod.metadata,
involvedObject=pod,
nodeName=pod.spec.nodeName,
)
dmesg_enricher(node_event, params)
Comment on lines +57 to +62
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify NodeEvent constructor and check how other playbooks create NodeEvent from pods
echo "=== NodeEvent class definition ==="
ast-grep --pattern $'class NodeEvent($$$):
  def __init__($$$):
    $$$
'

echo ""
echo "=== How other playbooks handle similar pod-to-node scenarios ==="
rg -n "NodeEvent\(" --type py -C 5 playbooks/

Repository: robusta-dev/robusta

Length of output: 3569


🏁 Script executed:

#!/bin/bash
# Check PodEvent class and verify named_sinks attribute
echo "=== PodEvent class definition ==="
rg -n "class PodEvent" --type py -A 10 src/robusta/integrations/kubernetes/autogenerated/events.py | head -20

echo ""
echo "=== Check if PodEvent inherits named_sinks ==="
rg -n "class KubernetesResourceEvent" --type py -A 5 src/robusta/integrations/kubernetes/

echo ""
echo "=== Verify named_sinks usage in PodEvent context ==="
rg -n "named_sinks" --type py src/robusta/integrations/kubernetes/autogenerated/events.py | head -10

Repository: robusta-dev/robusta

Length of output: 1818


Critical: NodeEvent constructor signature mismatch causes TypeError.

The NodeEvent.__init__ signature requires (obj: Node, named_sinks: List[str]), but the code passes metadata, involvedObject, and nodeName kwargs which do not exist in the constructor. This will raise a TypeError at runtime.

The current approach of synthesizing a NodeEvent from pod metadata cannot work. NodeEvent requires an actual Node object retrieved from the Kubernetes API, not pod metadata.

Use Node.readNode(pod.spec.nodeName) to fetch the real Node object:

Proposed fix
    node_event = NodeEvent(
-       metadata=pod.metadata,
-       involvedObject=pod,
-       nodeName=pod.spec.nodeName,
+       obj=Node.readNode(pod.spec.nodeName).obj,
+       named_sinks=event.named_sinks,
    )
    dmesg_enricher(node_event, params)

Add error handling to gracefully handle Node lookup failures.

🧰 Tools
🪛 Flake8 (7.3.0)

[error] 57-57: 'NodeEvent' may be undefined, or defined from star imports: robusta.api

(F405)

🪛 Ruff (0.15.7)

[error] 57-57: NodeEvent may be undefined, or defined from star imports

(F405)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@playbooks/robusta_playbooks/dmesg_enricher.py` around lines 57 - 62, The code
incorrectly constructs NodeEvent with pod metadata causing a TypeError; replace
the synthetic NodeEvent creation with a real Node lookup by calling
Node.readNode(pod.spec.nodeName), then construct NodeEvent with the returned
Node object and the required named_sinks list before calling
dmesg_enricher(node_event, params); add error handling around Node.readNode to
catch lookup failures (log/skip/early return) so dmesg_enricher is only invoked
when a valid Node is obtained.