Skip to content

Commit e82e29c

Browse files
matofederm-bull
andauthored
Support cluster name determination via label for ClusterClass compatibility (#165)
* Support cluster name determination via label for ClusterClass compatibility Cluster API, when using the ClusterClass approach, automatically sets the `cluster.x-k8s.io/cluster-name` label on the OpenStackCluster resource. It also generates the OpenStackCluster `metadata.name` by appending five random characters (e.g. `<cluster-name>-pr2wr`). As a result, relying solely on the resource name to determine the cluster name caused deletion to fail, since the expected cluster name could not be matched with existing resources, e.g. LBs. This commit updates the cluster name detection logic to first check for the presence of the `cluster.x-k8s.io/cluster-name` label. If the label is found, its value is used as the cluster name. If the label is not present, the OpenStackCluster's `metadata.name` is used as a fallback. This change maintains backward compatibility and does not introduce any breaking behavior for clusters that do not use ClusterClass. Signed-off-by: Matej Feder <[email protected]> * Apply suggestions from code review Co-authored-by: Matt Anson <[email protected]> --------- Signed-off-by: Matej Feder <[email protected]> Co-authored-by: Matt Anson <[email protected]>
1 parent b33aa5c commit e82e29c

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,22 @@ Cluster API `Cluster`, from being removed until the finalizer is removed.
6666
(specifically, it waits for the `deletionTimestamp` to be set, indicating that a deletion
6767
has been requested), at which point it uses the credential from
6868
`OpenStackCluster.spec.identityRef` to remove any dangling resources that were created by
69-
the OCCM or Cinder CSI with the same cluster name as the cluster being deleted. Once all
70-
the resources have been deleted, the finalizer is removed.
69+
the OCCM or Cinder CSI with the same cluster name as the cluster being deleted.
70+
The cluster name is determined by the `cluster.x-k8s.io/cluster-name` label on the
71+
OpenStackCluster resource, if present.
72+
If the label is not set, the name of the OpenStackCluster resource (`metadata.name`) is
73+
used instead.
74+
Once all the resources have been deleted, the finalizer is removed.
7175

7276
> **WARNING**
7377
>
7478
> The cluster name of the OCCM and Cinder CSI **must** be set to the `metadata.name`
75-
> of the `OpenStackCluster` resource.
79+
> of the OpenStackCluster resource, or to the value of the `cluster.x-k8s.io/cluster-name`
80+
> label if it is present on the OpenStackCluster resource.
7681
>
7782
> For instance, the `openstack-cluster` chart from the
7883
> [capi-helm-charts](https://github.com/azimuth-cloud/capi-helm-charts) ensures that this happens
79-
> automatically.
84+
> automatically and sets the OpenStackCluster's `metadata.name` for OCCM and Cinder CSI.
8085

8186
The advantage of this approach vs. a task that runs before the cluster deletion is started
8287
is that the external resource deletion happens _after_ all the machines have been deleted,

capi_janitor/openstack/operator.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,17 @@ async def wrapper(**kwargs):
334334

335335
@kopf.on.event(CAPO_API_GROUP, "openstackclusters")
336336
@retry_event
337-
async def on_openstackcluster_event(name, namespace, meta, spec, logger, **kwargs):
337+
async def on_openstackcluster_event(name, namespace, meta, labels, spec, logger, **kwargs):
338338
"""
339339
Executes whenever an event occurs for an OpenStack cluster.
340340
"""
341341
# Get the resource for manipulating OpenStackClusters at the preferred version
342342
capoapi = await ekclient.api_preferred_version(CAPO_API_GROUP)
343343
openstackclusters = await capoapi.resource("openstackclusters")
344+
# Use the value of the `cluster.x-k8s.io/cluster-name` label as the cluster name if it exists,
345+
# otherwise, fall back to the OpenStackCluster resource name.
346+
clustername = labels.get("cluster.x-k8s.io/cluster-name", name)
347+
logger.debug(f"cluster name that will be used for cleanup: '{clustername}'")
344348

345349
finalizers = meta.get("finalizers", [])
346350
# We add a custom finalizer to OpenStack cluster objects to
@@ -401,7 +405,7 @@ async def on_openstackcluster_event(name, namespace, meta, spec, logger, **kwarg
401405
clouds,
402406
cloud_name,
403407
cacert,
404-
name,
408+
clustername,
405409
volumes_annotation_value == VOLUMES_ANNOTATION_DELETE,
406410
remove_appcred and len(finalizers) == 1
407411
)

0 commit comments

Comments
 (0)