container.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 compute
  15. import (
  16. "reflect"
  17. "time"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/gotypes"
  21. "yunion.io/x/pkg/util/sets"
  22. "yunion.io/x/onecloud/pkg/apis"
  23. )
  24. func init() {
  25. gotypes.RegisterSerializable(reflect.TypeOf(new(ContainerSpec)), func() gotypes.ISerializable {
  26. return new(ContainerSpec)
  27. })
  28. }
  29. const (
  30. CONTAINER_DEV_CPH_AMD_GPU = "CPH_AMD_GPU"
  31. CONTAINER_DEV_CPH_AOSP_BINDER = "CPH_AOSP_BINDER"
  32. CONTAINER_DEV_NETINT_CA_ASIC = "NETINT_CA_ASIC"
  33. CONTAINER_DEV_NETINT_CA_QUADRA = "NETINT_CA_QUADRA"
  34. CONTAINER_DEV_NVIDIA_GPU = "NVIDIA_GPU"
  35. CONTAINER_DEV_NVIDIA_MPS = "NVIDIA_MPS"
  36. CONTAINER_DEV_NVIDIA_GPU_SHARE = "NVIDIA_GPU_SHARE"
  37. CONTAINER_DEV_ASCEND_NPU = "ASCEND_NPU"
  38. CONTAINER_DEV_VASTAITECH_GPU = "VASTAITECH_GPU"
  39. )
  40. var (
  41. CONTAINER_GPU_TYPES = []string{
  42. CONTAINER_DEV_CPH_AMD_GPU,
  43. CONTAINER_DEV_NVIDIA_GPU,
  44. CONTAINER_DEV_NVIDIA_MPS,
  45. CONTAINER_DEV_NVIDIA_GPU_SHARE,
  46. CONTAINER_DEV_VASTAITECH_GPU,
  47. }
  48. )
  49. var NVIDIA_GPU_TYPES = []string{
  50. CONTAINER_DEV_NVIDIA_GPU,
  51. CONTAINER_DEV_NVIDIA_MPS,
  52. CONTAINER_DEV_NVIDIA_GPU_SHARE,
  53. }
  54. const (
  55. CONTAINER_STORAGE_LOCAL_RAW = "local_raw"
  56. )
  57. const (
  58. CONTAINER_STATUS_PULLING_IMAGE = "pulling_image"
  59. CONTAINER_STATUS_PULL_IMAGE_FAILED = "pull_image_failed"
  60. CONTAINER_STATUS_PULLED_IMAGE = "pulled_image"
  61. CONTAINER_STATUS_CREATING = "creating"
  62. CONTAINER_STATUS_CREATE_FAILED = "create_failed"
  63. CONTAINER_STATUS_SAVING_IMAGE = "saving_image"
  64. CONTAINER_STATUS_SAVE_IMAGE_FAILED = "save_image_failed"
  65. CONTAINER_STATUS_STARTING = "starting"
  66. CONTAINER_STATUS_START_FAILED = "start_failed"
  67. CONTAINER_STATUS_SYNCING_CONF = "syncing_conf"
  68. CONTAINER_STATUS_SYNC_CONF_FAILED = "sync_conf_failed"
  69. CONTAINER_STATUS_STOPPING = "stopping"
  70. CONTAINER_STATUS_STOP_FAILED = "stop_failed"
  71. CONTAINER_STATUS_SYNC_STATUS = "sync_status"
  72. CONTAINER_STATUS_SYNC_STATUS_FAILED = "sync_status_failed"
  73. CONTAINER_STATUS_UNKNOWN = "unknown"
  74. CONTAINER_STATUS_CREATED = "created"
  75. CONTAINER_STATUS_EXITED = "exited"
  76. CONTAINER_STATUS_CRASH_LOOP_BACK_OFF = "crash_loop_back_off"
  77. CONTAINER_STATUS_RUNNING = "running"
  78. CONTAINER_STATUS_DELETING = "deleting"
  79. CONTAINER_STATUS_DELETE_FAILED = "delete_failed"
  80. CONTAINER_STATUS_COMMITTING = "committing"
  81. CONTAINER_STATUS_COMMIT_FAILED = "commit_failed"
  82. // for health check
  83. CONTAINER_STATUS_PROBING = "probing"
  84. CONTAINER_STATUS_PROBE_FAILED = "probe_failed"
  85. CONTAINER_STATUS_NET_FAILED = "net_failed"
  86. // post overlay
  87. CONTAINER_STATUS_ADD_POST_OVERLY = "adding_post_overly"
  88. CONTAINER_STATUS_ADD_POST_OVERLY_FAILED = "add_post_overly_failed"
  89. CONTAINER_STATUS_REMOVE_POST_OVERLY = "removing_post_overly"
  90. CONTAINER_STATUS_REMOVE_POST_OVERLY_FAILED = "remove_post_overly_failed"
  91. CONTAINER_STATUS_CACHE_IMAGE = "caching_image"
  92. CONTAINER_STATUS_CACHE_IMAGE_FAILED = "caching_image_failed"
  93. )
  94. var (
  95. ContainerRunningStatus = sets.NewString(
  96. CONTAINER_STATUS_RUNNING,
  97. CONTAINER_STATUS_PROBING,
  98. CONTAINER_STATUS_PROBE_FAILED,
  99. CONTAINER_STATUS_NET_FAILED,
  100. )
  101. ContainerNoFailedRunningStatus = sets.NewString(CONTAINER_STATUS_RUNNING, CONTAINER_STATUS_PROBING)
  102. ContainerExitedStatus = sets.NewString(
  103. CONTAINER_STATUS_EXITED,
  104. CONTAINER_STATUS_CRASH_LOOP_BACK_OFF,
  105. )
  106. ContainerFinalStatus = sets.NewString(
  107. CONTAINER_STATUS_RUNNING,
  108. CONTAINER_STATUS_PROBING,
  109. CONTAINER_STATUS_PROBE_FAILED,
  110. CONTAINER_STATUS_NET_FAILED,
  111. CONTAINER_STATUS_EXITED,
  112. CONTAINER_STATUS_CRASH_LOOP_BACK_OFF,
  113. )
  114. )
  115. const (
  116. CONTAINER_METADATA_CRI_ID = "cri_id"
  117. CONTAINER_METADATA_RELEASED_DEVICES = "released_devices"
  118. )
  119. type ContainerSpec struct {
  120. apis.ContainerSpec
  121. // Volume mounts
  122. RootFs *apis.ContainerRootfs `json:"rootfs"`
  123. VolumeMounts []*apis.ContainerVolumeMount `json:"volume_mounts"`
  124. Devices []*ContainerDevice `json:"devices"`
  125. }
  126. func (c *ContainerSpec) String() string {
  127. return jsonutils.Marshal(c).String()
  128. }
  129. func (c *ContainerSpec) IsZero() bool {
  130. if reflect.DeepEqual(*c, ContainerSpec{}) {
  131. return true
  132. }
  133. return false
  134. }
  135. type ContainerCreateInput struct {
  136. apis.VirtualResourceCreateInput
  137. GuestId string `json:"guest_id"`
  138. Spec ContainerSpec `json:"spec"`
  139. AutoStart bool `json:"auto_start"`
  140. // swagger:ignore
  141. SkipTask bool `json:"skip_task"`
  142. }
  143. type ContainerUpdateInput struct {
  144. apis.VirtualResourceBaseUpdateInput
  145. Spec ContainerSpec `json:"spec"`
  146. }
  147. type ContainerListInput struct {
  148. apis.VirtualResourceListInput
  149. GuestId string `json:"guest_id"`
  150. HostId string `json:"host_id"`
  151. }
  152. type ContainerStopInput struct {
  153. Timeout int `json:"timeout"`
  154. Force bool `json:"force"`
  155. }
  156. type ContainerRestartInput struct {
  157. Timeout int `json:"timeout"`
  158. Force bool `json:"force"`
  159. }
  160. type ContainerSyncStatusResponse struct {
  161. Status string `json:"status"`
  162. StartedAt time.Time `json:"started_at"`
  163. RestartCount int `json:"restart_count"`
  164. }
  165. type ContainerHostDevice struct {
  166. // Path of the device within the container.
  167. ContainerPath string `json:"container_path"`
  168. // Path of the device on the host.
  169. HostPath string `json:"host_path"`
  170. // Cgroups permissions of the device, candidates are one or more of
  171. // * r - allows container to read from the specified device.
  172. // * w - allows container to write to the specified device.
  173. // * m - allows container to create device files that do not yet exist.
  174. Permissions string `json:"permissions"`
  175. }
  176. type ContainerIsolatedDevice struct {
  177. Index *int `json:"index"`
  178. Id string `json:"id"`
  179. OnlyEnv []*apis.ContainerIsolatedDeviceOnlyEnv `json:"only_env"`
  180. CDI *apis.ContainerIsolatedDeviceCDI `json:"cdi"`
  181. }
  182. type ContainerDevice struct {
  183. Type apis.ContainerDeviceType `json:"type"`
  184. IsolatedDevice *ContainerIsolatedDevice `json:"isolated_device"`
  185. Host *ContainerHostDevice `json:"host"`
  186. }
  187. type ContainerSaveVolumeMountToImageInput struct {
  188. Name string `json:"name"`
  189. GenerateName string `json:"generate_name"`
  190. Notes string `json:"notes"`
  191. Index int `json:"index"`
  192. Dirs []string `json:"dirs"`
  193. UsedByPostOverlay bool `json:"used_by_post_overlay"`
  194. DirPrefix string `json:"dir_prefix"`
  195. ExcludePaths []string `json:"exclude_paths"`
  196. }
  197. type ContainerExecInfoOutput struct {
  198. HostUri string `json:"host_uri"`
  199. PodId string `json:"pod_id"`
  200. ContainerId string `json:"container_id"`
  201. }
  202. type ContainerExecInput struct {
  203. Command []string `json:"command"`
  204. Tty bool `json:"tty"`
  205. SetIO bool `json:"set_io"`
  206. Stdin bool `json:"stdin"`
  207. Stdout bool `json:"stdout"`
  208. }
  209. type ContainerExecSyncInput struct {
  210. Command []string `json:"command"`
  211. // Timeout in seconds to stop the command, 0 mean run forever.
  212. // default: 0
  213. Timeout int64 `json:"timeout"`
  214. }
  215. type ContainerExecSyncResponse struct {
  216. Stdout string `json:"stdout"`
  217. Stderr string `json:"stderr"`
  218. ExitCode int32 `json:"exit_code"`
  219. }
  220. type ContainerCommitExternalRegistry struct {
  221. // e.g.: registry.cn-beijing.aliyuncs.com/yunionio
  222. Url string `json:"url"`
  223. // authentication configuration
  224. Auth *apis.ContainerPullImageAuthConfig `json:"auth"`
  225. }
  226. type ContainerCommitInput struct {
  227. // Container registry id from kubeserver
  228. RegistryId string `json:"registry_id"`
  229. ExternalRegistry *ContainerCommitExternalRegistry `json:"external_registry"`
  230. // image name
  231. ImageName string `json:"image_name"`
  232. // image tag
  233. Tag string `json:"tag"`
  234. }
  235. type ContainerCommitOutput struct {
  236. Repository string `json:"repository"`
  237. }
  238. type KubeServerContainerRegistryConfigCommon struct {
  239. Username string `json:"username"`
  240. Password string `json:"password"`
  241. }
  242. type KubeServerContainerRegistryConfigHarbor struct {
  243. KubeServerContainerRegistryConfigCommon
  244. }
  245. type KubeServerContainerRegistryConfig struct {
  246. Type string `json:"type"`
  247. Common *KubeServerContainerRegistryConfigCommon `json:"common"`
  248. Harbor *KubeServerContainerRegistryConfigHarbor `json:"harbor"`
  249. }
  250. type KubeServerContainerRegistryDetails struct {
  251. Id string `json:"id"`
  252. Name string `json:"name"`
  253. Url string `json:"url"`
  254. Type string `json:"type"`
  255. Config *KubeServerContainerRegistryConfig `json:"config"`
  256. }
  257. type ContainerPerformStatusInput struct {
  258. apis.PerformStatusInput
  259. RestartCount int `json:"restart_count"`
  260. StartedAt *time.Time `json:"started_at"`
  261. LastFinishedAt *time.Time `json:"last_finished_at"`
  262. }
  263. type ContainerResourcesSetInput struct {
  264. apis.ContainerResources
  265. DisableLimitCheck bool `json:"disable_limit_check"`
  266. }
  267. type ContainerVolumeMountAddPostOverlayInput struct {
  268. Index int `json:"index"`
  269. PostOverlay []*apis.ContainerVolumeMountDiskPostOverlay `json:"post_overlay"`
  270. }
  271. type ContainerVolumeMountRemovePostOverlayInput struct {
  272. Index int `json:"index"`
  273. PostOverlay []*apis.ContainerVolumeMountDiskPostOverlay `json:"post_overlay"`
  274. UseLazy bool `json:"use_lazy"`
  275. ClearLayers bool `json:"clear_layers"`
  276. }
  277. type ContainerCacheImageInput struct {
  278. DiskId string `json:"disk_id"`
  279. Image *CacheImageInput `json:"image"`
  280. }
  281. type ContainerCacheImagesInput struct {
  282. Images []*ContainerCacheImageInput `json:"images"`
  283. }
  284. func (i *ContainerCacheImagesInput) isImageExists(diskId string, imgId string) bool {
  285. for idx := range i.Images {
  286. img := i.Images[idx]
  287. if img.DiskId != diskId {
  288. return false
  289. }
  290. if img.Image == nil {
  291. return false
  292. }
  293. if img.Image.ImageId == imgId {
  294. return true
  295. }
  296. }
  297. return false
  298. }
  299. func (i *ContainerCacheImagesInput) Add(diskId string, imgId string, format string) error {
  300. if diskId == "" {
  301. return errors.Errorf("diskId is empty")
  302. }
  303. if imgId == "" {
  304. return errors.Errorf("imageId is empty")
  305. }
  306. if !i.isImageExists(diskId, imgId) {
  307. if i.Images == nil {
  308. i.Images = []*ContainerCacheImageInput{}
  309. }
  310. i.Images = append(i.Images, &ContainerCacheImageInput{
  311. DiskId: diskId,
  312. Image: &CacheImageInput{
  313. ImageId: imgId,
  314. Format: format,
  315. SkipChecksumIfExists: true,
  316. },
  317. })
  318. }
  319. return nil
  320. }