Skip to content

Instantly share code, notes, and snippets.

@reidstidolph
Created May 28, 2019 15:32
Show Gist options
  • Save reidstidolph/9250bbceb1b993a58299ef3750b8225b to your computer and use it in GitHub Desktop.
Save reidstidolph/9250bbceb1b993a58299ef3750b8225b to your computer and use it in GitHub Desktop.
128T network plugin scrips for interfacing Docker containers.
#!/usr/bin/env bash
###################################################################
# #
# This plugin init script is used to network containers built #
# with Docker. #
# #
# It expects a container to have been created in advance #
# (e.g. with '--network="none"'). The name of the container #
# ('--name') is expected to be passed in as the #
# "network-namespace" setting of a host KNI. #
# #
# It also expects the host kni to be configured with a gateway, #
# which will become the container address. #
# #
# If the container is found to be stopped, it will start it. #
# #
# Example: #
# $ sudo docker run --name my-ctnr --network="none" -d -t alpine #
# #
# device-interface docker-int #
# name docker-int #
# type host #
# network-namespace my-ctnr #
# #
# network-interface container-int #
# name container-int #
# #
# address 10.128.128.1 #
# ip-address 10.128.128.1 #
# prefix-length 29 #
# gateway 10.128.128.2 #
# exit #
# exit #
# exit #
# #
###################################################################
# import common functions related to namespace operations
source /etc/128technology/plugins/network-scripts/common/namespace_helpers
# read arguments provided to script
for i in "$@"
do
case $i in
--kni-interface=*)
KNI_NAME="${i#*=}"
shift
;;
--kni-ip=*)
IPADDR="${i#*=}"
shift
;;
--kni-prefix-length=*)
PREFIX="${i#*=}"
shift
;;
--kni-gateway=*)
GATEWAY="${i#*=}"
shift
;;
--namespace=*)
NS_PLUS_NSID="${i#*=}"
shift
;;
*)
echo "Got unknown arg $i"
;;
esac
done
# use provided namespace:nsid with namespace_helpers to populate NS and NSID vars
echo "namespace arg provided is $NS_PLUS_NSID" | systemd-cat -t kni-attach
NS=$(namespace_get_name $NS_PLUS_NSID)
NSID=$(namespace_get_id $NS_PLUS_NSID)
echo "namespace is $NS, nsid is $NSID" | systemd-cat -t kni-attach
# check to see if docker is already running
systemctl is-active --quiet docker
if [ $? -ne 0 ] ; then
echo "docker not running, starting docker" | systemd-cat -t kni-attach
systemd-cat -t kni-attach systemctl start docker
fi
# check to see if docker container exists
docker container inspect $NS &> /dev/null
if [ $? -ne 0 ] ; then
echo "ERROR: $NS container does not exist in Docker"
exit 1
fi
# start container
echo "starting $NS docker container"
systemd-cat -t kni-attach docker start $NS
# populate docker container namespace variable
DOCKER_NS="$(docker inspect --format='{{.NetworkSettings.SandboxKey}}' $NS)"
# if the desired namespace isn't found, link it to the Docker namespace
if [ ! -e "/var/run/netns/$NS" ]; then
echo "Linking $NS to $DOCKER_NS" | systemd-cat -t kni-attach
systemd-cat -t kni-attach ln -sf $DOCKER_NS /var/run/netns/$NS
systemd-cat -t kni-attach ip netns set $NS $NSID
fi
# set up KNI if it exists in the default namespace
if [ -d "/sys/devices/virtual/net/$KNI_NAME" ]; then
echo "$KNI_NAME found in default namespace." | systemd-cat -t kni-attach
echo "Moving $KNI_NAME to $NS: net=$IPADDR/$PREFIX gw=$GATEWAY." | systemd-cat -t kni-attach
systemd-cat -t kni-attach ip link set $KNI_NAME netns $NS
systemd-cat -t kni-attach ip netns exec $NS ip address add $IPADDR/$PREFIX dev $KNI_NAME
systemd-cat -t kni-attach ip netns exec $NS ip link set $KNI_NAME up
systemd-cat -t kni-attach ip netns exec $NS ip route add default via $GATEWAY dev $KNI_NAME
fi
#!/usr/bin/env bash
# import common functions related to namespace operations
source /etc/128technology/plugins/network-scripts/common/namespace_helpers
# read arguments provided to script
for i in "$@"
do
case $i in
--kni-interface=*)
KNI_NAME="${i#*=}"
shift
;;
--kni-ip=*)
IPADDR="${i#*=}"
shift
;;
--kni-prefix-length=*)
PREFIX="${i#*=}"
shift
;;
--kni-gateway=*)
GATEWAY="${i#*=}"
shift
;;
--namespace=*)
NS_PLUS_NSID="${i#*=}"
shift
;;
*)
echo "Got unknown arg $i"
;;
esac
done
# use provided namespace:nsid with namespace_helpers to populate NS and NSID vars
NS=$(namespace_get_name $NS_PLUS_NSID)
# clean up namespace link
echo "unlinking /var/run/netns/$NS" | systemd-cat -t kni-attach
systemd-cat -t kni-attach unlink /var/run/netns/$NS
# stop container
echo "stopping $NS docker container" | systemd-cat -t kni-attach
docker stop $NS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment