Skip to content

Commit f7750e5

Browse files
committed
Use State from the spec
Signed-off-by: Mrunal Patel <[email protected]>
1 parent 60940c6 commit f7750e5

4 files changed

Lines changed: 21 additions & 22 deletions

File tree

libcontainer/configs/config.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"bytes"
55
"encoding/json"
66
"os/exec"
7+
8+
"github.com/opencontainers/specs"
79
)
810

911
type Rlimit struct {
@@ -180,30 +182,23 @@ type Hooks struct {
180182
Poststop []Hook
181183
}
182184

183-
// HookState is the payload provided to a hook on execution.
184-
type HookState struct {
185-
ID string `json:"id"`
186-
Pid int `json:"pid"`
187-
Root string `json:"root"`
188-
}
189-
190185
type Hook interface {
191186
// Run executes the hook with the provided state.
192-
Run(HookState) error
187+
Run(specs.State) error
193188
}
194189

195190
// NewFunctionHooks will call the provided function when the hook is run.
196-
func NewFunctionHook(f func(HookState) error) FuncHook {
191+
func NewFunctionHook(f func(specs.State) error) FuncHook {
197192
return FuncHook{
198193
run: f,
199194
}
200195
}
201196

202197
type FuncHook struct {
203-
run func(HookState) error
198+
run func(specs.State) error
204199
}
205200

206-
func (f FuncHook) Run(s HookState) error {
201+
func (f FuncHook) Run(s specs.State) error {
207202
return f.run(s)
208203
}
209204

@@ -225,7 +220,7 @@ type CommandHook struct {
225220
Command
226221
}
227222

228-
func (c Command) Run(s HookState) error {
223+
func (c Command) Run(s specs.State) error {
229224
b, err := json.Marshal(s)
230225
if err != nil {
231226
return err

libcontainer/container_linux.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/opencontainers/runc/libcontainer/cgroups"
1919
"github.com/opencontainers/runc/libcontainer/configs"
2020
"github.com/opencontainers/runc/libcontainer/criurpc"
21+
"github.com/opencontainers/specs"
2122
)
2223

2324
const stdioFdCount = 3
@@ -249,9 +250,10 @@ func (c *linuxContainer) Destroy() error {
249250
}
250251
c.initProcess = nil
251252
if c.config.Hooks != nil {
252-
s := configs.HookState{
253-
ID: c.id,
254-
Root: c.config.Rootfs,
253+
s := specs.State{
254+
Version: specs.Version,
255+
ID: c.id,
256+
Root: c.config.Rootfs,
255257
}
256258
for _, hook := range c.config.Hooks.Poststop {
257259
if err := hook.Run(s); err != nil {

libcontainer/integration/exec_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/opencontainers/runc/libcontainer"
1414
"github.com/opencontainers/runc/libcontainer/cgroups/systemd"
1515
"github.com/opencontainers/runc/libcontainer/configs"
16+
"github.com/opencontainers/specs"
1617
)
1718

1819
func TestExecPS(t *testing.T) {
@@ -948,7 +949,7 @@ func TestHook(t *testing.T) {
948949
config := newTemplateConfig(rootfs)
949950
config.Hooks = &configs.Hooks{
950951
Prestart: []configs.Hook{
951-
configs.NewFunctionHook(func(s configs.HookState) error {
952+
configs.NewFunctionHook(func(s specs.State) error {
952953
f, err := os.Create(filepath.Join(s.Root, "test"))
953954
if err != nil {
954955
return err
@@ -957,7 +958,7 @@ func TestHook(t *testing.T) {
957958
}),
958959
},
959960
Poststop: []configs.Hook{
960-
configs.NewFunctionHook(func(s configs.HookState) error {
961+
configs.NewFunctionHook(func(s specs.State) error {
961962
return os.RemoveAll(filepath.Join(s.Root, "test"))
962963
}),
963964
},

libcontainer/process_linux.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
"syscall"
1414

1515
"github.com/opencontainers/runc/libcontainer/cgroups"
16-
"github.com/opencontainers/runc/libcontainer/configs"
1716
"github.com/opencontainers/runc/libcontainer/system"
17+
"github.com/opencontainers/specs"
1818
)
1919

2020
type parentProcess interface {
@@ -202,10 +202,11 @@ func (p *initProcess) start() (err error) {
202202
}
203203
}()
204204
if p.config.Config.Hooks != nil {
205-
s := configs.HookState{
206-
ID: p.container.id,
207-
Pid: p.pid(),
208-
Root: p.config.Config.Rootfs,
205+
s := specs.State{
206+
Version: specs.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 {

0 commit comments

Comments
 (0)