Skip to content

Commit

Permalink
Parse flow filter configs and use it to update manifest
Browse files Browse the repository at this point in the history
Signed-off-by: Mohamed Mahmoud <[email protected]>
  • Loading branch information
msherif1234 committed Apr 16, 2024
1 parent 984b8e5 commit 4b21561
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 23 deletions.
16 changes: 3 additions & 13 deletions commands/netobserv-flows
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
#!/bin/bash
source "./scripts/functions.sh"

# interface filter such as 'br-ex'
filter=""

if [ -z "${1:-}" ]
then
echo "Filters not set"
else
echo "Filters set as $1"
filter=$1
fi

# CLI image to use
img="quay.io/netobserv/network-observability-cli:main"

trap cleanup EXIT

setup flows $filter

setup flows "$*"

echo "Running network-observability-cli get-flows... "
${K8S_CLI_BIN} run \
Expand All @@ -35,4 +25,4 @@ ${K8S_CLI_BIN} wait \
${K8S_CLI_BIN} exec -i --tty \
-n netobserv-cli \
collector \
-- /network-observability-cli get-flows ${filter:+"--filter" "$filter"}
-- /network-observability-cli get-flows
28 changes: 28 additions & 0 deletions res/flow-capture.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,34 @@ spec:
value: "true"
- name: ENABLE_DNS_TRACKING
value: "true"
- name: ENABLE_FLOW_FILTER
value: "{{ENABLE_FLOW_FILTER}}"
- name: FLOW_FILTER_DIRECTION
value: "{{FLOW_FILTER_DIRECTION}}"
- name: FLOW_FILTER_IP_CIDR
value: "{{FLOW_FILTER_IP_CIDR}}"
- name: FLOW_FILTER_PROTOCOL
value: "{{FLOW_FILTER_PROTOCOL}}"
- name: FLOW_FILTER_SOURCE_PORT
value: "{{FLOW_FILTER_SOURCE_PORT}}"
- name: FLOW_FILTER_DESTINATION_PORT
value: "{{FLOW_FILTER_DESTINATION_PORT}}"
- name: FLOW_FILTER_PORT
value: "{{FLOW_FILTER_PORT}}"
- name: FLOW_FILTER_SOURCE_PORT_RANGE
value: "{{FLOW_FILTER_SOURCE_PORT_RANGE}}"
- name: FLOW_FILTER_DESTINATION_PORT_RANGE
value: "{{FLOW_FILTER_DESTINATION_PORT_RANGE}}"
- name: FLOW_FILTER_PORT_RANGE
value: "{{FLOW_FILTER_PORT_RANGE}}"
- name: FLOW_FILTER_ICMP_TYPE
value: "{{FLOW_FILTER_ICMP_TYPE}}"
- name: FLOW_FILTER_ICMP_CODE
value: "{{FLOW_FILTER_ICMP_CODE}}"
- name: FLOW_FILTER_PEER_IP
value: "{{FLOW_FILTER_PEER_IP}}"
- name: FLOW_FILTER_ACTION
value: "{{FLOW_FILTER_ACTION}}"
- name: EXPORT
value: "direct-flp"
- name: FLP_CONFIG
Expand Down
80 changes: 70 additions & 10 deletions scripts/functions.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env bash
source ./scripts/update_flowcapture.sh

set -eu

