container.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Libcontainer provides a native Go implementation for creating containers
  2. // with namespaces, cgroups, capabilities, and filesystem access controls.
  3. // It allows you to manage the lifecycle of the container performing additional operations
  4. // after the container is created.
  5. package libcontainer
  6. import (
  7. "os"
  8. "time"
  9. "github.com/opencontainers/runc/libcontainer/configs"
  10. )
  11. // The status of a container.
  12. type Status int
  13. const (
  14. // The container exists but has not been run yet
  15. Created Status = iota
  16. // The container exists and is running.
  17. Running
  18. // The container exists, it is in the process of being paused.
  19. Pausing
  20. // The container exists, but all its processes are paused.
  21. Paused
  22. // The container does not exist.
  23. Destroyed
  24. )
  25. func (s Status) String() string {
  26. switch s {
  27. case Created:
  28. return "created"
  29. case Running:
  30. return "running"
  31. case Pausing:
  32. return "pausing"
  33. case Paused:
  34. return "paused"
  35. case Destroyed:
  36. return "destroyed"
  37. default:
  38. return "unknown"
  39. }
  40. }
  41. // BaseState represents the platform agnostic pieces relating to a
  42. // running container's state
  43. type BaseState struct {
  44. // ID is the container ID.
  45. ID string `json:"id"`
  46. // InitProcessPid is the init process id in the parent namespace.
  47. InitProcessPid int `json:"init_process_pid"`
  48. // InitProcessStartTime is the init process start time in clock cycles since boot time.
  49. InitProcessStartTime string `json:"init_process_start"`
  50. // Created is the unix timestamp for the creation time of the container in UTC
  51. Created time.Time `json:"created"`
  52. // Config is the container's configuration.
  53. Config configs.Config `json:"config"`
  54. }
  55. // A libcontainer container object.
  56. //
  57. // Each container is thread-safe within the same process. Since a container can
  58. // be destroyed by a separate process, any function may return that the container
  59. // was not found. BaseContainer includes methods that are platform agnostic.
  60. type BaseContainer interface {
  61. // Returns the ID of the container
  62. ID() string
  63. // Returns the current status of the container.
  64. //
  65. // errors:
  66. // ContainerDestroyed - Container no longer exists,
  67. // Systemerror - System error.
  68. Status() (Status, error)
  69. // State returns the current container's state information.
  70. //
  71. // errors:
  72. // Systemerror - System error.
  73. State() (*State, error)
  74. // Returns the current config of the container.
  75. Config() configs.Config
  76. // Returns the PIDs inside this container. The PIDs are in the namespace of the calling process.
  77. //
  78. // errors:
  79. // ContainerDestroyed - Container no longer exists,
  80. // Systemerror - System error.
  81. //
  82. // Some of the returned PIDs may no longer refer to processes in the Container, unless
  83. // the Container state is PAUSED in which case every PID in the slice is valid.
  84. Processes() ([]int, error)
  85. // Returns statistics for the container.
  86. //
  87. // errors:
  88. // ContainerDestroyed - Container no longer exists,
  89. // Systemerror - System error.
  90. Stats() (*Stats, error)
  91. // Set resources of container as configured
  92. //
  93. // We can use this to change resources when containers are running.
  94. //
  95. // errors:
  96. // Systemerror - System error.
  97. Set(config configs.Config) error
  98. // Start a process inside the container. Returns error if process fails to
  99. // start. You can track process lifecycle with passed Process structure.
  100. //
  101. // errors:
  102. // ContainerDestroyed - Container no longer exists,
  103. // ConfigInvalid - config is invalid,
  104. // ContainerPaused - Container is paused,
  105. // Systemerror - System error.
  106. Start(process *Process) (err error)
  107. // Destroys the container after killing all running processes.
  108. //
  109. // Any event registrations are removed before the container is destroyed.
  110. // No error is returned if the container is already destroyed.
  111. //
  112. // errors:
  113. // Systemerror - System error.
  114. Destroy() error
  115. // Signal sends the provided signal code to the container's initial process.
  116. //
  117. // errors:
  118. // Systemerror - System error.
  119. Signal(s os.Signal) error
  120. }