container.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 apis
  15. import (
  16. "encoding/json"
  17. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/pkg/util/sets"
  19. )
  20. type ContainerKeyValue struct {
  21. Key string `json:"key"`
  22. Value string `json:"value"`
  23. ValueFrom *ContainerValueSource `json:"value_from"`
  24. }
  25. type ContainerValueSource struct {
  26. Credential *ContainerValueSourceCredential `json:"credential"`
  27. }
  28. type ContainerValueSourceCredential struct {
  29. Id string `json:"id"`
  30. Key string `json:"key"`
  31. }
  32. type ContainerLifecyleHandlerType string
  33. const (
  34. ContainerLifecyleHandlerTypeExec ContainerLifecyleHandlerType = "exec"
  35. )
  36. type ContainerLifecyleHandlerExecAction struct {
  37. Command []string `json:"command"`
  38. }
  39. type ContainerLifecyleHandler struct {
  40. Type ContainerLifecyleHandlerType `json:"type"`
  41. Exec *ContainerLifecyleHandlerExecAction `json:"exec"`
  42. }
  43. type ContainerLifecyle struct {
  44. PostStart *ContainerLifecyleHandler `json:"post_start"`
  45. }
  46. type ContainerProcMountType string
  47. const (
  48. // DefaultProcMount uses the container runtime defaults for readonly and masked
  49. // paths for /proc. Most container runtimes mask certain paths in /proc to avoid
  50. // accidental security exposure of special devices or information.
  51. ContainerDefaultProcMount ContainerProcMountType = "Default"
  52. // UnmaskedProcMount bypasses the default masking behavior of the container
  53. // runtime and ensures the newly created /proc the container stays in tact with
  54. // no modifications.
  55. ContainerUnmaskedProcMount ContainerProcMountType = "Unmasked"
  56. )
  57. type ContainerSecurityContext struct {
  58. RunAsUser *int64 `json:"run_as_user,omitempty"`
  59. RunAsGroup *int64 `json:"run_as_group,omitempty"`
  60. // procMount denotes the type of proc mount to use for the containers.
  61. // The default is DefaultProcMount which uses the container runtime defaults for
  62. ProcMount ContainerProcMountType `json:"proc_mount"`
  63. ApparmorProfile string `json:"apparmor_profile"`
  64. }
  65. type ContainerResources struct {
  66. // CpuCfsQuota can be set to 0.5 that mapping to 0.5*100000 for cpu.cpu_cfs_quota_us
  67. CpuCfsQuota *float64 `json:"cpu_cfs_quota,omitempty"`
  68. // MemoryLimitMB will be transferred to memory.limit_in_bytes
  69. // MemoryLimitMB *int64 `json:"memory_limit_mb,omitempty"`
  70. // PidsMax will be set to pids.max
  71. PidsMax *int `json:"pids_max"`
  72. // DevicesAllow will be set to devices.allow
  73. DevicesAllow []string `json:"devices_allow"`
  74. // This flag only affects the cpuset controller. If the clone_children
  75. // flag is enabled in a cgroup, a new cpuset cgroup will copy its
  76. // configuration fromthe parent during initialization.
  77. CpusetCloneChildren bool `json:"cpuset_clone_children"`
  78. // cgroup memory.high
  79. MemoryHighRatio *float64 `json:"memory_high_ratio"`
  80. }
  81. type ContainerEnvRefValueType string
  82. const (
  83. ContainerEnvRefValueTypeIsolatedDevice ContainerEnvRefValueType = "isolated_device"
  84. )
  85. type ContainerIsolatedDeviceOnlyEnv struct {
  86. Key string `json:"key"`
  87. FromRenderPath bool `json:"from_render_path"`
  88. FromIndex bool `json:"from_index"`
  89. FromDeviceMinor bool `json:"from_device_minor"`
  90. }
  91. type ContainerCDIKind string
  92. var (
  93. CONTAINER_CDI_KIND_NVIDIA_GPU ContainerCDIKind = "nvidia.com/gpu"
  94. )
  95. type ContainerIsolatedDeviceCDI struct {
  96. Kind ContainerCDIKind
  97. }
  98. type ContainerSpec struct {
  99. // Image to use.
  100. Image string `json:"image"`
  101. // Image pull policy
  102. ImagePullPolicy ImagePullPolicy `json:"image_pull_policy"`
  103. // Image credential id
  104. ImageCredentialId string `json:"image_credential_id"`
  105. // Command to execute (i.e., entrypoint for docker)
  106. Command []string `json:"command"`
  107. // Args for the Command (i.e. command for docker)
  108. Args []string `json:"args"`
  109. // Current working directory of the command.
  110. WorkingDir string `json:"working_dir"`
  111. // List of environment variable to set in the container.
  112. Envs []*ContainerKeyValue `json:"envs"`
  113. // Enable lxcfs
  114. EnableLxcfs bool `json:"enable_lxcfs"`
  115. Capabilities *ContainerCapability `json:"capabilities"`
  116. Privileged bool `json:"privileged"`
  117. DisableNoNewPrivs bool `json:"disable_no_new_privs"`
  118. Lifecyle *ContainerLifecyle `json:"lifecyle"`
  119. CgroupDevicesAllow []string `json:"cgroup_devices_allow"`
  120. CgroupPidsMax int `json:"cgroup_pids_max"`
  121. ResourcesLimit *ContainerResources `json:"resources_limit"`
  122. SimulateCpu bool `json:"simulate_cpu"`
  123. ShmSizeMB int `json:"shm_size_mb"`
  124. SecurityContext *ContainerSecurityContext `json:"security_context,omitempty"`
  125. // Periodic probe of container liveness.
  126. // Container will be restarted if the probe fails.
  127. // Cannot be updated.
  128. //LivenessProbe *ContainerProbe `json:"liveness_probe,omitempty"`
  129. // StartupProbe indicates that the Pod has successfully initialized.
  130. // If specified, no other probes are executed until this completes successfully.
  131. StartupProbe *ContainerProbe `json:"startup_probe,omitempty"`
  132. AlwaysRestart bool `json:"always_restart"`
  133. Primary bool `json:"primary"`
  134. // DependsOn is a list of container name which this container depends on when pod start
  135. // Only works for containers created & started by pod-create & server-start
  136. DependsOn []string `json:"depends_on,omitempty"`
  137. }
  138. func (c *ContainerSpec) NeedProbe() bool {
  139. //if c.LivenessProbe != nil {
  140. // return true
  141. //}
  142. if c.StartupProbe != nil {
  143. return true
  144. }
  145. return false
  146. }
  147. type ContainerCapability struct {
  148. Add []string `json:"add"`
  149. Drop []string `json:"drop"`
  150. }
  151. type ImagePullPolicy string
  152. const (
  153. ImagePullPolicyAlways = "Always"
  154. ImagePullPolicyIfNotPresent = "IfNotPresent"
  155. )
  156. type ContainerVolumeMountType string
  157. const (
  158. CONTAINER_VOLUME_MOUNT_TYPE_DISK ContainerVolumeMountType = "disk"
  159. CONTAINER_VOLUME_MOUNT_TYPE_HOST_PATH ContainerVolumeMountType = "host_path"
  160. CONTAINER_VOLUME_MOUNT_TYPE_TEXT ContainerVolumeMountType = "text"
  161. CONTAINER_VOLUME_MOUNT_TYPE_CEPHF_FS ContainerVolumeMountType = "ceph_fs"
  162. )
  163. type ContainerDeviceType string
  164. const (
  165. CONTAINER_DEVICE_TYPE_ISOLATED_DEVICE ContainerDeviceType = "isolated_device"
  166. CONTAINER_DEVICE_TYPE_HOST ContainerDeviceType = "host"
  167. )
  168. type ContainerMountPropagation string
  169. const (
  170. // No mount propagation ("private" in Linux terminology).
  171. MOUNTPROPAGATION_PROPAGATION_PRIVATE ContainerMountPropagation = "private"
  172. // Mounts get propagated from the host to the container ("rslave" in Linux).
  173. MOUNTPROPAGATION_PROPAGATION_HOST_TO_CONTAINER ContainerMountPropagation = "rslave"
  174. // Mounts get propagated from the host to the container and from the
  175. // container to the host ("rshared" in Linux).
  176. MOUNTPROPAGATION_PROPAGATION_BIDIRECTIONAL ContainerMountPropagation = "rshared"
  177. )
  178. var (
  179. ContainerMountPropagations = sets.NewString(
  180. string(MOUNTPROPAGATION_PROPAGATION_PRIVATE), string(MOUNTPROPAGATION_PROPAGATION_HOST_TO_CONTAINER), string(MOUNTPROPAGATION_PROPAGATION_BIDIRECTIONAL))
  181. )
  182. type ContainerVolumeMount struct {
  183. // 用于标识当前 pod volume mount 的唯一性
  184. UniqueName string `json:"unique_name"`
  185. Type ContainerVolumeMountType `json:"type"`
  186. Disk *ContainerVolumeMountDisk `json:"disk"`
  187. HostPath *ContainerVolumeMountHostPath `json:"host_path"`
  188. Text *ContainerVolumeMountText `json:"text"`
  189. CephFS *ContainerVolumeMountCephFS `json:"ceph_fs"`
  190. // Mounted read-only if true, read-write otherwise (false or unspecified).
  191. ReadOnly bool `json:"read_only"`
  192. // Path within the container at which the volume should be mounted. Must
  193. // not contain ':'.
  194. MountPath string `json:"mount_path"`
  195. // If set, the mount needs SELinux relabeling.
  196. SelinuxRelabel bool `json:"selinux_relabel,omitempty"`
  197. // Requested propagation mode.
  198. Propagation ContainerMountPropagation `json:"propagation,omitempty"`
  199. // Owner permissions
  200. FsUser *int64 `json:"fs_user,omitempty"`
  201. FsGroup *int64 `json:"fs_group,omitempty"`
  202. }
  203. type ContainerOverlayDiskImage struct {
  204. DiskId string `json:"disk_id"`
  205. ImageId string `json:"image_id"`
  206. }
  207. type ContainerDiskOverlayType string
  208. const (
  209. CONTAINER_DISK_OVERLAY_TYPE_DIRECTORY ContainerDiskOverlayType = "directory"
  210. CONTAINER_DISK_OVERLAY_TYPE_DISK_IMAGE ContainerDiskOverlayType = "disk_image"
  211. CONTAINER_DISK_OVERLAY_TYPE_UNKNOWN ContainerDiskOverlayType = "unknown"
  212. )
  213. type ContainerVolumeMountDiskOverlay struct {
  214. LowerDir []string `json:"lower_dir"`
  215. UseDiskImage bool `json:"use_disk_image"`
  216. }
  217. func (o ContainerVolumeMountDiskOverlay) GetType() ContainerDiskOverlayType {
  218. if len(o.LowerDir) != 0 {
  219. return CONTAINER_DISK_OVERLAY_TYPE_DIRECTORY
  220. }
  221. if o.UseDiskImage {
  222. return CONTAINER_DISK_OVERLAY_TYPE_DISK_IMAGE
  223. }
  224. return CONTAINER_DISK_OVERLAY_TYPE_UNKNOWN
  225. }
  226. func (o ContainerVolumeMountDiskOverlay) IsValid() error {
  227. if o.GetType() == CONTAINER_DISK_OVERLAY_TYPE_UNKNOWN {
  228. return errors.ErrNotSupported
  229. }
  230. return nil
  231. }
  232. type HostLowerPath struct {
  233. PrePath string `json:"pre_path"`
  234. PostPath string `json:"post_path"`
  235. }
  236. type ContainerVolumeMountDiskPostImageOverlay struct {
  237. Id string `json:"id"`
  238. PathMap map[string]string `json:"path_map"`
  239. // 宿主机底层目录映射, key 为 PathMap 的 key,value 为 overlay lower 格式,多目录以 ":" 分隔
  240. HostLowerMap map[string]*HostLowerPath `json:"host_lower_map"`
  241. UpperConfig *PostOverlayUpperConfig `json:"upper_config"`
  242. }
  243. type PostOverlayUpperConfigType string
  244. const (
  245. PostOverlayUpperConfigTypeDisk PostOverlayUpperConfigType = "disk"
  246. )
  247. type PostOverlayUpperConfigDisk struct {
  248. SubPath string `json:"sub_path"`
  249. }
  250. type PostOverlayUpperConfig struct {
  251. Type PostOverlayUpperConfigType `json:"type"`
  252. Disk *PostOverlayUpperConfigDisk `json:"disk"`
  253. }
  254. type ContainerVolumeMountDiskPostImageOverlayUnpacker ContainerVolumeMountDiskPostImageOverlay
  255. func (ov *ContainerVolumeMountDiskPostImageOverlay) UnmarshalJSON(data []byte) error {
  256. nov := new(ContainerVolumeMountDiskPostImageOverlayUnpacker)
  257. if err := json.Unmarshal(data, nov); err != nil {
  258. return err
  259. }
  260. ov.Id = nov.Id
  261. // 防止 PathMap 被合并,总是用 Unarmshal data 里面的 path_map
  262. ov.PathMap = nov.PathMap
  263. ov.HostLowerMap = nov.HostLowerMap
  264. ov.UpperConfig = nov.UpperConfig
  265. return nil
  266. }
  267. type ContainerVolumeMountDiskPostOverlayType string
  268. const (
  269. CONTAINER_VOLUME_MOUNT_DISK_POST_OVERLAY_HOSTPATH ContainerVolumeMountDiskPostOverlayType = "host_path"
  270. CONTAINER_VOLUME_MOUNT_DISK_POST_OVERLAY_IMAGE ContainerVolumeMountDiskPostOverlayType = "image"
  271. )
  272. type ContainerVolumeMountDiskPostOverlay struct {
  273. // 宿主机底层目录
  274. HostLowerDir []string `json:"host_lower_dir"`
  275. // 宿主机上层目录
  276. HostUpperDir string `json:"host_upper_dir"`
  277. // 合并后要挂载到容器的目录
  278. ContainerTargetDir string `json:"container_target_dir"`
  279. Image *ContainerVolumeMountDiskPostImageOverlay `json:"image"`
  280. FsUser *int64 `json:"fs_user,omitempty"`
  281. FsGroup *int64 `json:"fs_group,omitempty"`
  282. FlattenLayers bool `json:"flatten_layers"`
  283. }
  284. func (o ContainerVolumeMountDiskPostOverlay) IsEqual(input ContainerVolumeMountDiskPostOverlay) bool {
  285. if o.GetType() != input.GetType() {
  286. return false
  287. }
  288. switch o.GetType() {
  289. case CONTAINER_VOLUME_MOUNT_DISK_POST_OVERLAY_HOSTPATH:
  290. return o.ContainerTargetDir == input.ContainerTargetDir
  291. case CONTAINER_VOLUME_MOUNT_DISK_POST_OVERLAY_IMAGE:
  292. return o.Image.Id == input.Image.Id
  293. }
  294. return false
  295. }
  296. func (o ContainerVolumeMountDiskPostOverlay) GetType() ContainerVolumeMountDiskPostOverlayType {
  297. if o.Image != nil {
  298. return CONTAINER_VOLUME_MOUNT_DISK_POST_OVERLAY_IMAGE
  299. }
  300. return CONTAINER_VOLUME_MOUNT_DISK_POST_OVERLAY_HOSTPATH
  301. }
  302. type ContainerVolumeMountDisk struct {
  303. Index *int `json:"index,omitempty"`
  304. Id string `json:"id"`
  305. SubDirectory string `json:"sub_directory"`
  306. StorageSizeFile string `json:"storage_size_file"`
  307. // lower overlay 设置,disk 的 volume 会作为 upper,最终 merged 的目录会传给容器
  308. Overlay *ContainerVolumeMountDiskOverlay `json:"overlay"`
  309. // case insensitive feature is incompatible with overlayfs
  310. CaseInsensitivePaths []string `json:"case_insensitive_paths"`
  311. // 当 disk volume 挂载完后,需要 overlay 的目录设置
  312. PostOverlay []*ContainerVolumeMountDiskPostOverlay `json:"post_overlay"`
  313. // The ext2 filesystem reserves a certain percentage of the available space (by default 5%, see mke2fs(8) and tune2fs(8)). These options determine who can use the reserved blocks. (Roughly: whoever has the specified uid, or belongs to the specified group.)
  314. ResGid int `json:"res_gid"`
  315. ResUid int `json:"res_uid"`
  316. }
  317. type ContainerVolumeMountHostPathType string
  318. const (
  319. CONTAINER_VOLUME_MOUNT_HOST_PATH_TYPE_DIRECTORY ContainerVolumeMountHostPathType = "directory"
  320. CONTAINER_VOLUME_MOUNT_HOST_PATH_TYPE_FILE ContainerVolumeMountHostPathType = "file"
  321. )
  322. type ContainerVolumeMountHostPathAutoCreateConfig struct {
  323. Uid uint `json:"uid"`
  324. Gid uint `json:"gid"`
  325. Permissions string `json:"permissions"`
  326. }
  327. type ContainerVolumeMountHostPath struct {
  328. Type ContainerVolumeMountHostPathType `json:"type"`
  329. Path string `json:"path"`
  330. AutoCreate bool `json:"auto_create"`
  331. AutoCreateConfig *ContainerVolumeMountHostPathAutoCreateConfig `json:"auto_create_config,omitempty"`
  332. }
  333. type ContainerVolumeMountText struct {
  334. Content string `json:"content"`
  335. }
  336. type ContainerVolumeMountCephFS struct {
  337. Id string `json:"id"`
  338. }
  339. type ContainerPullImageAuthConfig struct {
  340. Username string `json:"username,omitempty"`
  341. Password string `json:"password,omitempty"`
  342. Auth string `json:"auth,omitempty"`
  343. ServerAddress string `json:"server_address,omitempty"`
  344. // IdentityToken is used to authenticate the user and get
  345. // an access token for the registry.
  346. IdentityToken string `json:"identity_token,omitempty"`
  347. // RegistryToken is a bearer token to be sent to a registry
  348. RegistryToken string `json:"registry_token,omitempty"`
  349. }
  350. type ContainerRootfs struct {
  351. Type ContainerVolumeMountType `json:"type"`
  352. Disk *ContainerVolumeMountDisk `json:"disk"`
  353. //CephFS *ContainerVolumeMountCephFS `json:"ceph_fs"`
  354. // 是否持久化
  355. Persistent bool `json:"persistent"`
  356. }