Skip to content

Instantly share code, notes, and snippets.

@dougbtv
Created May 14, 2021 15:42
Show Gist options
  • Save dougbtv/294b9599d897be55a97396e03cba3dae to your computer and use it in GitHub Desktop.
Save dougbtv/294b9599d897be55a97396e03cba3dae to your computer and use it in GitHub Desktop.
Sample CNI plugin using Bash, for learning and debugging.
#!/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