| 1 |
# cnitool |
| 2 |
|
| 3 |
`cnitool` is a simple program that executes a CNI configuration. It will |
| 4 |
add or remove an interface in an already-created network namespace. |
| 5 |
|
| 6 |
## Environment Variables |
| 7 |
|
| 8 |
* `NETCONFPATH`: This environment variable needs to be set to a |
| 9 |
directory. It defaults to `/etc/cni/net.d`. The `cnitool` searches |
| 10 |
for CNI configuration files in this directory according to the following priorities: |
| 11 |
1. Search files with the extension `*.conflist`, representing a list of plugin configurations. |
| 12 |
2. If there are no `*.conflist` files in the directory, search files with the extension `*.conf` or `*.json`, |
| 13 |
representing a single plugin configuration. |
| 14 |
|
| 15 |
It loads all the CNI configuration files in |
| 16 |
this directory and if it finds a CNI configuration with the `network |
| 17 |
name` given to the cnitool it returns the corresponding CNI |
| 18 |
configuration, else it returns `nil`. |
| 19 |
* `CNI_PATH`: For a given CNI configuration `cnitool` will search for |
| 20 |
the corresponding CNI plugin in this path. |
| 21 |
|
| 22 |
## Example invocation |
| 23 |
|
| 24 |
First, install cnitool: |
| 25 |
|
| 26 |
```bash |
| 27 |
go get github.com/containernetworking/cni |
| 28 |
go install github.com/containernetworking/cni/cnitool |
| 29 |
``` |
| 30 |
|
| 31 |
Then, check out and build the plugins. All commands should be run from this directory. |
| 32 |
|
| 33 |
```bash |
| 34 |
git clone https://github.com/containernetworking/plugins.git |
| 35 |
cd plugins |
| 36 |
./build_linux.sh |
| 37 |
# or |
| 38 |
./build_windows.sh |
| 39 |
``` |
| 40 |
|
| 41 |
Create a network configuration |
| 42 |
|
| 43 |
```bash |
| 44 |
echo '{"cniVersion":"0.4.0","name":"myptp","type":"ptp","ipMasq":true,"ipam":{"type":"host-local","subnet":"172.16.29.0/24","routes":[{"dst":"0.0.0.0/0"}]}}' | sudo tee /etc/cni/net.d/10-myptp.conf |
| 45 |
``` |
| 46 |
|
| 47 |
Create a network namespace. This will be called `testing`: |
| 48 |
|
| 49 |
```bash |
| 50 |
sudo ip netns add testing |
| 51 |
``` |
| 52 |
|
| 53 |
Add the container to the network: |
| 54 |
|
| 55 |
```bash |
| 56 |
sudo CNI_PATH=./bin cnitool add myptp /var/run/netns/testing |
| 57 |
``` |
| 58 |
|
| 59 |
Check whether the container's networking is as expected (ONLY for spec v0.4.0+): |
| 60 |
|
| 61 |
```bash |
| 62 |
sudo CNI_PATH=./bin cnitool check myptp /var/run/netns/testing |
| 63 |
``` |
| 64 |
|
| 65 |
Test that it works: |
| 66 |
|
| 67 |
```bash |
| 68 |
sudo ip -n testing addr |
| 69 |
sudo ip netns exec testing ping -c 1 4.2.2.2 |
| 70 |
``` |
| 71 |
|
| 72 |
And clean up: |
| 73 |
|
| 74 |
```bash |
| 75 |
sudo CNI_PATH=./bin cnitool del myptp /var/run/netns/testing |
| 76 |
sudo ip netns del testing |
| 77 |
``` |