|
| 1 | +package managedcontrolplane |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 9 | + "k8s.io/apimachinery/pkg/util/sets" |
| 10 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 11 | + |
| 12 | + errutils "github.com/openmcp-project/controller-utils/pkg/errors" |
| 13 | + "github.com/openmcp-project/controller-utils/pkg/logging" |
| 14 | + |
| 15 | + clustersv1alpha1 "github.com/openmcp-project/openmcp-operator/api/clusters/v1alpha1" |
| 16 | + cconst "github.com/openmcp-project/openmcp-operator/api/clusters/v1alpha1/constants" |
| 17 | + corev2alpha1 "github.com/openmcp-project/openmcp-operator/api/core/v2alpha1" |
| 18 | + libutils "github.com/openmcp-project/openmcp-operator/lib/utils" |
| 19 | +) |
| 20 | + |
| 21 | +func (r *ManagedControlPlaneReconciler) deleteRelatedClusterRequests(ctx context.Context, mcp *corev2alpha1.ManagedControlPlane) (sets.Set[string], errutils.ReasonableError) { |
| 22 | + log := logging.FromContextOrPanic(ctx) |
| 23 | + |
| 24 | + // delete depending cluster requests, if any |
| 25 | + crNames := sets.New[string]() |
| 26 | + |
| 27 | + if mcp == nil { |
| 28 | + log.Debug("MCP is nil, no need to check for cluster requests") |
| 29 | + return crNames, nil |
| 30 | + } |
| 31 | + |
| 32 | + // identify cluster request finalizers |
| 33 | + for _, fin := range mcp.Finalizers { |
| 34 | + if strings.HasPrefix(fin, corev2alpha1.ClusterRequestFinalizerPrefix) { |
| 35 | + crNames.Insert(strings.TrimPrefix(fin, corev2alpha1.ClusterRequestFinalizerPrefix)) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + if crNames.Len() == 0 { |
| 40 | + log.Debug("No cluster request finalizers found on MCP") |
| 41 | + return crNames, nil |
| 42 | + } |
| 43 | + |
| 44 | + // fetch cluster requests, if any exist |
| 45 | + namespace := libutils.StableRequestNamespace(mcp.Namespace) |
| 46 | + resources := map[string]*clustersv1alpha1.ClusterRequest{} |
| 47 | + errs := errutils.NewReasonableErrorList() |
| 48 | + for crName := range crNames { |
| 49 | + cr := &clustersv1alpha1.ClusterRequest{} |
| 50 | + cr.SetName(crName) |
| 51 | + cr.SetNamespace(namespace) |
| 52 | + if err := r.PlatformCluster.Client().Get(ctx, client.ObjectKeyFromObject(cr), cr); err != nil { |
| 53 | + if !apierrors.IsNotFound(err) { |
| 54 | + errs.Append(errutils.WithReason(fmt.Errorf("error getting ClusterRequest '%s/%s': %w", namespace, crName, err), cconst.ReasonPlatformClusterInteractionProblem)) |
| 55 | + } |
| 56 | + continue |
| 57 | + } |
| 58 | + resources[crName] = cr |
| 59 | + } |
| 60 | + if rerr := errs.Aggregate(); rerr != nil { |
| 61 | + return sets.KeySet(resources), rerr |
| 62 | + } |
| 63 | + |
| 64 | + // delete cluster requests |
| 65 | + errs = errutils.NewReasonableErrorList() |
| 66 | + for crName, cr := range resources { |
| 67 | + if crName == mcp.Name && len(resources) > 1 { |
| 68 | + // skip the MCP's main ClusterRequest for now |
| 69 | + // we want to make sure that all other ClusterRequests are deleted first |
| 70 | + // in case the corresponding clusters are hosting resources that depend on the MCP cluster |
| 71 | + log.Debug("Skipping deletion of MCP's primary ClusterRequest, because there are other ClusterRequests to delete first", "crName", crName, "namespace", cr.GetNamespace()) |
| 72 | + continue |
| 73 | + } |
| 74 | + if !cr.GetDeletionTimestamp().IsZero() { |
| 75 | + log.Debug("ClusterRequest resource already marked for deletion", "crName", crName, "namespace", cr.GetNamespace()) |
| 76 | + continue |
| 77 | + } |
| 78 | + log.Info("Deleting ClusterRequest", "crName", crName, "namespace", cr.GetNamespace()) |
| 79 | + if err := r.PlatformCluster.Client().Delete(ctx, cr); err != nil { |
| 80 | + if !apierrors.IsNotFound(err) { |
| 81 | + errs.Append(errutils.WithReason(fmt.Errorf("error deleting ClusterRequest '%s/%s': %w", namespace, crName, err), cconst.ReasonPlatformClusterInteractionProblem)) |
| 82 | + } else { |
| 83 | + log.Debug("ClusterRequest not found during deletion", "crName", crName, "namespace", cr.GetNamespace()) |
| 84 | + delete(resources, crName) // remove from resources if not found |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + if rerr := errs.Aggregate(); rerr != nil { |
| 89 | + return sets.KeySet(resources), rerr |
| 90 | + } |
| 91 | + |
| 92 | + return sets.KeySet(resources), nil |
| 93 | +} |
0 commit comments