netns.go 962 B

12345678910111213141516171819202122232425262728293031323334
  1. package network
  2. import (
  3. "fmt"
  4. "os"
  5. "syscall"
  6. "github.com/dotcloud/docker/pkg/libcontainer"
  7. "github.com/dotcloud/docker/pkg/system"
  8. )
  9. // crosbymichael: could make a network strategy that instead of returning veth pair names it returns a pid to an existing network namespace
  10. type NetNS struct {
  11. }
  12. func (v *NetNS) Create(n *libcontainer.Network, nspid int, context libcontainer.Context) error {
  13. context["nspath"] = n.Context["nspath"]
  14. return nil
  15. }
  16. func (v *NetNS) Initialize(config *libcontainer.Network, context libcontainer.Context) error {
  17. nspath, exists := context["nspath"]
  18. if !exists {
  19. return fmt.Errorf("nspath does not exist in network context")
  20. }
  21. f, err := os.OpenFile(nspath, os.O_RDONLY, 0)
  22. if err != nil {
  23. return fmt.Errorf("failed get network namespace fd: %v", err)
  24. }
  25. if err := system.Setns(f.Fd(), syscall.CLONE_NEWNET); err != nil {
  26. return fmt.Errorf("failed to setns current network namespace: %v", err)
  27. }
  28. return nil
  29. }