scripts/priv-net-run.sh
raw| 1 | #!/usr/bin/env bash |
| 2 | set -e |
| 3 | if [[ ${DEBUG} -gt 0 ]]; then set -x; fi |
| 4 | |
| 5 | # Run a command in a private network namespace |
| 6 | # set up by CNI plugins |
| 7 | contid=$(printf '%x%x%x%x' $RANDOM $RANDOM $RANDOM $RANDOM) |
| 8 | netnspath=/var/run/netns/$contid |
| 9 | |
| 10 | ip netns add $contid |
| 11 | ./exec-plugins.sh add $contid $netnspath |
| 12 | |
| 13 | |
| 14 | function cleanup() { |
| 15 | ./exec-plugins.sh del $contid $netnspath |
| 16 | ip netns delete $contid |
| 17 | } |
| 18 | trap cleanup EXIT |
| 19 | |
| 20 | ip netns exec $contid "$@" |