Search in sources :

Example 1 with ContainerSpec

use of com.spotify.docker.client.messages.swarm.ContainerSpec in project docker-client by spotify.

the class DefaultDockerClientTest method testCreateServiceWithDefaults.

@Test
public void testCreateServiceWithDefaults() throws Exception {
    requireDockerApiVersionAtLeast("1.24", "swarm support");
    final String serviceName = randomName();
    final TaskSpec taskSpec = TaskSpec.builder().containerSpec(ContainerSpec.builder().image("alpine").command(new String[] { "ping", "-c1000", "localhost" }).mounts(Mount.builder().volumeOptions(VolumeOptions.builder().driverConfig(com.spotify.docker.client.messages.mount.Driver.builder().build()).build()).bindOptions(BindOptions.builder().build()).tmpfsOptions(TmpfsOptions.builder().build()).build()).build()).resources(ResourceRequirements.builder().build()).restartPolicy(RestartPolicy.builder().build()).placement(Placement.create(null)).networks(NetworkAttachmentConfig.builder().build()).logDriver(Driver.builder().build()).build();
    final ServiceMode serviceMode = ServiceMode.builder().replicated(ReplicatedService.builder().build()).build();
    final ServiceSpec serviceSpec = ServiceSpec.builder().name(serviceName).taskTemplate(taskSpec).mode(serviceMode).updateConfig(UpdateConfig.create(null, null, null)).networks(Collections.<NetworkAttachmentConfig>emptyList()).endpointSpec(EndpointSpec.builder().addPort(PortConfig.builder().build()).build()).build();
    final ServiceCreateResponse response = sut.createService(serviceSpec);
    assertThat(response.id(), is(notNullValue()));
    sut.listTasks();
    final Service service = sut.inspectService(serviceName);
    final ServiceSpec actualServiceSpec = service.spec();
    assertThat(actualServiceSpec.mode().replicated().replicas(), equalTo(1L));
    assertThat(actualServiceSpec.taskTemplate().logDriver().options(), equalTo(Collections.<String, String>emptyMap()));
    assertThat(actualServiceSpec.endpointSpec().mode(), equalTo(EndpointSpec.Mode.RESOLUTION_MODE_VIP));
    assertThat(actualServiceSpec.updateConfig().failureAction(), equalTo("pause"));
    final PortConfig.Builder portConfigBuilder = PortConfig.builder().protocol(PROTOCOL_TCP);
    if (dockerApiVersionAtLeast("1.25")) {
        // Ingress publish mode is the default for ports in API versions >= 1.25
        portConfigBuilder.publishMode(PortConfigPublishMode.INGRESS);
    }
    final PortConfig expectedPortConfig = portConfigBuilder.build();
    assertThat(actualServiceSpec.endpointSpec().ports(), contains(expectedPortConfig));
    assertThat(service.endpoint().spec().ports(), contains(expectedPortConfig));
    final ContainerSpec containerSpec = actualServiceSpec.taskTemplate().containerSpec();
    assertThat(containerSpec.labels(), equalTo(Collections.<String, String>emptyMap()));
    assertThat(containerSpec.mounts().size(), equalTo(1));
    final Mount mount = containerSpec.mounts().get(0);
    assertThat(mount.type(), equalTo("bind"));
    final VolumeOptions volumeOptions = mount.volumeOptions();
    assertThat(volumeOptions.noCopy(), nullValue());
    assertThat(volumeOptions.labels(), nullValue());
    final com.spotify.docker.client.messages.mount.Driver driver = volumeOptions.driverConfig();
    assertThat(driver.name(), nullValue());
    assertThat(driver.options(), nullValue());
    final RestartPolicy restartPolicy = actualServiceSpec.taskTemplate().restartPolicy();
    assertThat(restartPolicy.condition(), equalTo(RESTART_POLICY_ANY));
    assertThat(restartPolicy.maxAttempts(), equalTo(0));
}
Also used : TaskSpec(com.spotify.docker.client.messages.swarm.TaskSpec) ContainerSpec(com.spotify.docker.client.messages.swarm.ContainerSpec) ServiceSpec(com.spotify.docker.client.messages.swarm.ServiceSpec) ContainerMount(com.spotify.docker.client.messages.ContainerMount) Mount(com.spotify.docker.client.messages.mount.Mount) ReplicatedService(com.spotify.docker.client.messages.swarm.ReplicatedService) CompletionService(java.util.concurrent.CompletionService) Service(com.spotify.docker.client.messages.swarm.Service) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) ExecutorService(java.util.concurrent.ExecutorService) RestartPolicy(com.spotify.docker.client.messages.swarm.RestartPolicy) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) ServiceMode(com.spotify.docker.client.messages.swarm.ServiceMode) VolumeOptions(com.spotify.docker.client.messages.mount.VolumeOptions) ServiceCreateResponse(com.spotify.docker.client.messages.ServiceCreateResponse) PortConfig(com.spotify.docker.client.messages.swarm.PortConfig) Test(org.junit.Test)

