@@ -55,17 +55,18 @@ async def on_cleanup(**kwargs):
55
55
56
56
57
57
class FinalizerStillPresentError (Exception ):
58
- """
59
- Raised when a finalizer from another controller is preventing us from deleting an appcred.
58
+ """Raised when a finalizer from another controller is preventing us from
59
+
60
+ deleting an appcred.
60
61
"""
61
62
62
63
def __init__ (self , finalizer , cluster ):
63
64
super ().__init__ (f"finalizer '{ finalizer } ' still present for cluster { cluster } " )
64
65
65
66
66
67
class ResourcesStillPresentError (Exception ):
67
- """
68
- Raised when cluster resources are still present even after being deleted,
68
+ """Raised when cluster resources are still present even after being deleted,
69
+
69
70
e.g. while waiting for deletion.
70
71
"""
71
72
@@ -74,9 +75,7 @@ def __init__(self, resource, cluster):
74
75
75
76
76
77
async def fips_for_cluster (resource , cluster ):
77
- """
78
- Async iterator for FIPs belonging to the specified cluster.
79
- """
78
+ """Async iterator for FIPs belonging to the specified cluster."""
80
79
async for fip in resource .list ():
81
80
if not fip .description .startswith (
82
81
"Floating IP for Kubernetes external service"
@@ -88,18 +87,14 @@ async def fips_for_cluster(resource, cluster):
88
87
89
88
90
89
async def lbs_for_cluster (resource , cluster ):
91
- """
92
- Async iterator for loadbalancers belonging to the specified cluster.
93
- """
90
+ """Async iterator for loadbalancers belonging to the specified cluster."""
94
91
async for lb in resource .list ():
95
92
if lb .name .startswith (f"kube_service_{ cluster } _" ):
96
93
yield lb
97
94
98
95
99
96
async def secgroups_for_cluster (resource , cluster ):
100
- """
101
- Async iterator for security groups belonging to the specified cluster.
102
- """
97
+ """Async iterator for security groups belonging to the specified cluster."""
103
98
async for sg in resource .list ():
104
99
if not sg .description .startswith ("Security Group for" ):
105
100
continue
@@ -109,9 +104,8 @@ async def secgroups_for_cluster(resource, cluster):
109
104
110
105
111
106
async def filtered_volumes_for_cluster (resource , cluster ):
112
- """
113
- Async iterator for volumes belonging to the specified cluster.
114
- """
107
+ """Async iterator for volumes belonging to the specified cluster."""
108
+
115
109
async for vol in resource .list ():
116
110
# CSI Cinder sets metadata on the volumes that we can look for
117
111
owner = vol .metadata .get ("cinder.csi.openstack.org/cluster" )
@@ -125,9 +119,7 @@ async def filtered_volumes_for_cluster(resource, cluster):
125
119
126
120
127
121
async def snapshots_for_cluster (resource , cluster ):
128
- """
129
- Async iterator for snapshots belonging to the specified cluster.
130
- """
122
+ """Async iterator for snapshots belonging to the specified cluster."""
131
123
async for snapshot in resource .list ():
132
124
# CSI Cinder sets metadata on the volumes that we can look for
133
125
owner = snapshot .metadata .get ("cinder.csi.openstack.org/cluster" )
@@ -136,9 +128,7 @@ async def snapshots_for_cluster(resource, cluster):
136
128
137
129
138
130
async def empty (async_iterator ):
139
- """
140
- Returns True if the given async iterator is empty, False otherwise.
141
- """
131
+ """Returns True if the given async iterator is empty, False otherwise."""
142
132
try :
143
133
_ = await async_iterator .__anext__ ()
144
134
except StopAsyncIteration :
@@ -148,8 +138,7 @@ async def empty(async_iterator):
148
138
149
139
150
140
async def try_delete (logger , resource , instances , ** kwargs ):
151
- """
152
- Tries to delete the specified instances, catching 400 and 409 exceptions for retry.
141
+ """Tries to delete the specified instances, catches 400 & 409 exceptions for retry.
153
142
154
143
It returns a boolean indicating whether a check is required for the resource.
155
144
"""
@@ -161,7 +150,7 @@ async def try_delete(logger, resource, instances, **kwargs):
161
150
except httpx .HTTPStatusError as exc :
162
151
if exc .response .status_code in {400 , 409 }:
163
152
logger .warn (
164
- f"got status code { exc .response .status_code } when attempting to delete "
153
+ f"got status code { exc .response .status_code } when trying to delete "
165
154
f"{ resource .singular_name } with ID { instance .id } - will retry"
166
155
)
167
156
else :
@@ -172,9 +161,8 @@ async def try_delete(logger, resource, instances, **kwargs):
172
161
async def purge_openstack_resources (
173
162
logger , clouds , cloud_name , cacert , name , include_volumes , include_appcred
174
163
):
175
- """
176
- Cleans up the OpenStack resources created by the OCCM and CSI for a cluster.
177
- """
164
+ """Cleans up the OpenStack resources created by the OCCM and CSI for a cluster."""
165
+
178
166
# Use the credential to delete external resources as required
179
167
async with openstack .Cloud .from_clouds (clouds , cloud_name , cacert ) as cloud :
180
168
if not cloud .is_authenticated :
@@ -202,7 +190,7 @@ async def purge_openstack_resources(
202
190
)
203
191
logger .info ("deleted load balancers for LoadBalancer services" )
204
192
205
- # Delete any security groups associated with loadbalancer services for the cluster
193
+ # Delete security groups associated with loadbalancer services for the cluster
206
194
secgroups = networkapi .resource ("security-groups" )
207
195
check_secgroups = await try_delete (
208
196
logger , secgroups , secgroups_for_cluster (secgroups , name )
@@ -216,8 +204,8 @@ async def purge_openstack_resources(
216
204
# a historically valid alias, see:
217
205
# - https://docs.openstack.org/keystone/latest/contributor/service-catalog.html
218
206
# - https://service-types.openstack.org/service-types.json
219
- # TODO: Make use of https://opendev.org/openstack/os-service-types to improve
220
- # service type alias handling?
207
+ # TODO(sd109) : Make use of https://opendev.org/openstack/os-service-types to
208
+ # improve service type alias handling?
221
209
try :
222
210
volumeapi = cloud .api_client ("volumev3" )
223
211
except KeyError :
@@ -282,9 +270,9 @@ async def purge_openstack_resources(
282
270
283
271
284
272
async def patch_finalizers (resource , name , namespace , finalizers ):
285
- """
286
- Patches the finalizers of a resource. If the resource does not exist any
287
- more , that is classed as a success.
273
+ """Patches the finalizers of a resource.
274
+
275
+ If the resource does not exist anymore , that is classed as a success.
288
276
"""
289
277
try :
290
278
await resource .patch (
@@ -300,8 +288,7 @@ async def patch_finalizers(resource, name, namespace, finalizers):
300
288
301
289
302
290
def retry_event (handler ):
303
- """
304
- Decorator for retrying events on Kubernetes objects.
291
+ """Decorator for retrying events on Kubernetes objects.
305
292
306
293
Instead of retrying within the handler, potentially on stale data, the object is
307
294
annotated with the number of times it has been retried. This triggers a new event
@@ -370,14 +357,13 @@ async def on_openstackcluster_event(
370
357
async def _on_openstackcluster_event_impl (
371
358
name , namespace , meta , labels , spec , logger , ** kwargs
372
359
):
373
- """
374
- Executes whenever an event occurs for an OpenStack cluster.
375
- """
360
+ """Executes whenever an event occurs for an OpenStack cluster."""
361
+
376
362
# Get the resource for manipulating OpenStackClusters at the preferred version
377
363
openstackclusters = await _get_os_cluster_client ()
378
364
379
- # Use the value of the `cluster.x-k8s.io/cluster-name` label as the cluster name if it exists,
380
- # otherwise, fall back to the OpenStackCluster resource name.
365
+ # Use the value of the `cluster.x-k8s.io/cluster-name` label as the cluster name
366
+ # if it exists, otherwise, fall back to the OpenStackCluster resource name.
381
367
clustername = labels .get ("cluster.x-k8s.io/cluster-name" , name )
382
368
logger .debug (f"cluster name that will be used for cleanup: '{ clustername } '" )
383
369
@@ -451,7 +437,8 @@ async def _on_openstackcluster_event_impl(
451
437
await _delete_secret (clouds_secret .metadata ["name" ], namespace )
452
438
logger .info ("cloud credential secret deleted" )
453
439
elif remove_appcred :
454
- # If the annotation says delete but other controllers are still acting, go round again
440
+ # If the annotation says delete but other controllers are still acting,
441
+ # go round again
455
442
raise FinalizerStillPresentError (
456
443
next (f for f in finalizers if f != FINALIZER ), name
457
444
)
0 commit comments