diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f42f11309e..92e1b23cfde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### libcontainer API + * `configs.CommandHook` struct has changed, Command is now a pointer. + Also, `configs.NewCommandHook` now accepts a `*Command`. (#4325) + ## [1.2.0] - 2024-10-22 > できるときにできることをやるんだ。それが今だ。 diff --git a/libcontainer/configs/config.go b/libcontainer/configs/config.go index 0c7a0128c74..1d2454b2e5d 100644 --- a/libcontainer/configs/config.go +++ b/libcontainer/configs/config.go @@ -440,6 +440,16 @@ func (hooks Hooks) Run(name HookName, state *specs.State) error { return nil } +// SetDefaultEnv sets the environment for those CommandHook entries +// that do not have one set. +func (hooks HookList) SetDefaultEnv(env []string) { + for _, h := range hooks { + if ch, ok := h.(CommandHook); ok && len(ch.Env) == 0 { + ch.Env = env + } + } +} + type Hook interface { // Run executes the hook with the provided state. Run(*specs.State) error @@ -469,17 +479,17 @@ type Command struct { } // NewCommandHook will execute the provided command when the hook is run. -func NewCommandHook(cmd Command) CommandHook { +func NewCommandHook(cmd *Command) CommandHook { return CommandHook{ Command: cmd, } } type CommandHook struct { - Command + *Command } -func (c Command) Run(s *specs.State) error { +func (c *Command) Run(s *specs.State) error { b, err := json.Marshal(s) if err != nil { return err diff --git a/libcontainer/configs/config_test.go b/libcontainer/configs/config_test.go index ceabaca737f..644129941ce 100644 --- a/libcontainer/configs/config_test.go +++ b/libcontainer/configs/config_test.go @@ -15,7 +15,7 @@ import ( func TestUnmarshalHooks(t *testing.T) { timeout := time.Second - hookCmd := configs.NewCommandHook(configs.Command{ + hookCmd := configs.NewCommandHook(&configs.Command{ Path: "/var/vcap/hooks/hook", Args: []string{"--pid=123"}, Env: []string{"FOO=BAR"}, @@ -52,7 +52,7 @@ func TestUnmarshalHooksWithInvalidData(t *testing.T) { func TestMarshalHooks(t *testing.T) { timeout := time.Second - hookCmd := configs.NewCommandHook(configs.Command{ + hookCmd := configs.NewCommandHook(&configs.Command{ Path: "/var/vcap/hooks/hook", Args: []string{"--pid=123"}, Env: []string{"FOO=BAR"}, @@ -84,7 +84,7 @@ func TestMarshalHooks(t *testing.T) { func TestMarshalUnmarshalHooks(t *testing.T) { timeout := time.Second - hookCmd := configs.NewCommandHook(configs.Command{ + hookCmd := configs.NewCommandHook(&configs.Command{ Path: "/var/vcap/hooks/hook", Args: []string{"--pid=123"}, Env: []string{"FOO=BAR"}, @@ -194,7 +194,7 @@ exit 0 } defer os.Remove(filename) - cmdHook := configs.NewCommandHook(configs.Command{ + cmdHook := configs.NewCommandHook(&configs.Command{ Path: filename, Args: []string{filename, "testarg"}, Env: []string{"FOO=BAR"}, @@ -216,7 +216,7 @@ func TestCommandHookRunTimeout(t *testing.T) { } timeout := 100 * time.Millisecond - cmdHook := configs.NewCommandHook(configs.Command{ + cmdHook := configs.NewCommandHook(&configs.Command{ Path: "/bin/sleep", Args: []string{"/bin/sleep", "1"}, Timeout: &timeout, diff --git a/libcontainer/factory_linux_test.go b/libcontainer/factory_linux_test.go index dec21afb212..7e4fcecfde0 100644 --- a/libcontainer/factory_linux_test.go +++ b/libcontainer/factory_linux_test.go @@ -31,14 +31,14 @@ func TestFactoryLoadContainer(t *testing.T) { id = "1" expectedHooks = configs.Hooks{ configs.Prestart: configs.HookList{ - configs.CommandHook{Command: configs.Command{Path: "prestart-hook"}}, + configs.CommandHook{Command: &configs.Command{Path: "prestart-hook"}}, }, configs.Poststart: configs.HookList{ - configs.CommandHook{Command: configs.Command{Path: "poststart-hook"}}, + configs.CommandHook{Command: &configs.Command{Path: "poststart-hook"}}, }, configs.Poststop: configs.HookList{ unserializableHook{}, - configs.CommandHook{Command: configs.Command{Path: "poststop-hook"}}, + configs.CommandHook{Command: &configs.Command{Path: "poststop-hook"}}, }, } expectedConfig = &configs.Config{ diff --git a/libcontainer/integration/exec_test.go b/libcontainer/integration/exec_test.go index e3d26468dde..c9f37ada4c7 100644 --- a/libcontainer/integration/exec_test.go +++ b/libcontainer/integration/exec_test.go @@ -1022,13 +1022,13 @@ func TestHook(t *testing.T) { }), }, configs.CreateContainer: configs.HookList{ - configs.NewCommandHook(configs.Command{ + configs.NewCommandHook(&configs.Command{ Path: "/bin/bash", Args: []string{"/bin/bash", "-c", fmt.Sprintf("touch ./%s", hookFiles[configs.CreateContainer])}, }), }, configs.StartContainer: configs.HookList{ - configs.NewCommandHook(configs.Command{ + configs.NewCommandHook(&configs.Command{ Path: "/bin/sh", Args: []string{"/bin/sh", "-c", fmt.Sprintf("touch /%s", hookFiles[configs.StartContainer])}, }), diff --git a/libcontainer/specconv/spec_linux.go b/libcontainer/specconv/spec_linux.go index 79a9a790049..6e3e5bde371 100644 --- a/libcontainer/specconv/spec_linux.go +++ b/libcontainer/specconv/spec_linux.go @@ -1256,8 +1256,8 @@ func createHooks(rspec *specs.Spec, config *configs.Config) { } } -func createCommandHook(h specs.Hook) configs.Command { - cmd := configs.Command{ +func createCommandHook(h specs.Hook) *configs.Command { + cmd := &configs.Command{ Path: h.Path, Args: h.Args, Env: h.Env,