Skip to content

Instantly share code, notes, and snippets.

@rsevilla87
Last active October 17, 2019 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsevilla87/b6653c989e85ca793acc4acb18febb3b to your computer and use it in GitHub Desktop.
Save rsevilla87/b6653c989e85ca793acc4acb18febb3b to your computer and use it in GitHub Desktop.
Testing CNI plugins like a boss

host-device plugin

  • Create CNI config
cat > cni.cfg << EOF
{
  "name": "mynet",
  "cniVersion": "0.3.0",
  "type": "host-device",
  "device": "wlp4s0.12345",
  "ipam": {
      "type": "host-local",
      "subnet": "10.1.0.0/16",
      "gateway": "10.1.0.1"
    }
}
EOF
  • Create a dummy interface. e.g. a VLAN interface:
sudo ip l add link wlp4s0 wlp4s0.12345 type vlan id 10
  • In another terminal, create a new network namespace and its network namespace ID
 $ unshare -r -n bash
 $ echo /proc/$$/ns/net
 /proc/3488/ns/net
  • In the first terminal, configure environemnt variables and execute the plugin.
$ export PATH=$PATH:/usr/libexec/cni
$ export CNI_PATH=$PATH
$ export CNI_IFNAME=myif
$ export CNI_COMMAND=ADD
$ export CNI_CONTAINERID=my-container
  • Finally execute the plugin
$ sudo -E /usr/libexec/cni/host-device <<< $(cat test)
{
    "cniVersion": "0.3.0",
    "interfaces": [
        {
            "name": "myif2",
            "mac": "f0:d5:bf:ea:b4:7e",
            "sandbox": "/proc/6848/ns/net"
        }
    ],
    "ips": [
        {
            "version": "4",
            "interface": 0,
            "address": "10.1.0.2/16",
            "gateway": "10.1.0.1"
        }
    ],
    "dns": {}
}

You should see an interface (myif2) to the previously created network namespace.

$ ip -4 a
34: myif2@if3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 link-netnsid 0
    inet 10.1.0.2/16 brd 10.1.255.255 scope global myif2
       valid_lft forever preferred_lft forever
$ ip r
10.1.0.0/16 dev myif2 proto kernel scope link src 10.1.0.2 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment