#!/bin/bash
BASE_COLLECTION_PATH="/must-gather"
NETWORK_LOG_PATH="${BASE_COLLECTION_PATH}/network_logs/"

mkdir -p ${NETWORK_LOG_PATH}/

if [ $# -eq 0 ]; then
    echo "WARNING: Collecting network logs on ALL nodes in your cluster. This could take a large amount of time." >&2
fi

PIDS=()

NETWORK_TYPE=$(oc get network.config.openshift.io -o=jsonpath='{.items[0].spec.networkType}' | tr '[:upper:]' '[:lower:]')
if [ "$NETWORK_TYPE" == "openshiftsdn" ]; then
    NODES="${@:-$(oc get nodes --no-headers -o custom-columns=':metadata.name')}"
    for NODE in $NODES; do
        SDN_POD=$(oc -n openshift-sdn get pods --no-headers -o custom-columns=":metadata.name" --field-selector spec.nodeName=$NODE -l app=sdn)
        oc -n openshift-sdn exec $SDN_POD -- bash -c "iptables-save -c" > ${NETWORK_LOG_PATH}/${NODE}_iptables &
        PIDS+=($!)
        oc -n openshift-sdn exec $SDN_POD -- bash -c "ovs-vsctl show" > ${NETWORK_LOG_PATH}/${NODE}_ovs_dump &
        PIDS+=($!)

        OVS_POD=$(oc -n openshift-sdn get pods --no-headers -o custom-columns=":metadata.name" --field-selector spec.nodeName=$NODE -l app=ovs)
        oc -n openshift-sdn exec $OVS_POD -- bash -c "cat /var/log/openvswitch/ovs-vswitchd.log" > ${NETWORK_LOG_PATH}/${OVS_POD}_vswitchd_log &
        PIDS+=($!)
        oc -n openshift-sdn exec $OVS_POD -- bash -c "cat /var/log/openvswitch/ovsdb-server.log" > ${NETWORK_LOG_PATH}/${OVS_POD}_ovsdb_log &
        PIDS+=($!)
    done
else
    NODES="${@:-$(oc get nodes --no-headers -o custom-columns=':metadata.name')}"
    for NODE in $NODES; do
        OVNKUBE_NODE_POD=$(oc -n openshift-ovn-kubernetes get pods --no-headers -o custom-columns=":metadata.name" --field-selector spec.nodeName=$NODE -l app=ovnkube-node)
        oc -n openshift-ovn-kubernetes exec -c ovnkube-node $OVNKUBE_NODE_POD -- bash -c "iptables-save -c" > ${NETWORK_LOG_PATH}/${OVNKUBE_NODE_POD}_iptables &
        PIDS+=($!)
        oc -n openshift-ovn-kubernetes exec -c ovnkube-node $OVNKUBE_NODE_POD -- bash -c "ip addr" > ${NETWORK_LOG_PATH}/${OVNKUBE_NODE_POD}_ip_addr &
        PIDS+=($!)
        oc -n openshift-ovn-kubernetes exec -c ovnkube-node $OVNKUBE_NODE_POD -- bash -c "ip route" > ${NETWORK_LOG_PATH}/${OVNKUBE_NODE_POD}_ip_route &
        PIDS+=($!)
        oc -n openshift-ovn-kubernetes exec -c ovnkube-node $OVNKUBE_NODE_POD -- bash -c "ip -6 route" > ${NETWORK_LOG_PATH}/${OVNKUBE_NODE_POD}_ip_6_route &
        PIDS+=($!)
        oc -n openshift-ovn-kubernetes exec -c ovnkube-node $OVNKUBE_NODE_POD -- bash -c \
        "ovs-ofctl dump-ports-desc br-int" > ${NETWORK_LOG_PATH}/${OVNKUBE_NODE_POD}_ovs_ofctl_dump_ports_br_int &
        PIDS+=($!)
        oc -n openshift-ovn-kubernetes exec -c ovnkube-node $OVNKUBE_NODE_POD -- bash -c "ovs-ofctl dump-flows br-int" > \
        ${NETWORK_LOG_PATH}/${OVNKUBE_NODE_POD}_ovs_ofctl_dump_flows_br_int &
        PIDS+=($!)
        oc -n openshift-ovn-kubernetes exec -c ovnkube-node $OVNKUBE_NODE_POD -- bash -c \
        "ovs-ofctl dump-ports-desc br-local" > ${NETWORK_LOG_PATH}/${OVNKUBE_NODE_POD}_ovs_ofctl_dump_ports_br_local &
        PIDS+=($!)
        oc -n openshift-ovn-kubernetes exec -c ovnkube-node $OVNKUBE_NODE_POD -- bash -c "ovs-ofctl dump-flows br-local" > \
        ${NETWORK_LOG_PATH}/${OVNKUBE_NODE_POD}_ovs_ofctl_dump_flows_br_local &
        PIDS+=($!)
    done

    OVNKUBE_MASTER_IPS=($(oc -n openshift-ovn-kubernetes get pods -l app=ovnkube-master -o=jsonpath='{.items[*].status.podIP}'))
    OVNKUBE_MASTER_POD=$(oc -n openshift-ovn-kubernetes get pods --no-headers -o custom-columns=':metadata.name' -l app=ovnkube-master | tail -1)

    oc -n openshift-ovn-kubernetes exec -c ovnkube-master $OVNKUBE_MASTER_POD -- bash -c \
    "ovn-nbctl --db=ssl:${OVNKUBE_MASTER_IPS[0]}:9641,ssl:${OVNKUBE_MASTER_IPS[1]}:9641,ssl:${OVNKUBE_MASTER_IPS[2]}:9641 \
    -p /ovn-cert/tls.key -c /ovn-cert/tls.crt -C /ovn-ca/ca-bundle.crt show" > \
    ${NETWORK_LOG_PATH}/${OVNKUBE_MASTER_POD}_ovn_nbctl_show &
    PIDS+=($!)
    oc -n openshift-ovn-kubernetes exec -c ovnkube-master $OVNKUBE_MASTER_POD -- bash -c \
    "ovn-nbctl --db=ssl:${OVNKUBE_MASTER_IPS[0]}:9641,ssl:${OVNKUBE_MASTER_IPS[1]}:9641,ssl:${OVNKUBE_MASTER_IPS[2]}:9641 \
    -p /ovn-cert/tls.key -c /ovn-cert/tls.crt -C /ovn-ca/ca-bundle.crt list Logical_Switch_Port" > \
    ${NETWORK_LOG_PATH}/${OVNKUBE_MASTER_POD}_ovn_nbctl_list_lsp &
    PIDS+=($!)
    oc -n openshift-ovn-kubernetes exec -c ovnkube-master $OVNKUBE_MASTER_POD -- bash -c \
    "ovn-nbctl --db=ssl:${OVNKUBE_MASTER_IPS[0]}:9641,ssl:${OVNKUBE_MASTER_IPS[1]}:9641,ssl:${OVNKUBE_MASTER_IPS[2]}:9641 \
    -p /ovn-cert/tls.key -c /ovn-cert/tls.crt -C /ovn-ca/ca-bundle.crt list Load_Balancer" > \
    ${NETWORK_LOG_PATH}/${OVNKUBE_MASTER_POD}_ovn_nbctl_list_lb &
    PIDS+=($!)
    oc -n openshift-ovn-kubernetes exec -c ovnkube-master $OVNKUBE_MASTER_POD -- bash -c \
    "ovn-nbctl --db=ssl:${OVNKUBE_MASTER_IPS[0]}:9641,ssl:${OVNKUBE_MASTER_IPS[1]}:9641,ssl:${OVNKUBE_MASTER_IPS[2]}:9641 \
    -p /ovn-cert/tls.key -c /ovn-cert/tls.crt -C /ovn-ca/ca-bundle.crt list Port_Group" > \
    ${NETWORK_LOG_PATH}/${OVNKUBE_MASTER_POD}_ovn_nbctl_list_pg &
    PIDS+=($!)
    oc -n openshift-ovn-kubernetes exec -c ovnkube-master $OVNKUBE_MASTER_POD -- bash -c \
    "ovn-nbctl --db=ssl:${OVNKUBE_MASTER_IPS[0]}:9641,ssl:${OVNKUBE_MASTER_IPS[1]}:9641,ssl:${OVNKUBE_MASTER_IPS[2]}:9641 \
    -p /ovn-cert/tls.key -c /ovn-cert/tls.crt -C /ovn-ca/ca-bundle.crt list ACL" > \
    ${NETWORK_LOG_PATH}/${OVNKUBE_MASTER_POD}_ovn_nbctl_list_acl &
    PIDS+=($!)
    oc -n openshift-ovn-kubernetes exec -c ovnkube-master $OVNKUBE_MASTER_POD -- bash -c \
    "ovn-sbctl --db=ssl:${OVNKUBE_MASTER_IPS[0]}:9642,ssl:${OVNKUBE_MASTER_IPS[1]}:9642,ssl:${OVNKUBE_MASTER_IPS[2]}:9642 \
    -p /ovn-cert/tls.key -c /ovn-cert/tls.crt -C /ovn-ca/ca-bundle.crt show" > \
    ${NETWORK_LOG_PATH}/${OVNKUBE_MASTER_POD}_ovn_sbctl_show &
    PIDS+=($!)
fi
echo "INFO: Waiting for node network log collection to complete ..."
wait ${PIDS[@]}
echo "INFO: Node network log collection to complete."
