From 473be97a3a71c04537b237a25a0b98f1a32088de Mon Sep 17 00:00:00 2001 From: wayerr Date: Fri, 27 Oct 2017 18:22:06 +0300 Subject: [PATCH] improve mount targets filtering in ContainerCreator.getHostConfig --- .../cluman/ds/container/ContainerCreator.java | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/cluster-manager/src/main/java/com/codeabovelab/dm/cluman/ds/container/ContainerCreator.java b/cluster-manager/src/main/java/com/codeabovelab/dm/cluman/ds/container/ContainerCreator.java index 8eb2be6a..25ac56ae 100644 --- a/cluster-manager/src/main/java/com/codeabovelab/dm/cluman/ds/container/ContainerCreator.java +++ b/cluster-manager/src/main/java/com/codeabovelab/dm/cluman/ds/container/ContainerCreator.java @@ -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; @@ -331,9 +332,7 @@ private HostConfig getHostConfig(CreateContainerContext cc, ContainerSource arg) Long mem = arg.getMemoryLimit(); RestartPolicy restartPolicy = getRestartPolicy(cc, arg); List hostBindings = getHostBindings(arg); - Map binds = hostBindings.stream() - .map(hb -> hb.split(":")).filter(a -> a.length > 1) - .collect(Collectors.toMap(a -> a[0].trim(), a -> a[1].trim())); + Set bindedTargets = getBindedTargets(hostBindings); HostConfig.HostConfigBuilder builder = HostConfig.builder() .memory(mem) .blkioWeight(arg.getBlkioWeight()) @@ -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 getBindedTargets(List hostBindings) { + Set 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 networksSrc = arg.getNetworks();