Skip to content

Commit 13f1bab

Browse files
authored
Merge pull request docker-java#997 from fengxx/feature/tmpfs
add tmpfs mount support since v1.29
2 parents cdd0a71 + da8d677 commit 13f1bab

File tree

4 files changed

+129
-3
lines changed

4 files changed

+129
-3
lines changed

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

+31
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public class Mount implements Serializable {
5252
@JsonProperty("VolumeOptions")
5353
private VolumeOptions volumeOptions;
5454

55+
/**
56+
* @since 1.29
57+
*/
58+
@JsonProperty("TmpfsOptions")
59+
private TmpfsOptions tmpfsOptions;
60+
5561
/**
5662
* @see #type
5763
*/
@@ -129,6 +135,9 @@ public BindOptions getBindOptions() {
129135
*/
130136
public Mount withBindOptions(BindOptions bindOptions) {
131137
this.bindOptions = bindOptions;
138+
if (bindOptions != null) {
139+
this.type = MountType.BIND;
140+
}
132141
return this;
133142
}
134143

@@ -145,6 +154,28 @@ public VolumeOptions getVolumeOptions() {
145154
*/
146155
public Mount withVolumeOptions(VolumeOptions volumeOptions) {
147156
this.volumeOptions = volumeOptions;
157+
if (volumeOptions != null) {
158+
this.type = MountType.VOLUME;
159+
}
160+
return this;
161+
}
162+
163+
/**
164+
* @see #tmpfsOptions
165+
*/
166+
@CheckForNull
167+
public TmpfsOptions getTmpfsOptions() {
168+
return tmpfsOptions;
169+
}
170+
171+
/**
172+
* @see #tmpfsOptions
173+
*/
174+
public Mount withTmpfsOptions(TmpfsOptions tmpfsOptions) {
175+
this.tmpfsOptions = tmpfsOptions;
176+
if (tmpfsOptions != null) {
177+
this.type = MountType.TMPFS;
178+
}
148179
return this;
149180
}
150181

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,10 @@ public enum MountType {
1111
BIND,
1212

1313
@JsonProperty("volume")
14-
VOLUME
14+
VOLUME,
15+
16+
//@since 1.29
17+
@JsonProperty("tmpfs")
18+
TMPFS
19+
1520
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.github.dockerjava.api.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import com.github.dockerjava.core.RemoteApiVersion;
7+
import org.apache.commons.lang.builder.EqualsBuilder;
8+
import org.apache.commons.lang.builder.HashCodeBuilder;
9+
import org.apache.commons.lang.builder.ToStringBuilder;
10+
import org.apache.commons.lang.builder.ToStringStyle;
11+
12+
import java.io.Serializable;
13+
14+
/**
15+
* @since {@link RemoteApiVersion#VERSION_1_29}
16+
*/
17+
@JsonIgnoreProperties(ignoreUnknown = true)
18+
@JsonInclude(JsonInclude.Include.NON_NULL)
19+
public class TmpfsOptions implements Serializable {
20+
private static final long serialVersionUID = 1L;
21+
@JsonProperty("SizeBytes")
22+
//The size for the tmpfs mount in bytes.
23+
private Long sizeBytes;
24+
25+
//The permission mode for the tmpfs mount in an integer.
26+
@JsonProperty("Mode")
27+
private Integer mode;
28+
29+
public Long getSizeBytes() {
30+
return sizeBytes;
31+
}
32+
33+
public TmpfsOptions withSizeBytes(Long sizeBytes) {
34+
this.sizeBytes = sizeBytes;
35+
return this;
36+
}
37+
38+
public Integer getMode() {
39+
return mode;
40+
}
41+
42+
public TmpfsOptions withMode(Integer mode) {
43+
this.mode = mode;
44+
return this;
45+
}
46+
47+
@Override
48+
public String toString() {
49+
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
50+
}
51+
52+
@Override
53+
public boolean equals(Object o) {
54+
return EqualsBuilder.reflectionEquals(this, o);
55+
}
56+
57+
@Override
58+
public int hashCode() {
59+
return HashCodeBuilder.reflectionHashCode(this);
60+
}
61+
}

src/test/java/com/github/dockerjava/cmd/swarm/CreateServiceCmdExecIT.java

+31-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.github.dockerjava.api.model.ContainerSpec;
55
import com.github.dockerjava.api.model.EndpointResolutionMode;
66
import com.github.dockerjava.api.model.EndpointSpec;
7+
import com.github.dockerjava.api.model.Mount;
8+
import com.github.dockerjava.api.model.MountType;
79
import com.github.dockerjava.api.model.Network;
810
import com.github.dockerjava.api.model.NetworkAttachmentConfig;
911
import com.github.dockerjava.api.model.PortConfig;
@@ -14,12 +16,14 @@
1416
import com.github.dockerjava.api.model.ServiceSpec;
1517
import com.github.dockerjava.api.model.SwarmSpec;
1618
import com.github.dockerjava.api.model.TaskSpec;
19+
import com.github.dockerjava.api.model.TmpfsOptions;
1720
import com.google.common.collect.ImmutableMap;
1821
import com.google.common.collect.Lists;
1922
import org.junit.Test;
2023
import org.slf4j.Logger;
2124
import org.slf4j.LoggerFactory;
2225

26+
import java.util.Collections;
2327
import java.util.List;
2428

2529
import static com.github.dockerjava.junit.DockerRule.DEFAULT_IMAGE;
@@ -44,7 +48,7 @@ public void testCreateService() throws DockerException {
4448
.withTaskTemplate(new TaskSpec()
4549
.withContainerSpec(new ContainerSpec()
4650
.withImage(DEFAULT_IMAGE))))
47-
.exec();
51+
.exec();
4852

4953
List<Service> services = dockerRule.getClient().listServicesCmd()
5054
.withNameFilter(Lists.newArrayList(SERVICE_NAME))
@@ -79,7 +83,7 @@ public void testCreateServiceWithNetworks() {
7983
.withTarget(networkId)
8084
.withAliases(Lists.<String>newArrayList("alias1", "alias2"))
8185
))
82-
.withLabels(ImmutableMap.of("com.docker.java.usage","SwarmServiceIT"))
86+
.withLabels(ImmutableMap.of("com.docker.java.usage", "SwarmServiceIT"))
8387
.withMode(new ServiceModeConfig().withReplicated(
8488
new ServiceReplicatedModeOptions()
8589
.withReplicas(1)
@@ -104,4 +108,29 @@ public void testCreateServiceWithNetworks() {
104108
dockerRule.getClient().removeServiceCmd(SERVICE_NAME).exec();
105109
}
106110

111+
@Test
112+
public void testCreateServiceWithTmpfs() {
113+
dockerRule.getClient().initializeSwarmCmd(new SwarmSpec())
114+
.withListenAddr("127.0.0.1")
115+
.withAdvertiseAddr("127.0.0.1")
116+
.exec();
117+
Mount tmpMount = new Mount().withTmpfsOptions(new TmpfsOptions().withSizeBytes(600L)).withTarget("/tmp/foo");
118+
119+
dockerRule.getClient().createServiceCmd(new ServiceSpec()
120+
.withName(SERVICE_NAME)
121+
.withTaskTemplate(new TaskSpec()
122+
.withContainerSpec(new ContainerSpec().withImage(DEFAULT_IMAGE).withMounts(Collections.singletonList(tmpMount)))))
123+
.exec();
124+
125+
List<Service> services = dockerRule.getClient().listServicesCmd()
126+
.withNameFilter(Lists.newArrayList(SERVICE_NAME))
127+
.exec();
128+
129+
assertThat(services, hasSize(1));
130+
List<Mount> mounts = dockerRule.getClient().inspectServiceCmd(SERVICE_NAME).exec().getSpec().getTaskTemplate()
131+
.getContainerSpec().getMounts();
132+
assertThat(mounts, hasSize(1));
133+
assertThat(mounts.get(0), is(tmpMount));
134+
dockerRule.getClient().removeServiceCmd(SERVICE_NAME).exec();
135+
}
107136
}

0 commit comments

Comments
 (0)