From a9c691c289575b7355a513a33dd779d3134e0dcd Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Fri, 30 Jun 2023 15:56:03 +0900 Subject: [PATCH] Do not delete KeystoneService with associated KeystoneEndpoint The KeystoneEndpoint CR is dependent on the KeystoneService CR, because keystone endpoints requires that the target service is created in advance. This adds the finalizer to avoid removing KeystoneService before all associated KeystoneEndpoint instances are deleted. --- controllers/keystoneendpoint_controller.go | 29 +++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/controllers/keystoneendpoint_controller.go b/controllers/keystoneendpoint_controller.go index 29a1e3d9..d5835fef 100644 --- a/controllers/keystoneendpoint_controller.go +++ b/controllers/keystoneendpoint_controller.go @@ -47,7 +47,8 @@ type KeystoneEndpointReconciler struct { //+kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneendpoints/finalizers,verbs=update //+kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneapis,verbs=get;list;update;patch //+kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneapis/finalizers,verbs=update -//+kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneservices,verbs=get;list +//+kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneservices,verbs=get;list;update;patch +//+kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneservices/finalizers,verbs=update // Reconcile keystone endpoint requests func (r *KeystoneEndpointReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { @@ -269,6 +270,20 @@ func (r *KeystoneEndpointReconciler) reconcileDelete( } } + ksSvc, err := keystonev1.GetKeystoneServiceWithName(ctx, helper, instance.Spec.ServiceName, instance.Namespace) + if err == nil { + // Remove the finalizer for this endpoint from the Service + if controllerutil.RemoveFinalizer(ksSvc, fmt.Sprintf("%s-%s", helper.GetFinalizer(), instance.Name)) { + err := r.Update(ctx, ksSvc) + + if err != nil { + return ctrl.Result{}, err + } + } + } else if ! k8s_errors.IsNotFound(err) { + return ctrl.Result{}, err + } + // There are certain deletion scenarios where we might not have the keystoneAPI if keystoneAPI != nil { // Remove the finalizer for this endpoint from the KeystoneAPI @@ -324,6 +339,18 @@ func (r *KeystoneEndpointReconciler) reconcileNormal( instance.Status.ServiceID = ksSvc.Status.ServiceID + // + // Add a finalizer to KeystoneService, because KeystoneEndpoint is dependent on + // the service entry created by KeystoneService + // + if controllerutil.AddFinalizer(ksSvc, fmt.Sprintf("%s-%s", helper.GetFinalizer(), instance.Name)) { + err := r.Update(ctx, ksSvc) + + if err != nil { + return ctrl.Result{}, err + } + } + // // create/update endpoints //