Skip to content

Commit a344b2d

Browse files
committed
sync up HookState with OCI spec State
`HookState` struct should follow definition of `State` in runtime-spec: * modify json name of `version` to `ociVersion`. * Remove redundant `Rootfs` field as rootfs can be retrived from `bundlePath/config.json`. Signed-off-by: Zhang Wei <[email protected]>
1 parent 9a1e53e commit a344b2d

File tree

7 files changed

+73
-36
lines changed

7 files changed

+73
-36
lines changed

libcontainer/configs/config.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/Sirupsen/logrus"
11+
"github.com/opencontainers/runtime-spec/specs-go"
1112
)
1213

1314
type Rlimit struct {
@@ -243,13 +244,7 @@ func (hooks Hooks) MarshalJSON() ([]byte, error) {
243244
}
244245

245246
// HookState is the payload provided to a hook on execution.
246-
type HookState struct {
247-
Version string `json:"ociVersion"`
248-
ID string `json:"id"`
249-
Pid int `json:"pid"`
250-
Root string `json:"root"`
251-
BundlePath string `json:"bundlePath"`
252-
}
247+
type HookState specs.State
253248

254249
type Hook interface {
255250
// Run executes the hook with the provided state.

libcontainer/configs/config_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ func TestMarshalHooksWithUnexpectedType(t *testing.T) {
120120

121121
func TestFuncHookRun(t *testing.T) {
122122
state := configs.HookState{
123-
Version: "1",
124-
ID: "1",
125-
Pid: 1,
126-
Root: "root",
123+
Version: "1",
124+
ID: "1",
125+
Pid: 1,
126+
BundlePath: "/bundle",
127127
}
128128

129129
fHook := configs.NewFunctionHook(func(s configs.HookState) error {
@@ -138,10 +138,10 @@ func TestFuncHookRun(t *testing.T) {
138138

139139
func TestCommandHookRun(t *testing.T) {
140140
state := configs.HookState{
141-
Version: "1",
142-
ID: "1",
143-
Pid: 1,
144-
Root: "root",
141+
Version: "1",
142+
ID: "1",
143+
Pid: 1,
144+
BundlePath: "/bundle",
145145
}
146146
timeout := time.Second
147147

@@ -161,10 +161,10 @@ func TestCommandHookRun(t *testing.T) {
161161

162162
func TestCommandHookRunTimeout(t *testing.T) {
163163
state := configs.HookState{
164-
Version: "1",
165-
ID: "1",
166-
Pid: 1,
167-
Root: "root",
164+
Version: "1",
165+
ID: "1",
166+
Pid: 1,
167+
BundlePath: "/bundle",
168168
}
169169
timeout := (10 * time.Millisecond)
170170

libcontainer/container_linux.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,6 @@ func (c *linuxContainer) start(process *Process, isInit bool) error {
266266
Version: c.config.Version,
267267
ID: c.id,
268268
Pid: parent.pid(),
269-
Root: c.config.Rootfs,
270269
BundlePath: utils.SearchLabels(c.config.Labels, "bundle"),
271270
}
272271
for i, hook := range c.config.Hooks.Poststart {
@@ -1038,10 +1037,10 @@ func (c *linuxContainer) criuNotifications(resp *criurpc.CriuResp, process *Proc
10381037
case notify.GetScript() == "setup-namespaces":
10391038
if c.config.Hooks != nil {
10401039
s := configs.HookState{
1041-
Version: c.config.Version,
1042-
ID: c.id,
1043-
Pid: int(notify.GetPid()),
1044-
Root: c.config.Rootfs,
1040+
Version: c.config.Version,
1041+
ID: c.id,
1042+
Pid: int(notify.GetPid()),
1043+
BundlePath: utils.SearchLabels(c.config.Labels, "bundle"),
10451044
}
10461045
for i, hook := range c.config.Hooks.Prestart {
10471046
if err := hook.Run(s); err != nil {

libcontainer/integration/exec_test.go

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package integration
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"fmt"
67
"io/ioutil"
78
"os"
@@ -1048,25 +1049,44 @@ func TestHook(t *testing.T) {
10481049
if testing.Short() {
10491050
return
10501051
}
1051-
root, err := newTestRoot()
1052+
1053+
bundle, err := newTestBundle()
10521054
ok(t, err)
1053-
defer os.RemoveAll(root)
1055+
defer remove(bundle)
10541056

10551057
rootfs, err := newRootfs()
10561058
ok(t, err)
10571059
defer remove(rootfs)
10581060

10591061
config := newTemplateConfig(rootfs)
1060-
expectedBundlePath := "/path/to/bundle/path"
1062+
expectedBundlePath := bundle
10611063
config.Labels = append(config.Labels, fmt.Sprintf("bundle=%s", expectedBundlePath))
1064+
1065+
getRootfsFromBundle := func(bundle string) (string, error) {
1066+
f, err := os.Open(filepath.Join(bundle, "config.json"))
1067+
if err != nil {
1068+
return "", err
1069+
}
1070+
1071+
var config configs.Config
1072+
if err = json.NewDecoder(f).Decode(&config); err != nil {
1073+
return "", err
1074+
}
1075+
return config.Rootfs, nil
1076+
}
1077+
10621078
config.Hooks = &configs.Hooks{
10631079
Prestart: []configs.Hook{
10641080
configs.NewFunctionHook(func(s configs.HookState) error {
10651081
if s.BundlePath != expectedBundlePath {
10661082
t.Fatalf("Expected prestart hook bundlePath '%s'; got '%s'", expectedBundlePath, s.BundlePath)
10671083
}
10681084

1069-
f, err := os.Create(filepath.Join(s.Root, "test"))
1085+
root, err := getRootfsFromBundle(s.BundlePath)
1086+
if err != nil {
1087+
return err
1088+
}
1089+
f, err := os.Create(filepath.Join(root, "test"))
10701090
if err != nil {
10711091
return err
10721092
}
@@ -1079,7 +1099,11 @@ func TestHook(t *testing.T) {
10791099
t.Fatalf("Expected poststart hook bundlePath '%s'; got '%s'", expectedBundlePath, s.BundlePath)
10801100
}
10811101

1082-
return ioutil.WriteFile(filepath.Join(s.Root, "test"), []byte("hello world"), 0755)
1102+
root, err := getRootfsFromBundle(s.BundlePath)
1103+
if err != nil {
1104+
return err
1105+
}
1106+
return ioutil.WriteFile(filepath.Join(root, "test"), []byte("hello world"), 0755)
10831107
}),
10841108
},
10851109
Poststop: []configs.Hook{
@@ -1088,10 +1112,20 @@ func TestHook(t *testing.T) {
10881112
t.Fatalf("Expected poststop hook bundlePath '%s'; got '%s'", expectedBundlePath, s.BundlePath)
10891113
}
10901114

1091-
return os.RemoveAll(filepath.Join(s.Root, "test"))
1115+
root, err := getRootfsFromBundle(s.BundlePath)
1116+
if err != nil {
1117+
return err
1118+
}
1119+
return os.RemoveAll(filepath.Join(root, "test"))
10921120
}),
10931121
},
10941122
}
1123+
1124+
// write config of json format into config.json under bundle
1125+
f, err := os.OpenFile(filepath.Join(bundle, "config.json"), os.O_CREATE|os.O_RDWR, 0644)
1126+
ok(t, err)
1127+
ok(t, json.NewEncoder(f).Encode(config))
1128+
10951129
container, err := factory.Create("test", config)
10961130
ok(t, err)
10971131

libcontainer/integration/utils_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ func newTestRoot() (string, error) {
7676
return dir, nil
7777
}
7878

79+
func newTestBundle() (string, error) {
80+
dir, err := ioutil.TempDir("", "bundle")
81+
if err != nil {
82+
return "", err
83+
}
84+
if err := os.MkdirAll(dir, 0700); err != nil {
85+
return "", err
86+
}
87+
return dir, nil
88+
}
89+
7990
// newRootfs creates a new tmp directory and copies the busybox root filesystem
8091
func newRootfs() (string, error) {
8192
dir, err := ioutil.TempDir("", "")

libcontainer/process_linux.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,10 @@ func (p *initProcess) start() error {
339339
if !p.config.Config.Namespaces.Contains(configs.NEWNS) {
340340
if p.config.Config.Hooks != nil {
341341
s := configs.HookState{
342-
Version: p.container.config.Version,
343-
ID: p.container.id,
344-
Pid: p.pid(),
345-
Root: p.config.Config.Rootfs,
342+
Version: p.container.config.Version,
343+
ID: p.container.id,
344+
Pid: p.pid(),
345+
BundlePath: utils.SearchLabels(p.config.Config.Labels, "bundle"),
346346
}
347347
for i, hook := range p.config.Config.Hooks.Prestart {
348348
if err := hook.Run(s); err != nil {
@@ -362,7 +362,6 @@ func (p *initProcess) start() error {
362362
Version: p.container.config.Version,
363363
ID: p.container.id,
364364
Pid: p.pid(),
365-
Root: p.config.Config.Rootfs,
366365
BundlePath: utils.SearchLabels(p.config.Config.Labels, "bundle"),
367366
}
368367
for i, hook := range p.config.Config.Hooks.Prestart {

libcontainer/state_linux.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ func runPoststopHooks(c *linuxContainer) error {
6060
s := configs.HookState{
6161
Version: c.config.Version,
6262
ID: c.id,
63-
Root: c.config.Rootfs,
6463
BundlePath: utils.SearchLabels(c.config.Labels, "bundle"),
6564
}
6665
for _, hook := range c.config.Hooks.Poststop {

0 commit comments

Comments
 (0)