Last active
May 5, 2019 07:33
-
-
Save mvasilenko/f7dd2722c4d01602608bb31defef2613 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get_network_mode() { | |
docker inspect --format='{{.HostConfig.NetworkMode}}' "$1" | |
} | |
created_by_kubelet() { | |
[[ $(docker inspect --format='{{.Name}}' "$1") =~ ^/k8s_ ]] | |
} | |
for container_id in $(docker ps -q); do | |
network_mode=$(get_network_mode "${container_id}") | |
# skip the containers whose network_mode is 'host' or 'none', | |
# but do NOT skip the container created by kubelet. | |
if [[ "${network_mode}" == "host" || \ | |
$(! created_by_kubelet "${container_id}") && "${network_mode}" == "none" ]]; then | |
echo "${container_id} => ${network_mode}" | |
continue | |
fi | |
# if one container's network_mode is 'other container', | |
# then get its root parent container's network_mode. | |
while grep container <<< "${network_mode}" -q; do | |
network_mode=$(get_network_mode "${network_mode/container:/}") | |
# skip the containers whose network_mode is 'host' or 'none', | |
# but do NOT skip the container created by kubelet. | |
if [[ "${network_mode}" == "host" || \ | |
$(! created_by_kubelet "${container_id}") && "${network_mode}" == "none" ]]; then | |
echo "${container_id} => ${network_mode}" | |
continue 2 | |
fi | |
done | |
# get current container's 'container_id'. | |
pid=$(docker inspect --format='{{.State.Pid}}' "${container_id}") | |
# get the 'id' of veth device in the container. | |
veth_id=$(nsenter -t "${pid}" -n ip link show eth0 |grep -oP '(?<=eth0@if)\d+(?=:)') | |
# get the 'name' of veth device in the 'docker0' bridge (or other name), | |
# which is the peer of veth device in the container. | |
veth_name=$(ip link show |sed -nr "s/^${veth_id}: *([^ ]*)@if.*/\1/p") | |
echo "${container_id} => $(docker ps -af "id=${container_id}" --format '{{.Names}}') => ${veth_name}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment