- 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