Skip to content

Commit 20c23e7

Browse files
author
Néstor Salceda
committed
Accept same parameters that Sysdig cli accepts
1 parent bfb0c1b commit 20c23e7

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

kubectl-capture

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,63 +7,66 @@
77
pod=""
88
namespace="default"
99
duration="120"
10+
now=$(date +%s)
11+
capture_pod=""
12+
sysdig_params=""
1013

1114
function main() {
1215
parse_arguments "$@"
1316
start_capture
1417
}
1518

1619
function parse_arguments() {
17-
local params=""
20+
pod="$1"
21+
shift
22+
1823
while [ $# -gt 0 ]
1924
do
2025
case $1 in
21-
-d|--duration)
22-
duration=$2
26+
-ns|--namespace)
27+
namespace=$2
2328
shift
2429
;;
25-
-n|--namespace)
26-
namespace=$2
30+
-M)
31+
duration=$2
2732
shift
2833
;;
34+
-w|--write=*|-z|--compress|-pc|-pk|-pm|-print=*|-S|--summary)
35+
# Do not allow changes on these parameters
36+
echo $0: $1: skipping parameter for Sysdig>&2
37+
;;
2938
--)
3039
shift
3140
break
3241
;;
33-
-*)
34-
echo $0: $1: unrecognized option >&2
35-
;;
3642
*)
37-
params="$params $1"
43+
sysdig_params="${sysdig_params} $1"
3844
;;
3945
esac
4046
shift
4147
done
4248

43-
eval set -- "$params"
44-
pod="${1}"
45-
4649
if [[ -z "${pod}" ]]; then
4750
usage_and_die 1
4851
fi
52+
53+
capture_pod="capture-${pod}-${now}"
54+
sysdig_params="-S -M ${duration} -pk -z -w /${capture_pod}.scap.gz ${sysdig_params}"
4955
}
5056

5157
function usage_and_die() {
5258
cat << 'EOF'
53-
Usage: kubectl capture pod_name
59+
Usage: kubectl capture POD [-ns NAMESPACE] [sysdig options]
5460
Options:
55-
-d | --duration Capture system calls for this duration (default: 120 seconds)
56-
-n | --namespace The namespace where the target pod lives (default: default)
61+
-ns | --namespace The namespace where the target pod lives (default: default)
5762
EOF
5863
exit $1
5964
}
6065

6166
function start_capture() {
62-
local now=$(date +%s)
63-
local capture_pod="capture-${pod}-${now}"
64-
6567
node=$(kubectl -n ${namespace} get pod ${pod} -o jsonpath='{.spec.nodeName}' 2>/dev/null)
6668
if [[ $? -ne 0 ]];then
69+
echo "error: Unable to trigger a capture on pod ${pod}"
6770
exit 1
6871
fi
6972

@@ -79,7 +82,7 @@ spec:
7982
args:
8083
- /bin/bash
8184
- "-c"
82-
- "echo '* Capturing system calls'; sysdig -S -M ${duration} -pk -z -w /${capture_pod}.scap.gz; touch /.finished; trap 'exit 0' TERM; sleep infinity & wait $!"
85+
- "echo '* Capturing system calls'; sysdig ${sysdig_params}; touch /.finished; trap 'exit 0' TERM; sleep infinity & wait $!"
8386
imagePullPolicy: IfNotPresent
8487
securityContext:
8588
privileged: true
@@ -142,6 +145,7 @@ EOF
142145
echo "Node: ${node}"
143146
echo "Pod: ${pod}"
144147
echo "Duration: ${duration} seconds"
148+
echo "Parameters for Sysdig: ${sysdig_params}"
145149
echo ""
146150

147151
sleep ${duration}

test/kubectl-capture.bats

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
kubectl create deployment nginx --image=nginx
55
POD=$(kubectl get pod | grep nginx | cut -f1 -d" ")
66

7-
run ./kubectl-capture $POD -d 5
7+
run ./kubectl-capture $POD -M 5
88

99
kubectl delete deployment nginx
1010

@@ -16,7 +16,7 @@
1616
kubectl -n scope create deployment nginx --image=nginx
1717
POD=$(kubectl -n scope get pod | grep nginx | cut -f1 -d" ")
1818

19-
run ./kubectl-capture $POD -d 5 -n scope
19+
run ./kubectl-capture $POD -M 5 -ns scope
2020

2121
kubectl delete namespace scope
2222

0 commit comments

Comments
 (0)