Skip to content
This repository was archived by the owner on Mar 21, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private static String createPattern(String line) {
if (pattern.startsWith("#")) {
return null;
}
if (OSUtils.isLinux()) {
if (OSUtils.isLinux() || OSUtils.isOsX()) {
return pattern;
}
return pattern.replace("/", "\\\\");
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/spotify/docker/client/OSUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
*/
package com.spotify.docker.client;

import java.io.File;
import java.util.Locale;

public class OSUtils {

public static boolean isOsX() {
return System.getProperty("os.name").toLowerCase(Locale.US).contains("os x");
}

public static boolean isLinux() {
return File.separatorChar == '/';
return System.getProperty("os.name").equalsIgnoreCase("linux");
}
}
28 changes: 26 additions & 2 deletions src/main/java/com/spotify/docker/client/messages/HostConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public class HostConfig {
private Integer oomScoreAdj;
@JsonProperty("AutoRemove")
private Boolean autoRemove;
@JsonProperty("PidsLimit")
private Integer pidsLimit;

private HostConfig() {
}
Expand Down Expand Up @@ -138,6 +140,7 @@ private HostConfig(final Builder builder) {
this.oomKillDisable = builder.oomKillDisable;
this.oomScoreAdj = builder.oomScoreAdj;
this.autoRemove = builder.autoRemove;
this.pidsLimit = builder.pidsLimit;
}

public List<String> binds() {
Expand Down Expand Up @@ -260,6 +263,14 @@ public Integer oomScoreAdj() {
return oomScoreAdj;
}

/**
* Tune container pids limit (set -1 for unlimited).
* Only works for kernels >= 4.3
*/
public Integer pidsLimit() {
return pidsLimit;
}

public Boolean autoRemove() {
return autoRemove;
}
Expand Down Expand Up @@ -304,7 +315,8 @@ public boolean equals(final Object o) {
Objects.equals(this.ulimits, that.ulimits) &&
Objects.equals(this.oomKillDisable, that.oomKillDisable) &&
Objects.equals(this.oomScoreAdj, that.oomScoreAdj) &&
Objects.equals(this.autoRemove, that.autoRemove);
Objects.equals(this.autoRemove, that.autoRemove) &&
Objects.equals(this.pidsLimit, that.pidsLimit);
}

@Override
Expand All @@ -314,7 +326,7 @@ public int hashCode() {
capDrop, networkMode, securityOpt, devices, memory, memorySwap,
memoryReservation, cpuShares, cpusetCpus, cpuQuota, cgroupParent,
restartPolicy, logConfig, ipcMode, ulimits, pidMode, shmSize,
oomKillDisable, oomScoreAdj, autoRemove);
oomKillDisable, oomScoreAdj, autoRemove, pidsLimit);
}

@Override
Expand Down Expand Up @@ -352,6 +364,7 @@ public String toString() {
.add("oomKillDisable", oomKillDisable)
.add("oomScoreAdj", oomScoreAdj)
.add("autoRemove", autoRemove)
.add("pidsLimit", pidsLimit)
.toString();
}

Expand Down Expand Up @@ -511,6 +524,7 @@ public static class Builder {
private Boolean oomKillDisable;
private Integer oomScoreAdj;
private Boolean autoRemove;
private Integer pidsLimit;

private Builder() {
}
Expand Down Expand Up @@ -548,6 +562,7 @@ private Builder(final HostConfig hostConfig) {
this.oomKillDisable = hostConfig.oomKillDisable;
this.oomScoreAdj = hostConfig.oomScoreAdj;
this.autoRemove = hostConfig.autoRemove;
this.pidsLimit = hostConfig.pidsLimit;
}

/**
Expand Down Expand Up @@ -1065,6 +1080,15 @@ public Builder autoRemove(final Boolean autoRemove) {
return this;
}

public Builder pidsLimit(final Integer pidsLimit) {
this.pidsLimit = pidsLimit;
return this;
}

public Integer pidsLimit() {
return pidsLimit;
}

public HostConfig build() {
return new HostConfig(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,11 @@ private void requireDockerApiVersionAtLeast(final String required, final String
assumeTrue(msg, dockerApiVersionAtLeast(required));
}

private boolean dockerApiVersionAtLeast(String expected) throws Exception {
private boolean dockerApiVersionAtLeast(final String expected) throws Exception {
return compareVersion(dockerApiVersion, expected) >= 0;
}

private boolean dockerApiVersionLessThan(String expected) throws Exception {
private boolean dockerApiVersionLessThan(final String expected) throws Exception {
return compareVersion(dockerApiVersion, expected) < 0;
}

Expand Down Expand Up @@ -3357,6 +3357,30 @@ public void testOomScoreAdj() throws Exception {

assertThat(info.hostConfig().oomScoreAdj(), is(500));
}

@Test
public void testPidsLimit() throws Exception {
if (OSUtils.isLinux()) {
assumeTrue("Linux kernel must be at least 4.3.",
compareVersion(System.getProperty("os.version"), "4.3") >= 0);
}
requireDockerApiVersionAtLeast("1.23", "PidsLimit");

// Pull image
sut.pull(BUSYBOX_LATEST);

final ContainerConfig config = ContainerConfig.builder()
.image(BUSYBOX_LATEST)
.hostConfig(HostConfig.builder()
.pidsLimit(100) // Defaults to -1
.build())
.build();

final ContainerCreation container = sut.createContainer(config, randomName());
final ContainerInfo info = sut.inspectContainer(container.id());

assertThat(info.hostConfig().pidsLimit(), is(100));
}

@Test(expected = ContainerNotFoundException.class)
public void testAutoRemoveWhenSetToTrue() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public void testFromDockerConfig_MultiConfig() throws Exception {
}

private static Path getTestFilePath(final String path) {
if (OSUtils.isLinux()) {
if (OSUtils.isLinux() || OSUtils.isOsX()) {
return getLinuxPath(path);
} else {
return getWindowsPath(path);
Expand Down