Last active
January 29, 2019 13:15
-
-
Save superseb/4e0577a93ced88fa6fe5c39d2d778060 to your computer and use it in GitHub Desktop.
Cali interface per pod
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
#!/bin/bash | |
# Tested on: Calico, Flannel, Canal | |
# Based on https://github.com/moby/moby/issues/17064#issuecomment-294020260, thanks! | |
# Sample output: | |
#PODIP MAC . CONTAINERNAME . CONTAINERID HOSTINTERFACE HOSTINTERFACEID | |
#10.42.0.4 2a:9f:ae:37:72:1d /k8s_POD_kube-dns-7588d5b5f5-nfg7m_kube-system_d7bbd8b9-23c6-11e9-ae75-f2e6ffededb1_0 59c6dc5e0686 cali38ead6f8cf5@if3: 60 | |
#10.42.0.3 8e:2f:6b:2a:39:b3 /k8s_POD_kube-dns-autoscaler-5db9bbb766-q9mgx_kube-system_d8448b72-23c6-11e9-ae75-f2e6ffededb1_0 52a2bc450eb5 calia637e63e14c@if3: 59 | |
#10.42.0.5 2e:d4:e4:bf:5b:65 /k8s_POD_default-http-backend-797c5bc547-scms7_ingress-nginx_de34fbda-23c6-11e9-ae75-f2e6ffededb1_0 4bb45486b56e cali2f9425798d9@if3: 61 | |
#10.42.0.2 6a:fb:30:47:93:cf /k8s_POD_metrics-server-97bc649d5-5txbx_kube-system_da8d90c0-23c6-11e9-ae75-f2e6ffededb1_0 039ee9406434 calicfa1484b96a@if3: 58 | |
function cali_interface_for_container() { | |
container_name=$(docker inspect --format='{{.Name}}' "${1}") | |
# Get the process ID for the container named ${1}: | |
local pid=$(docker inspect -f '{{.State.Pid}}' "${1}") | |
# Make the container's network namespace available to the ip-netns command: | |
mkdir -p /var/run/netns | |
ln -sf /proc/$pid/ns/net "/var/run/netns/${1}" | |
# Get the interface index of the container's eth0: | |
local podip=$(ip netns exec $1 ip addr show eth0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1) | |
local index=$(ip netns exec $1 ethtool -S eth0 2>/dev/null | grep peer_ifindex | awk '{ print $2 }') | |
local mac=$(ip netns exec $1 ip addr show eth0 | grep ether | awk '{ print $2 }') | |
# Write the name of the veth interface to stdout: | |
VETH=$(ip link show | grep "^${index}:" | awk '{ print $2 }') | |
if [ "x$VETH" != "x" ]; then | |
echo "$(echo $podip) $mac $container_name $1 $VETH $index" | |
fi | |
# Clean up the netns symlink, since we don't need it anymore | |
rm -f "/var/run/netns/${1}" | |
} | |
if [ "$#" -eq 0 ]; then | |
for docker_container in `docker ps -q --filter=name=POD`; do | |
cali_interface_for_container $docker_container | |
done | |
fi | |
if [ "$#" -eq 1 ]; then | |
cali_interface_for_container $1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment