Skip to content

Commit 34a3bc1

Browse files
author
Chris Birchall
committed
Move memory and memoryReservation into HostConfig
1 parent 3b67038 commit 34a3bc1

File tree

5 files changed

+37
-26
lines changed

5 files changed

+37
-26
lines changed

config/src/main/scala/com/whisk/docker/config/DockerTypesafeConfig.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.whisk.docker.config
22

33
import com.whisk.docker.impl.dockerjava.DockerKitDockerJava
4-
import com.whisk.docker.{DockerContainer, DockerPortMapping, DockerReadyChecker, VolumeMapping}
4+
import com.whisk.docker.{DockerContainer, DockerPortMapping, DockerReadyChecker, HostConfig, VolumeMapping}
55

66
import scala.concurrent.duration._
77

@@ -62,6 +62,11 @@ object DockerTypesafeConfig extends DockerKitDockerJava {
6262

6363
val readyChecker = `ready-checker`.fold[DockerReadyChecker](AlwaysReady) { _.toReadyChecker }
6464

65+
val hostConfig = HostConfig(
66+
memory = memory,
67+
memoryReservation = `memory-reservation`
68+
)
69+
6570
DockerContainer(
6671
image = `image-name`,
6772
name = `container-name`,
@@ -71,8 +76,7 @@ object DockerTypesafeConfig extends DockerKitDockerJava {
7176
env = `environmental-variables`,
7277
readyChecker = readyChecker,
7378
volumeMappings = `volume-maps`,
74-
memory = memory,
75-
memoryReservation = `memory-reservation`
79+
hostConfig = Some(hostConfig)
7680
)
7781
}
7882
}

config/src/test/scala/com/whisk/docker/config/test/DockerConfigSpec.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.whisk.docker.config.test
22

3-
import com.whisk.docker.{DockerContainer, DockerReadyChecker, VolumeMapping}
3+
import com.whisk.docker.{DockerContainer, DockerReadyChecker, HostConfig, VolumeMapping}
44
import com.whisk.docker.config.DockerKitConfig
55
import org.scalatest._
66

@@ -40,8 +40,7 @@ class DockerConfigSpec extends FlatSpec with Matchers with DockerKitConfig {
4040
val elasticExpected = DockerContainer("elasticsearch:1.7.1")
4141
.withEntrypoint("my", "custom", "entrypoint")
4242
.withPorts(9200 -> None, 9300 -> None)
43-
.withMemory(536870912)
44-
.withMemoryReservation(268435456)
43+
.withHostConfig(HostConfig(memory = Some(536870912), memoryReservation = Some(268435456)))
4544
.withReadyChecker(
4645
DockerReadyChecker
4746
.HttpResponseCode(9200, "/")

core/src/main/scala/com/whisk/docker/DockerContainer.scala

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,19 @@ case class LogLineReceiver(withErr: Boolean, f: String => Unit)
1111
case class DockerPortMapping(hostPort: Option[Int] = None, address: String = "0.0.0.0")
1212

1313
case class HostConfig(
14-
tmpfs: Option[Map[String, String]] = None
14+
15+
tmpfs: Option[Map[String, String]] = None,
16+
17+
/**
18+
* the hard limit on memory usage (in bytes)
19+
*/
20+
memory: Option[Long] = None,
21+
22+
/**
23+
* the soft limit on memory usage (in bytes)
24+
*/
25+
memoryReservation: Option[Long] = None
26+
1527
)
1628

1729
case class DockerContainer(image: String,
@@ -30,8 +42,6 @@ case class DockerContainer(image: String,
3042
logLineReceiver: Option[LogLineReceiver] = None,
3143
user: Option[String] = None,
3244
hostname: Option[String] = None,
33-
memory: Option[Long] = None,
34-
memoryReservation: Option[Long] = None,
3545
hostConfig: Option[HostConfig] = None) {
3646

3747
def withCommand(cmd: String*) = copy(command = Some(cmd))
@@ -65,16 +75,6 @@ case class DockerContainer(image: String,
6575

6676
def withHostname(hostname: String) = copy(hostname = Some(hostname))
6777

68-
/**
69-
* Set the hard limit on memory usage (in bytes)
70-
*/
71-
def withMemory(memory: Long) = copy(memory = Some(memory))
72-
73-
/**
74-
* Set the soft limit on memory usage (in bytes)
75-
*/
76-
def withMemoryReservation(memoryReservation: Long) = copy(memoryReservation = Some(memoryReservation))
77-
7878
def withHostConfig(hostConfig: HostConfig) = copy(hostConfig = Some(hostConfig))
7979

8080
}

impl/docker-java/src/main/scala/com/whisk/docker/impl/dockerjava/DockerJavaExecutor.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ class DockerJavaExecutor(override val host: String, client: DockerClient)
4040
}: _*)
4141
)
4242
.withBinds(new Binds(volumeToBind.map(_._2): _*))
43+
.withOption(spec.hostConfig.flatMap(_.memory)) {
44+
case (config, memory) => config.withMemory(memory)
45+
}
46+
.withOption(spec.hostConfig.flatMap(_.memoryReservation)) {
47+
case (config, memoryReservation) => config.withMemoryReservation(memoryReservation)
48+
}
4349

4450
val cmd = client
4551
.createContainerCmd(spec.image)
@@ -61,10 +67,6 @@ class DockerJavaExecutor(override val host: String, client: DockerClient)
6167
.withOption(spec.name) { case (config, name) => config.withName(name) }
6268
.withOption(spec.command) { case (config, c) => config.withCmd(c: _*) }
6369
.withOption(spec.entrypoint) { case (config, entrypoint) => config.withEntrypoint(entrypoint: _*) }
64-
.withOption(spec.memory) { case (config, memory) => config.withMemory(memory) }
65-
.withOption(spec.memoryReservation) { case (config, memoryReservation) =>
66-
config.withHostConfig(config.getHostConfig.withMemoryReservation(memoryReservation))
67-
}
6870

6971
Future(cmd.exec()).map { resp =>
7072
if (resp.getId != null && resp.getId != "") {

impl/spotify/src/main/scala/com/whisk/docker/impl/spotify/SpotifyDockerCommandExecutor.scala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,18 @@ class SpotifyDockerCommandExecutor(override val host: String, client: DockerClie
4848
val hostConfigBuilder =
4949
if (links.isEmpty) hostConfigBase else hostConfigBase.links(links.asJava)
5050
hostConfigBuilder
51+
.withOption(spec.networkMode) {
52+
case (config, networkMode) => config.networkMode(networkMode)
53+
}
5154
.withOption(spec.hostConfig.flatMap(_.tmpfs)) {
5255
case (config, value) => config.tmpfs(value.asJava)
5356
}
54-
.withOption(spec.networkMode) { case (config, networkMode) => config.networkMode(networkMode) }
55-
.withOption(spec.memory) { case (config, memory) => config.memory(memory) }
56-
.withOption(spec.memoryReservation) { case (config, reservation) => config.memoryReservation(reservation) }
57+
.withOption(spec.hostConfig.flatMap(_.memory)) {
58+
case (config, memory) => config.memory(memory)
59+
}
60+
.withOption(spec.hostConfig.flatMap(_.memoryReservation)) {
61+
case (config, reservation) => config.memoryReservation(reservation)
62+
}
5763
.build()
5864
}
5965

0 commit comments

Comments
 (0)