Skip to content

Commit

Permalink
libct/configs: add HookList.SetDefaultEnv
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
kolyshkin authored and lifubang committed Dec 25, 2024
1 parent e9e9b36 commit 838711b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

> できるときにできることをやるんだ。それが今だ。
Expand Down
16 changes: 13 additions & 3 deletions libcontainer/configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions libcontainer/configs/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -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"},
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions libcontainer/factory_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions libcontainer/integration/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])},
}),
Expand Down
4 changes: 2 additions & 2 deletions libcontainer/specconv/spec_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 838711b

Please sign in to comment.