From 607470a122428dc69861dfd3ca39066aac707a94 Mon Sep 17 00:00:00 2001 From: Joao Gilberto Date: Thu, 10 Nov 2022 17:43:59 -0300 Subject: [PATCH] Issue #33 - handle networks --- docs/docker.md | 1 + docs/swarm.md | 3 ++- src/processor/__init__.py | 36 +++++++++++++++++++++++++++++++----- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/docs/docker.md b/docs/docker.md index c777b35..3ffdc7f 100644 --- a/docs/docker.md +++ b/docs/docker.md @@ -5,6 +5,7 @@ This method will use a docker standalone installation to discover the containers and configure the HAProxy. The only requirement is that containers and EasyHAProxy must be in the same docker network. +If not, EasyHAProxy will connect the container with the EasyHAProxy network. e.g.: diff --git a/docs/swarm.md b/docs/swarm.md index b6844a5..15f2457 100644 --- a/docs/swarm.md +++ b/docs/swarm.md @@ -3,9 +3,10 @@ ## Setup Docker EasyHAProxy This method will use a docker swarm installation to discover the containers and configure the HAProxy. -The advantage of this method is that you can discover containers in other nodes from the cluster. +The advantage of this method is that you can discover containers in other nodes from the cluster. The only requirement is that containers and EasyHAProxy must be in the same docker swarm network. +If not, EasyHAProxy will connect the service with the EasyHAProxy service network. e.g.: diff --git a/src/processor/__init__.py b/src/processor/__init__.py index 90c5972..c58e608 100644 --- a/src/processor/__init__.py +++ b/src/processor/__init__.py @@ -6,6 +6,7 @@ import os import json import base64 import docker +import socket from kubernetes import client, config from kubernetes.client.rest import ApiException @@ -127,11 +128,17 @@ class Docker(ProcessorInterface): super().__init__() def inspect_network(self): + ha_proxy_network_name = next(iter(self.client.containers.get(socket.gethostname()).attrs["NetworkSettings"]["Networks"])) + ha_proxy_network = self.client.networks.get(ha_proxy_network_name) + self.parsed_object = {} for container in self.client.containers.list(): - ip_address = container.attrs["NetworkSettings"]["IPAddress"] - if not ip_address: - ip_address = list(container.attrs["NetworkSettings"]["Networks"].values())[0]["IPAddress"] + # Issue 32 - Docker container cannot connect to containers in different network. + if ha_proxy_network_name not in container.attrs["NetworkSettings"]["Networks"].keys(): + ha_proxy_network.connect(container.name) + container = self.client.containers.get(container.name) # refresh object + + ip_address = container.attrs["NetworkSettings"]["Networks"][ha_proxy_network_name]["IPAddress"] self.parsed_object[ip_address] = container.labels @@ -141,9 +148,28 @@ class Swarm(ProcessorInterface): super().__init__() def inspect_network(self): + ha_proxy_service_name = self.client.containers.get(socket.gethostname()).name.split('.')[0] + for endpoint in self.client.services.get(ha_proxy_service_name).attrs['Endpoint']["VirtualIPs"]: + ha_proxy_network_id = endpoint["NetworkID"] + if self.client.networks.get(ha_proxy_network_id).name != 'ingress': + break + self.parsed_object = {} - for container in self.client.services.list(): - self.parsed_object[container.attrs["Endpoint"]["VirtualIPs"][0]["Addr"].split("/")[0]] = container.attrs["Spec"]["Labels"] + for service in self.client.services.list(): + ip_address = None + network_list = [] + for endpoint in service.attrs["Endpoint"]["VirtualIPs"]: + if ha_proxy_network_id == endpoint["NetworkID"]: + ip_address = endpoint["Addr"].split("/")[0] + break + network_list.append(endpoint["NetworkID"]) + + if ip_address is None: + network_list.append(ha_proxy_network_id) + service.update(networks = network_list) + continue # skip to the next service to give time to update the network + + self.parsed_object[ip_address] = service.attrs["Spec"]["Labels"] class Kubernetes(ProcessorInterface):