Skip to content

Commit 2935f53

Browse files
authored
NETOBSERV-2131 allow multiple regexes (#207)
* allow multiple regexes * add example
1 parent b5a86ca commit 2935f53

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

scripts/functions.sh

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ function edit_manifest() {
439439
echo "opt: $1, value: $2"
440440
fi
441441

442-
if [[ $1 == "filter_"* ]]; then
442+
if [[ $1 == "filter_"* && $1 != "filter_regexes" ]]; then
443443
"$YQ_BIN" e --inplace ".spec.template.spec.containers[0].env[] |= select(.name==\"ENABLE_FLOW_FILTER\").value|=\"true\"" "$3"
444444
# add first filter in the array
445445
currentFilters=$("$YQ_BIN" -r ".spec.template.spec.containers[0].env[] | select(.name == \"FLOW_FILTER_RULES\").value" "$3")
@@ -572,9 +572,6 @@ function edit_manifest() {
572572
"filter_regexes")
573573
copyFLPConfig "$3"
574574

575-
# remove send step
576-
"$YQ_BIN" e -oj --inplace "del(.pipeline[] | select(.name==\"send\"))" "$json"
577-
578575
# define rules from arg
579576
IFS=',' read -ra regexes <<<"$2"
580577
rules=()
@@ -589,13 +586,23 @@ function edit_manifest() {
589586
IFS=,
590587
echo "${rules[*]}"
591588
)
592-
593-
# add filter param & pipeline
594-
"$YQ_BIN" e -oj --inplace ".parameters += {\"name\":\"filter\",\"transform\":{\"type\":\"filter\",\"filter\":{\"rules\":[{\"type\":\"keep_entry_all_satisfied\",\"keepEntryAllSatisfied\":[$rulesStr]}]}}}" "$json"
595-
"$YQ_BIN" e -oj --inplace ".pipeline += {\"name\":\"filter\",\"follows\":\"enrich\"}" "$json"
596-
597-
# add send step back
598-
"$YQ_BIN" e -oj --inplace ".pipeline += {\"name\":\"send\",\"follows\":\"filter\"}" "$json"
589+
rule="{\"type\":\"keep_entry_all_satisfied\",\"keepEntryAllSatisfied\":[$rulesStr]}"
590+
591+
existingFilterStage=$("$YQ_BIN" -r ".pipeline[] | select(.name == \"filter\")" "$json")
592+
if [[ "$existingFilterStage" == "" ]]; then
593+
# remove send step
594+
"$YQ_BIN" e -oj --inplace "del(.pipeline[] | select(.name==\"send\"))" "$json"
595+
596+
# add filter param & pipeline
597+
"$YQ_BIN" e -oj --inplace ".parameters += {\"name\":\"filter\",\"transform\":{\"type\":\"filter\",\"filter\":{\"rules\":[$rule]}}}" "$json"
598+
"$YQ_BIN" e -oj --inplace ".pipeline += {\"name\":\"filter\",\"follows\":\"enrich\"}" "$json"
599+
600+
# add send step back
601+
"$YQ_BIN" e -oj --inplace ".pipeline += {\"name\":\"send\",\"follows\":\"filter\"}" "$json"
602+
else
603+
# add rules to existing filter param
604+
"$YQ_BIN" e --inplace " .parameters[] |= select(.name == \"filter\").transform.filter.rules += $rule" "$json"
605+
fi
599606

600607
updateFLPConfig "$json" "$3"
601608
;;
@@ -613,6 +620,7 @@ function edit_manifest() {
613620

614621
# define key and value at script level to make them available all the time
615622
# these will be updated by check_args_and_apply first and overriden by defaultValue when needed
623+
prevKey=""
616624
key=""
617625
value=""
618626

@@ -629,11 +637,14 @@ function defaultValue() {
629637
function check_args_and_apply() {
630638
# Iterate through the command-line arguments
631639
for option in $1; do
640+
prevKey="$key"
632641
key="${option%%=*}"
633642
value="${option#*=}"
634643
case "$key" in
635644
or) # Increment flow filter array
636-
edit_manifest "add_filter" "" "$2"
645+
if [[ "$prevKey" != *regexes ]]; then
646+
edit_manifest "add_filter" "" "$2"
647+
fi
637648
;;
638649
*background) # Run command in background
639650
defaultValue "true"

scripts/help.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ function help {
4040
echo " Capture flows from a specific pod"
4141
echo " netobserv flows # Capture flows"
4242
echo " --node-selector=kubernetes.io/hostname:my-node # on node matching label 'kubernetes.io/hostname=my-node'"
43-
echo " --regexes=SrcK8S_Name~.*my-pod.*,DstK8S_Name~.*my-pod.* # from or to any pod name containing 'my-pod'"
43+
echo " --regexes=SrcK8S_Name~.*my-pod.* # from any pod name containing 'my-pod'"
44+
echo " or --regexes=DstK8S_Name~.*my-pod.* # or to any pod name containing 'my-pod'"
4445
echo
4546
echo " Capture packets on specific nodes and port"
4647
echo " netobserv packets # Capture packets"

0 commit comments

Comments
 (0)