MCPcopy Create free account
hub / github.com/containerd/nerdctl / withNerdctlOCIHook

Function withNerdctlOCIHook

pkg/cmd/container/create.go:612–658  ·  view source on GitHub ↗
(cmd string, args []string)

Source from the content-addressed store, hash-verified

610}
611
612func withNerdctlOCIHook(cmd string, args []string) (oci.SpecOpts, error) {
613 if rootlessutil.IsRootless() {
614 detachedNetNS, err := rootlessutil.DetachedNetNS()
615 if err != nil {
616 return nil, fmt.Errorf("failed to check whether RootlessKit is running with --detach-netns: %w", err)
617 }
618 if detachedNetNS != "" {
619 // Rewrite {cmd, args} if RootlessKit is running with --detach-netns, so that the hook can gain
620 // CAP_NET_ADMIN in the namespaces.
621 // - Old:
622 // - cmd: "/usr/local/bin/nerdctl"
623 // - args: {"--data-root=/foo", "internal", "oci-hook"}
624 // - New:
625 // - cmd: "/usr/bin/nsenter"
626 // - args: {"-n/run/user/1000/containerd-rootless/netns", "-F", "--", "/usr/local/bin/nerdctl", "--data-root=/foo", "internal", "oci-hook"}
627 oldCmd, oldArgs := cmd, args
628 cmd, err = exec.LookPath("nsenter")
629 if err != nil {
630 return nil, err
631 }
632 args = append([]string{"-n" + detachedNetNS, "-F", "--", oldCmd}, oldArgs...)
633 }
634 }
635
636 args = append([]string{cmd}, append(args, "internal", "oci-hook")...)
637 // sbin is appended for iptables https://github.com/containerd/nerdctl/discussions/1536
638 env := append(os.Environ(), "PATH="+os.Getenv("PATH")+":/usr/sbin:/sbin")
639 return func(_ context.Context, _ oci.Client, _ *containers.Container, s *specs.Spec) error {
640 if s.Hooks == nil {
641 s.Hooks = &specs.Hooks{}
642 }
643 crArgs := append(args, "createRuntime")
644 s.Hooks.CreateRuntime = append(s.Hooks.CreateRuntime, specs.Hook{
645 Path: cmd,
646 Args: crArgs,
647 Env: env,
648 })
649 argsCopy := append([]string(nil), args...)
650 psArgs := append(argsCopy, "postStop")
651 s.Hooks.Poststop = append(s.Hooks.Poststop, specs.Hook{
652 Path: cmd,
653 Args: psArgs,
654 Env: env,
655 })
656 return nil
657 }, nil
658}
659
660func withContainerLabels(label, labelFile []string) ([]containerd.NewContainerOpts, error) {
661 var opts []containerd.NewContainerOpts

Callers 1

CreateFunction · 0.85

Calls 2

IsRootlessFunction · 0.92
DetachedNetNSFunction · 0.92

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…