Copilot commented on code in PR #931:
URL: https://github.com/apache/solr-operator/pull/931#discussion_r4787526183
##########
controllers/solrcloud_controller.go:
##########
@@ -1234,6 +1250,80 @@ func (r *SolrCloudReconciler)
storageClassAllowsExpansion(ctx context.Context, c
return allowed, className, nil
}
+// reconcileStatefulSetRecreation replaces the StatefulSet, keeping its pods,
once the data PVCs have been expanded
+// beyond the size in its volumeClaimTemplates, which Kubernetes does not
allow to be updated.
+// Otherwise pods added by a later scale-up would get PVCs of the original
size.
+//
+// The StatefulSet is deleted with its dependents orphaned. A finalizer keeps
it around until it is replaced,
+// so that nothing stored on it (e.g. the clusterOp annotations) is lost.
+func (r *SolrCloudReconciler) reconcileStatefulSetRecreation(ctx
context.Context, instance *solrv1beta1.SolrCloud, statefulSet
*appsv1.StatefulSet, logger logr.Logger) (inProgress bool, err error) {
+ if !statefulSet.DeletionTimestamp.IsZero() {
+ return true, r.finishStatefulSetRecreation(ctx, statefulSet,
logger)
+ }
+ newSize, isExpanded := util.ExpandedDataPvcSize(statefulSet)
+ if !isExpanded {
+ return false, nil
+ }
+ logger.Info("Recreating StatefulSet, keeping its pods, to use the
expanded data PVC size for new pods", "size", newSize.String())
+ if controllerutil.AddFinalizer(statefulSet,
util.SolrStatefulSetRecreateFinalizer) {
+ if err = r.Update(ctx, statefulSet); err != nil {
+ return true, err
+ }
+ }
+ r.Recorder.Eventf(instance, corev1.EventTypeNormal,
"StatefulSetRecreated",
+ "Recreating the StatefulSet, keeping its pods, so that new pods
get data PersistentVolumeClaims of the expanded size %s", newSize.String())
+ return true, r.Delete(ctx, statefulSet,
client.PropagationPolicy(metav1.DeletePropagationOrphan),
client.Preconditions{UID: &statefulSet.UID})
+}
+
+// finishStatefulSetRecreation replaces a StatefulSet that is being deleted
for a recreation,
+// once its pods have been orphaned.
+func (r *SolrCloudReconciler) finishStatefulSetRecreation(ctx context.Context,
statefulSet *appsv1.StatefulSet, logger logr.Logger) (err error) {
+ if !controllerutil.ContainsFinalizer(statefulSet,
util.SolrStatefulSetRecreateFinalizer) || len(statefulSet.Finalizers) > 1 {
+ // Either the pods are yet to be orphaned, or this deletion was
not started by the operator.
+ return nil
+ }
+ newStatefulSet := &appsv1.StatefulSet{
+ ObjectMeta: metav1.ObjectMeta{
+ Name: statefulSet.Name,
+ Namespace: statefulSet.Namespace,
+ Labels: statefulSet.Labels,
+ Annotations: statefulSet.Annotations,
+ OwnerReferences: statefulSet.OwnerReferences,
+ },
+ Spec: *statefulSet.Spec.DeepCopy(),
+ }
+ if newSize, isExpanded := util.ExpandedDataPvcSize(statefulSet);
isExpanded {
+
newStatefulSet.Spec.VolumeClaimTemplates[0].Spec.Resources.Requests =
corev1.ResourceList{corev1.ResourceStorage: newSize}
+ }
+ // Removing the last finalizer deletes the StatefulSet
+ controllerutil.RemoveFinalizer(statefulSet,
util.SolrStatefulSetRecreateFinalizer)
+ if err = r.Update(ctx, statefulSet); err != nil {
Review Comment:
Removing the last finalizer deletes the only durable copy of the current
replica count and cluster-operation annotations before the replacement exists.
If the operator restarts here or creation exhausts its retries, reconciliation
regenerates the StatefulSet from the SolrCloud spec, losing the lock/queue and
potentially applying a pending scale-down without replica evacuation. Persist a
recreation snapshot on a surviving resource before releasing the finalizer, and
recover it on the StatefulSet-NotFound path.
##########
controllers/solrcloud_controller.go:
##########
@@ -1234,6 +1250,80 @@ func (r *SolrCloudReconciler)
storageClassAllowsExpansion(ctx context.Context, c
return allowed, className, nil
}
+// reconcileStatefulSetRecreation replaces the StatefulSet, keeping its pods,
once the data PVCs have been expanded
+// beyond the size in its volumeClaimTemplates, which Kubernetes does not
allow to be updated.
+// Otherwise pods added by a later scale-up would get PVCs of the original
size.
+//
+// The StatefulSet is deleted with its dependents orphaned. A finalizer keeps
it around until it is replaced,
+// so that nothing stored on it (e.g. the clusterOp annotations) is lost.
+func (r *SolrCloudReconciler) reconcileStatefulSetRecreation(ctx
context.Context, instance *solrv1beta1.SolrCloud, statefulSet
*appsv1.StatefulSet, logger logr.Logger) (inProgress bool, err error) {
+ if !statefulSet.DeletionTimestamp.IsZero() {
+ return true, r.finishStatefulSetRecreation(ctx, statefulSet,
logger)
+ }
+ newSize, isExpanded := util.ExpandedDataPvcSize(statefulSet)
+ if !isExpanded {
+ return false, nil
+ }
+ logger.Info("Recreating StatefulSet, keeping its pods, to use the
expanded data PVC size for new pods", "size", newSize.String())
+ if controllerutil.AddFinalizer(statefulSet,
util.SolrStatefulSetRecreateFinalizer) {
+ if err = r.Update(ctx, statefulSet); err != nil {
+ return true, err
+ }
+ }
+ r.Recorder.Eventf(instance, corev1.EventTypeNormal,
"StatefulSetRecreated",
+ "Recreating the StatefulSet, keeping its pods, so that new pods
get data PersistentVolumeClaims of the expanded size %s", newSize.String())
+ return true, r.Delete(ctx, statefulSet,
client.PropagationPolicy(metav1.DeletePropagationOrphan),
client.Preconditions{UID: &statefulSet.UID})
+}
+
+// finishStatefulSetRecreation replaces a StatefulSet that is being deleted
for a recreation,
+// once its pods have been orphaned.
+func (r *SolrCloudReconciler) finishStatefulSetRecreation(ctx context.Context,
statefulSet *appsv1.StatefulSet, logger logr.Logger) (err error) {
+ if !controllerutil.ContainsFinalizer(statefulSet,
util.SolrStatefulSetRecreateFinalizer) || len(statefulSet.Finalizers) > 1 {
+ // Either the pods are yet to be orphaned, or this deletion was
not started by the operator.
+ return nil
+ }
+ newStatefulSet := &appsv1.StatefulSet{
+ ObjectMeta: metav1.ObjectMeta{
+ Name: statefulSet.Name,
+ Namespace: statefulSet.Namespace,
+ Labels: statefulSet.Labels,
+ Annotations: statefulSet.Annotations,
+ OwnerReferences: statefulSet.OwnerReferences,
+ },
+ Spec: *statefulSet.Spec.DeepCopy(),
+ }
+ if newSize, isExpanded := util.ExpandedDataPvcSize(statefulSet);
isExpanded {
+
newStatefulSet.Spec.VolumeClaimTemplates[0].Spec.Resources.Requests =
corev1.ResourceList{corev1.ResourceStorage: newSize}
+ }
+ // Removing the last finalizer deletes the StatefulSet
+ controllerutil.RemoveFinalizer(statefulSet,
util.SolrStatefulSetRecreateFinalizer)
+ if err = r.Update(ctx, statefulSet); err != nil {
+ return client.IgnoreNotFound(err)
+ }
+ if err = retry.OnError(retry.DefaultBackoff, func(error) bool { return
true }, func() error {
+ return r.Create(ctx, newStatefulSet)
+ }); err != nil {
+ logger.Error(err, "Could not create the replacement
StatefulSet, it will be generated from the SolrCloud instead")
+ }
+ return err
+}
+
+// finishInterruptedStatefulSetRecreation makes sure that a SolrCloud that is
deleted while its StatefulSet is being
+// recreated does not leave behind a StatefulSet that cannot be deleted, or
pods that are no longer owned.
+func (r *SolrCloudReconciler) finishInterruptedStatefulSetRecreation(ctx
context.Context, instance *solrv1beta1.SolrCloud, logger logr.Logger)
(reconcile.Result, error) {
+ statefulSet := &appsv1.StatefulSet{}
+ if err := r.Get(ctx, types.NamespacedName{Name:
instance.StatefulSetName(), Namespace: instance.Namespace}, statefulSet); err
!= nil {
+ return reconcile.Result{}, client.IgnoreNotFound(err)
+ }
+ if statefulSet.DeletionTimestamp.IsZero() ||
!controllerutil.ContainsFinalizer(statefulSet,
util.SolrStatefulSetRecreateFinalizer) {
+ return reconcile.Result{}, nil
+ }
+ if err := r.finishStatefulSetRecreation(ctx, statefulSet, logger); err
!= nil {
+ return reconcile.Result{}, err
+ }
Review Comment:
When the SolrCloud has already disappeared, the replacement StatefulSet
retains an owner reference to that deleted cloud, so garbage collection can
delete it before it adopts the orphaned pods. Those pods can remain ownerless
indefinitely because subsequent calls return NotFound without cleaning them up.
Explicitly clean up the orphaned dependents in the deleted-cloud path instead
of relying on adoption by a replacement that is itself eligible for garbage
collection.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]