Skip to content

Commit 31a6569

Browse files
author
Marcus Linke
committed
Merge branch 'cdancy-master't push origin master
2 parents 959f7ce + d2c942d commit 31a6569

File tree

5 files changed

+163
-4
lines changed

5 files changed

+163
-4
lines changed

src/main/java/com/github/dockerjava/api/command/CreateContainerCmd.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.github.dockerjava.api.model.Device;
1010
import com.github.dockerjava.api.model.ExposedPort;
1111
import com.github.dockerjava.api.model.Link;
12+
import com.github.dockerjava.api.model.LogConfig;
1213
import com.github.dockerjava.api.model.LxcConf;
1314
import com.github.dockerjava.api.model.PortBinding;
1415
import com.github.dockerjava.api.model.Ports;
@@ -87,6 +88,8 @@ public static interface Exec extends DockerCmdSyncExec<CreateContainerCmd, Creat
8788

8889
public LxcConf[] getLxcConf();
8990

91+
public LogConfig getLogConfig();
92+
9093
public String getMacAddress();
9194

9295
public long getMemoryLimit();
@@ -222,6 +225,8 @@ public static interface Exec extends DockerCmdSyncExec<CreateContainerCmd, Creat
222225

223226
public CreateContainerCmd withLxcConf(LxcConf... lxcConf);
224227

228+
public CreateContainerCmd withLogConfig(LogConfig logConfig);
229+
225230
public CreateContainerCmd withMemoryLimit(long memoryLimit);
226231

227232
public CreateContainerCmd withMemorySwap(long memorySwap);

