config.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. package specs
  2. import "os"
  3. // Spec is the base configuration for the container. It specifies platform
  4. // independent configuration. This information must be included when the
  5. // bundle is packaged for distribution.
  6. type Spec struct {
  7. // Version is the version of the specification that is supported.
  8. Version string `json:"ociVersion"`
  9. // Platform is the host information for OS and Arch.
  10. Platform Platform `json:"platform"`
  11. // Process is the container's main process.
  12. Process Process `json:"process"`
  13. // Root is the root information for the container's filesystem.
  14. Root Root `json:"root"`
  15. // Hostname is the container's host name.
  16. Hostname string `json:"hostname,omitempty"`
  17. // Mounts profile configuration for adding mounts to the container's filesystem.
  18. Mounts []Mount `json:"mounts"`
  19. // Hooks are the commands run at various lifecycle events of the container.
  20. Hooks Hooks `json:"hooks"`
  21. // Annotations is an unstructured key value map that may be set by external tools to store and retrieve arbitrary metadata.
  22. Annotations map[string]string `json:"annotations,omitempty"`
  23. // Linux is platform specific configuration for Linux based containers.
  24. Linux Linux `json:"linux" platform:"linux"`
  25. }
  26. // Process contains information to start a specific application inside the container.
  27. type Process struct {
  28. // Terminal creates an interactive terminal for the container.
  29. Terminal bool `json:"terminal"`
  30. // User specifies user information for the process.
  31. User User `json:"user"`
  32. // Args specifies the binary and arguments for the application to execute.
  33. Args []string `json:"args"`
  34. // Env populates the process environment for the process.
  35. Env []string `json:"env,omitempty"`
  36. // Cwd is the current working directory for the process and must be
  37. // relative to the container's root.
  38. Cwd string `json:"cwd"`
  39. // Capabilities are Linux capabilities that are kept for the container.
  40. Capabilities []string `json:"capabilities,omitempty" platform:"linux"`
  41. // Rlimits specifies rlimit options to apply to the process.
  42. Rlimits []Rlimit `json:"rlimits,omitempty"`
  43. // NoNewPrivileges controls whether additional privileges could be gained by processes in the container.
  44. NoNewPrivileges bool `json:"noNewPrivileges,omitempty"`
  45. // ApparmorProfile specified the apparmor profile for the container. (this field is platform dependent)
  46. ApparmorProfile string `json:"apparmorProfile,omitempty" platform:"linux"`
  47. // SelinuxLabel specifies the selinux context that the container process is run as. (this field is platform dependent)
  48. SelinuxLabel string `json:"selinuxLabel,omitempty" platform:"linux"`
  49. }
  50. // User specifies Linux specific user and group information for the container's
  51. // main process.
  52. type User struct {
  53. // UID is the user id. (this field is platform dependent)
  54. UID uint32 `json:"uid,omitempty" platform:"linux"`
  55. // GID is the group id. (this field is platform dependent)
  56. GID uint32 `json:"gid,omitempty" platform:"linux"`
  57. // AdditionalGids are additional group ids set for the container's process. (this field is platform dependent)
  58. AdditionalGids []uint32 `json:"additionalGids,omitempty" platform:"linux"`
  59. }
  60. // Root contains information about the container's root filesystem on the host.
  61. type Root struct {
  62. // Path is the absolute path to the container's root filesystem.
  63. Path string `json:"path"`
  64. // Readonly makes the root filesystem for the container readonly before the process is executed.
  65. Readonly bool `json:"readonly"`
  66. }
  67. // Platform specifies OS and arch information for the host system that the container
  68. // is created for.
  69. type Platform struct {
  70. // OS is the operating system.
  71. OS string `json:"os"`
  72. // Arch is the architecture
  73. Arch string `json:"arch"`
  74. }
  75. // Mount specifies a mount for a container.
  76. type Mount struct {
  77. // Destination is the path where the mount will be placed relative to the container's root. The path and child directories MUST exist, a runtime MUST NOT create directories automatically to a mount point.
  78. Destination string `json:"destination"`
  79. // Type specifies the mount kind.
  80. Type string `json:"type"`
  81. // Source specifies the source path of the mount. In the case of bind mounts on
  82. // Linux based systems this would be the file on the host.
  83. Source string `json:"source"`
  84. // Options are fstab style mount options.
  85. Options []string `json:"options,omitempty"`
  86. }
  87. // Hook specifies a command that is run at a particular event in the lifecycle of a container
  88. type Hook struct {
  89. Path string `json:"path"`
  90. Args []string `json:"args,omitempty"`
  91. Env []string `json:"env,omitempty"`
  92. Timeout *int `json:"timeout,omitempty"`
  93. }
  94. // Hooks for container setup and teardown
  95. type Hooks struct {
  96. // Prestart is a list of hooks to be run before the container process is executed.
  97. // On Linux, they are run after the container namespaces are created.
  98. Prestart []Hook `json:"prestart,omitempty"`
  99. // Poststart is a list of hooks to be run after the container process is started.
  100. Poststart []Hook `json:"poststart,omitempty"`
  101. // Poststop is a list of hooks to be run after the container process exits.
  102. Poststop []Hook `json:"poststop,omitempty"`
  103. }
  104. // Linux contains platform specific configuration for Linux based containers.
  105. type Linux struct {
  106. // UIDMapping specifies user mappings for supporting user namespaces on Linux.
  107. UIDMappings []IDMapping `json:"uidMappings,omitempty"`
  108. // GIDMapping specifies group mappings for supporting user namespaces on Linux.
  109. GIDMappings []IDMapping `json:"gidMappings,omitempty"`
  110. // Sysctl are a set of key value pairs that are set for the container on start
  111. Sysctl map[string]string `json:"sysctl,omitempty"`
  112. // Resources contain cgroup information for handling resource constraints
  113. // for the container
  114. Resources *Resources `json:"resources,omitempty"`
  115. // CgroupsPath specifies the path to cgroups that are created and/or joined by the container.
  116. // The path is expected to be relative to the cgroups mountpoint.
  117. // If resources are specified, the cgroups at CgroupsPath will be updated based on resources.
  118. CgroupsPath *string `json:"cgroupsPath,omitempty"`
  119. // Namespaces contains the namespaces that are created and/or joined by the container
  120. Namespaces []Namespace `json:"namespaces,omitempty"`
  121. // Devices are a list of device nodes that are created for the container
  122. Devices []Device `json:"devices,omitempty"`
  123. // Seccomp specifies the seccomp security settings for the container.
  124. Seccomp *Seccomp `json:"seccomp,omitempty"`
  125. // RootfsPropagation is the rootfs mount propagation mode for the container.
  126. RootfsPropagation string `json:"rootfsPropagation,omitempty"`
  127. // MaskedPaths masks over the provided paths inside the container.
  128. MaskedPaths []string `json:"maskedPaths,omitempty"`
  129. // ReadonlyPaths sets the provided paths as RO inside the container.
  130. ReadonlyPaths []string `json:"readonlyPaths,omitempty"`
  131. // MountLabel specifies the selinux context for the mounts in the container.
  132. MountLabel string `json:"mountLabel,omitempty"`
  133. }
  134. // Namespace is the configuration for a Linux namespace
  135. type Namespace struct {
  136. // Type is the type of Linux namespace
  137. Type NamespaceType `json:"type"`
  138. // Path is a path to an existing namespace persisted on disk that can be joined
  139. // and is of the same type
  140. Path string `json:"path,omitempty"`
  141. }
  142. // NamespaceType is one of the Linux namespaces
  143. type NamespaceType string
  144. const (
  145. // PIDNamespace for isolating process IDs
  146. PIDNamespace NamespaceType = "pid"
  147. // NetworkNamespace for isolating network devices, stacks, ports, etc
  148. NetworkNamespace = "network"
  149. // MountNamespace for isolating mount points
  150. MountNamespace = "mount"
  151. // IPCNamespace for isolating System V IPC, POSIX message queues
  152. IPCNamespace = "ipc"
  153. // UTSNamespace for isolating hostname and NIS domain name
  154. UTSNamespace = "uts"
  155. // UserNamespace for isolating user and group IDs
  156. UserNamespace = "user"
  157. )
  158. // IDMapping specifies UID/GID mappings
  159. type IDMapping struct {
  160. // HostID is the UID/GID of the host user or group
  161. HostID uint32 `json:"hostID"`
  162. // ContainerID is the UID/GID of the container's user or group
  163. ContainerID uint32 `json:"containerID"`
  164. // Size is the length of the range of IDs mapped between the two namespaces
  165. Size uint32 `json:"size"`
  166. }
  167. // Rlimit type and restrictions
  168. type Rlimit struct {
  169. // Type of the rlimit to set
  170. Type string `json:"type"`
  171. // Hard is the hard limit for the specified type
  172. Hard uint64 `json:"hard"`
  173. // Soft is the soft limit for the specified type
  174. Soft uint64 `json:"soft"`
  175. }
  176. // HugepageLimit structure corresponds to limiting kernel hugepages
  177. type HugepageLimit struct {
  178. // Pagesize is the hugepage size
  179. Pagesize *string `json:"pageSize,omitempty"`
  180. // Limit is the limit of "hugepagesize" hugetlb usage
  181. Limit *uint64 `json:"limit,omitempty"`
  182. }
  183. // InterfacePriority for network interfaces
  184. type InterfacePriority struct {
  185. // Name is the name of the network interface
  186. Name string `json:"name"`
  187. // Priority for the interface
  188. Priority uint32 `json:"priority"`
  189. }
  190. // blockIODevice holds major:minor format supported in blkio cgroup
  191. type blockIODevice struct {
  192. // Major is the device's major number.
  193. Major int64 `json:"major"`
  194. // Minor is the device's minor number.
  195. Minor int64 `json:"minor"`
  196. }
  197. // WeightDevice struct holds a `major:minor weight` pair for blkioWeightDevice
  198. type WeightDevice struct {
  199. blockIODevice
  200. // Weight is the bandwidth rate for the device, range is from 10 to 1000
  201. Weight *uint16 `json:"weight,omitempty"`
  202. // LeafWeight is the bandwidth rate for the device while competing with the cgroup's child cgroups, range is from 10 to 1000, CFQ scheduler only
  203. LeafWeight *uint16 `json:"leafWeight,omitempty"`
  204. }
  205. // ThrottleDevice struct holds a `major:minor rate_per_second` pair
  206. type ThrottleDevice struct {
  207. blockIODevice
  208. // Rate is the IO rate limit per cgroup per device
  209. Rate *uint64 `json:"rate,omitempty"`
  210. }
  211. // BlockIO for Linux cgroup 'blkio' resource management
  212. type BlockIO struct {
  213. // Specifies per cgroup weight, range is from 10 to 1000
  214. Weight *uint16 `json:"blkioWeight,omitempty"`
  215. // Specifies tasks' weight in the given cgroup while competing with the cgroup's child cgroups, range is from 10 to 1000, CFQ scheduler only
  216. LeafWeight *uint16 `json:"blkioLeafWeight,omitempty"`
  217. // Weight per cgroup per device, can override BlkioWeight
  218. WeightDevice []WeightDevice `json:"blkioWeightDevice,omitempty"`
  219. // IO read rate limit per cgroup per device, bytes per second
  220. ThrottleReadBpsDevice []ThrottleDevice `json:"blkioThrottleReadBpsDevice,omitempty"`
  221. // IO write rate limit per cgroup per device, bytes per second
  222. ThrottleWriteBpsDevice []ThrottleDevice `json:"blkioThrottleWriteBpsDevice,omitempty"`
  223. // IO read rate limit per cgroup per device, IO per second
  224. ThrottleReadIOPSDevice []ThrottleDevice `json:"blkioThrottleReadIOPSDevice,omitempty"`
  225. // IO write rate limit per cgroup per device, IO per second
  226. ThrottleWriteIOPSDevice []ThrottleDevice `json:"blkioThrottleWriteIOPSDevice,omitempty"`
  227. }
  228. // Memory for Linux cgroup 'memory' resource management
  229. type Memory struct {
  230. // Memory limit (in bytes).
  231. Limit *uint64 `json:"limit,omitempty"`
  232. // Memory reservation or soft_limit (in bytes).
  233. Reservation *uint64 `json:"reservation,omitempty"`
  234. // Total memory limit (memory + swap).
  235. Swap *uint64 `json:"swap,omitempty"`
  236. // Kernel memory limit (in bytes).
  237. Kernel *uint64 `json:"kernel,omitempty"`
  238. // Kernel memory limit for tcp (in bytes)
  239. KernelTCP *uint64 `json:"kernelTCP"`
  240. // How aggressive the kernel will swap memory pages. Range from 0 to 100.
  241. Swappiness *uint64 `json:"swappiness,omitempty"`
  242. }
  243. // CPU for Linux cgroup 'cpu' resource management
  244. type CPU struct {
  245. // CPU shares (relative weight (ratio) vs. other cgroups with cpu shares).
  246. Shares *uint64 `json:"shares,omitempty"`
  247. // CPU hardcap limit (in usecs). Allowed cpu time in a given period.
  248. Quota *uint64 `json:"quota,omitempty"`
  249. // CPU period to be used for hardcapping (in usecs).
  250. Period *uint64 `json:"period,omitempty"`
  251. // How much time realtime scheduling may use (in usecs).
  252. RealtimeRuntime *uint64 `json:"realtimeRuntime,omitempty"`
  253. // CPU period to be used for realtime scheduling (in usecs).
  254. RealtimePeriod *uint64 `json:"realtimePeriod,omitempty"`
  255. // CPUs to use within the cpuset. Default is to use any CPU available.
  256. Cpus *string `json:"cpus,omitempty"`
  257. // List of memory nodes in the cpuset. Default is to use any available memory node.
  258. Mems *string `json:"mems,omitempty"`
  259. }
  260. // Pids for Linux cgroup 'pids' resource management (Linux 4.3)
  261. type Pids struct {
  262. // Maximum number of PIDs. Default is "no limit".
  263. Limit *int64 `json:"limit,omitempty"`
  264. }
  265. // Network identification and priority configuration
  266. type Network struct {
  267. // Set class identifier for container's network packets
  268. ClassID *uint32 `json:"classID"`
  269. // Set priority of network traffic for container
  270. Priorities []InterfacePriority `json:"priorities,omitempty"`
  271. }
  272. // Resources has container runtime resource constraints
  273. type Resources struct {
  274. // Devices are a list of device rules for the whitelist controller
  275. Devices []DeviceCgroup `json:"devices"`
  276. // DisableOOMKiller disables the OOM killer for out of memory conditions
  277. DisableOOMKiller *bool `json:"disableOOMKiller,omitempty"`
  278. // Specify an oom_score_adj for the container.
  279. OOMScoreAdj *int `json:"oomScoreAdj,omitempty"`
  280. // Memory restriction configuration
  281. Memory *Memory `json:"memory,omitempty"`
  282. // CPU resource restriction configuration
  283. CPU *CPU `json:"cpu,omitempty"`
  284. // Task resource restriction configuration.
  285. Pids *Pids `json:"pids,omitempty"`
  286. // BlockIO restriction configuration
  287. BlockIO *BlockIO `json:"blockIO,omitempty"`
  288. // Hugetlb limit (in bytes)
  289. HugepageLimits []HugepageLimit `json:"hugepageLimits,omitempty"`
  290. // Network restriction configuration
  291. Network *Network `json:"network,omitempty"`
  292. }
  293. // Device represents the mknod information for a Linux special device file
  294. type Device struct {
  295. // Path to the device.
  296. Path string `json:"path"`
  297. // Device type, block, char, etc.
  298. Type string `json:"type"`
  299. // Major is the device's major number.
  300. Major int64 `json:"major"`
  301. // Minor is the device's minor number.
  302. Minor int64 `json:"minor"`
  303. // FileMode permission bits for the device.
  304. FileMode *os.FileMode `json:"fileMode,omitempty"`
  305. // UID of the device.
  306. UID *uint32 `json:"uid,omitempty"`
  307. // Gid of the device.
  308. GID *uint32 `json:"gid,omitempty"`
  309. }
  310. // DeviceCgroup represents a device rule for the whitelist controller
  311. type DeviceCgroup struct {
  312. // Allow or deny
  313. Allow bool `json:"allow"`
  314. // Device type, block, char, etc.
  315. Type *string `json:"type,omitempty"`
  316. // Major is the device's major number.
  317. Major *int64 `json:"major,omitempty"`
  318. // Minor is the device's minor number.
  319. Minor *int64 `json:"minor,omitempty"`
  320. // Cgroup access permissions format, rwm.
  321. Access *string `json:"access,omitempty"`
  322. }
  323. // Seccomp represents syscall restrictions
  324. type Seccomp struct {
  325. DefaultAction Action `json:"defaultAction"`
  326. Architectures []Arch `json:"architectures"`
  327. Syscalls []Syscall `json:"syscalls,omitempty"`
  328. }
  329. // Arch used for additional architectures
  330. type Arch string
  331. // Additional architectures permitted to be used for system calls
  332. // By default only the native architecture of the kernel is permitted
  333. const (
  334. ArchX86 Arch = "SCMP_ARCH_X86"
  335. ArchX86_64 Arch = "SCMP_ARCH_X86_64"
  336. ArchX32 Arch = "SCMP_ARCH_X32"
  337. ArchARM Arch = "SCMP_ARCH_ARM"
  338. ArchAARCH64 Arch = "SCMP_ARCH_AARCH64"
  339. ArchMIPS Arch = "SCMP_ARCH_MIPS"
  340. ArchMIPS64 Arch = "SCMP_ARCH_MIPS64"
  341. ArchMIPS64N32 Arch = "SCMP_ARCH_MIPS64N32"
  342. ArchMIPSEL Arch = "SCMP_ARCH_MIPSEL"
  343. ArchMIPSEL64 Arch = "SCMP_ARCH_MIPSEL64"
  344. ArchMIPSEL64N32 Arch = "SCMP_ARCH_MIPSEL64N32"
  345. )
  346. // Action taken upon Seccomp rule match
  347. type Action string
  348. // Define actions for Seccomp rules
  349. const (
  350. ActKill Action = "SCMP_ACT_KILL"
  351. ActTrap Action = "SCMP_ACT_TRAP"
  352. ActErrno Action = "SCMP_ACT_ERRNO"
  353. ActTrace Action = "SCMP_ACT_TRACE"
  354. ActAllow Action = "SCMP_ACT_ALLOW"
  355. )
  356. // Operator used to match syscall arguments in Seccomp
  357. type Operator string
  358. // Define operators for syscall arguments in Seccomp
  359. const (
  360. OpNotEqual Operator = "SCMP_CMP_NE"
  361. OpLessThan Operator = "SCMP_CMP_LT"
  362. OpLessEqual Operator = "SCMP_CMP_LE"
  363. OpEqualTo Operator = "SCMP_CMP_EQ"
  364. OpGreaterEqual Operator = "SCMP_CMP_GE"
  365. OpGreaterThan Operator = "SCMP_CMP_GT"
  366. OpMaskedEqual Operator = "SCMP_CMP_MASKED_EQ"
  367. )
  368. // Arg used for matching specific syscall arguments in Seccomp
  369. type Arg struct {
  370. Index uint `json:"index"`
  371. Value uint64 `json:"value"`
  372. ValueTwo uint64 `json:"valueTwo"`
  373. Op Operator `json:"op"`
  374. }
  375. // Syscall is used to match a syscall in Seccomp
  376. type Syscall struct {
  377. Name string `json:"name"`
  378. Action Action `json:"action"`
  379. Args []Arg `json:"args,omitempty"`
  380. }