Skip to content

Commit

Permalink
experimental stack dns name resolution #2
Browse files Browse the repository at this point in the history
  • Loading branch information
s4ke committed Oct 21, 2024
1 parent b6116a3 commit 48a7340
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 10 additions & 2 deletions dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ def resolve_dnsA_to_ip(network_data, networks, domain):
"""
Resolves DNS A records to IP addresses based on the network data.
"""
network_data = get_network_data()

print_debug(f"Resolving DNS A records for domain: {domain}")
print_debug(f"Networks: {networks}")
Expand All @@ -240,11 +239,15 @@ def resolve_dnsA_to_ip(network_data, networks, domain):
if 'containers' in network_info:
for container_data in network_info['containers']:
ip_address = container_data['ip_address']

stack_name = container_data.get('stack_name', None)
service_name = container_data['service']

if f'tasks.{service_name}' == domain:
dnsA_records.add(ip_address)

if stack_name is not None and f'tasks.{stack_name}_{service_name}' == domain:
dnsA_records.add(ip_address)

dns_names = container_data.get('dns_names', [])
if dns_names is not None:
for dns_name in dns_names:
Expand All @@ -262,6 +265,7 @@ def resolve_dnsA_to_ip(network_data, networks, domain):
relevant_ips = service_data['virtual_ips']
endpoint_mode = service_data['endpoint_mode']
service_name = service_data['service_name']
stack_name = service_data.get('stack_name', None)

if endpoint_mode == 'dnsrr':
service_name = service_data['service_name']
Expand All @@ -279,6 +283,10 @@ def resolve_dnsA_to_ip(network_data, networks, domain):
for ip in relevant_ips:
dnsA_records.add(ip)

if stack_name is not None and f'{stack_name}_{service_name}' == domain:
for ip in relevant_ips:
dnsA_records.add(ip_address)

dns_names = service_data.get('dns_names', [])
if dns_names is not None:
for dns_name in dns_names:
Expand Down
8 changes: 6 additions & 2 deletions exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def fetch_containers_and_aliases(network_data):
container_id = container_info.get('Id', None)

service = container_info.get('Config', {}).get('Labels', {}).get('com.docker.swarm.service.name', None)
stack_name = container_info.get('Config', {}).get('Labels', {}).get('com.docker.stack.namespace', None)

# If the network exists in our network_data (it should), add container info
if network_id in network_data:
Expand All @@ -107,7 +108,8 @@ def fetch_containers_and_aliases(network_data):
'aliases': aliases,
# add the service name so that we can match it to the service later
# so that we can do a lookup of the virtual IPs
'service': service
'service': service,
'stack_name': stack_name
})

return network_data
Expand Down Expand Up @@ -141,6 +143,7 @@ def fetch_and_attach_swarm_services(network_data):
virtual_ips = service_info.get('Endpoint', {}).get('VirtualIPs', [])
endpoint_mode = service_info.get('Spec', {}).get('EndpointSpec', {}).get('Mode', 'vip')
service_id = service_info.get('ID', None)
stack_name = service_info.get('Spec', {}).get('Labels', {}).get('com.docker.stack.namespace', None)

# Attach the service to each network it belongs to
for network in networks:
Expand All @@ -158,7 +161,8 @@ def fetch_and_attach_swarm_services(network_data):
'replicas': replicas,
'aliases': aliases,
'endpoint_mode': endpoint_mode,
'virtual_ips': vip_list # Add Virtual IPs to the service info
'virtual_ips': vip_list, # Add Virtual IPs to the service info,
'stack_name': stack_name
})
break

Expand Down

0 comments on commit 48a7340

Please sign in to comment.