Skip to content

Commit

Permalink
update docker apiInfo detail
Browse files Browse the repository at this point in the history
  • Loading branch information
wangmin9 committed Nov 7, 2023
1 parent 79bcddb commit d6ff754
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
2 changes: 1 addition & 1 deletion jcommon/docker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>docker</artifactId>
<version>1.6-SNAPSHOT</version>
<version>1.7-SNAPSHOT</version>

<dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ public class AppInfo {

private String appName;

private int cpuNum;

private long mem;

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.github.dockerjava.core.command.*;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
Expand Down Expand Up @@ -472,6 +473,28 @@ private String getAppName(String imageName) {
*/
public UseInfo containerUseInfo(String ip) {
final List<Container> list = this.listContainers(Lists.newArrayList(), false);

Map<String, String> cpuAndMem = new HashMap<>();//envId:cpu,mem

List<String> ids = list.stream().map(it -> it.getId()).collect(Collectors.toList());
Optional<UseInfo> res = ids.stream().map(it -> {
try {
InspectContainerResponse info = this.inspectContainer(it);
UseInfo ui = UseInfo.builder().useCpuNum(getCpuNum(info.getHostConfig().getCpusetCpus()))
.useMemNum(info.getHostConfig().getMemory())
.build();
Map<String, String> labels = info.getConfig().getLabels();
if (!CollectionUtils.isEmpty(labels) && labels.containsKey("ENV_ID")){
cpuAndMem.put(labels.get("ENV_ID"), ui.getUseCpuNum()+","+ui.getUseMemNum());
}
return ui;
} catch (Throwable ex) {
log.warn("error:{}", ex.getMessage());
return new UseInfo(0, 0, new HashSet<>(), Lists.newArrayList());
}
}
).reduce((a, b) -> UseInfo.builder().useCpuNum(a.getUseCpuNum() + b.getUseCpuNum()).useMemNum(a.getUseMemNum() + b.getUseMemNum()).build());

List<AppInfo> appList = Lists.newLinkedList();
Safe.run(() -> appList.addAll(list.stream().filter(it -> {
Map<String, String> labels = it.getLabels();
Expand All @@ -486,26 +509,16 @@ public UseInfo containerUseInfo(String ip) {
info.setEnvId(labels.get("ENV_ID"));
info.setAppName(getAppName(it.getImage()));
info.setIp(ip);
if (cpuAndMem.containsKey(info.getEnvId())){
String[] cpuMem = cpuAndMem.get(info.getEnvId()).split(",");
info.setCpuNum(Integer.parseInt(cpuMem[0]));
info.setMem(Long.parseLong(cpuMem[1]));
}
return info;
}).collect(Collectors.toList())));

log.info("docker app list size:{}", appList.size());

List<String> ids = list.stream().map(it -> it.getId()).collect(Collectors.toList());
Optional<UseInfo> res = ids.stream().map(it -> {
try {
InspectContainerResponse info = this.inspectContainer(it);
UseInfo ui = UseInfo.builder().useCpuNum(getCpuNum(info.getHostConfig().getCpusetCpus()))
.useMemNum(info.getHostConfig().getMemory())
.build();
return ui;
} catch (Throwable ex) {
log.warn("error:{}", ex.getMessage());
return new UseInfo(0, 0, new HashSet<>(), Lists.newArrayList());
}
}
).reduce((a, b) -> UseInfo.builder().useCpuNum(a.getUseCpuNum() + b.getUseCpuNum()).useMemNum(a.getUseMemNum() + b.getUseMemNum()).build());

Set<String> appNames = list.stream().map(it -> getAppName(it.getImage())).collect(Collectors.toSet());
UseInfo info = null;
if (res.isPresent()) {
Expand Down

0 comments on commit d6ff754

Please sign in to comment.