Skip to content

Commit b7e9d7a

Browse files
authored
Feat: Allow process filtering by container id (#4)
- Part of #2110 in testsuite. Signed-off-by: svteb <[email protected]>
1 parent 7f2fda7 commit b7e9d7a

File tree

1 file changed

+15
-0
lines changed
  • src/kernel_introspection

1 file changed

+15
-0
lines changed

src/kernel_introspection/k8s.cr

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@ module KernelIntrospection
1212
pids
1313
end
1414

15+
def self.pids_by_container(container_id, node)
16+
# Command explanation:
17+
# 1. Get all process directories in /proc with find.
18+
# 2. Exec grep <container_id> for every returned process cgroup file,
19+
# redirect any errors (in case some temporary process disappears/find cannot access a directory)
20+
# 3. Grep returns paths with desired cgroup in format /proc/<pid>/cgroup, this output gets trimmed
21+
# by sed to only return a list of <pid>s
22+
command = "/bin/sh -c \"find /proc -maxdepth 1 -regex '/proc/[0-9]+' -exec grep -l '#{container_id}' {}/cgroup \\; 2>/dev/null | sed -e 's,/proc/\\([0-9]*\\)/cgroup,\\1,'\""
23+
result = ClusterTools.exec_by_node(command, node)
24+
output = result["output"].strip
25+
26+
pids = output.split("\n")
27+
pids
28+
end
29+
1530
def self.all_statuses_by_pids(pids : Array(String), node) : Array(String)
1631
Log.info { "all_statuses_by_pids" }
1732
proc_statuses = pids.map do |pid|

0 commit comments

Comments
 (0)