Skip to content

Commit

Permalink
Merge pull request #444 from yuleichun-striving/release-0.3.0
Browse files Browse the repository at this point in the history
fix: Elegant deletion of pods in sub-clusters (consistent with k8s)
  • Loading branch information
duanmengkk authored Mar 21, 2024
2 parents d7a183e + c08fb82 commit c4923b1
Showing 1 changed file with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package pod

import (
"context"
"time"

"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -85,9 +87,28 @@ func NewRootDeleteOption(pod *corev1.Pod) client.DeleteOption {
}

func NewLeafDeleteOption(pod *corev1.Pod) client.DeleteOption {
gracePeriodSeconds := new(int64)
if pod.DeletionGracePeriodSeconds != nil {
gracePeriodSeconds = pod.DeletionGracePeriodSeconds
var gracePeriodSeconds *int64
// Check if DeletionTimestamp is set and before the current time
current := metav1.NewTime(time.Now())
if pod.DeletionTimestamp != nil && pod.DeletionTimestamp.Before(&current) {
//force
gracePeriodSeconds = new(int64)
} else {
if pod.DeletionGracePeriodSeconds != nil {
//DeletionGracePeriodSeconds determines how long it will take before the pod status changes to Termination
//wait
gracePeriodSeconds = pod.DeletionGracePeriodSeconds
} else if pod.Spec.TerminationGracePeriodSeconds != nil {
//TerminationGracePeriodSeconds is how long to wait after the pod is marked as Termination state before the container process is forcibly deleted.
//wait
gracePeriodSeconds = pod.Spec.TerminationGracePeriodSeconds
}

if pod.DeletionGracePeriodSeconds != nil && pod.Spec.TerminationGracePeriodSeconds != nil {
//Sum both grace periods
totalGracePeriod := *pod.DeletionGracePeriodSeconds + *pod.Spec.TerminationGracePeriodSeconds
gracePeriodSeconds = &totalGracePeriod
}
}

return &rootDeleteOption{
Expand Down Expand Up @@ -163,8 +184,3 @@ func (r *LeafPodReconciler) SetupWithManager(mgr manager.Manager) error {
})).
Complete(r)
}

// func ShouldSkipStatusUpdate(pod *corev1.Pod) bool {
// return pod.Status.Phase == corev1.PodSucceeded ||
// pod.Status.Phase == corev1.PodFailed
// }

0 comments on commit c4923b1

Please sign in to comment.