Skip to content

Commit

Permalink
Add ForceUpdate option TaskSpec (fixes spotify#1065)
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Rey committed Jan 18, 2019
1 parent 6580ae8 commit 0d38cf2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public abstract class TaskSpec {
@JsonProperty("LogDriver")
public abstract Driver logDriver();

@Nullable
@JsonProperty("ForceUpdate")
public abstract Integer forceUpdate();

@AutoValue.Builder
public abstract static class Builder {

Expand Down Expand Up @@ -144,6 +148,8 @@ public Builder withLogDriver(final Driver logDriver) {
return this;
}

public abstract Builder forceUpdate(Integer forceUpdate);

public abstract TaskSpec build();
}

Expand All @@ -158,14 +164,16 @@ static TaskSpec create(
@JsonProperty("RestartPolicy") final RestartPolicy restartPolicy,
@JsonProperty("Placement") final Placement placement,
@JsonProperty("Networks") final List<NetworkAttachmentConfig> networks,
@JsonProperty("LogDriver") final Driver logDriver) {
@JsonProperty("LogDriver") final Driver logDriver,
@JsonProperty("ForceUpdate") final Integer forceUpdate) {
return builder()
.containerSpec(containerSpec)
.resources(resources)
.restartPolicy(restartPolicy)
.placement(placement)
.logDriver(logDriver)
.networks(networks)
.forceUpdate(forceUpdate)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@
import com.spotify.docker.client.messages.swarm.Swarm;
import com.spotify.docker.client.messages.swarm.SwarmInit;
import com.spotify.docker.client.messages.swarm.SwarmSpec;
import com.spotify.docker.client.messages.swarm.Task;
import com.spotify.docker.client.messages.swarm.TaskDefaults;
import com.spotify.docker.client.messages.swarm.TaskSpec;
import com.spotify.docker.client.messages.swarm.UnlockKey;
Expand Down Expand Up @@ -247,7 +246,6 @@
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -498,7 +496,7 @@ public void testPullBadImage() throws Exception {
public void testPullPrivateRepoWithoutAuth() throws Exception {
sut.pull(CIRROS_PRIVATE_LATEST);
}

private static Path getResource(String name) throws URISyntaxException {
// Resources.getResources(...).getPath() does not work correctly on windows,
// hence this workaround. See: https://github.com/spotify/docker-client/pull/780
Expand Down Expand Up @@ -5401,23 +5399,23 @@ public void testListServicesFilterByName() throws Exception {
assertThat(services.size(), is(1));
assertThat(services.get(0).spec().name(), is(serviceName));
}

@Test
public void testListServicesFilterByLabel() throws Exception {
requireDockerApiVersionAtLeast("1.24", "swarm support");
final String serviceName = randomName();

Map<String, String> labels = new HashMap<>();
labels.put("foo", "bar");

final ServiceSpec spec = createServiceSpec(serviceName, labels);
sut.createService(spec);

final List<Service> services = sut.listServices(Service.find().addLabel("foo", "bar").build());

assertThat(services.size(), is(1));
assertThat(services.get(0).spec().labels().get("foo"), is("bar"));

final List<Service> notFoundServices = sut.listServices(Service.find()
.addLabel("bar", "foo").build());
assertThat(notFoundServices.size(), is(0));
Expand Down Expand Up @@ -5552,6 +5550,48 @@ public void testMountTmpfsOptions() throws Exception {
}
}

@Test
public void testUpdateServiceWithForceUpdate() throws Exception {
requireDockerApiVersionAtLeast("1.25", "swarm support");
final ServiceSpec spec = createServiceSpec(randomName());

final ServiceCreateResponse response = sut.createService(spec);
assertThat(response.id(), is(notNullValue()));

Service service = sut.inspectService(response.id());
assertThat(service.spec().taskTemplate().forceUpdate(), is(nullValue()));

ServiceSpec.Builder newServiceSpecBuilder = ServiceSpec.builder()
.name(service.spec().name())
.mode(service.spec().mode())
.endpointSpec(service.spec().endpointSpec())
.updateConfig(service.spec().updateConfig());

TaskSpec oldTaskSpec = service.spec().taskTemplate();
TaskSpec.Builder newTaskSpecBuilder = TaskSpec.builder()
.resources(oldTaskSpec.resources())
.restartPolicy(oldTaskSpec.restartPolicy())
.placement(oldTaskSpec.placement())
.containerSpec(oldTaskSpec.containerSpec())
.logDriver(oldTaskSpec.logDriver())
.networks(oldTaskSpec.networks());

for (int i = 0; i < 3; i++) {
service = sut.inspectService(response.id());
Integer atualForceUpdate = service.spec().taskTemplate().forceUpdate() != null
? service.spec().taskTemplate().forceUpdate() : new Integer(0);
newTaskSpecBuilder.forceUpdate(atualForceUpdate + 1).build();

newServiceSpecBuilder.taskTemplate(newTaskSpecBuilder.build());

// update service with same spec, but bump the number of replicas by 1
sut.updateService(response.id(), service.version().index(), newServiceSpecBuilder.build());

service = sut.inspectService(response.id());
assertThat(service.spec().taskTemplate().forceUpdate(), is(i + 1));
}
}

private ServiceSpec createServiceSpec(final String serviceName) {
return this.createServiceSpec(serviceName, new HashMap<String, String>());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ public void test1_32_WithoutNullables() throws Exception {
assertThat(spec.placement(), is(nullValue()));
assertThat(spec.networks(), is(nullValue()));
assertThat(spec.logDriver(), is(nullValue()));
assertThat(spec.forceUpdate(), is(nullValue()));
}
}

0 comments on commit 0d38cf2

Please sign in to comment.