# get either oc (favorite) or kubectl paths
Expand All @@ -22,13 +24,6 @@ function loadYAMLs() {
saYAML="$(cat ./res/service-account.yml)"
fi

flowAgentYAML='
flowAgentYAMLContent
'
if [ -f ./res/flow-capture.yml ]; then
flowAgentYAML="$(cat ./res/flow-capture.yml)"
fi

packetAgentYAML='
packetAgentYAMLContent
'
Expand Down Expand Up @@ -57,6 +52,9 @@ function clusterIsReady() {
fi
}

MANIFEST_FILE="flow-capture.yml"
MANIFEST_OUTPUT_PATH="output"

function setup {
echo "Setting up... "

Expand Down Expand Up @@ -85,9 +83,71 @@ function setup {
echo "$collectorServiceYAML" | ${K8S_CLI_BIN} apply -f -

if [ "$1" = "flows" ]; then
echo "creating flow-capture agents"
echo "${flowAgentYAML/"{{FLOW_FILTER_VALUE}}"/${2:-}}" | ${K8S_CLI_BIN} apply -f -
${K8S_CLI_BIN} rollout status daemonset netobserv-cli -n netobserv-cli --timeout 60s
shift
echo "creating flow-capture agents:"
options="$*"
# Iterate through the command-line arguments
for option in $options; do
key="${option%%=*}"
value="${option#*=}"
case "$key" in
--interfaces) # Interfaces
edit_manifest "interfaces" "$value"
;;
--enable) # Enable flow filter
edit_manifest "filter_enable" "$value"
;;
--direction) # Configure flow filter direction
edit_manifest "filter_direction" "$value"
;;
--cidr) # Configure flow filter CIDR
edit_manifest "filter_cidr" "$value"
;;
--protocol) # Configure flow filter protocol
edit_manifest "filter_protocol" "$value"
;;
--sport) # Configure flow filter source port
edit_manifest "filter_src_port" "$value"
;;
--dport) # Configure flow filter destination port
edit_manifest "filter_dst_port" "$value"
;;
--port) # Configure flow filter port
edit_manifest "filter_port" "$value"
;;
--sport-range) # Configure flow filter source port range
edit_manifest "filter_src_port_range" "$value"
;;
--dport-range) # Configure flow filter destination port range
edit_manifest "filter_dst_port_range" "$value"
;;
--port-range) # Configure flow filter port range
edit_manifest "filter_port_range" "$value"
;;
--icmp-type) # ICMP type
edit_manifest "filter_icmp_type" "$value"
;;
--icmp-code) # ICMP code
edit_manifest "filter_icmp_code" "$value"
;;
--peer-ip) # Peer IP
edit_manifest "filter_peer_ip" "$value"
;;
--action) # Filter action
edit_manifest "filter_action" "$value"
;;
*) # Invalid option
echo "Invalid option: $key" >&2
exit 1
;;
esac
done

manifest="$MANIFEST_OUTPUT_PATH"/"$MANIFEST_FILE"
if [[ -f "$manifest" ]]; then
${K8S_CLI_BIN} apply -f "$manifest"
${K8S_CLI_BIN} rollout status daemonset netobserv-cli -n netobserv-cli --timeout 60s
fi
elif [ "$1" = "packets" ]; then
echo "creating packet-capture agents"
echo "${packetAgentYAML/"{{PCA_FILTER_VALUE}}"/${2:-}}" | ${K8S_CLI_BIN} apply -f -
Expand Down
69 changes: 69 additions & 0 deletions scripts/update_flowcapture.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/bin/bash

MANIFEST_FILE="flow-capture.yml"
MANIFEST_PATH="res"
MANIFEST_OUTPUT_PATH="output"

if ! command -v yq &> /dev/null
then
echo "yq binary not found, installing... "
go install -mod='' github.com/mikefarah/yq/[email protected]
fi

