github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/specgen/specgen.go (about) 1 package specgen 2 3 import ( 4 "net" 5 "syscall" 6 7 "github.com/containers/image/v5/manifest" 8 "github.com/containers/libpod/pkg/rootless" 9 "github.com/containers/storage" 10 "github.com/cri-o/ocicni/pkg/ocicni" 11 spec "github.com/opencontainers/runtime-spec/specs-go" 12 ) 13 14 // LogConfig describes the logging characteristics for a container 15 type LogConfig struct { 16 // LogDriver is the container's log driver. 17 // Optional. 18 Driver string `json:"driver,omitempty"` 19 // LogPath is the path the container's logs will be stored at. 20 // Only available if LogDriver is set to "json-file" or "k8s-file". 21 // Optional. 22 Path string `json:"path,omitempty"` 23 // A set of options to accompany the log driver. 24 // Optional. 25 Options map[string]string `json:"options,omitempty"` 26 } 27 28 // ContainerBasicConfig contains the basic parts of a container. 29 type ContainerBasicConfig struct { 30 // Name is the name the container will be given. 31 // If no name is provided, one will be randomly generated. 32 // Optional. 33 Name string `json:"name,omitempty"` 34 // Pod is the ID of the pod the container will join. 35 // Optional. 36 Pod string `json:"pod,omitempty"` 37 // Entrypoint is the container's entrypoint. 38 // If not given and Image is specified, this will be populated by the 39 // image's configuration. 40 // Optional. 41 Entrypoint []string `json:"entrypoint,omitempty"` 42 // Command is the container's command. 43 // If not given and Image is specified, this will be populated by the 44 // image's configuration. 45 // Optional. 46 Command []string `json:"command,omitempty"` 47 // Env is a set of environment variables that will be set in the 48 // container. 49 // Optional. 50 Env map[string]string `json:"env,omitempty"` 51 // Terminal is whether the container will create a PTY. 52 Terminal bool `json:"terminal,omitempty"` 53 // Stdin is whether the container will keep its STDIN open. 54 Stdin bool `json:"stdin,omitempty"` 55 // Labels are key-value pairs that are used to add metadata to 56 // containers. 57 // Optional. 58 Labels map[string]string `json:"labels,omitempty"` 59 // Annotations are key-value options passed into the container runtime 60 // that can be used to trigger special behavior. 61 // Optional. 62 Annotations map[string]string `json:"annotations,omitempty"` 63 // StopSignal is the signal that will be used to stop the container. 64 // Must be a non-zero integer below SIGRTMAX. 65 // If not provided, the default, SIGTERM, will be used. 66 // Will conflict with Systemd if Systemd is set to "true" or "always". 67 // Optional. 68 StopSignal *syscall.Signal `json:"stop_signal,omitempty"` 69 // StopTimeout is a timeout between the container's stop signal being 70 // sent and SIGKILL being sent. 71 // If not provided, the default will be used. 72 // If 0 is used, stop signal will not be sent, and SIGKILL will be sent 73 // instead. 74 // Optional. 75 StopTimeout *uint `json:"stop_timeout,omitempty"` 76 // LogConfiguration describes the logging for a container including 77 // driver, path, and options. 78 // Optional 79 LogConfiguration *LogConfig `json:"log_configuration,omitempty"` 80 // ConmonPidFile is a path at which a PID file for Conmon will be 81 // placed. 82 // If not given, a default location will be used. 83 // Optional. 84 ConmonPidFile string `json:"conmon_pid_file,omitempty"` 85 // RestartPolicy is the container's restart policy - an action which 86 // will be taken when the container exits. 87 // If not given, the default policy, which does nothing, will be used. 88 // Optional. 89 RestartPolicy string `json:"restart_policy,omitempty"` 90 // RestartRetries is the number of attempts that will be made to restart 91 // the container. 92 // Only available when RestartPolicy is set to "on-failure". 93 // Optional. 94 RestartRetries *uint `json:"restart_tries,omitempty"` 95 // OCIRuntime is the name of the OCI runtime that will be used to create 96 // the container. 97 // If not specified, the default will be used. 98 // Optional. 99 OCIRuntime string `json:"oci_runtime,omitempty"` 100 // Systemd is whether the container will be started in systemd mode. 101 // Valid options are "true", "false", and "always". 102 // "true" enables this mode only if the binary run in the container is 103 // /sbin/init or systemd. "always" unconditionally enables systemd mode. 104 // "false" unconditionally disables systemd mode. 105 // If enabled, mounts and stop signal will be modified. 106 // If set to "always" or set to "true" and conditionally triggered, 107 // conflicts with StopSignal. 108 // If not specified, "false" will be assumed. 109 // Optional. 110 Systemd string `json:"systemd,omitempty"` 111 // Namespace is the libpod namespace the container will be placed in. 112 // Optional. 113 Namespace string `json:"namespace,omitempty"` 114 // PidNS is the container's PID namespace. 115 // It defaults to private. 116 // Mandatory. 117 PidNS Namespace `json:"pidns,omitempty"` 118 // UtsNS is the container's UTS namespace. 119 // It defaults to private. 120 // Must be set to Private to set Hostname. 121 // Mandatory. 122 UtsNS Namespace `json:"utsns,omitempty"` 123 // Hostname is the container's hostname. If not set, the hostname will 124 // not be modified (if UtsNS is not private) or will be set to the 125 // container ID (if UtsNS is private). 126 // Conflicts with UtsNS if UtsNS is not set to private. 127 // Optional. 128 Hostname string `json:"hostname,omitempty"` 129 // Sysctl sets kernel parameters for the container 130 Sysctl map[string]string `json:"sysctl,omitempty"` 131 // Remove indicates if the container should be removed once it has been started 132 // and exits 133 Remove bool `json:"remove"` 134 } 135 136 // ContainerStorageConfig contains information on the storage configuration of a 137 // container. 138 type ContainerStorageConfig struct { 139 // Image is the image the container will be based on. The image will be 140 // used as the container's root filesystem, and its environment vars, 141 // volumes, and other configuration will be applied to the container. 142 // Conflicts with Rootfs. 143 // At least one of Image or Rootfs must be specified. 144 Image string `json:"image"` 145 // RawImageName is the unprocessed and not-normalized user-specified image 146 // name. One use case for having this data at hand are auto-updates where 147 // the _exact_ user input is needed in order to look-up the correct image. 148 RawImageName string `json:"raw_image_name,omitempty"` 149 // Rootfs is the path to a directory that will be used as the 150 // container's root filesystem. No modification will be made to the 151 // directory, it will be directly mounted into the container as root. 152 // Conflicts with Image. 153 // At least one of Image or Rootfs must be specified. 154 Rootfs string `json:"rootfs,omitempty"` 155 // ImageVolumeMode indicates how image volumes will be created. 156 // Supported modes are "ignore" (do not create), "tmpfs" (create as 157 // tmpfs), and "anonymous" (create as anonymous volumes). 158 // The default is anonymous. 159 // Optional. 160 ImageVolumeMode string `json:"image_volume_mode,omitempty"` 161 // VolumesFrom is a list of containers whose volumes will be added to 162 // this container. Supported mount options may be added after the 163 // container name with a : and include "ro" and "rw". 164 // Optional. 165 VolumesFrom []string `json:"volumes_from,omitempty"` 166 // Mounts are mounts that will be added to the container. 167 // These will supersede Image Volumes and VolumesFrom volumes where 168 // there are conflicts. 169 // Optional. 170 Mounts []spec.Mount `json:"mounts,omitempty"` 171 // Volumes are named volumes that will be added to the container. 172 // These will supersede Image Volumes and VolumesFrom volumes where 173 // there are conflicts. 174 // Optional. 175 Volumes []*Volumes `json:"volumes,omitempty"` 176 // Devices are devices that will be added to the container. 177 // Optional. 178 Devices []spec.LinuxDevice `json:"devices,omitempty"` 179 // IpcNS is the container's IPC namespace. 180 // Default is private. 181 // Conflicts with ShmSize if not set to private. 182 // Mandatory. 183 IpcNS Namespace `json:"ipcns,omitempty"` 184 // ShmSize is the size of the tmpfs to mount in at /dev/shm, in bytes. 185 // Conflicts with ShmSize if IpcNS is not private. 186 // Optional. 187 ShmSize *int64 `json:"shm_size,omitempty"` 188 // WorkDir is the container's working directory. 189 // If unset, the default, /, will be used. 190 // Optional. 191 WorkDir string `json:"work_dir,omitempty"` 192 // RootfsPropagation is the rootfs propagation mode for the container. 193 // If not set, the default of rslave will be used. 194 // Optional. 195 RootfsPropagation string `json:"rootfs_propagation,omitempty"` 196 } 197 198 // ContainerSecurityConfig is a container's security features, including 199 // SELinux, Apparmor, and Seccomp. 200 type ContainerSecurityConfig struct { 201 // Privileged is whether the container is privileged. 202 // Privileged does the following: 203 // - Adds all devices on the system to the container. 204 // - Adds all capabilities to the container. 205 // - Disables Seccomp, SELinux, and Apparmor confinement. 206 // TODO: this conflicts with things. 207 // TODO: this does more. 208 Privileged bool `json:"privileged,omitempty"` 209 // User is the user the container will be run as. 210 // Can be given as a UID or a username; if a username, it will be 211 // resolved within the container, using the container's /etc/passwd. 212 // If unset, the container will be run as root. 213 // Optional. 214 User string `json:"user,omitempty"` 215 // Groups are a list of supplemental groups the container's user will 216 // be granted access to. 217 // Optional. 218 Groups []string `json:"groups,omitempty"` 219 // CapAdd are capabilities which will be added to the container. 220 // Conflicts with Privileged. 221 // Optional. 222 CapAdd []string `json:"cap_add,omitempty"` 223 // CapDrop are capabilities which will be removed from the container. 224 // Conflicts with Privileged. 225 // Optional. 226 CapDrop []string `json:"cap_drop,omitempty"` 227 // SelinuxProcessLabel is the process label the container will use. 228 // If SELinux is enabled and this is not specified, a label will be 229 // automatically generated if not specified. 230 // Optional. 231 SelinuxProcessLabel string `json:"selinux_process_label,omitempty"` 232 // SelinuxMountLabel is the mount label the container will use. 233 // If SELinux is enabled and this is not specified, a label will be 234 // automatically generated if not specified. 235 // Optional. 236 SelinuxMountLabel string `json:"selinux_mount_label,omitempty"` 237 // SelinuxOpts are options for configuring SELinux. 238 // Optional. 239 SelinuxOpts []string `json:"selinux_opts,omitempty"` 240 // ApparmorProfile is the name of the Apparmor profile the container 241 // will use. 242 // Optional. 243 ApparmorProfile string `json:"apparmor_profile,omitempty"` 244 // SeccompPolicy determines which seccomp profile gets applied 245 // the container. valid values: empty,default,image 246 SeccompPolicy string `json:"seccomp_policy,omitempty"` 247 // SeccompProfilePath is the path to a JSON file containing the 248 // container's Seccomp profile. 249 // If not specified, no Seccomp profile will be used. 250 // Optional. 251 SeccompProfilePath string `json:"seccomp_profile_path,omitempty"` 252 // NoNewPrivileges is whether the container will set the no new 253 // privileges flag on create, which disables gaining additional 254 // privileges (e.g. via setuid) in the container. 255 NoNewPrivileges bool `json:"no_new_privileges,omitempty"` 256 // UserNS is the container's user namespace. 257 // It defaults to host, indicating that no user namespace will be 258 // created. 259 // If set to private, IDMappings must be set. 260 // Mandatory. 261 UserNS Namespace `json:"userns,omitempty"` 262 // IDMappings are UID and GID mappings that will be used by user 263 // namespaces. 264 // Required if UserNS is private. 265 IDMappings *storage.IDMappingOptions `json:"idmappings,omitempty"` 266 // ReadOnlyFilesystem indicates that everything will be mounted 267 // as read-only 268 ReadOnlyFilesystem bool `json:"read_only_filesystem,omittempty"` 269 } 270 271 // ContainerCgroupConfig contains configuration information about a container's 272 // cgroups. 273 type ContainerCgroupConfig struct { 274 // CgroupNS is the container's cgroup namespace. 275 // It defaults to private. 276 // Mandatory. 277 CgroupNS Namespace `json:"cgroupns,omitempty"` 278 // CgroupsMode sets a policy for how cgroups will be created in the 279 // container, including the ability to disable creation entirely. 280 CgroupsMode string `json:"cgroups_mode,omitempty"` 281 // CgroupParent is the container's CGroup parent. 282 // If not set, the default for the current cgroup driver will be used. 283 // Optional. 284 CgroupParent string `json:"cgroup_parent,omitempty"` 285 } 286 287 // ContainerNetworkConfig contains information on a container's network 288 // configuration. 289 type ContainerNetworkConfig struct { 290 // NetNS is the configuration to use for the container's network 291 // namespace. 292 // Mandatory. 293 NetNS Namespace `json:"netns,omitempty"` 294 // ConfigureNetNS is whether Libpod will configure the container's 295 // network namespace to send and receive traffic. 296 // Only available is NetNS is private - conflicts with other NetNS 297 // modes. 298 ConfigureNetNS bool `json:"configure_netns,omitempty"` 299 // StaticIP is the a IPv4 address of the container. 300 // Only available if ConfigureNetNS is true. 301 // Optional. 302 StaticIP *net.IP `json:"static_ip,omitempty"` 303 // StaticIPv6 is a static IPv6 address to set in the container. 304 // Only available if ConfigureNetNS is true. 305 // Optional. 306 StaticIPv6 *net.IP `json:"static_ipv6,omitempty"` 307 // StaticMAC is a static MAC address to set in the container. 308 // Only available if ConfigureNetNS is true. 309 // Optional. 310 StaticMAC *net.HardwareAddr `json:"static_mac,omitempty"` 311 // PortBindings is a set of ports to map into the container. 312 // Only available if ConfigureNetNS is true. 313 // Optional. 314 PortMappings []ocicni.PortMapping `json:"portmappings,omitempty"` 315 // PublishImagePorts will publish ports specified in the image to random 316 // ports outside. 317 // Requires Image to be set. 318 PublishImagePorts bool `json:"publish_image_ports,omitempty"` 319 // CNINetworks is a list of CNI networks to join the container to. 320 // If this list is empty, the default CNI network will be joined 321 // instead. If at least one entry is present, we will not join the 322 // default network (unless it is part of this list). 323 // Only available if ConfigureNetNS is true. 324 // Optional. 325 CNINetworks []string `json:"cni_networks,omitempty"` 326 // UseImageResolvConf indicates that resolv.conf should not be managed 327 // by Podman, but instead sourced from the image. 328 // Conflicts with DNSServer, DNSSearch, DNSOption. 329 UseImageResolvConf bool `json:"use_image_resolve_conf,omitempty"` 330 // DNSServer is a set of DNS servers that will be used in the 331 // container's resolv.conf, replacing the host's DNS Servers which are 332 // used by default. 333 // Conflicts with UseImageResolvConf. 334 // Optional. 335 DNSServer []net.IP `json:"dns_server,omitempty"` 336 // DNSSearch is a set of DNS search domains that will be used in the 337 // container's resolv.conf, replacing the host's DNS search domains 338 // which are used by default. 339 // Conflicts with UseImageResolvConf. 340 // Optional. 341 DNSSearch []string `json:"dns_search,omitempty"` 342 // DNSOption is a set of DNS options that will be used in the 343 // container's resolv.conf, replacing the host's DNS options which are 344 // used by default. 345 // Conflicts with UseImageResolvConf. 346 // Optional. 347 DNSOption []string `json:"dns_option,omitempty"` 348 // UseImageHosts indicates that /etc/hosts should not be managed by 349 // Podman, and instead sourced from the image. 350 // Conflicts with HostAdd. 351 UseImageHosts bool `json:"use_image_hosts,omitempty"` 352 // HostAdd is a set of hosts which will be added to the container's 353 // /etc/hosts file. 354 // Conflicts with UseImageHosts. 355 // Optional. 356 HostAdd []string `json:"hostadd,omitempty"` 357 } 358 359 // ContainerResourceConfig contains information on container resource limits. 360 type ContainerResourceConfig struct { 361 // ResourceLimits are resource limits to apply to the container., 362 // Can only be set as root on cgroups v1 systems, but can be set as 363 // rootless as well for cgroups v2. 364 // Optional. 365 ResourceLimits *spec.LinuxResources `json:"resource_limits,omitempty"` 366 // Rlimits are POSIX rlimits to apply to the container. 367 // Optional. 368 Rlimits []spec.POSIXRlimit `json:"r_limits,omitempty"` 369 // OOMScoreAdj adjusts the score used by the OOM killer to determine 370 // processes to kill for the container's process. 371 // Optional. 372 OOMScoreAdj *int `json:"oom_score_adj,omitempty"` 373 // Weight per cgroup per device, can override BlkioWeight 374 WeightDevice map[string]spec.LinuxWeightDevice `json:"weightDevice,omitempty"` 375 // IO read rate limit per cgroup per device, bytes per second 376 ThrottleReadBpsDevice map[string]spec.LinuxThrottleDevice `json:"throttleReadBpsDevice,omitempty"` 377 // IO write rate limit per cgroup per device, bytes per second 378 ThrottleWriteBpsDevice map[string]spec.LinuxThrottleDevice `json:"throttleWriteBpsDevice,omitempty"` 379 // IO read rate limit per cgroup per device, IO per second 380 ThrottleReadIOPSDevice map[string]spec.LinuxThrottleDevice `json:"throttleReadIOPSDevice,omitempty"` 381 // IO write rate limit per cgroup per device, IO per second 382 ThrottleWriteIOPSDevice map[string]spec.LinuxThrottleDevice `json:"throttleWriteIOPSDevice,omitempty"` 383 } 384 385 // ContainerHealthCheckConfig describes a container healthcheck with attributes 386 // like command, retries, interval, start period, and timeout. 387 type ContainerHealthCheckConfig struct { 388 HealthConfig *manifest.Schema2HealthConfig `json:"healthconfig,omitempty"` 389 } 390 391 // SpecGenerator creates an OCI spec and Libpod configuration options to create 392 // a container based on the given configuration. 393 // swagger:model SpecGenerator 394 type SpecGenerator struct { 395 ContainerBasicConfig 396 ContainerStorageConfig 397 ContainerSecurityConfig 398 ContainerCgroupConfig 399 ContainerNetworkConfig 400 ContainerResourceConfig 401 ContainerHealthCheckConfig 402 } 403 404 // Volumes is a temporary struct to hold input from the User 405 type Volumes struct { 406 Name string 407 Dest string 408 Options []string 409 } 410 411 // NewSpecGenerator returns a SpecGenerator struct given one of two mandatory inputs 412 func NewSpecGenerator(image string) *SpecGenerator { 413 networkConfig := ContainerNetworkConfig{ 414 NetNS: Namespace{ 415 NSMode: Bridge, 416 }, 417 } 418 csc := ContainerStorageConfig{Image: image} 419 if rootless.IsRootless() { 420 networkConfig.NetNS.NSMode = Slirp 421 } 422 return &SpecGenerator{ 423 ContainerStorageConfig: csc, 424 ContainerNetworkConfig: networkConfig, 425 } 426 } 427 428 // NewSpecGenerator returns a SpecGenerator struct given one of two mandatory inputs 429 func NewSpecGeneratorWithRootfs(rootfs string) *SpecGenerator { 430 csc := ContainerStorageConfig{Rootfs: rootfs} 431 return &SpecGenerator{ContainerStorageConfig: csc} 432 }