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 <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin
2024-12-22 17:32:02 -08:00
committed by lfbzhm
parent c49b891681
commit 6171da6005
6 changed files with 29 additions and 15 deletions
+4
View File
@@ -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
> できるときにできることをやるんだ。それが今だ。
+13 -3
View File
@@ -434,6 +434,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
@@ -463,17 +473,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
+5 -5
View File
@@ -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,
+3 -3
View File
@@ -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{
+2 -2
View File
@@ -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])},
}),
+2 -2
View File
@@ -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,