Skip to content

Commit 85d5400

Browse files
author
Marc Schöchlin
committed
Add first draft
1 parent 7f37f76 commit 85d5400

2 files changed

Lines changed: 151 additions & 4 deletions

File tree

src/openstack_workload_generator/entities/helpers.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class Config:
3232
"verify_ssl_certificate": "false",
3333
"cloud_init_extra_script": """#!/bin/bash\necho "HELLO WORLD"; date > READY; whoami >> READY""",
3434
"wait_for_server_timeout": "300",
35+
"lb_name": "workload-generator-lb",
36+
"lb_listener_name": "workload-generator-listener",
37+
"lb_pool_name": "workload-generator-pool",
38+
"lb_member_tcp_port": "22",
39+
"lb_tcp_port": "22",
3540
}
3641

3742
_file: str | None = None
@@ -142,8 +147,10 @@ def get_number_of_floating_ips_per_project() -> int:
142147
@staticmethod
143148
def get_admin_vm_password() -> str:
144149
if Config.get("admin_vm_password").upper() == "ASK_PASSWORD":
145-
Config._config["admin_vm_password"] = getpass.getpass("Enter the wanted admin_vm_password: ")
146-
return Config.get("admin_vm_password", regex=r".{5,}")
150+
Config._config["admin_vm_password"] = getpass.getpass(
151+
"Enter the wanted admin_vm_password: "
152+
)
153+
return Config.get("admin_vm_password", regex=r".{5,}")
147154

148155
@staticmethod
149156
def get_vm_flavor() -> str:
@@ -180,7 +187,9 @@ def get_admin_vm_ssh_key() -> str:
180187
@staticmethod
181188
def get_admin_domain_password() -> str:
182189
if Config.get("admin_domain_password").upper() == "ASK_PASSWORD":
183-
Config._config["admin_domain_password"] = getpass.getpass("Enter the wanted admin_domain_password: ")
190+
Config._config["admin_domain_password"] = getpass.getpass(
191+
"Enter the wanted admin_domain_password: "
192+
)
184193
return Config.get("admin_domain_password", regex=r".{5,}")
185194

186195
@staticmethod
@@ -216,9 +225,29 @@ def quota(quota_name: str, quota_category: str, default_value: int) -> int:
216225
return default_value
217226

218227
@staticmethod
219-
def get_network_mtu():
228+
def get_network_mtu() -> int:
220229
return int(Config.get("network_mtu", regex=r"\d+"))
221230

231+
@staticmethod
232+
def get_lb_name() -> str:
233+
return Config.get("lb_name", regex=r"\s+")
234+
235+
@staticmethod
236+
def get_lb_listener_name() -> str:
237+
return Config.get("lb_listener_name", regex=r"\s+")
238+
239+
@staticmethod
240+
def get_lb_pool_name() -> str:
241+
return Config.get("lb_pool_name", regex=r"\s+")
242+
243+
@staticmethod
244+
def get_lb_member_tcp_port() -> int:
245+
return int(Config.get("lb_member_tcp_port", regex=r"\d+"))
246+
247+
@staticmethod
248+
def get_lb_tcp_port() -> int:
249+
return int(Config.get("lb_tcp_port", regex=r"\d+"))
250+
222251

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

0 commit comments

Comments
 (0)