From 48a73406112a5561b47b44f645f25cd2e6d5dad5 Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Mon, 21 Oct 2024 08:01:31 +0200 Subject: [PATCH] experimental stack dns name resolution #2 --- dns.py | 12 ++++++++++-- exporter.py | 8 ++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/dns.py b/dns.py index 6269e3b..44da7de 100644 --- a/dns.py +++ b/dns.py @@ -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}") @@ -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: @@ -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'] @@ -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: diff --git a/exporter.py b/exporter.py index 6ff1d8d..0693e90 100644 --- a/exporter.py +++ b/exporter.py @@ -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: @@ -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 @@ -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: @@ -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