Sample CNI plugin using Bash, for learning and debugging.
This file contains 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
#!/usr/bin/env bash | |
DEBUG=true | |
# LOGFILE=/tmp/seamless.log | |
# Outputs errors to stderr | |
errorlog () { | |
>&2 echo $1 | |
} | |
# Logs for debugging. | |
debuglog () { | |
# Set DEBUG to anything to enable debug output. | |
if [ -n "$DEBUG" ]; then | |
# touch /tmp/seamless_a | |
# Set logfile if you want to log to a flat file. | |
if [ -n "$LOGFILE" ]; then | |
# touch /tmp/seamless_b | |
echo $1 >> $LOGFILE.$CNI_CONTAINERID | |
else | |
# Otherwise, log to stderr. | |
>&2 echo $1 | |
fi | |
fi | |
} | |
# Outputs an essentially dummy CNI result that's borderline acceptable by the spec. | |
# https://github.com/containernetworking/cni/blob/master/SPEC.md#result | |
cniresult () { | |
cat << EOF | |
{ | |
"cniVersion": "0.4.0", | |
"interfaces": [ | |
{ | |
"name": "dummy" | |
} | |
], | |
"ips": [] | |
} | |
EOF | |
} | |
# Certain failures we want to exit on. | |
exit_on_error() { | |
exit_code=$1 | |
last_command=${@:2} | |
if [ $exit_code -ne 0 ]; then | |
>&2 echo "dummy: \"${last_command}\" command failed with exit code ${exit_code}." | |
cniresult | |
exit $exit_code | |
fi | |
} | |
# Overarching basic parameters. | |
containerifname=eth0 | |
# --------------------------------------- REFERENCE: Common environment variables. | |
debuglog "CNI method: $CNI_COMMAND" | |
debuglog "CNI container id: $CNI_CONTAINERID" | |
debuglog "CNI netns: $CNI_NETNS" | |
cniresult |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment