Skip to content

Commit

Permalink
fix: delete pod who is terminating
Browse files Browse the repository at this point in the history
Signed-off-by: OrangeBao <[email protected]>
  • Loading branch information
OrangeBao committed Nov 24, 2023
1 parent e923865 commit a0b8710
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit a0b8710

Please sign in to comment.