|
28 | 28 | import com.spotify.docker.client.messages.swarm.EndpointSpec; |
29 | 29 | import com.spotify.docker.client.messages.swarm.NetworkAttachmentConfig; |
30 | 30 | import com.spotify.docker.client.messages.swarm.PortConfig; |
| 31 | +import com.spotify.docker.client.messages.swarm.ResourceRequirements; |
| 32 | +import com.spotify.docker.client.messages.swarm.Resources; |
31 | 33 | import com.spotify.docker.client.messages.swarm.Secret; |
32 | 34 | import com.spotify.docker.client.messages.swarm.SecretBind; |
33 | 35 | import com.spotify.docker.client.messages.swarm.SecretFile; |
@@ -116,12 +118,35 @@ protected Container startContainer(ContainerSpec spec, Proxy proxy) throws Excep |
116 | 118 | networks[networks.length - 1] = NetworkAttachmentConfig.builder().target(spec.getNetwork()).build(); |
117 | 119 | } |
118 | 120 |
|
| 121 | + Resources.Builder reservationsBuilder = Resources.builder(); |
| 122 | + // reservations are used by the Docker swarm scheduler |
| 123 | + if (spec.getCpuRequest() != null) { |
| 124 | + // note: 1 CPU = 1 * 10e8 nanoCpu -> equivalent to --cpus option |
| 125 | + reservationsBuilder.nanoCpus((long) (Double.parseDouble(spec.getCpuRequest()) * 10e8)); |
| 126 | + } |
| 127 | + if (spec.getMemoryRequest() != null) { |
| 128 | + reservationsBuilder.memoryBytes(memoryToBytes(spec.getMemoryRequest())); |
| 129 | + } |
| 130 | + |
| 131 | + Resources.Builder limitsBuilder = Resources.builder(); |
| 132 | + if (spec.getCpuLimit() != null) { |
| 133 | + // note: 1 CPU = 1 * 10e8 nanoCpu -> equivalent to --cpus option |
| 134 | + limitsBuilder.nanoCpus((long) (Double.parseDouble(spec.getCpuLimit()) * 10e8)); |
| 135 | + } |
| 136 | + if (spec.getMemoryLimit() != null) { |
| 137 | + limitsBuilder.memoryBytes(memoryToBytes(spec.getMemoryLimit())); |
| 138 | + } |
| 139 | + |
119 | 140 | String serviceName = "sp-service-" + UUID.randomUUID().toString(); |
120 | 141 | ServiceSpec.Builder serviceSpecBuilder = ServiceSpec.builder() |
121 | 142 | .networks(networks) |
122 | 143 | .name(serviceName) |
123 | 144 | .taskTemplate(TaskSpec.builder() |
124 | 145 | .containerSpec(containerSpec) |
| 146 | + .resources(ResourceRequirements.builder() |
| 147 | + .reservations(reservationsBuilder.build()) |
| 148 | + .limits(limitsBuilder.build()) |
| 149 | + .build()) |
125 | 150 | .build()); |
126 | 151 |
|
127 | 152 | List<PortConfig> portsToPublish = new ArrayList<>(); |
@@ -159,7 +184,9 @@ protected Container startContainer(ContainerSpec spec, Proxy proxy) throws Excep |
159 | 184 | Task serviceTask = dockerClient |
160 | 185 | .listTasks(Task.Criteria.builder().serviceName(serviceName).build()) |
161 | 186 | .stream().findAny().orElseThrow(() -> new IllegalStateException("Swarm service has no tasks")); |
162 | | - container.setId(serviceTask.status().containerStatus().containerId()); |
| 187 | + if (serviceTask.status().containerStatus() != null) { |
| 188 | + container.setId(serviceTask.status().containerStatus().containerId()); |
| 189 | + } |
163 | 190 | } catch (Exception e) { |
164 | 191 | throw new RuntimeException("Failed to inspect swarm service tasks", e); |
165 | 192 | } |
|
0 commit comments