Skip to content

Instantly share code, notes, and snippets.

@oskapt
Forked from superseb/cali_interface_per_pod.sh
Created January 29, 2019 12:39
Show Gist options
  • Select an option

  • Save oskapt/d041d4f5d0d87c221096bc0cc3416e70 to your computer and use it in GitHub Desktop.

Select an option

Save oskapt/d041d4f5d0d87c221096bc0cc3416e70 to your computer and use it in GitHub Desktop.

Revisions

  1. @superseb superseb created this gist Jan 29, 2019.
    37 changes: 37 additions & 0 deletions cali_interface_per_pod.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    #!/bin/bash
    # Based on https://github.com/moby/moby/issues/17064#issuecomment-294020260, thanks!

    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 }')

    echo "$1 $container_name $(echo $podip) $mac $VETH $index"

    # 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