Skip to content

Commit

Permalink
improve mount targets filtering in ContainerCreator.getHostConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
wayerr authored and vpronto@yandex-team.ru committed Oct 28, 2017
1 parent c49d57f commit 473be97
Showing 1 changed file with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.codeabovelab.dm.cluman.validate.ExtendedAssert;
import com.codeabovelab.dm.common.utils.Consumers;
import com.google.common.base.MoreObjects;
import com.google.common.base.Splitter;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -331,9 +332,7 @@ private HostConfig getHostConfig(CreateContainerContext cc, ContainerSource arg)
Long mem = arg.getMemoryLimit();
RestartPolicy restartPolicy = getRestartPolicy(cc, arg);
List<String> hostBindings = getHostBindings(arg);
Map<String, String> binds = hostBindings.stream()
.map(hb -> hb.split(":")).filter(a -> a.length > 1)
.collect(Collectors.toMap(a -> a[0].trim(), a -> a[1].trim()));
Set<String> bindedTargets = getBindedTargets(hostBindings);
HostConfig.HostConfigBuilder builder = HostConfig.builder()
.memory(mem)
.blkioWeight(arg.getBlkioWeight())
Expand All @@ -343,15 +342,30 @@ private HostConfig getHostConfig(CreateContainerContext cc, ContainerSource arg)
.portBindings(getBindings(arg.getPorts()))
.publishAllPorts(arg.isPublishAllPorts())
.restartPolicy(restartPolicy);
//TODO: fix ugly code

builder.mounts(arg.getMounts().stream()
.filter(m -> !binds.containsValue(m.getTarget().trim()))
.filter(m -> !binds.containsValue(m.getTarget().trim() + "/"))
.filter(m -> !bindedTargets.contains(m.getTarget()))
.map(SourceUtil::fromMountSource).collect(Collectors.toList()));
makeNetwork(cc, arg, builder);
return builder.build();
}

private Set<String> getBindedTargets(List<String> hostBindings) {
Set<String> binds = new HashSet<>();
for (String binding : hostBindings) {
String[] arr = StringUtils.delimitedListToStringArray(binding, ":");
if (arr.length > 1) {
String target = arr[1];
// remove trailing slashes
while(target.endsWith("/") && target.length() > 1) {
target = target.substring(0, target.length() - 1);
}
binds.add(target);
}
}
return binds;
}

private void makeNetwork(CreateContainerContext cc, ContainerSource arg, HostConfig.HostConfigBuilder b) {
String networkSrc = arg.getNetwork();
List<String> networksSrc = arg.getNetworks();
Expand Down

0 comments on commit 473be97

Please sign in to comment.