Example 2 with ContainerSpec

use of com.spotify.docker.client.messages.swarm.ContainerSpec in project docker-client by spotify.

the class DefaultDockerClientTest method testInspectTask.

@Test
public void testInspectTask() throws Exception {
    requireDockerApiVersionAtLeast("1.24", "swarm support");
    final Date start = new Date();
    final ServiceSpec serviceSpec = createServiceSpec(randomName());
    assertThat(sut.listTasks().size(), is(0));
    final ServiceCreateResponse serviceCreateResponse = sut.createService(serviceSpec);
    await().until(numberOfTasks(sut), is(greaterThan(0)));
    final Task someTask = sut.listTasks().get(0);
    final Task inspectedTask = sut.inspectTask(someTask.id());
    final Date now = new Date();
    assertThat(inspectedTask.id(), notNullValue());
    assertThat(inspectedTask.version().index(), allOf(notNullValue(), greaterThan(0L)));
    assertThat(inspectedTask.createdAt(), allOf(notNullValue(), greaterThanOrEqualTo(start), lessThanOrEqualTo(now)));
    assertThat(inspectedTask.updatedAt(), allOf(notNullValue(), greaterThanOrEqualTo(start), lessThanOrEqualTo(now)));
    assertThat(inspectedTask.slot(), allOf(notNullValue(), greaterThan(0)));
    assertThat(inspectedTask.status(), notNullValue());
    assertThat(inspectedTask.name(), nullValue());
    assertEquals(serviceCreateResponse.id(), inspectedTask.serviceId());
    if (serviceSpec.labels() == null || serviceSpec.labels().isEmpty()) {
        // Hamcrest has generally bad support for "is null or empty",
        // and no support at all for empty maps
        assertTrue(inspectedTask.labels() == null || inspectedTask.labels().isEmpty());
    } else {
        assertEquals(serviceSpec.labels(), inspectedTask.labels());
    }
    assertThat(inspectedTask.desiredState(), is(anything()));
    assertThat(inspectedTask.networkAttachments(), is(anything()));
    final TaskSpec taskSpecTemplate = serviceSpec.taskTemplate();
    final TaskSpec taskSpecActual = inspectedTask.spec();
    assertEquals(taskSpecTemplate.resources(), taskSpecActual.resources());
    assertEquals(taskSpecTemplate.restartPolicy(), taskSpecActual.restartPolicy());
    assertEquals(taskSpecTemplate.placement(), taskSpecActual.placement());
    assertEquals(taskSpecTemplate.networks(), taskSpecActual.networks());
    assertEquals(taskSpecTemplate.logDriver(), taskSpecActual.logDriver());
    final ContainerSpec containerSpecTemplate = taskSpecTemplate.containerSpec();
    final ContainerSpec containerSpecActual = taskSpecActual.containerSpec();
    assertThat(containerSpecActual.image(), latestImageNameMatcher(containerSpecTemplate.image()));
    assertEquals(containerSpecTemplate.labels(), containerSpecActual.labels());
    assertEquals(containerSpecTemplate.command(), containerSpecActual.command());
    assertEquals(containerSpecTemplate.args(), containerSpecActual.args());
    assertEquals(containerSpecTemplate.env(), containerSpecActual.env());
    assertEquals(containerSpecTemplate.dir(), containerSpecActual.dir());
    assertEquals(containerSpecTemplate.user(), containerSpecActual.user());
    assertEquals(containerSpecTemplate.groups(), containerSpecActual.groups());
    assertEquals(containerSpecTemplate.tty(), containerSpecActual.tty());
    assertEquals(containerSpecTemplate.mounts(), containerSpecActual.mounts());
    assertEquals(containerSpecTemplate.stopGracePeriod(), containerSpecActual.stopGracePeriod());
}
Also used : Task(com.spotify.docker.client.messages.swarm.Task) TaskSpec(com.spotify.docker.client.messages.swarm.TaskSpec) ContainerSpec(com.spotify.docker.client.messages.swarm.ContainerSpec) ServiceSpec(com.spotify.docker.client.messages.swarm.ServiceSpec) ServiceCreateResponse(com.spotify.docker.client.messages.ServiceCreateResponse) Date(java.util.Date) Test(org.junit.Test)

Example 3 with ContainerSpec

use of com.spotify.docker.client.messages.swarm.ContainerSpec in project shinyproxy by openanalytics.

the class DockerSwarmBackend method doStartProxy.

