Skip to content

Commit 4e1c3e5

Browse files
committed
fix grace period set for force pod deletion
Signed-off-by: Olga Shestopalova <[email protected]>
1 parent 0af674a commit 4e1c3e5

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

pkg/kubelet/pod_workers.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -979,23 +979,26 @@ func calculateEffectiveGracePeriod(status *podSyncStatus, pod *v1.Pod, options *
979979
// enforce the restriction that a grace period can only decrease and track whatever our value is,
980980
// then ensure a calculated value is passed down to lower levels
981981
gracePeriod := status.gracePeriod
982+
overridden := false
982983
// this value is bedrock truth - the apiserver owns telling us this value calculated by apiserver
983984
if override := pod.DeletionGracePeriodSeconds; override != nil {
984985
if gracePeriod == 0 || *override < gracePeriod {
985986
gracePeriod = *override
987+
overridden = true
986988
}
987989
}
988990
// we allow other parts of the kubelet (namely eviction) to request this pod be terminated faster
989991
if options != nil {
990992
if override := options.PodTerminationGracePeriodSecondsOverride; override != nil {
991993
if gracePeriod == 0 || *override < gracePeriod {
992994
gracePeriod = *override
995+
overridden = true
993996
}
994997
}
995998
}
996999
// make a best effort to default this value to the pod's desired intent, in the event
9971000
// the kubelet provided no requested value (graceful termination?)
998-
if gracePeriod == 0 && pod.Spec.TerminationGracePeriodSeconds != nil {
1001+
if !overridden && gracePeriod == 0 && pod.Spec.TerminationGracePeriodSeconds != nil {
9991002
gracePeriod = *pod.Spec.TerminationGracePeriodSeconds
10001003
}
10011004
// no matter what, we always supply a grace period of 1

pkg/kubelet/pod_workers_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,3 +2382,52 @@ func Test_allowPodStart(t *testing.T) {
23822382
})
23832383
}
23842384
}
2385+
2386+
func Test_calculateEffectiveGracePeriod(t *testing.T) {
2387+
zero := int64(0)
2388+
two := int64(2)
2389+
five := int64(5)
2390+
thirty := int64(30)
2391+
// no overrides, use what's on the spec
2392+
pod := newNamedPod("1", "ns", "running-pod", false)
2393+
pod.Spec.TerminationGracePeriodSeconds = &thirty
2394+
gracePeriod, _ := calculateEffectiveGracePeriod(&podSyncStatus{}, pod, &KillPodOptions{})
2395+
expectedGracePeriod := int64(30)
2396+
if gracePeriod != expectedGracePeriod {
2397+
t.Errorf("Expected a grace period of %v, but was %v", expectedGracePeriod, gracePeriod)
2398+
}
2399+
2400+
// pod DeletionGracePeriodSeconds is set
2401+
pod.DeletionGracePeriodSeconds = &five
2402+
gracePeriod, _ = calculateEffectiveGracePeriod(&podSyncStatus{}, pod, &KillPodOptions{})
2403+
expectedGracePeriod = five
2404+
if gracePeriod != expectedGracePeriod {
2405+
t.Errorf("Expected a grace period of %v, but was %v", expectedGracePeriod, gracePeriod)
2406+
}
2407+
2408+
// grace period override
2409+
gracePeriod, _ = calculateEffectiveGracePeriod(&podSyncStatus{}, pod, &KillPodOptions{
2410+
PodTerminationGracePeriodSecondsOverride: &two,
2411+
})
2412+
expectedGracePeriod = two
2413+
if gracePeriod != expectedGracePeriod {
2414+
t.Errorf("Expected a grace period of %v, but was %v", expectedGracePeriod, gracePeriod)
2415+
}
2416+
2417+
// pod DeletionGracePeriodSeconds is zero
2418+
pod.DeletionGracePeriodSeconds = &zero
2419+
gracePeriod, _ = calculateEffectiveGracePeriod(&podSyncStatus{}, pod, &KillPodOptions{})
2420+
expectedGracePeriod = int64(1)
2421+
if gracePeriod != expectedGracePeriod {
2422+
t.Errorf("Expected a grace period of %v, but was %v", expectedGracePeriod, gracePeriod)
2423+
}
2424+
2425+
// grace period override is zero
2426+
gracePeriod, _ = calculateEffectiveGracePeriod(&podSyncStatus{}, pod, &KillPodOptions{
2427+
PodTerminationGracePeriodSecondsOverride: &zero,
2428+
})
2429+
expectedGracePeriod = int64(1)
2430+
if gracePeriod != expectedGracePeriod {
2431+
t.Errorf("Expected a grace period of %v, but was %v", expectedGracePeriod, gracePeriod)
2432+
}
2433+
}

0 commit comments

Comments
 (0)