README.md 2.9 KB
Newer Older
O
overweight 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
# isulad-hooks

This is a simple custom isulad hook for our own need,
it interacts with isulad as a multifunctional hook.

 1. allow user to add your own devices or binds into the container and update device Qos for container(device hook in prestart state).
 2. allow user to remove udev rule which added by isulad-tools when container is exiting(device hook in post-stop state).
 3. allow user to add network interface and route rule to container(network hook in prestart state).
 4. allow user to remove network interface on host when container is exiting(network hook in post-stop state).
 5. allow user to do oci relabel for container in both prestart and post-stop state for container.

Actually, this hook only handles the container restart process, we use isulad-tools to
add device/binds/network interface/route rule to container. And isulad-tools will save the device/network config to disk.
And the hook will make sure the resources you added to container will be persistent after restart.

Rename it to your favourite name afterwards.

## build

To build the binary, you need to download it then run 

```
# make
# sudo make install
```

Note: make install will install the binary into your "/usr/bin",
it's not a mandatory step, make your own choice for your convenience :)


## customise hook service

We could use `isulad-hooks` to customise the hook service.
```
Usage of isulad-hooks:
  -log string
        set output log file
  -state string
        set isulad hook state mode: prestart or poststop
  -with-relabel
        isulad hook enable oci relabel hook function
```

As block device and network interface are both in our requirement, so these two function are mandantory.
We could use `--with-relabel=true` to add oci-relabel hook service for container.
We could use `--state` to specify which state the hook will be running in.

Full hook config:
[hook spec example of isulad-hooks](hooks/isulad-hooks/example/hookspec.json)

## Try it!

53
First you need an enhanced `isula` with newly added `--hook-spec` flag,
O
overweight 已提交
54 55 56 57 58
after that, you can run it like this:

1.run isulad container with hook spec in `example` directory

```
59
$ isula run -d --name test_device --hook-spec $PWD/example/hookspec.json busybox sleep 20000
O
overweight 已提交
60 61 62 63 64 65 66 67 68 69
```
2.use isulad-tools to add device or binds to container

```
isulad-tools add-device test_device /dev/zero:/dev/test_zero:rwm /dev/zero:/dev/test_zero2:rwm
```

3.restart the container. to check the device is still in container.

```
70
isula restart test_device
O
overweight 已提交
71 72 73 74 75 76 77 78
```

Let's check the [`hookspec.json`](example/hookspec.json) file:

```
{
        "prestart": [
            {
79
                "path": "/var/lib/isulad/hooks/device-hook",
O
overweight 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
                "args": ["device-hook"],
                "env": []
            }
        ],
        "poststart":[],
        "poststop":[]
}
```

# Contact me

If you have any question or suggestion, contact me!
Also welcome for any issue or MR! Thanks!

Author: Zhang Wentao <zhangwentao234@huawei.com>