mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-11 06:03:57 +08:00
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:
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user