|
| 1 | +import logging |
| 2 | + |
| 3 | +from openstack.connection import Connection |
| 4 | +from . import WorkloadGeneratorProject |
| 5 | +from .helpers import Config |
| 6 | + |
| 7 | +LOGGER = logging.getLogger() |
| 8 | + |
| 9 | + |
| 10 | +class WorkloadGeneratorLoadBalancer: |
| 11 | + def __init__( |
| 12 | + self, |
| 13 | + project: WorkloadGeneratorProject, |
| 14 | + ): |
| 15 | + self.conn: Connection = project.project_conn |
| 16 | + self.project = project |
| 17 | + self.lb = None |
| 18 | + self.listener = None |
| 19 | + self.pool = None |
| 20 | + self.health_monitor = None |
| 21 | + |
| 22 | + def get_load_balancer_by_name(self, name: str): |
| 23 | + for lb in self.conn.load_balancer.load_balancers(name=name): |
| 24 | + if lb.name == name: |
| 25 | + return lb |
| 26 | + return None |
| 27 | + |
| 28 | + def get_listener_by_name(self, name: str): |
| 29 | + for listener in self.conn.load_balancer.listeners(name=name): |
| 30 | + if listener.name == name: |
| 31 | + return listener |
| 32 | + return None |
| 33 | + |
| 34 | + def get_pool_by_name(self, name: str): |
| 35 | + for pool in self.conn.load_balancer.pools(name=name): |
| 36 | + if pool.name == name: |
| 37 | + return pool |
| 38 | + return None |
| 39 | + |
| 40 | + def get_health_monitor_for_pool(self, pool_id: str): |
| 41 | + for hm in self.conn.load_balancer.health_monitors(): |
| 42 | + if hm.pool_id == pool_id: |
| 43 | + return hm |
| 44 | + return None |
| 45 | + |
| 46 | + def get_and_create_load_balancer( |
| 47 | + self, |
| 48 | + ): |
| 49 | + self.lb = self.get_load_balancer_by_name(Config.get_lb_name()) |
| 50 | + if self.lb is not None: |
| 51 | + LOGGER.info( |
| 52 | + f"Load balancer with name {Config.get_lb_name()} already exists" |
| 53 | + ) |
| 54 | + else: |
| 55 | + LOGGER.info(f"Create load balancer with name {Config.get_lb_name()}") |
| 56 | + self.lb = self.conn.load_balancer.create_load_balancer( |
| 57 | + name=Config.get_lb_name(), |
| 58 | + vip_subnet_id=self.project.vip_subnet_id, |
| 59 | + ) |
| 60 | + self.conn.load_balancer.wait_for_load_balancer(self.lb.id) |
| 61 | + |
| 62 | + self.listener = self.get_listener_by_name(Config.get_lb_listener_name()) |
| 63 | + if self.listener is not None: |
| 64 | + LOGGER.info( |
| 65 | + f"Load balancer listener with name {Config.get_lb_listener_name()} already exists" |
| 66 | + ) |
| 67 | + else: |
| 68 | + LOGGER.info( |
| 69 | + f"Create load balancer listener with name {Config.get_lb_listener_name()} on tcp port {Config.get_lb_listener_port()}" |
| 70 | + ) |
| 71 | + self.listener = self.conn.load_balancer.create_listener( |
| 72 | + name=Config.get_lb_listener_name(), |
| 73 | + loadbalancer_id=self.lb.id, |
| 74 | + protocol="TCP", |
| 75 | + protocol_port=Config.get_lb_member_tcp_port(), |
| 76 | + ) |
| 77 | + self.conn.load_balancer.wait_for_load_balancer(self.lb.id) |
| 78 | + |
| 79 | + self.pool = self.get_pool_by_name(Config.get_lb_pool_name()) |
| 80 | + if self.pool is not None: |
| 81 | + LOGGER.info( |
| 82 | + f"Load balancer pool with name {Config.get_lb_pool_name()} already exists" |
| 83 | + ) |
| 84 | + else: |
| 85 | + LOGGER.info( |
| 86 | + f"Create load balancer pool with name {Config.get_lb_pool_name()}" |
| 87 | + ) |
| 88 | + self.pool = self.conn.load_balancer.create_pool( |
| 89 | + name=Config.get_lb_pool_name(), |
| 90 | + listener_id=self.listener.id, |
| 91 | + protocol="TCP", |
| 92 | + lb_algorithm="ROUND_ROBIN", |
| 93 | + ) |
| 94 | + self.conn.load_balancer.wait_for_load_balancer(self.lb.id) |
| 95 | + |
| 96 | + self.health_monitor = self.get_health_monitor_for_pool(self.pool.id) |
| 97 | + if self.health_monitor is not None: |
| 98 | + LOGGER.info( |
| 99 | + f"Health monitor for pool with name {Config.get_lb_pool_name()} already exists" |
| 100 | + ) |
| 101 | + else: |
| 102 | + self.conn.load_balancer.create_health_monitor( |
| 103 | + self.pool.id, type="TCP", delay=5, timeout=3, max_retries=3 |
| 104 | + ) |
| 105 | + |
| 106 | + def add_member_to_load_balancer(self, ip: str): |
| 107 | + if self.lb is None: |
| 108 | + raise RuntimeError("Loadbalancer has not been created of gathered") |
| 109 | + |
| 110 | + self.conn.load_balancer.create_member( |
| 111 | + self.pool.id, |
| 112 | + address=ip, |
| 113 | + protocol_port=Config.get_lb_member_tcp_port(), |
| 114 | + subnet_id=self.project.vip_subnet_id, |
| 115 | + ) |
| 116 | + |
| 117 | + def add_members_to_load_balancer(self): |
| 118 | + for machine in self.project.workload_machines: |
| 119 | + LOGGER.info(f"Adding member to load balancer with ip {machine.internal_ip}") |
| 120 | + self.add_member_to_load_balancer(machine.internal_ip) |
0 commit comments