|
1 | 1 | use std::time::Duration;
|
2 | 2 |
|
3 |
| -use bollard::models::HostConfig; |
4 | 3 | use bollard_stubs::models::ResourcesUlimits;
|
5 | 4 |
|
6 | 5 | use crate::{
|
@@ -158,8 +157,65 @@ pub trait ImageExt<I: Image> {
|
158 | 157 | /// Allows to follow the container logs for the whole lifecycle of the container, starting from the creation.
|
159 | 158 | fn with_log_consumer(self, log_consumer: impl LogConsumer + 'static) -> ContainerRequest<I>;
|
160 | 159 |
|
161 |
| - /// Adds a Bollard HostConfig override to the container |
162 |
| - fn with_host_config(self, host_config: impl Into<HostConfig>) -> ContainerRequest<I>; |
| 160 | + /// Sets the CPU period for the container. |
| 161 | + /// The default is defined by the underlying image. |
| 162 | + /// The length of a CPU period in microseconds. |
| 163 | + /// https://docs.docker.com/engine/reference/commandline/run/#cpu-period |
| 164 | + fn with_cpu_period(self, cpu_period: impl Into<i64>) -> ContainerRequest<I>; |
| 165 | + |
| 166 | + /// Sets the CPU quota for the container. |
| 167 | + /// The default is defined by the underlying image. |
| 168 | + /// Microseconds of CPU time that the container can get in a CPU period. |
| 169 | + /// https://docs.docker.com/engine/reference/commandline/run/#cpu-quota |
| 170 | + /// Most users will want to set CPU quota to their desired CPU count * 100000. |
| 171 | + /// For example, to limit a container to 2 CPUs, set CPU quota to 200000. |
| 172 | + /// This is based on the default CPU period of 100000. |
| 173 | + /// If CPU quota is set to 0, the container will not be limited. |
| 174 | + fn with_cpu_quota(self, cpu_quota: impl Into<i64>) -> ContainerRequest<I>; |
| 175 | + |
| 176 | + /// Sets the CPU realtime period for the container. |
| 177 | + /// The default is defined by the underlying image. |
| 178 | + /// The length of a CPU real-time period in microseconds. |
| 179 | + fn with_cpu_realtime_period(self, cpu_realtime_period: impl Into<i64>) -> ContainerRequest<I>; |
| 180 | + |
| 181 | + /// Sets the CPU realtime runtime for the container. |
| 182 | + fn with_cpu_realtime_runtime(self, cpu_realtime_runtime: impl Into<i64>) |
| 183 | + -> ContainerRequest<I>; |
| 184 | + |
| 185 | + /// Sets the CPUs in which to allow execution (e.g., `0-3`, `0,1`). |
| 186 | + /// Core pinning should help with performance consistency and context switching in some cases. |
| 187 | + /// The default is defined by the underlying image. |
| 188 | + fn with_cpuset_cpus(self, cpuset_cpus: impl Into<String>) -> ContainerRequest<I>; |
| 189 | + |
| 190 | + /// Memory limit for the container, the _minimum_ is 6 MiB. |
| 191 | + /// This is the same as `HostConfig::memory`. |
| 192 | + fn with_memory(self, bytes: i64) -> ContainerRequest<I>; |
| 193 | + |
| 194 | + /// Memory reservation, soft limit. Analogous to the JVM's `-Xms` option. |
| 195 | + /// The _minimum_ is 6 MiB. |
| 196 | + /// This is the same as `HostConfig::memory_reservation`. |
| 197 | + fn with_memory_reservation(self, bytes: i64) -> ContainerRequest<I>; |
| 198 | + |
| 199 | + /// Total memory limit (memory + swap). Set as `-1` to enable unlimited swap. |
| 200 | + /// Same 6 MiB minimum as `memory`. I do not know why everything is i64. |
| 201 | + fn with_memory_swap(self, bytes: i64) -> ContainerRequest<I>; |
| 202 | + |
| 203 | + /// Tune a container's memory swappiness behavior. Accepts an integer between 0 and 100. |
| 204 | + fn with_memory_swappiness(self, swappiness: i64) -> ContainerRequest<I>; |
| 205 | + |
| 206 | + /// Disable OOM Killer for the container. This will not do anything unless -m (memory limit, cf. memory on this struct) is set. |
| 207 | + /// You can disable OOM-killer by writing "1" to memory.oom_control file, as: |
| 208 | + /// ```ignore |
| 209 | + /// echo 1 > memory.oom_control |
| 210 | + /// ``` |
| 211 | + /// This operation is only allowed to the top cgroup of sub-hierarchy. |
| 212 | + /// If OOM-killer is disabled, tasks under cgroup will hang/sleep |
| 213 | + /// in memory cgroup's OOM-waitqueue when they request accountable memory. |
| 214 | + /// https://lwn.net/Articles/432224/ |
| 215 | + fn with_oom_kill_disable(self, disable: bool) -> ContainerRequest<I>; |
| 216 | + |
| 217 | + /// Tune a container's PIDs limit. Set `0` or `-1` for unlimited, or `null` to not change. |
| 218 | + fn with_pids_limit(self, limit: i64) -> ContainerRequest<I>; |
163 | 219 |
|
164 | 220 | /// Flag the container as being exempt from the default `testcontainers` remove-on-drop lifecycle,
|
165 | 221 | /// indicating that the container should be kept running, and that executions with the same configuration
|
@@ -377,10 +433,93 @@ impl<RI: Into<ContainerRequest<I>>, I: Image> ImageExt<I> for RI {
|
377 | 433 | container_req
|
378 | 434 | }
|
379 | 435 |
|
380 |
| - fn with_host_config(self, host_config: impl Into<HostConfig>) -> ContainerRequest<I> { |
| 436 | + fn with_cpu_period(self, cpu_period: impl Into<i64>) -> ContainerRequest<I> { |
| 437 | + let container_req = self.into(); |
| 438 | + ContainerRequest { |
| 439 | + cpu_period: Some(cpu_period.into()), |
| 440 | + ..container_req |
| 441 | + } |
| 442 | + } |
| 443 | + |
| 444 | + fn with_cpu_quota(self, cpu_quota: impl Into<i64>) -> ContainerRequest<I> { |
| 445 | + let container_req = self.into(); |
| 446 | + ContainerRequest { |
| 447 | + cpu_quota: Some(cpu_quota.into()), |
| 448 | + ..container_req |
| 449 | + } |
| 450 | + } |
| 451 | + |
| 452 | + fn with_cpu_realtime_period(self, cpu_realtime_period: impl Into<i64>) -> ContainerRequest<I> { |
| 453 | + let container_req = self.into(); |
| 454 | + ContainerRequest { |
| 455 | + cpu_realtime_period: Some(cpu_realtime_period.into()), |
| 456 | + ..container_req |
| 457 | + } |
| 458 | + } |
| 459 | + |
| 460 | + fn with_cpu_realtime_runtime( |
| 461 | + self, |
| 462 | + cpu_realtime_runtime: impl Into<i64>, |
| 463 | + ) -> ContainerRequest<I> { |
| 464 | + let container_req = self.into(); |
| 465 | + ContainerRequest { |
| 466 | + cpu_realtime_runtime: Some(cpu_realtime_runtime.into()), |
| 467 | + ..container_req |
| 468 | + } |
| 469 | + } |
| 470 | + |
| 471 | + fn with_cpuset_cpus(self, cpuset_cpus: impl Into<String>) -> ContainerRequest<I> { |
| 472 | + let container_req = self.into(); |
| 473 | + ContainerRequest { |
| 474 | + cpuset_cpus: Some(cpuset_cpus.into()), |
| 475 | + ..container_req |
| 476 | + } |
| 477 | + } |
| 478 | + |
| 479 | + fn with_memory(self, bytes: i64) -> ContainerRequest<I> { |
| 480 | + let container_req = self.into(); |
| 481 | + ContainerRequest { |
| 482 | + memory: Some(bytes), |
| 483 | + ..container_req |
| 484 | + } |
| 485 | + } |
| 486 | + |
| 487 | + fn with_memory_reservation(self, bytes: i64) -> ContainerRequest<I> { |
| 488 | + let container_req = self.into(); |
| 489 | + ContainerRequest { |
| 490 | + memory_reservation: Some(bytes), |
| 491 | + ..container_req |
| 492 | + } |
| 493 | + } |
| 494 | + |
| 495 | + fn with_memory_swap(self, bytes: i64) -> ContainerRequest<I> { |
| 496 | + let container_req = self.into(); |
| 497 | + ContainerRequest { |
| 498 | + memory_swap: Some(bytes), |
| 499 | + ..container_req |
| 500 | + } |
| 501 | + } |
| 502 | + |
| 503 | + fn with_memory_swappiness(self, swappiness: i64) -> ContainerRequest<I> { |
| 504 | + let container_req = self.into(); |
| 505 | + ContainerRequest { |
| 506 | + memory_swappiness: Some(swappiness), |
| 507 | + ..container_req |
| 508 | + } |
| 509 | + } |
| 510 | + |
| 511 | + fn with_oom_kill_disable(self, disable: bool) -> ContainerRequest<I> { |
| 512 | + let container_req = self.into(); |
| 513 | + ContainerRequest { |
| 514 | + oom_kill_disable: Some(disable), |
| 515 | + ..container_req |
| 516 | + } |
| 517 | + } |
| 518 | + |
| 519 | + fn with_pids_limit(self, limit: i64) -> ContainerRequest<I> { |
381 | 520 | let container_req = self.into();
|
382 | 521 | ContainerRequest {
|
383 |
| - host_config: Some(host_config.into()), |
| 522 | + pids_limit: Some(limit), |
384 | 523 | ..container_req
|
385 | 524 | }
|
386 | 525 | }
|
|
0 commit comments