Skip to content

Commit

Permalink
change identifier of networks to be network_id instead of network_name
Browse files Browse the repository at this point in the history
  • Loading branch information
s4ke committed Oct 14, 2024
1 parent 5c0f43f commit b6116a3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
16 changes: 7 additions & 9 deletions dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand Down
15 changes: 8 additions & 7 deletions exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit b6116a3

Please sign in to comment.