Skip to content

Commit a3be9a5

Browse files
Lubos.PalisekKostyaSha
authored andcommitted
Added healthcheck support in container inspect JSON result
1 parent e8146ac commit a3be9a5

File tree

7 files changed

+326
-3
lines changed

7 files changed

+326
-3
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.github.dockerjava.api.command;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import java.util.List;
6+
7+
@JsonIgnoreProperties(ignoreUnknown = true)
8+
public class HealthState {
9+
10+
@JsonProperty("Status")
11+
private String status;
12+
13+
@JsonProperty("FailingStreak")
14+
private Integer failingStreak;
15+
16+
@JsonProperty("Log")
17+
private List<HealthStateLog> log;
18+
19+
public String getStatus() {
20+
return status;
21+
}
22+
23+
public Integer getFailingStreak() {
24+
return failingStreak;
25+
}
26+
27+
public List<HealthStateLog> getLog() {
28+
return log;
29+
}
30+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.github.dockerjava.api.command;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
6+
@JsonIgnoreProperties(ignoreUnknown = true)
7+
public class HealthStateLog {
8+
9+
@JsonProperty("Start")
10+
private String start;
11+
12+
@JsonProperty("End")
13+
private String end;
14+
15+
@JsonProperty("ExitCode")
16+
private Integer exitCode;
17+
18+
@JsonProperty("Output")
19+
private String output;
20+
21+
public String getStart() {
22+
return start;
23+
}
24+
25+
public String getEnd() {
26+
return end;
27+
}
28+
29+
public Integer getExitCode() {
30+
return exitCode;
31+
}
32+
33+
public String getOutput() {
34+
return output;
35+
}
36+
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public String getId() {
112112
}
113113

114114
public Integer getSizeRootFs() {
115-
return sizeRootFs;
115+
return sizeRootFs;
116116
}
117117

118118
public String getCreated() {
@@ -302,6 +302,13 @@ public class ContainerState {
302302
@JsonProperty("FinishedAt")
303303
private String finishedAt;
304304

305+
306+
/**
307+
* @since Docker version 1.12
308+
*/
309+
@JsonProperty("Health")
310+
private HealthState health;
311+
305312
/**
306313
* See {@link #status}
307314
*/
@@ -390,6 +397,10 @@ public String getFinishedAt() {
390397
return finishedAt;
391398
}
392399

400+
public HealthState getHealth() {
401+
return health;
402+
}
403+
393404
@Override
394405
public boolean equals(Object o) {
395406
return EqualsBuilder.reflectionEquals(this, o);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ public class ContainerConfig implements Serializable {
8686
@JsonProperty("WorkingDir")
8787
private String workingDir;
8888

89+
@JsonProperty("Healthcheck")
90+
private HealthCheck healthCheck;
91+
8992
@JsonIgnore
9093
public ExposedPort[] getExposedPorts() {
9194
return exposedPorts != null ? exposedPorts.getExposedPorts() : null;
@@ -411,6 +414,14 @@ public String getWorkingDir() {
411414
return workingDir;
412415
}
413416

417+
/**
418+
* @see #healthCheck
419+
*/
420+
@CheckForNull
421+
public HealthCheck getHealthcheck() {
422+
return healthCheck;
423+
}
424+
414425
/**
415426
* @see #workingDir
416427
*/
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2017 cdancy.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.dockerjava.api.model;
17+
18+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
19+
import com.fasterxml.jackson.annotation.JsonInclude;
20+
import com.fasterxml.jackson.annotation.JsonProperty;
21+
import java.io.Serializable;
22+
23+
/**
24+
*
25+
* @author cdancy
26+
*/
27+
@JsonIgnoreProperties(ignoreUnknown = true)
28+
@JsonInclude(JsonInclude.Include.NON_NULL)
29+
public class HealthCheck implements Serializable {
30+
private static final long serialVersionUID = 1L;
31+
32+
@JsonProperty("Interval")
33+
private Long interval;
34+
35+
@JsonProperty("Timeout")
36+
private Long timeout;
37+
38+
public Long getInterval() {
39+
return interval;
40+
}
41+
42+
public Long getTimeout() {
43+
return timeout;
44+
}
45+
}

src/test/java/com/github/dockerjava/api/command/InspectContainerResponseTest.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@
1515
*/
1616
package com.github.dockerjava.api.command;
1717

18-
import org.testng.annotations.Test;
19-
18+
import com.fasterxml.jackson.databind.JavaType;
19+
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import com.github.dockerjava.api.model.Event;
21+
import com.github.dockerjava.core.RemoteApiVersion;
22+
import static com.github.dockerjava.test.serdes.JSONSamples.testRoundTrip;
2023
import java.io.IOException;
2124

25+
import org.testng.annotations.Test;
26+
2227
import static com.github.dockerjava.test.serdes.JSONTestHelper.testRoundTrip;
2328
import static org.hamcrest.MatcherAssert.assertThat;
2429
import static org.hamcrest.Matchers.containsString;
@@ -55,6 +60,24 @@ public void roundTrip_full() throws IOException {
5560
assertThat(response.getLogPath(), is("/mnt/sda1/var/lib/docker/containers/469e5edd8d5b33e3c905a7ffc97360ec6ee211d6782815fbcd144568045819e1/469e5edd8d5b33e3c905a7ffc97360ec6ee211d6782815fbcd144568045819e1-json.log"));
5661
}
5762

63+
@Test
64+
public void roundTrip_full_healthcheck() throws IOException {
65+
66+
final ObjectMapper mapper = new ObjectMapper();
67+
final JavaType type = mapper.getTypeFactory().uncheckedSimpleType(InspectContainerResponse.class);
68+
69+
final InspectContainerResponse response = testRoundTrip(RemoteApiVersion.VERSION_1_24,
70+
"/containers/inspect/1.json",
71+
type
72+
);
73+
74+
assertEquals(response.getState().getHealth().getStatus(), "healthy");
75+
assertEquals(response.getState().getHealth().getFailingStreak(), new Integer(0));
76+
assertEquals(response.getState().getHealth().getLog().size(), 2);
77+
assertEquals(response.getState().getHealth().getLog().get(0).getOutput(), "Hello");
78+
assertEquals(response.getState().getHealth().getLog().get(1).getOutput(), "World");
79+
}
80+
5881
@Test
5982
public void roundTrip_1_21_full() throws IOException {
6083
InspectContainerResponse[] responses = testRoundTrip(CommandJSONSamples.inspectContainerResponse_full_1_21,
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
{
2+
"Id": "fc1243c01bbb791d9eca64204d76f72fc47fbebbca892d7dd0a5cd0e4e346045",
3+
"Created": "2015-11-20T21:10:34.775649753Z",
4+
"Path": "cat",
5+
"Args": [],
6+
"State": {
7+
"Status": "running",
8+
"Running": true,
9+
"Paused": false,
10+
"Restarting": false,
11+
"OOMKilled": false,
12+
"Dead": false,
13+
"Pid": 10912,
14+
"ExitCode": 0,
15+
"Error": "",
16+
"StartedAt": "2015-11-20T21:10:34.845546358Z",
17+
"FinishedAt": "0001-01-01T00:00:00Z",
18+
"Health": {
19+
"Status": "healthy",
20+
"FailingStreak": 0,
21+
"Log": [
22+
{
23+
"Start": "2016-11-12T03:23:03.351561Z",
24+
"End": "2016-11-12T03:23:03.422176171Z",
25+
"ExitCode": 0,
26+
"Output" : "Hello"
27+
},
28+
{
29+
"Start": "2016-11-12T03:23:08.423558928Z",
30+
"End": "2016-11-12T03:23:08.510122392Z",
31+
"ExitCode": 0,
32+
"Output" : "World"
33+
}
34+
]
35+
}
36+
},
37+
"Image": "c51f86c283408d1749d066333f7acd5d33b053b003a61ff6a7b36819ddcbc7b7",
38+
"ResolvConfPath": "/mnt/sda1/var/lib/docker/containers/fc1243c01bbb791d9eca64204d76f72fc47fbebbca892d7dd0a5cd0e4e346045/resolv.conf",
39+
"HostnamePath": "/mnt/sda1/var/lib/docker/containers/fc1243c01bbb791d9eca64204d76f72fc47fbebbca892d7dd0a5cd0e4e346045/hostname",
40+
"HostsPath": "/mnt/sda1/var/lib/docker/containers/fc1243c01bbb791d9eca64204d76f72fc47fbebbca892d7dd0a5cd0e4e346045/hosts",
41+
"LogPath": "/mnt/sda1/var/lib/docker/containers/fc1243c01bbb791d9eca64204d76f72fc47fbebbca892d7dd0a5cd0e4e346045/fc1243c01bbb791d9eca64204d76f72fc47fbebbca892d7dd0a5cd0e4e346045-json.log",
42+
"Name": "/small_hodgkin",
43+
"RestartCount": 0,
44+
"Driver": "aufs",
45+
"ExecDriver": "native-0.2",
46+
"MountLabel": "",
47+
"ProcessLabel": "",
48+
"AppArmorProfile": "",
49+
"ExecIDs": null,
50+
"HostConfig": {
51+
"Binds": null,
52+
"ContainerIDFile": "",
53+
"LxcConf": [],
54+
"Memory": 0,
55+
"MemoryReservation": 0,
56+
"MemorySwap": 0,
57+
"KernelMemory": 0,
58+
"CpuShares": 0,
59+
"CpuPeriod": 0,
60+
"CpusetCpus": "",
61+
"CpusetMems": "",
62+
"CpuQuota": 0,
63+
"BlkioWeight": 0,
64+
"OomKillDisable": false,
65+
"MemorySwappiness": -1,
66+
"Privileged": false,
67+
"PortBindings": {},
68+
"Links": null,
69+
"PublishAllPorts": false,
70+
"Dns": null,
71+
"DnsOptions": null,
72+
"DnsSearch": null,
73+
"ExtraHosts": null,
74+
"VolumesFrom": null,
75+
"Devices": [],
76+
"NetworkMode": "default",
77+
"IpcMode": "",
78+
"PidMode": "",
79+
"UTSMode": "",
80+
"CapAdd": null,
81+
"CapDrop": null,
82+
"GroupAdd": null,
83+
"RestartPolicy": {
84+
"Name": "no",
85+
"MaximumRetryCount": 0
86+
},
87+
"SecurityOpt": null,
88+
"ReadonlyRootfs": false,
89+
"Ulimits": null,
90+
"LogConfig": {
91+
"Type": "json-file",
92+
"Config": {}
93+
},
94+
"CgroupParent": "",
95+
"ConsoleSize": [
96+
0,
97+
0
98+
],
99+
"VolumeDriver": ""
100+
},
101+
"GraphDriver": {
102+
"Name": "aufs",
103+
"Data": null
104+
},
105+
"Mounts": [],
106+
"Config": {
107+
"Hostname": "fc1243c01bbb",
108+
"Domainname": "",
109+
"User": "",
110+
"AttachStdin": false,
111+
"AttachStdout": false,
112+
"AttachStderr": false,
113+
"Tty": false,
114+
"OpenStdin": true,
115+
"StdinOnce": false,
116+
"Env": null,
117+
"Cmd": [
118+
"cat"
119+
],
120+
"Healthcheck": {
121+
"Test": [
122+
"CMD-SHELL",
123+
"mysqladmin ping --silent"
124+
],
125+
"Interval": 2000000000,
126+
"Timeout": 3000000000
127+
},
128+
"Image": "busybox",
129+
"Volumes": null,
130+
"WorkingDir": "",
131+
"Entrypoint": null,
132+
"OnBuild": null,
133+
"Labels": {},
134+
"StopSignal": "SIGTERM"
135+
},
136+
"NetworkSettings": {
137+
"Bridge": "",
138+
"SandboxID": "5a6ded01bf23cc180e8ba6a059449ac832f28fa1d8367127e316607f92d3228c",
139+
"HairpinMode": false,
140+
"LinkLocalIPv6Address": "",
141+
"LinkLocalIPv6PrefixLen": 0,
142+
"Ports": {},
143+
"SandboxKey": "/var/run/docker/netns/5a6ded01bf23",
144+
"SecondaryIPAddresses": null,
145+
"SecondaryIPv6Addresses": null,
146+
"EndpointID": "6b4bb2aff9981d6e132cf37ebbfd370069061fab848ae56247b154717a99aba7",
147+
"Gateway": "172.17.0.1",
148+
"GlobalIPv6Address": "",
149+
"GlobalIPv6PrefixLen": 0,
150+
"IPAddress": "172.17.0.2",
151+
"IPPrefixLen": 16,
152+
"IPv6Gateway": "",
153+
"MacAddress": "02:42:ac:11:00:02",
154+
"Networks": {
155+
"bridge": {
156+
"EndpointID": "6b4bb2aff9981d6e132cf37ebbfd370069061fab848ae56247b154717a99aba7",
157+
"Gateway": "172.17.0.1",
158+
"IPAddress": "172.17.0.2",
159+
"IPPrefixLen": 16,
160+
"IPv6Gateway": "",
161+
"GlobalIPv6Address": "",
162+
"GlobalIPv6PrefixLen": 0,
163+
"MacAddress": "02:42:ac:11:00:02"
164+
}
165+
}
166+
}
167+
}

0 commit comments

Comments
 (0)