runtime.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package runtime
  15. import (
  16. "time"
  17. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
  18. )
  19. // Runtime interface defines the interfaces that should be implemented
  20. // by a container runtime.
  21. // Thread safety is required from implementations of this interface.
  22. type Runtime interface {
  23. // Type returns the type of the container runtime.
  24. Type() string
  25. // GetPods returns a list of containers grouped by pods. The boolean parameter
  26. // specifies whether the runtime returns all containers including those already
  27. // exited and dead containers (used for garbage collection).
  28. GetPods(all bool) ([]*Pod, error)
  29. // GetPodStatus retrieves the status of the pod, including the
  30. // information of all containers in the pod that are visible in Runtime.
  31. GetPodStatus(uid, name, namespace string) (*PodStatus, error)
  32. }
  33. // Pod is a group of containers.
  34. type Pod struct {
  35. // The ID of the pod, which can be used to retrieve a particular pod
  36. // from the pod list returned by GetPods().
  37. Id string
  38. CRIId string
  39. // The name and namespace of the pod, which is readable by human.
  40. Name string
  41. Namespace string
  42. // List of containers that belongs to the pod. It may contain only
  43. // running containers, or mixed with dead ones (when GetPods(true)).
  44. Containers []*Container
  45. // List of sandboxes associated with this pod. The sandboxes are converted
  46. // to Container temporarily to avoid substantial changes to other
  47. // components. This is only populated by kuberuntime.
  48. Sandboxes []*Container
  49. }
  50. // ContainerID is a type that identifies a container.
  51. type ContainerID struct {
  52. // The type of the container runtime. e.g. 'docker'.
  53. Type string
  54. // The identification of the container, this is comsumable by
  55. // the underlying container runtime. (Note that the container
  56. // runtime interface still takes the whole struct as input).
  57. ID string
  58. }
  59. // State represents the state of a container
  60. type State string
  61. const (
  62. // ContainerStateCreated indicates a container that has been created (e.g. with docker create) but not started.
  63. ContainerStateCreated State = "created"
  64. // ContainerStateRunning indicates a currently running container.
  65. ContainerStateRunning State = "running"
  66. // ContainerStateExited indicates a container that ran and completed ("stopped" in other contexts, although a created container is technically also "stopped").
  67. ContainerStateExited State = "exited"
  68. // ContainerStateUnknown encompasses all the states that we currently don't care about (like restarting, paused, dead).
  69. ContainerStateUnknown State = "unknown"
  70. )
  71. // Container provides the runtime information for a container, such as ID, hash,
  72. // state of the container.
  73. type Container struct {
  74. // The ID of the container, used by the container runtime to identify
  75. // a container.
  76. ID ContainerID
  77. // The name of the container, which should be the same as specified by
  78. // v1.Container.
  79. Name string
  80. // The image name of the container, this also includes the tag of the image,
  81. // the expected form is "NAME:TAG".
  82. Image string
  83. // The id of the image used by the container.
  84. ImageID string
  85. // State is the state of the container.
  86. State State
  87. }
  88. // Pods represents the list of pods
  89. type Pods []*Pod
  90. // FindPodByID finds and returns a pod in the pod list by UID. It will return an empty pod
  91. // if not found.
  92. func (p Pods) FindPodByID(podUID string) Pod {
  93. for i := range p {
  94. if p[i].Id == podUID {
  95. return *p[i]
  96. }
  97. }
  98. return Pod{}
  99. }
  100. // FindPodByFullName finds and returns a pod in the pod list by the full name.
  101. // It will return an empty pod if not found.
  102. func (p Pods) FindPodByFullName(podFullName string) Pod {
  103. for i := range p {
  104. if BuildPodFullName(p[i].Name, p[i].Namespace) == podFullName {
  105. return *p[i]
  106. }
  107. }
  108. return Pod{}
  109. }
  110. // FindPod combines FindPodByID and FindPodByFullName, it finds and returns a pod in the
  111. // pod list either by the full name or the pod ID. It will return an empty pod
  112. // if not found.
  113. func (p Pods) FindPod(podFullName string, podUID string) Pod {
  114. if len(podFullName) > 0 {
  115. return p.FindPodByFullName(podFullName)
  116. }
  117. return p.FindPodByID(podUID)
  118. }
  119. // FindContainerByName returns a container in the pod with the given name.
  120. // When there are multiple containers with the same name, the first match will
  121. // be returned.
  122. func (p *Pod) FindContainerByName(containerName string) *Container {
  123. for _, c := range p.Containers {
  124. if c.Name == containerName {
  125. return c
  126. }
  127. }
  128. return nil
  129. }
  130. // FindContainerByID returns a container in the pod with the given ContainerID.
  131. func (p *Pod) FindContainerByID(id ContainerID) *Container {
  132. for _, c := range p.Containers {
  133. if c.ID == id {
  134. return c
  135. }
  136. }
  137. return nil
  138. }
  139. // FindSandboxByID returns a sandbox in the pod with the given ContainerID.
  140. func (p *Pod) FindSandboxByID(id ContainerID) *Container {
  141. for _, c := range p.Sandboxes {
  142. if c.ID == id {
  143. return c
  144. }
  145. }
  146. return nil
  147. }
  148. // BuildPodFullName builds the pod full name from pod name and namespace.
  149. func BuildPodFullName(name, namespace string) string {
  150. return name + "_" + namespace
  151. }
  152. type PodStatus struct {
  153. ID string
  154. Name string
  155. Namespace string
  156. IPs []string
  157. ContainerStatuses []*Status
  158. SandboxStatuses []*runtimeapi.PodSandboxStatus
  159. }
  160. func (ps PodStatus) GetContainerStatus(ctrId string) *Status {
  161. for i := range ps.ContainerStatuses {
  162. cs := ps.ContainerStatuses[i]
  163. if cs.ID.ID == ctrId {
  164. return cs
  165. }
  166. }
  167. return nil
  168. }
  169. // Status represents the status of a container.
  170. type Status struct {
  171. // ID of the container.
  172. ID ContainerID
  173. // Name of the container.
  174. Name string
  175. // ID of the sandbox to which this container belongs.
  176. PodSandboxID string
  177. // Status of the container.
  178. State State
  179. // Creation time of the container.
  180. CreatedAt time.Time
  181. // Start time of the container.
  182. StartedAt time.Time
  183. // Finish time of the container.
  184. FinishedAt time.Time
  185. // Exit code of the container.
  186. ExitCode int
  187. // Name of the image, this also includes the tag of the image,
  188. // the expected form is "NAME:TAG".
  189. Image string
  190. // ID of the image.
  191. ImageID string
  192. // Hash of the container, used for comparison.
  193. Hash uint64
  194. // Number of times that the container has been restarted.
  195. RestartCount int
  196. // A string explains why container is in such a status.
  197. Reason string
  198. // Message written by the container before exiting (stored in
  199. // TerminationMessagePath).
  200. Message string
  201. }