src/main/java/com/github/dockerjava/api/model/HostConfig.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public class HostConfig {
1818
@JsonProperty("LxcConf")
1919
private LxcConf[] lxcConf;
2020

21+
@JsonProperty("LogConfig")
22+
private LogConfig logConfig;
23+
2124
@JsonProperty("PortBindings")
2225
private Ports portBindings;
2326

@@ -66,13 +69,14 @@ public class HostConfig {
6669
public HostConfig() {
6770
}
6871

69-
public HostConfig(Bind[] binds, Link[] links, LxcConf[] lxcConf, Ports portBindings, boolean publishAllPorts,
70-
boolean privileged, boolean readonlyRootfs, String[] dns, String[] dnsSearch, VolumesFrom[] volumesFrom,
71-
String containerIDFile, Capability[] capAdd, Capability[] capDrop, RestartPolicy restartPolicy,
72-
String networkMode, Device[] devices, String[] extraHosts, Ulimit[] ulimits) {
72+
public HostConfig(Bind[] binds, Link[] links, LxcConf[] lxcConf, LogConfig logConfig, Ports portBindings,
73+
boolean publishAllPorts, boolean privileged, boolean readonlyRootfs, String[] dns, String[] dnsSearch,
74+
VolumesFrom[] volumesFrom, String containerIDFile, Capability[] capAdd, Capability[] capDrop,
75+
RestartPolicy restartPolicy, String networkMode, Device[] devices, String[] extraHosts, Ulimit[] ulimits) {
7376
this.binds = new Binds(binds);
7477
this.links = new Links(links);
7578
this.lxcConf = lxcConf;
79+
this.logConfig = logConfig;
7680
this.portBindings = portBindings;
7781
this.publishAllPorts = publishAllPorts;
7882
this.privileged = privileged;
@@ -99,6 +103,11 @@ public LxcConf[] getLxcConf() {
99103
return lxcConf;
100104
}
101105

106+
@JsonIgnore
107+
public LogConfig getLogConfig() {
108+
return (logConfig == null) ? new LogConfig() : logConfig;
109+
}
110+
102111
public Ports getPortBindings() {
103112
return portBindings;
104113
}
@@ -178,6 +187,11 @@ public void setLxcConf(LxcConf[] lxcConf) {
178187
this.lxcConf = lxcConf;
179188
}
180189

190+
@JsonIgnore
191+
public void setLogConfig(LogConfig logConfig) {
192+
this.logConfig = logConfig;
193+
}
194+
181195
public void setPortBindings(Ports portBindings) {
182196
this.portBindings = portBindings;
183197
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.github.dockerjava.api.model;
2+
3+
import java.io.IOException;
4+
import java.util.Map;
5+
6+
import com.fasterxml.jackson.annotation.JsonIgnore;
7+
import com.fasterxml.jackson.annotation.JsonProperty;
8+
import com.fasterxml.jackson.core.JsonGenerator;
9+
import com.fasterxml.jackson.core.JsonParser;
10+
import com.fasterxml.jackson.core.JsonProcessingException;
11+
import com.fasterxml.jackson.core.ObjectCodec;
12+
import com.fasterxml.jackson.databind.DeserializationContext;
13+
import com.fasterxml.jackson.databind.JsonDeserializer;
14+
import com.fasterxml.jackson.databind.JsonNode;
15+
import com.fasterxml.jackson.databind.JsonSerializer;
16+
import com.fasterxml.jackson.databind.SerializerProvider;
17+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
18+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
19+
20+
/**
21+
* Log driver to use for a created/running container. The available types are:
22+
*
23+
* json-file (default) syslog journald none
24+
*
25+
* If a driver is specified that is NOT supported,docker will default to null. If configs are supplied that are not
26+
* supported by the type docker will ignore them. In most cases setting the config option to null will suffice. Consult
27+
* the docker remote API for a more detailed and up-to-date explanation of the available types and their options.
28+
*/
29+
public class LogConfig {
30+
31+
@JsonProperty("Type")
32+
public LoggingType type = null;
33+
34+
@JsonProperty("Config")
35+
public Map<String, String> config;
36+
37+
public LogConfig(LoggingType type, Map<String, String> config) {
38+
this.type = type;
39+
this.config = config;
40+
}
41+
42+
public LogConfig(LoggingType type) {
43+
this(type, null);
44+
}
45+
46+
public LogConfig() {
47+
}
48+
49+
public LoggingType getType() {
50+
return type;
51+
}
52+
53+
public LogConfig setType(LoggingType type) {
54+
this.type = type;
55+
return this;
56+
}
57+
58+
@JsonIgnore
59+
public Map<String, String> getConfig() {
60+
return config;
61+
}
62+
63+
@JsonIgnore
64+
public LogConfig setConfig(Map<String, String> config) {
65+
this.config = config;
66+
return this;
67+
}
68+
69+
@JsonDeserialize(using = LoggingType.Deserializer.class)
70+
@JsonSerialize(using = LoggingType.Serializer.class)
71+
public static enum LoggingType {
72+
DEFAULT("json-file"), JSON_FILE("json-file"), NONE("none"), SYSLOG("syslog"), JOURNALD("journald");
73+
74+
private String type;
75+
76+
private LoggingType(String type) {
77+
this.type = type;
78+
}
79+
80+
public String getType() {
81+
return type;
82+
}
83+
84+
public static final class Serializer extends JsonSerializer<LoggingType> {
85+
@Override
86+
public void serialize(LoggingType value, JsonGenerator jgen, SerializerProvider provider)
87+
throws IOException, JsonProcessingException {
88+
jgen.writeString(value.getType());
89+
}
90+
}
91+
92+
public static final class Deserializer extends JsonDeserializer<LoggingType> {
93+
@Override
94+
public LoggingType deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
95+
throws IOException, JsonProcessingException {
96+
97+
ObjectCodec oc = jsonParser.getCodec();
98+
JsonNode node = oc.readTree(jsonParser);
99+
100+
for (LoggingType loggingType : values()) {
101+
if (loggingType.getType().equals(node.asText()))
102+
return loggingType;
103+
}
104+
105+
throw new IllegalArgumentException("No enum constant " + LoggingType.class + "." + node.asText());
106+
}
107+
}
108+
}
109+
}

src/main/java/com/github/dockerjava/core/command/CreateContainerCmdImpl.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.github.dockerjava.api.model.ExposedPorts;
2020
import com.github.dockerjava.api.model.HostConfig;
2121
import com.github.dockerjava.api.model.Link;
22+
import com.github.dockerjava.api.model.LogConfig;
2223
import com.github.dockerjava.api.model.LxcConf;
2324
import com.github.dockerjava.api.model.PortBinding;
2425
import com.github.dockerjava.api.model.Ports;
@@ -265,6 +266,12 @@ public LxcConf[] getLxcConf() {
265266
return hostConfig.getLxcConf();
266267
}
267268

269+
@Override
270+
@JsonIgnore
271+
public LogConfig getLogConfig() {
272+
return hostConfig.getLogConfig();
273+
}
274+
268275
public String getMacAddress() {
269276
return macAddress;
270277
}
@@ -574,6 +581,13 @@ public CreateContainerCmd withLxcConf(LxcConf... lxcConf) {
574581
return this;
575582
}
576583

584+
@Override
585+
public CreateContainerCmd withLogConfig(LogConfig logConfig) {
586+
checkNotNull(logConfig, "logConfig was not specified");
587+
this.hostConfig.setLogConfig(logConfig);
588+
return this;
589+
}
590+
577591
@Override
578592
public CreateContainerCmdImpl withMacAddress(String macAddress) {
579593
checkNotNull(macAddress, "macAddress was not specified");

src/test/java/com/github/dockerjava/core/command/CreateContainerCmdImplTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.github.dockerjava.api.model.ExposedPort;
3939
import com.github.dockerjava.api.model.HostConfig;
4040
import com.github.dockerjava.api.model.Link;
41+
import com.github.dockerjava.api.model.LogConfig;
4142
import com.github.dockerjava.api.model.Ports;
4243
import com.github.dockerjava.api.model.RestartPolicy;
4344
import com.github.dockerjava.api.model.Ulimit;
@@ -526,4 +527,20 @@ public void createContainerWithLabels() throws DockerException {
526527
labels.put("com.github.dockerjava.null", "");
527528
assertThat(inspectContainerResponse.getConfig().getLabels(), is(equalTo(labels)));
528529
}
530+
531+
@Test(groups = "ignoreInCircleCi")
532+
public void createContainerWithLogConfig() throws DockerException {
533+
534+
LogConfig logConfig = new LogConfig(LogConfig.LoggingType.NONE, null);
535+
CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withLogConfig(logConfig).exec();
536+
537+
LOG.info("Created container {}", container.toString());
538+
539+
assertThat(container.getId(), not(isEmptyString()));
540+
541+
InspectContainerResponse inspectContainerResponse = dockerClient.inspectContainerCmd(container.getId()).exec();
542+
543+
// null becomes empty string
544+
assertEquals(inspectContainerResponse.getHostConfig().getLogConfig().type, logConfig.type);
545+
}
529546
}

0 commit comments

Comments
 (0)