Skip to content

Commit c775539

Browse files
fengxxKostyaSha
authored andcommitted
allow client to update swarm node and lists swarm node (#963)
1 parent e80848b commit c775539

File tree

9 files changed

+102
-11
lines changed

9 files changed

+102
-11
lines changed

src/main/java/com/github/dockerjava/api/DockerClient.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.github.dockerjava.api.command.ListImagesCmd;
3535
import com.github.dockerjava.api.command.ListNetworksCmd;
3636
import com.github.dockerjava.api.command.ListServicesCmd;
37+
import com.github.dockerjava.api.command.ListSwarmNodesCmd;
3738
import com.github.dockerjava.api.command.ListTasksCmd;
3839
import com.github.dockerjava.api.command.ListVolumesCmd;
3940
import com.github.dockerjava.api.command.LoadImageCmd;
@@ -60,6 +61,7 @@
6061
import com.github.dockerjava.api.command.UpdateContainerCmd;
6162
import com.github.dockerjava.api.command.UpdateServiceCmd;
6263
import com.github.dockerjava.api.command.UpdateSwarmCmd;
64+
import com.github.dockerjava.api.command.UpdateSwarmNodeCmd;
6365
import com.github.dockerjava.api.command.VersionCmd;
6466
import com.github.dockerjava.api.command.WaitContainerCmd;
6567
import com.github.dockerjava.api.exception.DockerException;
@@ -309,6 +311,22 @@ public interface DockerClient extends Closeable {
309311
*/
310312
UpdateSwarmCmd updateSwarmCmd(SwarmSpec swarmSpec);
311313

314+
/**
315+
* Updates the swarm node
316+
*
317+
* @return the command
318+
* @since 1.24
319+
*/
320+
UpdateSwarmNodeCmd updateSwarmNodeCmd();
321+
322+
/**
323+
* List nodes in swarm
324+
*
325+
* @return the command
326+
* @since 1.24
327+
*/
328+
ListSwarmNodesCmd listSwarmNodesCmd();
329+
312330
/**
313331
* Command to list all services in a docker swarm. Only applicable if docker runs in swarm mode.
314332
*

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public interface UpdateSwarmNodeCmd extends SyncDockerCmd<Void> {
2323

2424
UpdateSwarmNodeCmd withSwarmNodeSpec(SwarmNodeSpec swarmNodeSpec);
2525

26+
UpdateSwarmNodeCmd withVersion(@Nonnull Integer versionId);
27+
28+
@CheckForNull
29+
Integer getVersion();
30+
2631
interface Exec extends DockerCmdSyncExec<UpdateSwarmNodeCmd, Void> {
2732
}
2833
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ public class SwarmNode implements Serializable {
2727
/**
2828
* @since 1.24
2929
*/
30-
@JsonProperty("Id")
30+
@JsonProperty("ID")
3131
private String id;
3232

3333
/**
3434
* @since 1.24
3535
*/
3636
@JsonProperty("Version")
37-
private String[] version;
37+
private ObjectVersion version;
3838

3939
/**
4040
* @since 1.24
@@ -92,14 +92,14 @@ public SwarmNode withId(String id) {
9292
* @see #version
9393
*/
9494
@CheckForNull
95-
public String[] getVersion() {
95+
public ObjectVersion getVersion() {
9696
return version;
9797
}
9898

9999
/**
100100
* @see #version
101101
*/
102-
public SwarmNode withVersion(String[] version) {
102+
public SwarmNode withVersion(ObjectVersion version) {
103103
this.version = version;
104104
return this;
105105
}

src/main/java/com/github/dockerjava/core/DockerClientImpl.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.github.dockerjava.api.command.ListImagesCmd;
3737
import com.github.dockerjava.api.command.ListNetworksCmd;
3838
import com.github.dockerjava.api.command.ListServicesCmd;
39+
import com.github.dockerjava.api.command.ListSwarmNodesCmd;
3940
import com.github.dockerjava.api.command.ListTasksCmd;
4041
import com.github.dockerjava.api.command.ListVolumesCmd;
4142
import com.github.dockerjava.api.command.LoadImageCmd;
@@ -62,6 +63,7 @@
6263
import com.github.dockerjava.api.command.UpdateContainerCmd;
6364
import com.github.dockerjava.api.command.UpdateServiceCmd;
6465
import com.github.dockerjava.api.command.UpdateSwarmCmd;
66+
import com.github.dockerjava.api.command.UpdateSwarmNodeCmd;
6567
import com.github.dockerjava.api.command.VersionCmd;
6668
import com.github.dockerjava.api.command.WaitContainerCmd;
6769
import com.github.dockerjava.api.model.AuthConfig;
@@ -102,6 +104,7 @@
102104
import com.github.dockerjava.core.command.ListImagesCmdImpl;
103105
import com.github.dockerjava.core.command.ListNetworksCmdImpl;
104106
import com.github.dockerjava.core.command.ListServicesCmdImpl;
107+
import com.github.dockerjava.core.command.ListSwarmNodesCmdImpl;
105108
import com.github.dockerjava.core.command.ListTasksCmdImpl;
106109
import com.github.dockerjava.core.command.ListVolumesCmdImpl;
107110
import com.github.dockerjava.core.command.LoadImageCmdImpl;
@@ -128,6 +131,7 @@
128131
import com.github.dockerjava.core.command.UpdateContainerCmdImpl;
129132
import com.github.dockerjava.core.command.UpdateServiceCmdImpl;
130133
import com.github.dockerjava.core.command.UpdateSwarmCmdImpl;
134+
import com.github.dockerjava.core.command.UpdateSwarmNodeCmdImpl;
131135
import com.github.dockerjava.core.command.VersionCmdImpl;
132136
import com.github.dockerjava.core.command.WaitContainerCmdImpl;
133137

@@ -528,6 +532,16 @@ public UpdateSwarmCmd updateSwarmCmd(SwarmSpec swarmSpec) {
528532
return new UpdateSwarmCmdImpl(getDockerCmdExecFactory().createUpdateSwarmCmdExec(), swarmSpec);
529533
}
530534

535+
@Override
536+
public UpdateSwarmNodeCmd updateSwarmNodeCmd() {
537+
return new UpdateSwarmNodeCmdImpl(getDockerCmdExecFactory().updateSwarmNodeCmdExec());
538+
}
539+
540+
@Override
541+
public ListSwarmNodesCmd listSwarmNodesCmd() {
542+
return new ListSwarmNodesCmdImpl(getDockerCmdExecFactory().listSwarmNodeCmdExec());
543+
}
544+
531545
@Override public ListServicesCmd listServicesCmd() {
532546
return new ListServicesCmdImpl(getDockerCmdExecFactory().createListServicesCmdExec());
533547
}

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import javax.annotation.CheckForNull;
1313
import javax.annotation.Nonnull;
1414

15-
import static com.google.common.base.Preconditions.checkNotNull;
16-
1715
/**
1816
* Update swarmNode spec
1917
*
@@ -26,6 +24,12 @@ public class UpdateSwarmNodeCmdImpl extends AbstrDockerCmd<UpdateSwarmNodeCmd, V
2624

2725
private SwarmNodeSpec swarmNodeSpec;
2826

27+
private Integer version;
28+
29+
public UpdateSwarmNodeCmdImpl(Exec exec) {
30+
super(exec);
31+
}
32+
2933
public UpdateSwarmNodeCmdImpl(Exec exec, String swarmNodeId, SwarmNodeSpec swarmNodeSpec) {
3034
super(exec);
3135
withSwarmNodeId(swarmNodeId);
@@ -60,12 +64,22 @@ public SwarmNodeSpec getSwarmNodeSpec() {
6064
* @see #swarmNodeSpec
6165
*/
6266
public UpdateSwarmNodeCmd withSwarmNodeSpec(SwarmNodeSpec swarmNodeSpec) {
63-
checkNotNull(swarmNodeSpec.getAvailability(), "Availability in SwarmNodeSpec was not specified");
64-
checkNotNull(swarmNodeSpec.getRole(), "Role in SwarmNodeSpec was not specified");
6567
this.swarmNodeSpec = swarmNodeSpec;
6668
return this;
6769
}
6870

71+
@Override
72+
public UpdateSwarmNodeCmd withVersion(@Nonnull Integer versionId) {
73+
this.version = versionId;
74+
return this;
75+
}
76+
77+
@CheckForNull
78+
@Override
79+
public Integer getVersion() {
80+
return version;
81+
}
82+
6983
/**
7084
* @throws NotFoundException No such swarmNode
7185
*/

src/main/java/com/github/dockerjava/jaxrs/UpdateSwarmNodeCmdExec.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public UpdateSwarmNodeCmdExec(WebTarget baseResource, DockerClientConfig dockerC
2525
@Override
2626
protected Void execute(UpdateSwarmNodeCmd command) {
2727
WebTarget webResource = getBaseResource().path("/nodes/{id}/update")
28-
.resolveTemplate("id", command.getSwarmNodeId());
28+
.resolveTemplate("id", command.getSwarmNodeId())
29+
.queryParam("version", command.getVersion());
2930

3031
LOGGER.trace("POST: {}", webResource);
3132
webResource.request().accept(MediaType.APPLICATION_JSON)

src/main/java/com/github/dockerjava/netty/exec/UpdateSwarmNodeCmdExec.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public UpdateSwarmNodeCmdExec(WebTarget baseResource, DockerClientConfig dockerC
2323
@Override
2424
protected Void execute(UpdateSwarmNodeCmd command) {
2525
WebTarget webResource = getBaseResource().path("/nodes/{id}/update")
26-
.resolveTemplate("id", command.getSwarmNodeId());
26+
.resolveTemplate("id", command.getSwarmNodeId())
27+
.queryParam("version", command.getVersion());
2728

2829
LOGGER.trace("POST: {}", webResource);
2930
webResource.request().accept(MediaType.APPLICATION_JSON)

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.github.dockerjava.api.DockerClient;
44
import com.github.dockerjava.api.command.CreateContainerResponse;
55
import com.github.dockerjava.api.exception.ConflictException;
6+
import com.github.dockerjava.api.exception.DockerException;
67
import com.github.dockerjava.api.exception.NotAcceptableException;
78
import com.github.dockerjava.api.exception.NotFoundException;
89
import com.github.dockerjava.api.model.ExposedPort;
@@ -56,7 +57,7 @@ public void afterMethod() {
5657
protected void removeDockersInDocker() {
5758
for (int i = 1; i <= numberOfDockersInDocker; i++) {
5859
try {
59-
dockerRule.getClient().removeContainerCmd(DOCKER_IN_DOCKER_CONTAINER_PREFIX + i);
60+
dockerRule.getClient().removeContainerCmd(DOCKER_IN_DOCKER_CONTAINER_PREFIX + i).withForce(true).exec();
6061
} catch (NotFoundException e) {
6162
// container does not exist
6263
}
@@ -129,6 +130,10 @@ private void leaveIfInSwarm() {
129130
dockerRule.getClient().leaveSwarmCmd().withForceEnabled(true).exec();
130131
} catch (NotAcceptableException e) {
131132
// do nothing, node is not part of a swarm
133+
} catch (DockerException ex) {
134+
if (!ex.getMessage().contains("node is not part of a swarm")) {
135+
throw ex;
136+
}
132137
}
133138
}
134139
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.github.dockerjava.cmd.swarm;
2+
3+
import com.github.dockerjava.api.DockerClient;
4+
import com.github.dockerjava.api.model.SwarmNode;
5+
import com.github.dockerjava.api.model.SwarmNodeAvailability;
6+
import com.github.dockerjava.api.model.SwarmNodeSpec;
7+
import com.github.dockerjava.api.model.SwarmNodeState;
8+
import com.github.dockerjava.api.model.SwarmSpec;
9+
import org.junit.Test;
10+
11+
import java.util.List;
12+
13+
import static org.hamcrest.MatcherAssert.assertThat;
14+
import static org.hamcrest.Matchers.is;
15+
16+
public class UpdateSwarmNodeIT extends SwarmCmdIT {
17+
@Test
18+
public void testUpdateSwarmNode() {
19+
DockerClient docker1 = startDockerInDocker();
20+
docker1.initializeSwarmCmd(new SwarmSpec()).exec();
21+
List<SwarmNode> nodes = docker1.listSwarmNodesCmd().exec();
22+
assertThat(1, is(nodes.size()));
23+
SwarmNode node = nodes.get(0);
24+
assertThat(SwarmNodeState.READY, is(node.getStatus().getState()));
25+
//update the node availability
26+
SwarmNodeSpec nodeSpec = node.getSpec().withAvailability(SwarmNodeAvailability.PAUSE);
27+
docker1.updateSwarmNodeCmd().withSwarmNodeId(node.getId()).withVersion(node.getVersion().getIndex())
28+
.withSwarmNodeSpec(nodeSpec).exec();
29+
nodes = docker1.listSwarmNodesCmd().exec();
30+
assertThat(1, is(nodes.size()));
31+
assertThat(SwarmNodeAvailability.PAUSE, is(nodes.get(0).getSpec().getAvailability()));
32+
}
33+
}

0 commit comments

Comments
 (0)