Skip to content

Commit 15dee2e

Browse files
committed
runtime: Add prestart/poststop hooks
Signed-off-by: Mrunal Patel <mrunalp@gmail.com> Add hooks to the spec Signed-off-by: Mrunal Patel <mrunalp@gmail.com>
1 parent 16aac94 commit 15dee2e

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

runtime.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,45 @@ Runs a process in a container. Can be invoked several times.
1515
Not sure we need that from runc cli. Process is killed from the outside.
1616

1717
This event needs to be captured by runc to run onstop event handlers.
18+
19+
## Hooks
20+
Hooks allow one to run code before/after various lifecycle events of the container.
21+
The state of the container is passed to the hooks over stdin, so the hooks could get the information they need to do their work.
22+
23+
Hook paths are absolute and are executed from the host's filesystem.
24+
25+
### Pre-start
26+
The pre-start hooks are called after the container process is spawned, but before the user supplied command is executed.
27+
They are called after the container namespaces are created on Linux, so they provide an opportunity to customize the container.
28+
In Linux, for e.g., the network namespace could be configured in this hook.
29+
30+
If a hook returns a non-zero exit code, then an error including the exit code and the stderr is returned to the caller and the container is torn down.
31+
32+
### Post-stop
33+
The post-stop hooks are called after the container process is stopped. Cleanup or debugging could be performed in such a hook.
34+
If a hook returns a non-zero exit code, then an error is logged and the remaining hooks are executed.
35+
36+
*Example*
37+
38+
```json
39+
"hooks" : {
40+
"prestart": [
41+
{
42+
"path": "/usr/bin/fix-mounts",
43+
"args": ["arg1", "arg2"],
44+
"env": [ "key1=value1"]
45+
},
46+
{
47+
"path": "/usr/bin/setup-network"
48+
}
49+
],
50+
"poststop": [
51+
{
52+
"path": "/usr/sbin/cleanup.sh",
53+
"args": ["-f"]
54+
}
55+
]
56+
}
57+
```
58+
59+
`path` is required for a hook. `args` and `env` are optional.

spec.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ type Spec struct {
1515
Hostname string `json:"hostname"`
1616
// Mounts profile configuration for adding mounts to the container's filesystem.
1717
Mounts []Mount `json:"mounts"`
18+
// Hooks are the commands run at various lifecycle events of the container.
19+
Hooks Hooks `json:"hooks"`
20+
}
21+
22+
type Hooks struct {
23+
// Prestart is a list of hooks to be run before the container process is executed.
24+
// On Linux, they are run after the container namespaces are created.
25+
Prestart []Hook `json:"prestart"`
26+
// Poststop is a list of hooks to be run after the container process exits.
27+
Poststop []Hook `json:"poststop"`
1828
}
1929

2030
// Mount specifies a mount for a container.
@@ -61,3 +71,10 @@ type Platform struct {
6171
// Arch is the architecture
6272
Arch string `json:"arch"`
6373
}
74+
75+
// Hook specifies a command that is run at a particular event in the lifecycle of a container.
76+
type Hook struct {
77+
Path string `json:"path"`
78+
Args []string `json:"args"`
79+
Env []string `json:"env"`
80+
}

0 commit comments

Comments
 (0)