143 lines
5.2 KiB
Python
143 lines
5.2 KiB
Python
import docker
|
|
|
|
def find_namespace_controller(container_name):
|
|
"""
|
|
Finds the namespace controller (master container) for a given container name.
|
|
"""
|
|
# Initialize the Docker client
|
|
client = docker.from_env()
|
|
|
|
try:
|
|
# Get the container by name
|
|
container = client.containers.get(container_name)
|
|
container_info = container.attrs
|
|
|
|
# Get the NetworkMode from HostConfig
|
|
network_mode = container_info['HostConfig']['NetworkMode']
|
|
|
|
# Check if the network mode is linked to another container
|
|
if network_mode.startswith('container:'):
|
|
# Extract the master container ID
|
|
master_container_id = network_mode.split(':')[1]
|
|
|
|
# Look up the master container's name
|
|
try:
|
|
master_container = client.containers.get(master_container_id)
|
|
return {
|
|
'master_container_id': master_container_id,
|
|
'master_container_name': master_container.name
|
|
}
|
|
except docker.errors.NotFound:
|
|
return {
|
|
'master_container_id': master_container_id,
|
|
'master_container_name': "Unknown (container not found)"
|
|
}
|
|
else:
|
|
return None # No namespace controller found
|
|
except docker.errors.NotFound:
|
|
return None # Container not found
|
|
|
|
def find_linked_containers(master_container_name):
|
|
"""
|
|
Finds all child/linked containers for a given namespace controller (specified by container name).
|
|
"""
|
|
# Initialize the Docker client
|
|
client = docker.from_env()
|
|
|
|
try:
|
|
# Get the master container by name
|
|
master_container = client.containers.get(master_container_name)
|
|
master_container_id = master_container.id
|
|
|
|
# Get all containers
|
|
containers = client.containers.list(all=True)
|
|
|
|
# List to store linked containers
|
|
linked_containers = []
|
|
|
|
# Iterate through all containers
|
|
for container in containers:
|
|
container_info = container.attrs
|
|
|
|
# Get the NetworkMode from HostConfig
|
|
network_mode = container_info['HostConfig']['NetworkMode']
|
|
|
|
# Check if the network mode is linked to the master container
|
|
if network_mode == f'container:{master_container_id}':
|
|
linked_containers.append({
|
|
'container_id': container.id,
|
|
'container_name': container.name
|
|
})
|
|
|
|
return linked_containers
|
|
except docker.errors.NotFound:
|
|
return None # Master container not found
|
|
|
|
def check_network_links():
|
|
"""
|
|
Main function to check all containers and their network links.
|
|
"""
|
|
# Initialize the Docker client
|
|
client = docker.from_env()
|
|
|
|
# Get all containers
|
|
containers = client.containers.list(all=True)
|
|
|
|
# Dictionary to store container network links
|
|
network_links = {}
|
|
|
|
# Iterate through all containers
|
|
for container in containers:
|
|
# Inspect the container to get detailed information
|
|
container_info = container.attrs
|
|
|
|
# Get the NetworkMode from HostConfig
|
|
network_mode = container_info['HostConfig']['NetworkMode']
|
|
|
|
# Check if the network mode is linked to another container
|
|
if network_mode.startswith('container:'):
|
|
# Extract the master container ID
|
|
master_container_id = network_mode.split(':')[1]
|
|
|
|
# Look up the master container's name
|
|
try:
|
|
master_container = client.containers.get(master_container_id)
|
|
master_container_name = master_container.name
|
|
except docker.errors.NotFound:
|
|
master_container_name = "Unknown (container not found)"
|
|
|
|
# Store the link in the dictionary
|
|
network_links[container.id] = {
|
|
'master_container_id': master_container_id,
|
|
'master_container_name': master_container_name
|
|
}
|
|
|
|
# Print the results
|
|
if network_links:
|
|
print("Containers with linked networks:")
|
|
for container_id, link_info in network_links.items():
|
|
print(
|
|
f"Container ID: {container_id[:12]} "
|
|
f"is linked to master container ID: {link_info['master_container_id'][:12]} "
|
|
f"(Name: {link_info['master_container_name']})"
|
|
)
|
|
else:
|
|
print("No containers are linked to another container's network.")
|
|
|
|
if __name__ == "__main__":
|
|
# Example usage of the new functions
|
|
print("Testing find_namespace_controller:")
|
|
controller_info = find_namespace_controller("ac39a08c-7b7a-43f9-8545-82f54ef7ea0d")
|
|
if controller_info:
|
|
print(f"Namespace controller for 'my_container_name': {controller_info}")
|
|
else:
|
|
print("No namespace controller found or container does not exist.")
|
|
|
|
print("\nTesting find_linked_containers:")
|
|
linked_containers = find_linked_containers("nscontroller-3")
|
|
if linked_containers:
|
|
print(f"Containers linked to 'nscontroller-3':")
|
|
for container in linked_containers:
|
|
print(f"Container ID: {container['container_id'][:12]} (Name: {container['container_name']})")
|
|
else:
|
|
print("No linked containers found or master container does not exist.") |