Skip to content

Instantly share code, notes, and snippets.

@ormergi
Created May 29, 2023 10:56
Show Gist options
  • Save ormergi/747b6699f8e6fc747e9b6d2e22922262 to your computer and use it in GitHub Desktop.
Save ormergi/747b6699f8e6fc747e9b6d2e22922262 to your computer and use it in GitHub Desktop.
Kubveirt - dump VM pod NAT rules
# /bin/bash
set -ex
vm_name="myvmi"
vm_pod_name=$(kubectl get po -o custom-columns=:.metadata.name --no-headers | grep $vm_name | grep Running)
vm_node_name=$(kubectl get po $vm_pod_name -o custom-columns=:.spec.nodeName --no-headers)
vm_node_virt_handler_pod=$(kubectl get pod -n kubevirt -l kubevirt.io=virt-handler -o wide --no-headers | grep "$vm_node_name" | awk '{print $1}')
vm_pod_pid=$(kubectl exec -it $vm_node_virt_handler_pod -n kubevirt -c virt-handler -- bash -c "ps -ef | grep /usr/bin/virt-launcher.*--name.*$vm_name" | grep -Pv "grep|virt-launcher-monitor" | awk '{print $2}')
# dump NFT nat rules:
cmd="virt-chroot --mount /proc/$vm_pod_pid/ns/mnt exec -- /usr/sbin/nft list table nat"
kubectl exec -it $vm_node_virt_handler_pod -n kubevirt -- bash <<< "$cmd"
# in case Kubevirt version is older than v0.59.0, using iptables may also work:
cmd="virt-chroot --mount /proc/$vm_pod_pid/ns/mnt exec -- /usr/sbin/iptables -L -t nat"
kubectl exec -it $vm_node_virt_handler_pod -n kubevirt -- bash <<< "$cmd"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment