本文整理汇总了Golang中github.com/openshift/geard/docker.DockerClient.GetContainerIPs方法的典型用法代码示例。如果您正苦于以下问题:Golang DockerClient.GetContainerIPs方法的具体用法?Golang DockerClient.GetContainerIPs怎么用?Golang DockerClient.GetContainerIPs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/openshift/geard/docker.DockerClient
的用法示例。
在下文中一共展示了DockerClient.GetContainerIPs方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GetDockerContainerPacketCounts
func GetDockerContainerPacketCounts(d *docker.DockerClient) (map[containers.Identifier]int, error) {
serviceFiles, err := filepath.Glob(filepath.Join(gearconfig.ContainerBasePath(), "units", "**", containers.IdentifierPrefix+"*.service"))
if err != nil {
return nil, err
}
ids := make([]string, 0)
packetCount := make(map[containers.Identifier]int)
for _, s := range serviceFiles {
id := filepath.Base(s)
if strings.HasPrefix(id, containers.IdentifierPrefix) && strings.HasSuffix(id, ".service") {
id = id[len(containers.IdentifierPrefix):(len(id) - len(".service"))]
if id, err := containers.NewIdentifier(id); err == nil {
ids = append(ids, string(id))
packetCount[id] = 0
}
}
}
containerIPs, err := d.GetContainerIPs(ids)
if err != nil {
return nil, err
}
cmd := exec.Command("/sbin/iptables-save", "-c")
output, err := cmd.Output()
if err != nil {
return nil, err
}
scan := bufio.NewScanner(bytes.NewBuffer(output))
for scan.Scan() {
line := scan.Text()
if strings.Contains(line, "-A DOCKER ! -i docker0") && strings.Contains(line, "-j DNAT") {
//Example: [0:0] -A DOCKER ! -i docker0 -p tcp -m tcp --dport 4000 -j DNAT --to-destination 172.17.0.3:8080
items := strings.Fields(line)
packets, _ := strconv.Atoi(strings.Split(items[0], ":")[0][1:])
destIp := strings.Split(items[15], ":")[0]
id, _ := containers.NewIdentifier(containerIPs[destIp])
packetCount[id] = packetCount[id] + packets
}
if strings.Contains(line, "-A OUTPUT -d 127.0.0.1/32 -p tcp -m tcp --dport") && strings.Contains(line, "-m comment --comment ") {
//Example: [5850:394136] -A OUTPUT -d 127.0.0.1/32 -p tcp -m tcp --dport 4000 -m comment --comment 0001 -j ACCEPT
items := strings.Fields(line)
packets, _ := strconv.Atoi(strings.Split(items[0], ":")[0][1:])
if id, err := containers.NewIdentifier(items[14]); err == nil {
packetCount[id] = packetCount[id] + packets
}
}
}
return packetCount, nil
}