port.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package port
  2. import (
  3. "context"
  4. "net"
  5. "github.com/rootless-containers/rootlesskit/pkg/api"
  6. )
  7. type Spec struct {
  8. // Proto is one of ["tcp", "tcp4", "tcp6", "udp", "udp4", "udp6"].
  9. // "tcp" may cause listening on both IPv4 and IPv6. (Corresponds to Go's net.Listen .)
  10. Proto string `json:"proto,omitempty"`
  11. ParentIP string `json:"parentIP,omitempty"` // IPv4 or IPv6 address. can be empty (0.0.0.0).
  12. ParentPort int `json:"parentPort,omitempty"`
  13. ChildPort int `json:"childPort,omitempty"`
  14. // ChildIP is an IPv4 or IPv6 address.
  15. // Default values:
  16. // - builtin driver: 127.0.0.1
  17. // - slirp4netns driver: slirp4netns's child IP, e.g., 10.0.2.100
  18. ChildIP string `json:"childIP,omitempty"`
  19. }
  20. type Status struct {
  21. ID int `json:"id"`
  22. Spec Spec `json:"spec"`
  23. }
  24. // Manager MUST be thread-safe.
  25. type Manager interface {
  26. AddPort(ctx context.Context, spec Spec) (*Status, error)
  27. ListPorts(ctx context.Context) ([]Status, error)
  28. RemovePort(ctx context.Context, id int) error
  29. }
  30. // ChildContext is used for RunParentDriver
  31. type ChildContext struct {
  32. // PID of the child, can be used for ns-entering to the child namespaces.
  33. PID int
  34. // IP of the tap device
  35. IP net.IP
  36. }
  37. // ParentDriver is a driver for the parent process.
  38. type ParentDriver interface {
  39. Manager
  40. Info(ctx context.Context) (*api.PortDriverInfo, error)
  41. // OpaqueForChild typically consists of socket path
  42. // for controlling child from parent
  43. OpaqueForChild() map[string]string
  44. // RunParentDriver signals initComplete when ParentDriver is ready to
  45. // serve as Manager.
  46. // RunParentDriver blocks until quit is signaled.
  47. //
  48. // ChildContext is optional.
  49. RunParentDriver(initComplete chan struct{}, quit <-chan struct{}, cctx *ChildContext) error
  50. }
  51. type ChildDriver interface {
  52. RunChildDriver(opaque map[string]string, quit <-chan struct{}) error
  53. }