From 46686cdf76f03c67d847adbdd6e2e28ebd6eebb9 Mon Sep 17 00:00:00 2001 From: GabrielSVinha Date: Sun, 3 Jun 2018 19:17:58 -0300 Subject: [PATCH] Add cpus options for host config --- .../docker/client/messages/HostConfig.java | 8 +++++++ .../client/DefaultDockerClientUnitTest.java | 24 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/main/java/com/spotify/docker/client/messages/HostConfig.java b/src/main/java/com/spotify/docker/client/messages/HostConfig.java index 1c51698cc..77eca13a2 100644 --- a/src/main/java/com/spotify/docker/client/messages/HostConfig.java +++ b/src/main/java/com/spotify/docker/client/messages/HostConfig.java @@ -175,6 +175,10 @@ public abstract class HostConfig { @JsonProperty("CpusetMems") public abstract String cpusetMems(); + @Nullable + @JsonProperty("Cpus") + public abstract Float cpus(); + @Nullable @JsonProperty("CpuQuota") public abstract Long cpuQuota(); @@ -269,6 +273,7 @@ static HostConfig create( @JsonProperty("MemorySwap") final Long memorySwap, @JsonProperty("MemorySwappiness") final Integer memorySwappiness, @JsonProperty("MemoryReservation") final Long memoryReservation, + @JsonProperty("Cpus") final Float cpus, @JsonProperty("NanoCpus") final Long nanoCpus, @JsonProperty("CpuPeriod") final Long cpuPeriod, @JsonProperty("CpuShares") final Long cpuShares, @@ -317,6 +322,7 @@ static HostConfig create( .memorySwap(memorySwap) .memorySwappiness(memorySwappiness) .memoryReservation(memoryReservation) + .cpus(cpus) .nanoCpus(nanoCpus) .cpuPeriod(cpuPeriod) .cpuShares(cpuShares) @@ -566,6 +572,8 @@ private static ImmutableList copyWithoutDuplicates(final List input) { public abstract Builder memoryReservation(Long memoryReservation); + public abstract Builder cpus(Float cpus); + public abstract Builder nanoCpus(Long nanoCpus); public abstract Builder cpuPeriod(Long cpuPeriod); diff --git a/src/test/java/com/spotify/docker/client/DefaultDockerClientUnitTest.java b/src/test/java/com/spotify/docker/client/DefaultDockerClientUnitTest.java index 23867eb5c..3ca9f40f6 100644 --- a/src/test/java/com/spotify/docker/client/DefaultDockerClientUnitTest.java +++ b/src/test/java/com/spotify/docker/client/DefaultDockerClientUnitTest.java @@ -393,6 +393,30 @@ public void testNanoCpus() throws Exception { assertThat(hostConfig.nanoCpus(), is(nanoCpus.longValue())); } + @Test + public void testCpus() throws Exception { + final DefaultDockerClient dockerClient = new DefaultDockerClient(builder); + + final HostConfig hostConfig = HostConfig.builder() + .cpus((float)1.5) + .build(); + final ContainerConfig containerConfig = ContainerConfig.builder() + .hostConfig(hostConfig) + .build(); + + server.enqueue(new MockResponse()); + + dockerClient.createContainer(containerConfig); + + final RecordedRequest recordedRequest = takeRequestImmediately(); + + final JsonNode requestJson = toJson(recordedRequest.getBody()); + final JsonNode cpus = requestJson.get("HostConfig").get("Cpus"); + + assertThat(hostConfig.cpus(), is(cpus.floatValue())); + assertThat(hostConfig.cpus(), is((float)1.5)); + } + @Test public void testInspectNode() throws Exception { final DefaultDockerClient dockerClient = new DefaultDockerClient(builder);