Skip to content

Commit 20024b9

Browse files
author
Matt Pryor
authored
Remove security groups created for LBs by OCCM (#147)
1 parent ff9a597 commit 20024b9

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

capi_janitor/openstack/openstack.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def __init__(self, client, name, prefix = None, plural_name = None, singular_nam
8787
super().__init__(client, name, prefix)
8888
# Some resources support a /detail endpoint
8989
# In this case, we just want to use the name up to the slash as the plural name
90-
self._plural_name = plural_name or self._name.split("/")[0]
90+
self._plural_name = plural_name or self._name.split("/")[0].replace("-", "_")
9191
# If no singular name is given, assume the name ends in 's'
9292
self._singular_name = singular_name or self._plural_name[:-1]
9393

capi_janitor/openstack/operator.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ async def lbs_for_cluster(resource, cluster):
8282
yield lb
8383

8484

85+
async def secgroups_for_cluster(resource, cluster):
86+
"""
87+
Async iterator for security groups belonging to the specified cluster.
88+
"""
89+
async for sg in resource.list():
90+
if not sg.description.startswith("Security Group for"):
91+
continue
92+
if not sg.description.endswith(f"Service LoadBalancer in cluster {cluster}"):
93+
continue
94+
yield sg
95+
96+
8597
async def volumes_for_cluster(resource, cluster):
8698
"""
8799
Async iterator for volumes belonging to the specified cluster.
@@ -180,6 +192,15 @@ async def purge_openstack_resources(
180192
)
181193
logger.info("deleted load balancers for LoadBalancer services")
182194

195+
# Delete any security groups associated with loadbalancer services for the cluster
196+
secgroups = networkapi.resource("security-groups")
197+
check_secgroups = await try_delete(
198+
logger,
199+
secgroups,
200+
secgroups_for_cluster(secgroups, name)
201+
)
202+
logger.info("deleted security groups for LoadBalancer services")
203+
183204
# Delete volumes and snapshots associated with PVCs, unless requested
184205
# otherwise via the annotation
185206
volumeapi = cloud.api_client("volumev3")
@@ -208,6 +229,8 @@ async def purge_openstack_resources(
208229
raise ResourcesStillPresentError("floatingips", name)
209230
if check_lbs and not await empty(lbs_for_cluster(loadbalancers, name)):
210231
raise ResourcesStillPresentError("loadbalancers", name)
232+
if check_secgroups and not await empty(secgroups_for_cluster(secgroups, name)):
233+
raise ResourcesStillPresentError("security-groups", name)
211234
if check_volumes and not await empty(volumes_for_cluster(volumes_detail, name)):
212235
raise ResourcesStillPresentError("volumes", name)
213236
if check_snapshots and not await empty(snapshots_for_cluster(snapshots_detail, name)):

0 commit comments

Comments
 (0)