From b6116a3f63b3887f86a669619018edbcb7c7d679 Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Mon, 14 Oct 2024 17:08:32 +0200 Subject: [PATCH] change identifier of networks to be network_id instead of network_name --- dns.py | 16 +++++++--------- exporter.py | 15 ++++++++------- merger.py | 26 +++++++++++++------------- 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/dns.py b/dns.py index 9a72325..6269e3b 100644 --- a/dns.py +++ b/dns.py @@ -134,18 +134,16 @@ def fetch_networks_info(): # get network details by id network_details = client.api.inspect_network(network_id) - # print(network_details) - network_name = network_details["Name"] containers = network_details["Containers"] - network_info[network_name] = {} + network_info[network_id] = {} for container_id, container_details in containers.items(): ip_address = container_details.get("IPv4Address") if ip_address: # Strip subnet from the IP address (since it's in CIDR format) ip_address = ip_address.split('/')[0] - network_info[network_name][container_id] = ip_address + network_info[network_id][container_id] = ip_address return network_info @@ -157,7 +155,7 @@ def find_container_id_from_ip(ip_address): global network_info_cache network_info = network_info_cache - for network_name, container_info in network_info.items(): + for network_id, container_info in network_info.items(): for container_id, container_ip in container_info.items(): if container_ip == ip_address: return container_id @@ -174,8 +172,8 @@ def get_networks_from_container_id(container_id): network_info = container.attrs["NetworkSettings"]["Networks"] networks = [] - for network_name, network_details in network_info.items(): - networks.append(network_name) + for network_id, network_details in network_info.items(): + networks.append(network_id) return networks @@ -235,8 +233,8 @@ def resolve_dnsA_to_ip(network_data, networks, domain): dnsA_records = set() - for network_name, network_info in network_data.items(): - if network_name not in networks: + for network_id, network_info in network_data.items(): + if network_id not in networks: continue if 'containers' in network_info: diff --git a/exporter.py b/exporter.py index c0386b8..6ff1d8d 100644 --- a/exporter.py +++ b/exporter.py @@ -53,7 +53,7 @@ def fetch_networks_and_subnets(): for network in networks: network_info = network.attrs - network_name = network_info['Name'] + network_id = network_info['Id'] # Fetch subnets from IPAM config (IP Address Management) subnets = [] @@ -66,9 +66,10 @@ def fetch_networks_and_subnets(): subnet = config.get('Subnet', 'No Subnet') subnets.append(subnet) - network_data[network_name] = { + network_data[network_id] = { 'subnets': subnets, - 'network_id': network_info['Id'], + 'network_id': network_id, + 'name': network_info['Name'], 'containers': [], 'services': [] # Initialize services list for each network } @@ -88,7 +89,7 @@ def fetch_containers_and_aliases(network_data): # Loop over each network the container is connected to networks = container_info['NetworkSettings']['Networks'] - for network_name, network_info in networks.items(): + for network_id, network_info in networks.items(): ip_address = network_info.get('IPAddress', None) aliases = network_info.get('Aliases', []) dns_names = network_info.get('DNSNames', []) # Fetch DNS names from network info @@ -97,8 +98,8 @@ def fetch_containers_and_aliases(network_data): service = container_info.get('Config', {}).get('Labels', {}).get('com.docker.swarm.service.name', None) # If the network exists in our network_data (it should), add container info - if network_name in network_data: - network_data[network_name]['containers'].append({ + if network_id in network_data: + network_data[network_id]['containers'].append({ 'container_name': container_name, # DNS name (container name), 'id': container_id, 'ip_address': ip_address, @@ -146,7 +147,7 @@ def fetch_and_attach_swarm_services(network_data): network_id = network.get('Target', '') aliases = network.get('Aliases', []) # Match network ID to network names in network_data - for network_name, network_info in network_data.items(): + for network_id, network_info in network_data.items(): if network_id == network_info.get('network_id'): # Match by network ID vip_list = [vip.get('Addr', '') for vip in virtual_ips if vip.get('NetworkID') == network_id] # remove /24 etc from the end of the IP diff --git a/merger.py b/merger.py index 0fadfc9..214a26a 100644 --- a/merger.py +++ b/merger.py @@ -56,36 +56,36 @@ def save_json_to_s3(data, bucket, key): def merge_data(merged_data, node_data): """Merge node data into the merged data, avoiding duplicates using 'id' for containers and 'service_id' for services.""" - for network_name, network_info in node_data.items(): + for network_id, network_info in node_data.items(): # Initialize the network in merged data if it doesn't exist - if network_name not in merged_data: - merged_data[network_name] = { + if network_id not in merged_data: + merged_data[network_id] = { "subnets": [], "containers": [], "services": [] } - print_timed(f"Created new network entry for {network_name}") + print_timed(f"Created new network entry for {network_id}") # Merge subnets - initial_subnets_count = len(merged_data[network_name]["subnets"]) - merged_data[network_name]["subnets"] = list(set(merged_data[network_name]["subnets"] + network_info["subnets"])) - print_timed(f"Merged subnets for network {network_name}. Added {len(merged_data[network_name]['subnets']) - initial_subnets_count} new subnets.") + initial_subnets_count = len(merged_data[network_id]["subnets"]) + merged_data[network_id]["subnets"] = list(set(merged_data[network_id]["subnets"] + network_info["subnets"])) + print_timed(f"Merged subnets for network {network_id}. Added {len(merged_data[network_id]['subnets']) - initial_subnets_count} new subnets.") # Merge containers (avoiding duplicates by 'id') - container_ids = {c["id"] for c in merged_data[network_name]["containers"]} + container_ids = {c["id"] for c in merged_data[network_id]["containers"]} for container in network_info["containers"]: if container["id"] not in container_ids: - merged_data[network_name]["containers"].append(container) + merged_data[network_id]["containers"].append(container) container_ids.add(container["id"]) - print_timed(f"Added container {container['container_name']} (ID: {container['id']}) to network {network_name}") + print_timed(f"Added container {container['container_name']} (ID: {container['id']}) to network {network_id}") # Merge services (avoiding duplicates by 'service_id') - service_ids = {s["service_id"] for s in merged_data[network_name]["services"]} + service_ids = {s["service_id"] for s in merged_data[network_id]["services"]} for service in network_info["services"]: if service["service_id"] not in service_ids: - merged_data[network_name]["services"].append(service) + merged_data[network_id]["services"].append(service) service_ids.add(service["service_id"]) - print_timed(f"Added service {service['service_name']} (ID: {service['service_id']}) to network {network_name}") + print_timed(f"Added service {service['service_name']} (ID: {service['service_id']}) to network {network_id}") return merged_data