Skip to content

Commit

Permalink
libcontainer: Set 'status' in hook stdin
Browse files Browse the repository at this point in the history
Finish off the work started in 398a409 (sync up `HookState` with OCI
spec `State`, 2016-12-19, #1201).

And drop HookState, since there's no need for a local alias for
specs.State.

Also set c.initProcess in newInitProcess to support OCIState calls
from within initProcess.start().  I think the cyclic references
between linuxContainer and initProcess are unfortunate, but didn't
want to address that here.

I've also left the timing of the Prestart hooks alone, although the
spec calls for them to happen before start (not as part of creation)
[1,2].  Once the timing gets fixed we can drop the
initProcessStartTime hacks which initProcess.start currently needs.

I'm not sure why we trigger the prestart hooks in response to both
procReady and procHooks.  But we've had two prestart rounds in
initProcess.start since 2f27649 (Move pre-start hooks after container
mounts, 2016-02-17, #568).  I've left that alone too.

I really think we should have len() guards to avoid computing the
state when .Hooks is non-nil but the particular phase we're looking at
is empty.  Aleksa, however, is adamantly against them [3] citing a
risk of sloppy copy/pastes causing the hook slice being len-guarded to
diverge from the hook slice being iterated over within the guard.  I
think that ort of thing is very lo-risk, because:

* We shouldn't be copy/pasting this, right?  DRY for the win :).
* There's only ever a few lines between the guard and the guarded
  loop.  That makes broken copy/pastes easy to catch in review.
* We should have test coverage for these.  Guarding with the wrong
  slice is certainly not the only thing you can break with a sloppy
  copy/paste.

But I'm not a maintainer ;).

[1]: https://github.com/opencontainers/runtime-spec/blob/v1.0.0/config.md#prestart
[2]: opencontainers/runc#1710
[3]: opencontainers/runc#1741 (comment)

Signed-off-by: W. Trevor King <[email protected]>
  • Loading branch information
wking committed Nov 14, 2018
1 parent ab4bd5c commit 3602186
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
13 changes: 5 additions & 8 deletions libcontainer/configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,26 +272,23 @@ func (hooks Hooks) MarshalJSON() ([]byte, error) {
})
}

// HookState is the payload provided to a hook on execution.
type HookState specs.State

type Hook interface {
// Run executes the hook with the provided state.
Run(HookState) error
Run(*specs.State) error
}

// NewFunctionHook will call the provided function when the hook is run.
func NewFunctionHook(f func(HookState) error) FuncHook {
func NewFunctionHook(f func(*specs.State) error) FuncHook {
return FuncHook{
run: f,
}
}

type FuncHook struct {
run func(HookState) error
run func(*specs.State) error
}

func (f FuncHook) Run(s HookState) error {
func (f FuncHook) Run(s *specs.State) error {
return f.run(s)
}

Expand All @@ -314,7 +311,7 @@ type CommandHook struct {
Command
}

func (c Command) Run(s HookState) error {
func (c Command) Run(s *specs.State) error {
b, err := json.Marshal(s)
if err != nil {
return err
Expand Down
14 changes: 9 additions & 5 deletions libcontainer/configs/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runtime-spec/specs-go"
)

func TestUnmarshalHooks(t *testing.T) {
Expand Down Expand Up @@ -101,7 +102,7 @@ func TestMarshalUnmarshalHooks(t *testing.T) {
}

func TestMarshalHooksWithUnexpectedType(t *testing.T) {
fHook := configs.NewFunctionHook(func(configs.HookState) error {
fHook := configs.NewFunctionHook(func(*specs.State) error {
return nil
})
hook := configs.Hooks{
Expand All @@ -119,14 +120,15 @@ func TestMarshalHooksWithUnexpectedType(t *testing.T) {
}

func TestFuncHookRun(t *testing.T) {
state := configs.HookState{
state := &specs.State{
Version: "1",
ID: "1",
Status: "created",
Pid: 1,
Bundle: "/bundle",
}

fHook := configs.NewFunctionHook(func(s configs.HookState) error {
fHook := configs.NewFunctionHook(func(s *specs.State) error {
if !reflect.DeepEqual(state, s) {
t.Errorf("Expected state %+v to equal %+v", state, s)
}
Expand All @@ -137,9 +139,10 @@ func TestFuncHookRun(t *testing.T) {
}

func TestCommandHookRun(t *testing.T) {
state := configs.HookState{
state := &specs.State{
Version: "1",
ID: "1",
Status: "created",
Pid: 1,
Bundle: "/bundle",
}
Expand All @@ -160,9 +163,10 @@ func TestCommandHookRun(t *testing.T) {
}

func TestCommandHookRunTimeout(t *testing.T) {
state := configs.HookState{
state := &specs.State{
Version: "1",
ID: "1",
Status: "created",
Pid: 1,
Bundle: "/bundle",
}
Expand Down

0 comments on commit 3602186

Please sign in to comment.