Skip to content

Commit aac9179

Browse files
committed
Merge pull request #160 from mrunalp/feature/hooks
Add prestart/poststop hooks to runc
2 parents dbfcbc4 + dcafe48 commit aac9179

4 files changed

Lines changed: 36 additions & 8 deletions

File tree

libcontainer/configs/config.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ type Config struct {
173173
// Hooks are a collection of actions to perform at various container lifecycle events.
174174
// Hooks are not able to be marshaled to json but they are also not needed to.
175175
Hooks *Hooks `json:"-"`
176+
177+
// Version is the version of opencontainer specification that is supported.
178+
Version string `json:"version"`
176179
}
177180

178181
type Hooks struct {
@@ -186,9 +189,10 @@ type Hooks struct {
186189

187190
// HookState is the payload provided to a hook on execution.
188191
type HookState struct {
189-
ID string `json:"id"`
190-
Pid int `json:"pid"`
191-
Root string `json:"root"`
192+
Version string `json:"version"`
193+
ID string `json:"id"`
194+
Pid int `json:"pid"`
195+
Root string `json:"root"`
192196
}
193197

194198
type Hook interface {

libcontainer/container_linux.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,9 @@ func (c *linuxContainer) Destroy() error {
250250
c.initProcess = nil
251251
if c.config.Hooks != nil {
252252
s := configs.HookState{
253-
ID: c.id,
254-
Root: c.config.Rootfs,
253+
Version: c.config.Version,
254+
ID: c.id,
255+
Root: c.config.Rootfs,
255256
}
256257
for _, hook := range c.config.Hooks.Poststop {
257258
if err := hook.Run(s); err != nil {

libcontainer/process_linux.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,10 @@ func (p *initProcess) start() (err error) {
203203
}()
204204
if p.config.Config.Hooks != nil {
205205
s := configs.HookState{
206-
ID: p.container.id,
207-
Pid: p.pid(),
208-
Root: p.config.Config.Rootfs,
206+
Version: p.container.config.Version,
207+
ID: p.container.id,
208+
Pid: p.pid(),
209+
Root: p.config.Config.Rootfs,
209210
}
210211
for _, hook := range p.config.Config.Hooks.Prestart {
211212
if err := hook.Run(s); err != nil {

spec.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,8 @@ func createLibcontainerConfig(cgroupName string, spec *specs.LinuxSpec, rspec *s
391391
config.Sysctl = rspec.Linux.Sysctl
392392
config.ProcessLabel = rspec.Linux.SelinuxProcessLabel
393393
config.AppArmorProfile = rspec.Linux.ApparmorProfile
394+
createHooks(rspec, config)
395+
config.Version = specs.Version
394396
return config, nil
395397
}
396398

@@ -653,3 +655,23 @@ func setupSeccomp(config *specs.Seccomp) (*configs.Seccomp, error) {
653655

654656
return newConfig, nil
655657
}
658+
659+
func createHooks(rspec *specs.LinuxRuntimeSpec, config *configs.Config) {
660+
config.Hooks = &configs.Hooks{}
661+
for _, h := range rspec.Hooks.Prestart {
662+
cmd := configs.Command{
663+
Path: h.Path,
664+
Args: h.Args,
665+
Env: h.Env,
666+
}
667+
config.Hooks.Prestart = append(config.Hooks.Prestart, configs.NewCommandHook(cmd))
668+
}
669+
for _, h := range rspec.Hooks.Poststop {
670+
cmd := configs.Command{
671+
Path: h.Path,
672+
Args: h.Args,
673+
Env: h.Env,
674+
}
675+
config.Hooks.Poststop = append(config.Hooks.Poststop, configs.NewCommandHook(cmd))
676+
}
677+
}

0 commit comments

Comments
 (0)