Skip to content

Commit 6171da6

Browse files
kolyshkinlifubang
authored andcommitted
libct/configs: add HookList.SetDefaultEnv
1. Make CommandHook.Command a pointer, which reduces the amount of data being copied when using hooks, and allows to modify command hooks. 2. Add SetDefaultEnv, which is to be used by the next commit. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent c49b891 commit 6171da6

File tree

6 files changed

+29
-15
lines changed

6 files changed

+29
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### libcontainer API
10+
* `configs.CommandHook` struct has changed, Command is now a pointer.
11+
Also, `configs.NewCommandHook` now accepts a `*Command`. (#4325)
12+
913
## [1.2.0] - 2024-10-22
1014

1115
> できるときにできることをやるんだ。それが今だ。

libcontainer/configs/config.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,16 @@ func (hooks Hooks) Run(name HookName, state *specs.State) error {
434434
return nil
435435
}
436436

437+
// SetDefaultEnv sets the environment for those CommandHook entries
438+
// that do not have one set.
439+
func (hooks HookList) SetDefaultEnv(env []string) {
440+
for _, h := range hooks {
441+
if ch, ok := h.(CommandHook); ok && len(ch.Env) == 0 {
442+
ch.Env = env
443+
}
444+
}
445+
}
446+
437447
type Hook interface {
438448
// Run executes the hook with the provided state.
439449
Run(*specs.State) error
@@ -463,17 +473,17 @@ type Command struct {
463473
}
464474

465475
// NewCommandHook will execute the provided command when the hook is run.
466-
func NewCommandHook(cmd Command) CommandHook {
476+
func NewCommandHook(cmd *Command) CommandHook {
467477
return CommandHook{
468478
Command: cmd,
469479
}
470480
}
471481

472482
type CommandHook struct {
473-
Command
483+
*Command
474484
}
475485

476-
func (c Command) Run(s *specs.State) error {
486+
func (c *Command) Run(s *specs.State) error {
477487
b, err := json.Marshal(s)
478488
if err != nil {
479489
return err

libcontainer/configs/config_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func TestUnmarshalHooks(t *testing.T) {
1616
timeout := time.Second
1717

18-
hookCmd := configs.NewCommandHook(configs.Command{
18+
hookCmd := configs.NewCommandHook(&configs.Command{
1919
Path: "/var/vcap/hooks/hook",
2020
Args: []string{"--pid=123"},
2121
Env: []string{"FOO=BAR"},
@@ -52,7 +52,7 @@ func TestUnmarshalHooksWithInvalidData(t *testing.T) {
5252
func TestMarshalHooks(t *testing.T) {
5353
timeout := time.Second
5454

55-
hookCmd := configs.NewCommandHook(configs.Command{
55+
hookCmd := configs.NewCommandHook(&configs.Command{
5656
Path: "/var/vcap/hooks/hook",
5757
Args: []string{"--pid=123"},
5858
Env: []string{"FOO=BAR"},
@@ -84,7 +84,7 @@ func TestMarshalHooks(t *testing.T) {
8484
func TestMarshalUnmarshalHooks(t *testing.T) {
8585
timeout := time.Second
8686

87-
hookCmd := configs.NewCommandHook(configs.Command{
87+
hookCmd := configs.NewCommandHook(&configs.Command{
8888
Path: "/var/vcap/hooks/hook",
8989
Args: []string{"--pid=123"},
9090
Env: []string{"FOO=BAR"},
@@ -194,7 +194,7 @@ exit 0
194194
}
195195
defer os.Remove(filename)
196196

197-
cmdHook := configs.NewCommandHook(configs.Command{
197+
cmdHook := configs.NewCommandHook(&configs.Command{
198198
Path: filename,
199199
Args: []string{filename, "testarg"},
200200
Env: []string{"FOO=BAR"},
@@ -216,7 +216,7 @@ func TestCommandHookRunTimeout(t *testing.T) {
216216
}
217217
timeout := 100 * time.Millisecond
218218

219-
cmdHook := configs.NewCommandHook(configs.Command{
219+
cmdHook := configs.NewCommandHook(&configs.Command{
220220
Path: "/bin/sleep",
221221
Args: []string{"/bin/sleep", "1"},
222222
Timeout: &timeout,

libcontainer/factory_linux_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ func TestFactoryLoadContainer(t *testing.T) {
3131
id = "1"
3232
expectedHooks = configs.Hooks{
3333
configs.Prestart: configs.HookList{
34-
configs.CommandHook{Command: configs.Command{Path: "prestart-hook"}},
34+
configs.CommandHook{Command: &configs.Command{Path: "prestart-hook"}},
3535
},
3636
configs.Poststart: configs.HookList{
37-
configs.CommandHook{Command: configs.Command{Path: "poststart-hook"}},
37+
configs.CommandHook{Command: &configs.Command{Path: "poststart-hook"}},
3838
},
3939
configs.Poststop: configs.HookList{
4040
unserializableHook{},
41-
configs.CommandHook{Command: configs.Command{Path: "poststop-hook"}},
41+
configs.CommandHook{Command: &configs.Command{Path: "poststop-hook"}},
4242
},
4343
}
4444
expectedConfig = &configs.Config{

libcontainer/integration/exec_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,13 +1022,13 @@ func TestHook(t *testing.T) {
10221022
}),
10231023
},
10241024
configs.CreateContainer: configs.HookList{
1025-
configs.NewCommandHook(configs.Command{
1025+
configs.NewCommandHook(&configs.Command{
10261026
Path: "/bin/bash",
10271027
Args: []string{"/bin/bash", "-c", fmt.Sprintf("touch ./%s", hookFiles[configs.CreateContainer])},
10281028
}),
10291029
},
10301030
configs.StartContainer: configs.HookList{
1031-
configs.NewCommandHook(configs.Command{
1031+
configs.NewCommandHook(&configs.Command{
10321032
Path: "/bin/sh",
10331033
Args: []string{"/bin/sh", "-c", fmt.Sprintf("touch /%s", hookFiles[configs.StartContainer])},
10341034
}),

libcontainer/specconv/spec_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,8 +1256,8 @@ func createHooks(rspec *specs.Spec, config *configs.Config) {
12561256
}
12571257
}
12581258

1259-
func createCommandHook(h specs.Hook) configs.Command {
1260-
cmd := configs.Command{
1259+
func createCommandHook(h specs.Hook) *configs.Command {
1260+
cmd := &configs.Command{
12611261
Path: h.Path,
12621262
Args: h.Args,
12631263
Env: h.Env,

0 commit comments

Comments
 (0)