This is an automated email from the ASF dual-hosted git repository.

janhoy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr-operator.git


The following commit(s) were added to refs/heads/main by this push:
     new b607cd91 Let a SolrCloud be deleted when its providedConfigMap is gone 
(#888)
b607cd91 is described below

commit b607cd916d2db49223ebe264352ae01aa8bb7869
Author: Syed Ahnaf Raza <[email protected]>
AuthorDate: Fri Nov 20 14:52:26 2026 +0500

    Let a SolrCloud be deleted when its providedConfigMap is gone (#888)
    
    Co-authored-by: Jan Høydahl <[email protected]>
---
 controllers/solrcloud_controller.go              |  12 ++-
 controllers/solrcloud_controller_storage_test.go | 118 +++++++++++++++++++++++
 helm/solr-operator/Chart.yaml                    |   7 ++
 3 files changed, 132 insertions(+), 5 deletions(-)

diff --git a/controllers/solrcloud_controller.go 
b/controllers/solrcloud_controller.go
index d3b09d02..89e9dda2 100644
--- a/controllers/solrcloud_controller.go
+++ b/controllers/solrcloud_controller.go
@@ -117,13 +117,15 @@ func (r *SolrCloudReconciler) Reconcile(ctx 
context.Context, req ctrl.Request) (
                return reconcile.Result{Requeue: true}, nil
        }
 
+       // Owned resources are garbage collected, so a SolrCloud being deleted 
only needs its storage finalizer handled.
+       // This keeps e.g. a deleted providedConfigMap or an unsupported Solr 
version from blocking the deletion.
+       if !instance.DeletionTimestamp.IsZero() {
+               return r.reconcileStorageFinalizerForDeletion(ctx, instance, 
logger)
+       }
+
        // Version-specific resources cannot be generated without a supported 
Solr major version.
        // Wait for the user to fix the image tag, which triggers a new 
reconcile.
        if _, versionErr := instance.SolrVersion(); versionErr != nil {
-               if !instance.DeletionTimestamp.IsZero() {
-                       // Don't block the deletion of the SolrCloud on its 
storage finalizer
-                       return r.reconcileStorageFinalizerForDeletion(ctx, 
instance, logger)
-               }
                logger.Error(versionErr, "Not reconciling SolrCloud, its Solr 
version is not supported")
                r.Recorder.Eventf(instance, corev1.EventTypeWarning, 
"UnsupportedSolrVersion",
                        "Not reconciling SolrCloud: %s", versionErr.Error())
@@ -1275,7 +1277,7 @@ func (r *SolrCloudReconciler) 
reconcileStorageFinalizer(ctx context.Context, clo
 }
 
 // reconcileStorageFinalizerForDeletion handles the storage finalizer of a 
SolrCloud that is being deleted,
-// without reconciling anything else. It is used when the rest of the 
SolrCloud cannot be reconciled.
+// without reconciling anything else.
 func (r *SolrCloudReconciler) reconcileStorageFinalizerForDeletion(ctx 
context.Context, cloud *solrv1beta1.SolrCloud, logger logr.Logger) 
(reconcile.Result, error) {
        statefulSet := &appsv1.StatefulSet{}
        if err := r.Get(ctx, types.NamespacedName{Namespace: cloud.Namespace, 
Name: cloud.StatefulSetName()}, statefulSet); err != nil {
diff --git a/controllers/solrcloud_controller_storage_test.go 
b/controllers/solrcloud_controller_storage_test.go
index 87a5c357..1623b175 100644
--- a/controllers/solrcloud_controller_storage_test.go
+++ b/controllers/solrcloud_controller_storage_test.go
@@ -25,6 +25,7 @@ import (
        . "github.com/onsi/gomega"
        appsv1 "k8s.io/api/apps/v1"
        corev1 "k8s.io/api/core/v1"
+       "k8s.io/apimachinery/pkg/api/errors"
        "k8s.io/apimachinery/pkg/api/resource"
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 )
@@ -169,6 +170,123 @@ var _ = FDescribe("SolrCloud controller - Storage", 
func() {
                })
        })
 
+       FContext("Persistent Data - PVC Delete on Reclaim with a deleted 
provided ConfigMap", func() {
+               var providedConfigMap *corev1.ConfigMap
+
+               BeforeEach(func(ctx context.Context) {
+                       providedConfigMap = &corev1.ConfigMap{
+                               ObjectMeta: metav1.ObjectMeta{
+                                       Name:      
"provided-config-deleted-first",
+                                       Namespace: solrCloud.Namespace,
+                               },
+                               Data: map[string]string{
+                                       util.SolrXmlFile: "<solr><int 
name=\"hostPort\">${solr.port.advertise:80}</int></solr>",
+                               },
+                       }
+                       Expect(k8sClient.Create(ctx, 
providedConfigMap)).To(Succeed())
+
+                       solrCloud.Spec.CustomSolrKubeOptions.ConfigMapOptions = 
&solrv1beta1.ConfigMapOptions{
+                               ProvidedConfigMap: providedConfigMap.Name,
+                       }
+                       solrCloud.Spec.StorageOptions = 
solrv1beta1.SolrDataStorageOptions{
+                               PersistentStorage: 
&solrv1beta1.SolrPersistentDataStorageOptions{
+                                       VolumeReclaimPolicy: 
solrv1beta1.VolumeReclaimPolicyDelete,
+                                       PersistentVolumeClaimTemplate: 
solrv1beta1.PersistentVolumeClaimTemplate{
+                                               Spec: 
corev1.PersistentVolumeClaimSpec{
+                                                       Resources: 
corev1.VolumeResourceRequirements{
+                                                               Requests: 
corev1.ResourceList{
+                                                                       
corev1.ResourceStorage: resource.MustParse("5Gi"),
+                                                               },
+                                                       },
+                                               },
+                                       },
+                               },
+                       }
+               })
+               // dataClaim carries the StatefulSet selector labels; 
otherClaim only shares the cloud label and must survive.
+               createClaims := func(ctx context.Context) (dataClaim, 
otherClaim *corev1.PersistentVolumeClaim) {
+                       claim := func(name string, labels map[string]string) 
*corev1.PersistentVolumeClaim {
+                               pvc := &corev1.PersistentVolumeClaim{
+                                       ObjectMeta: metav1.ObjectMeta{Name: 
name, Namespace: solrCloud.Namespace, Labels: labels},
+                                       Spec: corev1.PersistentVolumeClaimSpec{
+                                               AccessModes: 
[]corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
+                                               Resources: 
corev1.VolumeResourceRequirements{
+                                                       Requests: 
corev1.ResourceList{corev1.ResourceStorage: resource.MustParse("5Gi")},
+                                               },
+                                       },
+                               }
+                               Expect(k8sClient.Create(ctx, pvc)).To(Succeed())
+                               // envtest has no controller to drop the 
pvc-protection finalizer, so clear it to really remove the PVC.
+                               DeferCleanup(func(ctx context.Context) {
+                                       found := &corev1.PersistentVolumeClaim{}
+                                       if err := k8sClient.Get(ctx, 
resourceKey(solrCloud, name), found); errors.IsNotFound(err) {
+                                               return
+                                       }
+                                       found.Finalizers = nil
+                                       Expect(k8sClient.Update(ctx, 
found)).To(Succeed())
+                                       _ = k8sClient.Delete(ctx, found)
+                               })
+                               return pvc
+                       }
+                       return claim("data-"+solrCloud.StatefulSetName()+"-0", 
util.SolrCloudSelectorLabels(solrCloud)), claim(solrCloud.Name+"-other-claim", 
solrCloud.SharedLabels())
+               }
+
+               deleteCloudAndCheckClaims := func(ctx context.Context, 
dataClaim, otherClaim *corev1.PersistentVolumeClaim) {
+                       By("deleting the SolrCloud")
+                       Expect(k8sClient.Delete(ctx, solrCloud)).To(Succeed(), 
"Could not delete solrCloud")
+
+                       Eventually(func() error {
+                               return k8sClient.Get(ctx, 
resourceKey(solrCloud, solrCloud.Name), &solrv1beta1.SolrCloud{})
+                       }).Should(MatchError("solrclouds.solr.apache.org 
\""+solrCloud.Name+"\" not found"), "Cloud has not been deleted, thus the 
finalizers have not been removed from the object.")
+
+                       // envtest runs no controller manager, so a deleted PVC 
may linger with only the pvc-protection finalizer.
+                       found := &corev1.PersistentVolumeClaim{}
+                       err := k8sClient.Get(ctx, resourceKey(solrCloud, 
dataClaim.Name), found)
+                       if err == nil {
+                               Expect(found.DeletionTimestamp).ToNot(BeNil(), 
"The Solr data PVC was not deleted")
+                       } else {
+                               Expect(errors.IsNotFound(err)).To(BeTrue(), 
"Unexpected error getting the Solr data PVC: %v", err)
+                       }
+
+                       Expect(k8sClient.Get(ctx, resourceKey(solrCloud, 
otherClaim.Name), found)).To(Succeed(), "A PVC outside the StatefulSet selector 
was deleted")
+                       Expect(found.DeletionTimestamp).To(BeNil(), "A PVC 
outside the StatefulSet selector was deleted")
+               }
+
+               FIt("removes the finalizer and the data PVCs even though the 
provided ConfigMap no longer exists", func(ctx context.Context) {
+                       By("waiting for the storage finalizer to be added")
+                       expectSolrCloudWithChecks(ctx, solrCloud, func(g 
Gomega, found *solrv1beta1.SolrCloud) {
+                               
g.Expect(found.GetFinalizers()).To(ContainElement(util.SolrStorageFinalizer), 
"Storage finalizer was not added")
+                       })
+                       expectStatefulSet(ctx, solrCloud, 
solrCloud.StatefulSetName())
+                       dataClaim, otherClaim := createClaims(ctx)
+
+                       By("deleting the provided ConfigMap before the 
SolrCloud, as Helm can do")
+                       Expect(k8sClient.Delete(ctx, 
providedConfigMap)).To(Succeed())
+
+                       deleteCloudAndCheckClaims(ctx, dataClaim, otherClaim)
+               })
+
+               FIt("removes only the Solr data PVCs when the StatefulSet is 
already gone", func(ctx context.Context) {
+                       By("waiting for the storage finalizer to be added")
+                       expectSolrCloudWithChecks(ctx, solrCloud, func(g 
Gomega, found *solrv1beta1.SolrCloud) {
+                               
g.Expect(found.GetFinalizers()).To(ContainElement(util.SolrStorageFinalizer), 
"Storage finalizer was not added")
+                       })
+                       statefulSet := expectStatefulSet(ctx, solrCloud, 
solrCloud.StatefulSetName())
+                       dataClaim, otherClaim := createClaims(ctx)
+
+                       By("deleting the provided ConfigMap, so the reconcile 
cannot recreate the StatefulSet")
+                       Expect(k8sClient.Delete(ctx, 
providedConfigMap)).To(Succeed())
+
+                       By("deleting the StatefulSet before the SolrCloud, as 
foreground deletion can do")
+                       Expect(k8sClient.Delete(ctx, statefulSet)).To(Succeed())
+                       Eventually(func() bool {
+                               return errors.IsNotFound(k8sClient.Get(ctx, 
resourceKey(solrCloud, statefulSet.Name), &appsv1.StatefulSet{}))
+                       }).Should(BeTrue(), "The StatefulSet was not deleted")
+
+                       deleteCloudAndCheckClaims(ctx, dataClaim, otherClaim)
+               })
+       })
+
        FContext("Ephemeral Data - Default", func() {
                BeforeEach(func() {
                        solrCloud.Spec.StorageOptions = 
solrv1beta1.SolrDataStorageOptions{
diff --git a/helm/solr-operator/Chart.yaml b/helm/solr-operator/Chart.yaml
index 9722870b..8c5e475a 100644
--- a/helm/solr-operator/Chart.yaml
+++ b/helm/solr-operator/Chart.yaml
@@ -248,6 +248,13 @@ annotations:
           url: https://github.com/apache/solr-operator/issues/813
         - name: Github PR
           url: https://github.com/apache/solr-operator/pull/900
+    - kind: fixed
+      description: A SolrCloud that is being deleted no longer gets stuck when 
its providedConfigMap has already been removed, e.g. by Helm in the same 
uninstall
+      links:
+        - name: Github Issue
+          url: https://github.com/apache/solr-operator/issues/828
+        - name: Github PR
+          url: https://github.com/apache/solr-operator/pull/888
   artifacthub.io/images: |
     - name: solr-operator
       image: apache/solr-operator:v0.10.0-prerelease

Reply via email to