Skip to content

Commit 18f29fb

Browse files
committed
fix: add onstartcontainer hook
Signed-off-by: Alessio Greggi <[email protected]>
1 parent 265d6b9 commit 18f29fb

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

pkg/cmd/container/create.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,12 @@ func withNerdctlOCIHook(cmd string, args []string) (oci.SpecOpts, error) {
484484
Args: crArgs,
485485
Env: os.Environ(),
486486
})
487+
scArgs := append(args, "startContainer")
488+
s.Hooks.CreateRuntime = append(s.Hooks.StartContainer, specs.Hook{
489+
Path: cmd,
490+
Args: scArgs,
491+
Env: os.Environ(),
492+
})
487493
argsCopy := append([]string(nil), args...)
488494
psArgs := append(argsCopy, "postStop")
489495
s.Hooks.Poststop = append(s.Hooks.Poststop, specs.Hook{

pkg/ocihook/ocihook.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ func Run(stdin io.Reader, stderr io.Writer, event, dataStore, cniPath, cniNetcon
8989
switch event {
9090
case "createRuntime":
9191
return onCreateRuntime(opts)
92+
case "startContainer":
93+
return onStartContainer(opts)
9294
case "postStop":
9395
return onPostStop(opts)
9496
default:
@@ -468,6 +470,31 @@ func onCreateRuntime(opts *handlerOpts) error {
468470
return nil
469471
}
470472

473+
func onStartContainer(opts *handlerOpts) error {
474+
// restore portmapping in case it was removed by kill/stop command
475+
if opts.cni != nil {
476+
portMapOpts, err := getPortMapOpts(opts)
477+
if err != nil {
478+
return err
479+
}
480+
nsPath, err := getNetNSPath(opts.state)
481+
if err != nil {
482+
return err
483+
}
484+
ctx := context.Background()
485+
fmt.Println(portMapOpts)
486+
487+
var namespaceOpts []gocni.NamespaceOpts
488+
namespaceOpts = append(namespaceOpts, portMapOpts...)
489+
cniRes, err := opts.cni.Setup(ctx, opts.fullID, nsPath, namespaceOpts...)
490+
if err != nil {
491+
return fmt.Errorf("failed to call cni.Setup: %w", err)
492+
}
493+
fmt.Println(cniRes)
494+
}
495+
return nil
496+
}
497+
471498
func onPostStop(opts *handlerOpts) error {
472499
ctx := context.Background()
473500
ns := opts.state.Annotations[labels.Namespace]

0 commit comments

Comments
 (0)