@@ -111,8 +111,48 @@ function namespaceFound() {
111111FLOWS_MANIFEST_FILE=" flow-capture.yml"
112112PACKETS_MANIFEST_FILE=" packet-capture.yml"
113113CONFIG_JSON_TEMP=" config.json"
114+ CLUSTER_CONFIG=" cluster-config-v1.yaml"
115+ NETWORK_CONFIG=" cluster-network.yaml"
114116MANIFEST_OUTPUT_PATH=" tmp"
115117
118+ function getSubnets() {
119+ declare -n sn=" $1 "
120+
121+ # get cluster-config-v1 Configmap to retreive machine networks
122+ installConfig=$( ${K8S_CLI_BIN} get configmap cluster-config-v1 -n kube-system -o custom-columns=" :data.install-config" )
123+ yaml=" ${MANIFEST_OUTPUT_PATH} /${CLUSTER_CONFIG} "
124+ echo " $installConfig " > ${yaml}
125+
126+ machines=$( yq e -oj ' .networking.machineNetwork[] | select(has("cidr")).cidr' " $yaml " )
127+ if [ " ${# machines} " -gt 0 ]; then
128+ sn[" Machines" ]=$machines
129+ fi
130+
131+ # get OCP cluster Network to retreive pod / services / external networks
132+ networkConfig=$( ${K8S_CLI_BIN} get network cluster -o yaml)
133+ yaml=" ${MANIFEST_OUTPUT_PATH} /${NETWORK_CONFIG} "
134+ echo " $networkConfig " > ${yaml}
135+
136+ pods=$( yq e -oj ' .spec.clusterNetwork[] | select(has("cidr")).cidr' " $yaml " )
137+ if [ " ${# pods} " -gt 0 ]; then
138+ sn[" Pods" ]=$pods
139+ fi
140+
141+ services=$( yq e -oj ' .spec.serviceNetwork[] | select(.)' " $yaml " )
142+ if [ " ${# services} " -gt 0 ]; then
143+ sn[" Services" ]=$services
144+ fi
145+
146+ if [ " ${# sn[@]} " -gt 0 ]; then
147+ echo " Found subnets:"
148+ for key in " ${! sn[@]} " ; do
149+ echo " $key : ${sn[$key]} "
150+ done
151+ else
152+ echo " Didn't found subnets"
153+ fi
154+ }
155+
116156function setup {
117157 echo " Setting up... "
118158
@@ -240,6 +280,8 @@ function common_usage {
240280 echo " --max-bytes: maximum capture bytes (default: 50000000 = 50MB)"
241281 echo " --background: run in background (default: false)"
242282 echo " --copy: copy the output files locally (default: prompt)"
283+ # enrichment
284+ echo " --get-subnets: get subnets informations (default: false)"
243285 # filters
244286 echo " --node-selector: capture on specific nodes (default: n/a)"
245287 echo " --direction: filter direction (default: n/a)"
@@ -282,9 +324,27 @@ function packets_usage {
282324 common_usage
283325}
284326
327+ # get current config and save it to temp file
328+ function copyFLPConfig {
329+ jsonContent=$( yq e ' .spec.template.spec.containers[0].env[] | select(.name=="FLP_CONFIG").value' " $1 " )
330+ # json temp file location is set as soon as this function is called
331+ json=" ${MANIFEST_OUTPUT_PATH} /${CONFIG_JSON_TEMP} "
332+ echo " $jsonContent " > ${json}
333+ }
334+
335+ # update FLP Config
336+ function updateFLPConfig {
337+ # get json as string with escaped quotes
338+ jsonContent=$( cat " $1 " )
339+ jsonContent=${jsonContent// \" / \\\" }
340+
341+ # update FLP_CONFIG env
342+ yq e --inplace " .spec.template.spec.containers[0].env[] |= select(.name==\" FLP_CONFIG\" ).value|=\" $jsonContent \" " " $2 "
343+ }
344+
285345function edit_manifest() {
286- # # replace the env variable in the manifest file
287- echo " env : $1 , env_value : $2 "
346+ # # replace the configuration in the manifest file
347+ echo " opt : $1 , evalue : $2 "
288348 case " $1 " in
289349 " interfaces" )
290350 yq e --inplace " .spec.template.spec.containers[0].env[] |= select(.name==\" INTERFACES\" ).value|=\" $2 \" " " $3 "
@@ -301,6 +361,38 @@ function edit_manifest() {
301361 " network_events_enable" )
302362 yq e --inplace " .spec.template.spec.containers[0].env[] |= select(.name==\" ENABLE_NETWORK_EVENTS_MONITORING\" ).value|=\" $2 \" " " $3 "
303363 ;;
364+ " get_subnets" )
365+ if [[ " $2 " == " true" ]]; then
366+ declare -A subnets
367+ getSubnets subnets
368+
369+ if [ " ${# subnets[@]} " -gt 0 ]; then
370+ copyFLPConfig " $3 "
371+
372+ # get network enrich stage
373+ enrichIndex=$( yq e -oj " .parameters[] | select(.name==\" enrich\" ) | document_index" " $json " )
374+ enrichContent=$( yq e -oj " .parameters[$enrichIndex ]" " $json " )
375+ enrichJson=" ${MANIFEST_OUTPUT_PATH} /enrich.json"
376+ echo " $enrichContent " > ${enrichJson}
377+
378+ # add rules to network
379+ yq e -oj --inplace " .transform.network.rules +={\" type\" :\" add_subnet_label\" ,\" add_subnet_label\" :{\" input\" :\" SrcAddr\" ,\" output\" :\" SrcSubnetLabel\" }}" " $enrichJson "
380+ yq e -oj --inplace " .transform.network.rules +={\" type\" :\" add_subnet_label\" ,\" add_subnet_label\" :{\" input\" :\" DstAddr\" ,\" output\" :\" DstSubnetLabel\" }}" " $enrichJson "
381+
382+ # add subnetLabels to network
383+ yq e -oj --inplace " .transform.network.subnetLabels = []" " $enrichJson "
384+ for key in " ${! subnets[@]} " ; do
385+ yq e -oj --inplace " .transform.network.subnetLabels += {\" name\" :\" $key \" ,\" cidrs\" :[${subnets[$key]} ]}" " $enrichJson "
386+ done
387+
388+ # override network
389+ enrichJsonStr=$( cat $enrichJson )
390+ yq e -oj --inplace " .parameters[$enrichIndex ] = $enrichJsonStr " " $json "
391+
392+ updateFLPConfig " $json " " $3 "
393+ fi
394+ fi
395+ ;;
304396 " filter_enable" )
305397 yq e --inplace " .spec.template.spec.containers[0].env[] |= select(.name==\" ENABLE_FLOW_FILTER\" ).value|=\" $2 \" " " $3 "
306398 ;;
@@ -359,10 +451,7 @@ function edit_manifest() {
359451 yq e --inplace " .spec.template.spec.containers[0].env[] |= select(.name==\" FILTER_DROPS\" ).value|=\" $2 \" " " $3 "
360452 ;;
361453 " filter_regexes" )
362- # get current config and save it to temp file
363- jsonContent=$( yq e ' .spec.template.spec.containers[0].env[] | select(.name=="FLP_CONFIG").value' " $3 " )
364- json=" ${MANIFEST_OUTPUT_PATH} /${CONFIG_JSON_TEMP} "
365- echo " $jsonContent " > ${json}
454+ copyFLPConfig " $3 "
366455
367456 # remove send step
368457 yq e -oj --inplace " del(.pipeline[] | select(.name==\" send\" ))" " $json "
@@ -389,12 +478,7 @@ function edit_manifest() {
389478 # add send step back
390479 yq e -oj --inplace " .pipeline += {\" name\" :\" send\" ,\" follows\" :\" filter\" }" " $json "
391480
392- # get json as string with escaped quotes
393- jsonContent=$( cat $json )
394- jsonContent=${jsonContent// \" / \\\" }
395-
396- # update FLP_CONFIG env
397- yq e --inplace " .spec.template.spec.containers[0].env[] |= select(.name==\" FLP_CONFIG\" ).value|=\" $jsonContent \" " " $3 "
481+ updateFLPConfig " $json " " $3 "
398482 ;;
399483 " log_level" )
400484 yq e --inplace " .spec.template.spec.containers[0].env[] |= select(.name==\" LOG_LEVEL\" ).value|=\" $2 \" " " $3 "
@@ -603,6 +687,13 @@ function check_args_and_apply() {
603687 exit 1
604688 fi
605689 ;;
690+ --get-subnets) # Get subnets
691+ if [[ " $value " == " true" || " $value " == " false" ]]; then
692+ edit_manifest " get_subnets" " $value " " $2 "
693+ else
694+ echo " invalid value for --get-subnets"
695+ fi
696+ ;;
606697 * ) # Invalid option
607698 echo " Invalid option: $key " >&2
608699 exit 1
0 commit comments