@@ -55,17 +55,18 @@ async def on_cleanup(**kwargs):
5555
5656
5757class 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.
6061 """
6162
6263 def __init__ (self , finalizer , cluster ):
6364 super ().__init__ (f"finalizer '{ finalizer } ' still present for cluster { cluster } " )
6465
6566
6667class 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+
6970 e.g. while waiting for deletion.
7071 """
7172
@@ -74,9 +75,7 @@ def __init__(self, resource, cluster):
7475
7576
7677async 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."""
8079 async for fip in resource .list ():
8180 if not fip .description .startswith (
8281 "Floating IP for Kubernetes external service"
@@ -88,18 +87,14 @@ async def fips_for_cluster(resource, cluster):
8887
8988
9089async 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."""
9491 async for lb in resource .list ():
9592 if lb .name .startswith (f"kube_service_{ cluster } _" ):
9693 yield lb
9794
9895
9996async 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."""
10398 async for sg in resource .list ():
10499 if not sg .description .startswith ("Security Group for" ):
105100 continue
@@ -109,9 +104,8 @@ async def secgroups_for_cluster(resource, cluster):
109104
110105
111106async 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+
115109 async for vol in resource .list ():
116110 # CSI Cinder sets metadata on the volumes that we can look for
117111 owner = vol .metadata .get ("cinder.csi.openstack.org/cluster" )
@@ -125,9 +119,7 @@ async def filtered_volumes_for_cluster(resource, cluster):
125119
126120
127121async 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."""
131123 async for snapshot in resource .list ():
132124 # CSI Cinder sets metadata on the volumes that we can look for
133125 owner = snapshot .metadata .get ("cinder.csi.openstack.org/cluster" )
@@ -136,9 +128,7 @@ async def snapshots_for_cluster(resource, cluster):
136128
137129
138130async 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."""
142132 try :
143133 _ = await async_iterator .__anext__ ()
144134 except StopAsyncIteration :
@@ -148,8 +138,7 @@ async def empty(async_iterator):
148138
149139
150140async 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.
153142
154143 It returns a boolean indicating whether a check is required for the resource.
155144 """
@@ -161,7 +150,7 @@ async def try_delete(logger, resource, instances, **kwargs):
161150 except httpx .HTTPStatusError as exc :
162151 if exc .response .status_code in {400 , 409 }:
163152 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 "
165154 f"{ resource .singular_name } with ID { instance .id } - will retry"
166155 )
167156 else :
@@ -172,9 +161,8 @@ async def try_delete(logger, resource, instances, **kwargs):
172161async def purge_openstack_resources (
173162 logger , clouds , cloud_name , cacert , name , include_volumes , include_appcred
174163):
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+
178166 # Use the credential to delete external resources as required
179167 async with openstack .Cloud .from_clouds (clouds , cloud_name , cacert ) as cloud :
180168 if not cloud .is_authenticated :
@@ -202,7 +190,7 @@ async def purge_openstack_resources(
202190 )
203191 logger .info ("deleted load balancers for LoadBalancer services" )
204192
205- # Delete any security groups associated with loadbalancer services for the cluster
193+ # Delete security groups associated with loadbalancer services for the cluster
206194 secgroups = networkapi .resource ("security-groups" )
207195 check_secgroups = await try_delete (
208196 logger , secgroups , secgroups_for_cluster (secgroups , name )
@@ -216,8 +204,8 @@ async def purge_openstack_resources(
216204 # a historically valid alias, see:
217205 # - https://docs.openstack.org/keystone/latest/contributor/service-catalog.html
218206 # - 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?
221209 try :
222210 volumeapi = cloud .api_client ("volumev3" )
223211 except KeyError :
@@ -282,9 +270,9 @@ async def purge_openstack_resources(
282270
283271
284272async 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.
288276 """
289277 try :
290278 await resource .patch (
@@ -300,8 +288,7 @@ async def patch_finalizers(resource, name, namespace, finalizers):
300288
301289
302290def retry_event (handler ):
303- """
304- Decorator for retrying events on Kubernetes objects.
291+ """Decorator for retrying events on Kubernetes objects.
305292
306293 Instead of retrying within the handler, potentially on stale data, the object is
307294 annotated with the number of times it has been retried. This triggers a new event
@@ -370,14 +357,13 @@ async def on_openstackcluster_event(
370357async def _on_openstackcluster_event_impl (
371358 name , namespace , meta , labels , spec , logger , ** kwargs
372359):
373- """
374- Executes whenever an event occurs for an OpenStack cluster.
375- """
360+ """Executes whenever an event occurs for an OpenStack cluster."""
361+
376362 # Get the resource for manipulating OpenStackClusters at the preferred version
377363 openstackclusters = await _get_os_cluster_client ()
378364
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.
381367 clustername = labels .get ("cluster.x-k8s.io/cluster-name" , name )
382368 logger .debug (f"cluster name that will be used for cleanup: '{ clustername } '" )
383369
@@ -451,7 +437,8 @@ async def _on_openstackcluster_event_impl(
451437 await _delete_secret (clouds_secret .metadata ["name" ], namespace )
452438 logger .info ("cloud credential secret deleted" )
453439 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
455442 raise FinalizerStillPresentError (
456443 next (f for f in finalizers if f != FINALIZER ), name
457444 )
0 commit comments