Skip to content

Commit 886fb4e

Browse files
committed
init containers
1 parent 93fcce8 commit 886fb4e

5 files changed

Lines changed: 35 additions & 22 deletions

File tree

build/Dockerfile

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,5 @@ RUN apt-get update && apt-get install -y \
2525
strace \
2626
&& rm -rf /var/lib/apt/lists/*
2727

28-
# Set the working directory
29-
WORKDIR /usr/local/app
30-
31-
# Copy the files into the working directory
32-
COPY src/ .
33-
34-
# Set executable permissions for the trace script
35-
RUN chmod +x /usr/local/app/trace.sh
36-
3728
# Default command
38-
CMD ["/bin/bash"]
39-
29+
CMD ["/bin/sh"]

k8s/bpftrace.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: bpftrace-test
5+
spec:
6+
shareProcessNamespace: true # allow containers to see each other's processes
7+
initContainers:
8+
- name: bpftrace
9+
image: ghcr.io/amirhnajafiz/flap:v0.0.0-beta-6
10+
securityContext:
11+
privileged: true # or at least add CAP_SYS_PTRACE
12+
capabilities:
13+
add: ["SYS_PTRACE"]
14+
command: ["sleep", "infinity"]
15+
containers:
16+
- name: nginx
17+
image: nginx:latest
18+
19+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env bpftrace
2+
13
/* ctract.bt
24
* About: Traces all file access events of a command and its subprocs and
35
* outputs the total bytes read/write and access times.
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env bpftrace
2+
13
/* ptrace.bt
24
* About: Traces all file access events of a process and its subprocs and
35
* outputs the total bytes read/write and access times.
@@ -10,12 +12,12 @@ BEGIN
1012
{
1113
printf("[%s] START tracing file-access events\n", strftime("%Y-%m-%d %H:%M:%S", nsecs));
1214

13-
@fname[cpid, 0] = "STDIN";
14-
@fname[cpid, 1] = "STDOUT";
15-
@fname[cpid, 2] = "STDERR";
16-
@tracked[cpid] = 1;
15+
@fname[$1, 0] = "STDIN";
16+
@fname[$1, 1] = "STDOUT";
17+
@fname[$1, 2] = "STDERR";
18+
@tracked[$1] = 1;
1719

18-
printf("[%s] Tracking PID %d...\n", strftime("%Y-%m-%d %H:%M:%S", nsecs), cpid);
20+
printf("[%s] Tracking PID %d...\n", strftime("%Y-%m-%d %H:%M:%S", nsecs), $1);
1921
}
2022

2123
/* when we see a fork, if parent is tracked then also track child */

src/trace.sh

100644100755
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,26 +59,26 @@ ensure_script() {
5959

6060
# decide what to run
6161
if [ -n "$cmd" ]; then
62-
ensure_script "bpftrace/ctrace.bt"
63-
printf "Running: bpftrace -c %s bpftrace/ctrace.bt > %s\n" "'$cmd'" "$out"
62+
ensure_script "scripts/ctrace.bt"
63+
printf "Running: bpftrace -c %s scripts/ctrace.bt > %s\n" "'$cmd'" "$out"
6464
# Use exec so the shell is replaced by bpftrace (optional). We capture exit code.
6565
# Quoting $cmd carefully so it's passed as a single argument to -c.
66-
bpftrace -c "$cmd" bpftrace/ctrace.bt > "$out"
66+
bpftrace -c "$cmd" scripts/ctrace.bt > "$out"
6767
rc=$?
6868
if [ $rc -ne 0 ]; then
6969
printf "bpftrace exited with code %d\n" "$rc" >&2
7070
fi
7171
exit $rc
7272

7373
elif [ -n "$pid" ]; then
74-
ensure_script "bpftrace/ptrace.bt"
74+
ensure_script "scripts/ptrace.bt"
7575
# Validate pid is numeric
7676
case $pid in
7777
''|*[!0-9]*) printf "Error: pid must be a positive integer.\n" >&2; exit 2 ;;
7878
esac
7979

80-
printf "Running: bpftrace -p %s bpftrace/ptrace.bt > %s\n" "$pid" "$out"
81-
bpftrace bpftrace/ptrace.bt "$pid" > "$out"
80+
printf "Running: bpftrace -p %s scripts/ptrace.bt > %s\n" "$pid" "$out"
81+
bpftrace scripts/ptrace.bt "$pid" > "$out"
8282
rc=$?
8383
if [ $rc -ne 0 ]; then
8484
printf "bpftrace exited with code %d\n" "$rc" >&2

0 commit comments

Comments
 (0)