|
| 1 | +import ipaddress |
| 2 | +from typing import List, Union |
| 3 | + |
1 | 4 | import docker |
2 | 5 |
|
3 | 6 | from threading import Thread |
4 | 7 |
|
5 | 8 |
|
6 | 9 | class DockerHost: |
7 | | - def __init__(self, host_names, ip, interface=None): |
| 10 | + def __init__(self, host_names: List[str], ip: Union[ipaddress.IPv4Address, ipaddress.IPv6Address], interface=None): |
8 | 11 | self.host_names = host_names |
9 | 12 | self.ip = ip |
10 | 13 | self.interface = interface |
@@ -85,22 +88,25 @@ def collect_from_containers(self): |
85 | 88 | name = c.attrs['Name'][1:] |
86 | 89 | settings = c.attrs['NetworkSettings'] |
87 | 90 | for netname, network in settings.get('Networks', {}).items(): |
88 | | - ip = network.get('IPAddress', False) |
89 | | - if not ip or ip == "": |
| 91 | + ips = [network[field] for field in ['IPAddress', 'GlobalIPv6Address'] if |
| 92 | + field in network and network[field] != ""] |
| 93 | + if not ips: |
90 | 94 | if netname == 'host': |
91 | | - ip = self.default_host_ip |
| 95 | + ips = [self.default_host_ip] |
92 | 96 | else: |
93 | 97 | continue |
94 | 98 |
|
95 | 99 | # record the container name DOT network |
96 | 100 | # eg. container is named "foo", and network is "demo", |
97 | 101 | # so create "foo.demo" domain name |
98 | 102 | # (avoiding default network named "bridge") |
99 | | - record = domain_records.get(ip, [*common_hostnames]) |
100 | | - if netname != "bridge": |
101 | | - record.append('%s.%s' % (name, netname)) |
| 103 | + for ip in ips: |
| 104 | + ipr = ipaddress.ip_address(ip) |
| 105 | + record = domain_records.get(ipr, [*common_hostnames]) |
| 106 | + if netname != "bridge": |
| 107 | + record.append('%s.%s' % (name, netname)) |
102 | 108 |
|
103 | | - domain_records[ip] = record |
| 109 | + domain_records[ipr] = record |
104 | 110 |
|
105 | 111 | for ip, hosts in domain_records.items(): |
106 | 112 | domain_records[ip] = list(filter(lambda h: h not in duplicate_hostnames, hosts)) |
|
0 commit comments