function edit_manifest() {
echo "Editing manifest file..."
if [[ ! -d ${MANIFEST_OUTPUT_PATH} ]]; then
mkdir -p ${MANIFEST_OUTPUT_PATH} > /dev/null
cp ${MANIFEST_PATH}/${MANIFEST_FILE} ${MANIFEST_OUTPUT_PATH}/${MANIFEST_FILE}
fi
## replace the env variable in the manifest file
echo "env: $1, env_value: $2"
case "$1" in
"interfaces")
yq e --inplace ".spec.template.spec.containers[0].env[] |= select(.name==\"INTERFACES\").value|=\"$2\"" ${MANIFEST_OUTPUT_PATH}/${MANIFEST_FILE}
;;
"filter_enable")
yq e --inplace ".spec.template.spec.containers[0].env[] |= select(.name==\"ENABLE_FLOW_FILTER\").value|=\"$2\"" ${MANIFEST_OUTPUT_PATH}/${MANIFEST_FILE}
;;
"filter_direction")
yq e --inplace ".spec.template.spec.containers[0].env[] |= select(.name==\"FLOW_FILTER_DIRECTION\").value|=\"$2\"" ${MANIFEST_OUTPUT_PATH}/${MANIFEST_FILE}
;;
"filter_cidr")
yq e --inplace ".spec.template.spec.containers[0].env[] |= select(.name==\"FLOW_FILTER_IP_CIDR\").value|=\"$2\"" ${MANIFEST_OUTPUT_PATH}/${MANIFEST_FILE}
;;
"filter_protocol")
yq e --inplace ".spec.template.spec.containers[0].env[] |= select(.name==\"FLOW_FILTER_PROTOCOL\").value|=\"$2\"" ${MANIFEST_OUTPUT_PATH}/${MANIFEST_FILE}
;;
"filter_sport")
yq e --inplace ".spec.template.spec.containers[0].env[] |= select(.name==\"FLOW_FILTER_SOURCE_PORT\").value|=\"$2\"" ${MANIFEST_OUTPUT_PATH}/${MANIFEST_FILE}
;;
"filter_dport")
yq e --inplace ".spec.template.spec.containers[0].env[] |= select(.name==\"FLOW_FILTER_DESTINATION_PORT\").value|=\"$2\"" ${MANIFEST_OUTPUT_PATH}/${MANIFEST_FILE}
;;
"filter_port")
yq e --inplace ".spec.template.spec.containers[0].env[] |= select(.name==\"FLOW_FILTER_PORT\").value|=\"$2\"" ${MANIFEST_OUTPUT_PATH}/${MANIFEST_FILE}
;;
"filter_sport_range")
yq e --inplace ".spec.template.spec.containers[0].env[] |= select(.name==\"FLOW_FILTER_SOURCE_PORT_RANGE\").value|=\"$2\"" ${MANIFEST_OUTPUT_PATH}/${MANIFEST_FILE}
;;
"filter_dport_range")
yq e --inplace ".spec.template.spec.containers[0].env[] |= select(.name==\"FLOW_FILTER_DESTINATION_PORT_RANGE\").value|=\"$2\"" ${MANIFEST_OUTPUT_PATH}/${MANIFEST_FILE}
;;
"filter_port_range")
yq e --inplace ".spec.template.spec.containers[0].env[] |= select(.name==\"FLOW_FILTER_PORT_RANGE\").value|=\"$2\"" ${MANIFEST_OUTPUT_PATH}/${MANIFEST_FILE}
;;
"filter_icmp_type")
yq e --inplace ".spec.template.spec.containers[0].env[] |= select(.name==\"FLOW_FILTER_ICMP_TYPE\").value|=\"$2\"" ${MANIFEST_OUTPUT_PATH}/${MANIFEST_FILE}
;;
"filter_icmp_code")
yq e --inplace ".spec.template.spec.containers[0].env[] |= select(.name==\"FLOW_FILTER_ICMP_CODE\").value|=\"$2\"" ${MANIFEST_OUTPUT_PATH}/${MANIFEST_FILE}
;;
"filter_peer_ip")
yq e --inplace ".spec.template.spec.containers[0].env[] |= select(.name==\"FLOW_FILTER_PEER_IP\").value|=\"$2\"" ${MANIFEST_OUTPUT_PATH}/${MANIFEST_FILE}
;;
"filter_action")
yq e --inplace ".spec.template.spec.containers[0].env[] |= select(.name==\"FLOW_FILTER_ACTION\").value|=\"$2\"" ${MANIFEST_OUTPUT_PATH}/${MANIFEST_FILE}
;;
esac

}

0 comments on commit 4b21561

Please sign in to comment.