func RecreateStatefulSets()

in pkg/controller/common/volume/pvc_expansion.go [168:233]


func RecreateStatefulSets(ctx context.Context, k8sClient k8s.Client, owner client.Object, ownerKind string) (int, error) {
	log := ulog.FromContext(ctx)
	recreateList, err := ssetsToRecreate(owner, ownerKind)
	if err != nil {
		return 0, err
	}
	recreations := len(recreateList)

	for annotation, toRecreate := range recreateList {
		toRecreate := toRecreate

		namespacedName := namespacedNameFromObject(owner)

		var existing appsv1.StatefulSet
		err = k8sClient.Get(ctx, k8s.ExtractNamespacedName(&toRecreate), &existing)
		switch {
		// error case
		case err != nil && !apierrors.IsNotFound(err):
			return recreations, err

		// already exists with the same UID: deletion case
		case existing.UID == toRecreate.UID && !apierrors.IsNotFound(err):

			log.Info("Deleting StatefulSet to account for resized PVCs, it will be recreated automatically",
				"namespace", namespacedName.Namespace, "name", namespacedName.Name, "statefulset_name", existing.Name)
			// mark the Pod as owned by the component resource while the StatefulSet is removed
			if err := updatePodOwners(ctx, k8sClient, owner, ownerKind, existing); err != nil {
				return recreations, err
			}
			if err := deleteStatefulSet(ctx, k8sClient, existing); err != nil {
				if apierrors.IsNotFound(err) {
					return recreations, nil
				}
				return recreations, err
			}

		// already deleted: creation case
		case err != nil && apierrors.IsNotFound(err):
			log.Info("Re-creating StatefulSet to account for resized PVCs",
				"namespace", namespacedName.Namespace, "name", namespacedName.Name, "statefulset_name", toRecreate.Name)
			if err := createStatefulSet(ctx, k8sClient, toRecreate); err != nil {
				return recreations, err
			}

		// already recreated (existing.UID != toRecreate.UID): we're done
		default:
			// remove the temporary pod owner set before the StatefulSet was deleted
			if err := removePodOwner(ctx, k8sClient, owner, ownerKind, existing); err != nil {
				return recreations, err
			}
			// remove the annotation
			err := deleteAnnotation(owner, annotation)
			if err != nil {
				return recreations, err
			}

			if err := k8sClient.Update(ctx, owner); err != nil {
				return recreations, err
			}
			// one less recreation
			recreations--
		}
	}

	return recreations, nil
}