From a0b8710be256541e025d23e93770fb3a49a59153 Mon Sep 17 00:00:00 2001 From: OrangeBao Date: Fri, 24 Nov 2023 15:22:40 +0800 Subject: [PATCH] fix: delete pod who is terminating Signed-off-by: OrangeBao --- .../controllers/pod/leaf_pod_controller.go | 9 +++++---- .../controllers/pod/root_pod_controller.go | 16 +++++++++++++--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/pkg/clustertree/cluster-manager/controllers/pod/leaf_pod_controller.go b/pkg/clustertree/cluster-manager/controllers/pod/leaf_pod_controller.go index 7320e2747..9f90f5595 100644 --- a/pkg/clustertree/cluster-manager/controllers/pod/leaf_pod_controller.go +++ b/pkg/clustertree/cluster-manager/controllers/pod/leaf_pod_controller.go @@ -8,6 +8,7 @@ import ( "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/types" "k8s.io/klog/v2" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" @@ -38,7 +39,7 @@ func (r *LeafPodReconciler) Reconcile(ctx context.Context, request reconcile.Req if err := r.Get(ctx, request.NamespacedName, &pod); err != nil { if apierrors.IsNotFound(err) { // delete pod in root - if err := r.safeDeletePodInRootCluster(ctx, request); err != nil { + if err := DeletePodInRootCluster(ctx, request.NamespacedName, r.RootClient); err != nil { return reconcile.Result{RequeueAfter: LeafPodRequeueTime}, nil } return reconcile.Result{}, nil @@ -96,9 +97,9 @@ func NewLeafDeleteOption(pod *corev1.Pod) client.DeleteOption { } } -func (r *LeafPodReconciler) safeDeletePodInRootCluster(ctx context.Context, request reconcile.Request) error { +func DeletePodInRootCluster(ctx context.Context, rootnamespacedname types.NamespacedName, rootClient client.Client) error { rPod := corev1.Pod{} - err := r.RootClient.Get(ctx, request.NamespacedName, &rPod) + err := rootClient.Get(ctx, rootnamespacedname, &rPod) if err != nil { if apierrors.IsNotFound(err) { @@ -111,7 +112,7 @@ func (r *LeafPodReconciler) safeDeletePodInRootCluster(ctx context.Context, requ rPodCopy := rPod.DeepCopy() deleteOption := NewRootDeleteOption(rPodCopy) - if err := r.RootClient.Delete(ctx, rPodCopy, deleteOption); err != nil { + if err := rootClient.Delete(ctx, rPodCopy, deleteOption); err != nil { if !apierrors.IsNotFound(err) { return err } diff --git a/pkg/clustertree/cluster-manager/controllers/pod/root_pod_controller.go b/pkg/clustertree/cluster-manager/controllers/pod/root_pod_controller.go index f527f28cf..11b7bb3df 100644 --- a/pkg/clustertree/cluster-manager/controllers/pod/root_pod_controller.go +++ b/pkg/clustertree/cluster-manager/controllers/pod/root_pod_controller.go @@ -128,7 +128,7 @@ func (r *RootPodReconciler) Reconcile(ctx context.Context, request reconcile.Req // wait for leaf resource init return reconcile.Result{RequeueAfter: RootPodRequeueTime}, nil } - if err := r.DeletePodInLeafCluster(ctx, lr, request.NamespacedName); err != nil { + if err := r.DeletePodInLeafCluster(ctx, lr, request.NamespacedName, false); err != nil { klog.Errorf("delete pod in leaf error[1]: %v, %s", err, request.NamespacedName) return reconcile.Result{RequeueAfter: RootPodRequeueTime}, nil } @@ -185,7 +185,7 @@ func (r *RootPodReconciler) Reconcile(ctx context.Context, request reconcile.Req // delete pod in leaf if !rootpod.GetDeletionTimestamp().IsZero() { - if err := r.DeletePodInLeafCluster(ctx, lr, request.NamespacedName); err != nil { + if err := r.DeletePodInLeafCluster(ctx, lr, request.NamespacedName, true); err != nil { klog.Errorf("delete pod in leaf error[1]: %v, %s", err, request.NamespacedName) return reconcile.Result{RequeueAfter: RootPodRequeueTime}, nil } @@ -802,14 +802,21 @@ func (r *RootPodReconciler) UpdatePodInLeafCluster(ctx context.Context, lr *leaf return nil } -func (r *RootPodReconciler) DeletePodInLeafCluster(ctx context.Context, lr *leafUtils.LeafResource, rootnamespacedname types.NamespacedName) error { +func (r *RootPodReconciler) DeletePodInLeafCluster(ctx context.Context, lr *leafUtils.LeafResource, rootnamespacedname types.NamespacedName, cleanflag bool) error { klog.V(4).Infof("Deleting pod %v/%+v", rootnamespacedname.Namespace, rootnamespacedname.Name) leafPod := &corev1.Pod{} + cleanRootPodFunc := func() error { + return DeletePodInRootCluster(ctx, rootnamespacedname, r.Client) + } + err := lr.Client.Get(ctx, rootnamespacedname, leafPod) if err != nil { if errors.IsNotFound(err) { + if cleanflag { + return cleanRootPodFunc() + } return nil } return err @@ -825,6 +832,9 @@ func (r *RootPodReconciler) DeletePodInLeafCluster(ctx context.Context, lr *leaf if err != nil { if errors.IsNotFound(err) { klog.V(4).Infof("Tried to delete pod %s/%s, but it did not exist in the cluster", leafPod.Namespace, leafPod.Name) + if cleanflag { + return cleanRootPodFunc() + } return nil } return fmt.Errorf("could not delete pod: %v", err)