@Override
protected void doStartProxy(DockerContainerProxy proxy) throws Exception {
    Mount[] mounts = getBindVolumes(proxy.getApp()).stream().map(b -> b.split(":")).map(fromTo -> Mount.builder().source(fromTo[0]).target(fromTo[1]).type("bind").build()).toArray(i -> new Mount[i]);
    ContainerSpec containerSpec = ContainerSpec.builder().image(proxy.getApp().getDockerImage()).command(proxy.getApp().getDockerCmd()).env(buildEnv(proxy.getUserId(), proxy.getApp())).dnsConfig(DnsConfig.builder().nameServers(proxy.getApp().getDockerDns()).build()).mounts(mounts).build();
    NetworkAttachmentConfig[] networks = Arrays.stream(Optional.ofNullable(proxy.getApp().getDockerNetworkConnections()).orElse(new String[0])).map(n -> NetworkAttachmentConfig.builder().target(n).build()).toArray(i -> new NetworkAttachmentConfig[i]);
    ServiceSpec.Builder serviceSpecBuilder = ServiceSpec.builder().networks(networks).name(proxy.getName()).taskTemplate(TaskSpec.builder().containerSpec(containerSpec).build());
    if (proxy.getPort() > 0) {
        serviceSpecBuilder.endpointSpec(EndpointSpec.builder().ports(PortConfig.builder().publishedPort(proxy.getPort()).targetPort(getAppPort(proxy)).build()).build());
    }
    proxy.setServiceId(dockerClient.createService(serviceSpecBuilder.build()).id());
    boolean containerFound = Utils.retry(i -> {
        try {
            Task serviceTask = dockerClient.listTasks(Task.Criteria.builder().serviceName(proxy.getName()).build()).stream().findAny().orElseThrow(() -> new IllegalStateException("Swarm service has no tasks"));
            proxy.setContainerId(serviceTask.status().containerStatus().containerId());
        } catch (Exception e) {
            throw new RuntimeException("Failed to inspect swarm service tasks", e);
        }
        return (proxy.getContainerId() != null);
    }, 10, 2000, true);
    if (!containerFound) {
        dockerClient.removeService(proxy.getServiceId());
        throw new IllegalStateException("Swarm container did not start in time");
    }
}
Also used : Mount(com.spotify.docker.client.messages.mount.Mount) Arrays(java.util.Arrays) ServiceSpec(com.spotify.docker.client.messages.swarm.ServiceSpec) ContainerSpec(com.spotify.docker.client.messages.swarm.ContainerSpec) NetworkAttachmentConfig(com.spotify.docker.client.messages.swarm.NetworkAttachmentConfig) ContainerProxyRequest(eu.openanalytics.shinyproxy.container.ContainerProxyRequest) Random(java.util.Random) TaskSpec(com.spotify.docker.client.messages.swarm.TaskSpec) Hex(org.apache.commons.codec.binary.Hex) DnsConfig(com.spotify.docker.client.messages.swarm.DnsConfig) EndpointSpec(com.spotify.docker.client.messages.swarm.EndpointSpec) ShinyProxyException(eu.openanalytics.shinyproxy.ShinyProxyException) Task(com.spotify.docker.client.messages.swarm.Task) Optional(java.util.Optional) Utils(eu.openanalytics.shinyproxy.util.Utils) PortConfig(com.spotify.docker.client.messages.swarm.PortConfig) NetworkAttachmentConfig(com.spotify.docker.client.messages.swarm.NetworkAttachmentConfig) Task(com.spotify.docker.client.messages.swarm.Task) ContainerSpec(com.spotify.docker.client.messages.swarm.ContainerSpec) Mount(com.spotify.docker.client.messages.mount.Mount) ServiceSpec(com.spotify.docker.client.messages.swarm.ServiceSpec) ShinyProxyException(eu.openanalytics.shinyproxy.ShinyProxyException)

Aggregations

ContainerSpec (com.spotify.docker.client.messages.swarm.ContainerSpec)3 ServiceSpec (com.spotify.docker.client.messages.swarm.ServiceSpec)3 TaskSpec (com.spotify.docker.client.messages.swarm.TaskSpec)3 ServiceCreateResponse (com.spotify.docker.client.messages.ServiceCreateResponse)2 Mount (com.spotify.docker.client.messages.mount.Mount)2 PortConfig (com.spotify.docker.client.messages.swarm.PortConfig)2 Task (com.spotify.docker.client.messages.swarm.Task)2 Test (org.junit.Test)2 ContainerMount (com.spotify.docker.client.messages.ContainerMount)1 VolumeOptions (com.spotify.docker.client.messages.mount.VolumeOptions)1 DnsConfig (com.spotify.docker.client.messages.swarm.DnsConfig)1 EndpointSpec (com.spotify.docker.client.messages.swarm.EndpointSpec)1 NetworkAttachmentConfig (com.spotify.docker.client.messages.swarm.NetworkAttachmentConfig)1 ReplicatedService (com.spotify.docker.client.messages.swarm.ReplicatedService)1 RestartPolicy (com.spotify.docker.client.messages.swarm.RestartPolicy)1 Service (com.spotify.docker.client.messages.swarm.Service)1 ServiceMode (com.spotify.docker.client.messages.swarm.ServiceMode)1 ShinyProxyException (eu.openanalytics.shinyproxy.ShinyProxyException)1 ContainerProxyRequest (eu.openanalytics.shinyproxy.container.ContainerProxyRequest)1 Utils (eu.openanalytics.shinyproxy.util.Utils)1