mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Treat negative as 1s in delete path
This commit is contained in:
parent
40593fa4d3
commit
45ce2dfacc
@ -167,6 +167,11 @@ func (podStrategy) CheckGracefulDelete(ctx context.Context, obj runtime.Object,
|
|||||||
if pod.Status.Phase == api.PodFailed || pod.Status.Phase == api.PodSucceeded {
|
if pod.Status.Phase == api.PodFailed || pod.Status.Phase == api.PodSucceeded {
|
||||||
period = 0
|
period = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if period < 0 {
|
||||||
|
period = 1
|
||||||
|
}
|
||||||
|
|
||||||
// ensure the options and the pod are in sync
|
// ensure the options and the pod are in sync
|
||||||
options.GracePeriodSeconds = &period
|
options.GracePeriodSeconds = &period
|
||||||
return true
|
return true
|
||||||
|
@ -42,6 +42,7 @@ import (
|
|||||||
api "k8s.io/kubernetes/pkg/apis/core"
|
api "k8s.io/kubernetes/pkg/apis/core"
|
||||||
"k8s.io/kubernetes/pkg/features"
|
"k8s.io/kubernetes/pkg/features"
|
||||||
"k8s.io/kubernetes/pkg/kubelet/client"
|
"k8s.io/kubernetes/pkg/kubelet/client"
|
||||||
|
utilpointer "k8s.io/utils/pointer"
|
||||||
|
|
||||||
// ensure types are installed
|
// ensure types are installed
|
||||||
_ "k8s.io/kubernetes/pkg/apis/core/install"
|
_ "k8s.io/kubernetes/pkg/apis/core/install"
|
||||||
@ -266,55 +267,82 @@ func TestGetPodQOS(t *testing.T) {
|
|||||||
func TestCheckGracefulDelete(t *testing.T) {
|
func TestCheckGracefulDelete(t *testing.T) {
|
||||||
defaultGracePeriod := int64(30)
|
defaultGracePeriod := int64(30)
|
||||||
tcs := []struct {
|
tcs := []struct {
|
||||||
in *api.Pod
|
name string
|
||||||
gracePeriod int64
|
pod *api.Pod
|
||||||
|
deleteGracePeriod *int64
|
||||||
|
gracePeriod int64
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
in: &api.Pod{
|
name: "in pending phase with has node name",
|
||||||
|
pod: &api.Pod{
|
||||||
Spec: api.PodSpec{NodeName: "something"},
|
Spec: api.PodSpec{NodeName: "something"},
|
||||||
Status: api.PodStatus{Phase: api.PodPending},
|
Status: api.PodStatus{Phase: api.PodPending},
|
||||||
},
|
},
|
||||||
|
deleteGracePeriod: &defaultGracePeriod,
|
||||||
gracePeriod: defaultGracePeriod,
|
gracePeriod: defaultGracePeriod,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
in: &api.Pod{
|
name: "in failed phase with has node name",
|
||||||
|
pod: &api.Pod{
|
||||||
Spec: api.PodSpec{NodeName: "something"},
|
Spec: api.PodSpec{NodeName: "something"},
|
||||||
Status: api.PodStatus{Phase: api.PodFailed},
|
Status: api.PodStatus{Phase: api.PodFailed},
|
||||||
},
|
},
|
||||||
gracePeriod: 0,
|
deleteGracePeriod: &defaultGracePeriod,
|
||||||
|
gracePeriod: 0,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
in: &api.Pod{
|
name: "in failed phase",
|
||||||
|
pod: &api.Pod{
|
||||||
Spec: api.PodSpec{},
|
Spec: api.PodSpec{},
|
||||||
Status: api.PodStatus{Phase: api.PodPending},
|
Status: api.PodStatus{Phase: api.PodPending},
|
||||||
},
|
},
|
||||||
gracePeriod: 0,
|
deleteGracePeriod: &defaultGracePeriod,
|
||||||
|
gracePeriod: 0,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
in: &api.Pod{
|
name: "in succeeded phase",
|
||||||
|
pod: &api.Pod{
|
||||||
Spec: api.PodSpec{},
|
Spec: api.PodSpec{},
|
||||||
Status: api.PodStatus{Phase: api.PodSucceeded},
|
Status: api.PodStatus{Phase: api.PodSucceeded},
|
||||||
},
|
},
|
||||||
gracePeriod: 0,
|
deleteGracePeriod: &defaultGracePeriod,
|
||||||
|
gracePeriod: 0,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
in: &api.Pod{
|
name: "no phase",
|
||||||
|
pod: &api.Pod{
|
||||||
Spec: api.PodSpec{},
|
Spec: api.PodSpec{},
|
||||||
Status: api.PodStatus{},
|
Status: api.PodStatus{},
|
||||||
},
|
},
|
||||||
gracePeriod: 0,
|
deleteGracePeriod: &defaultGracePeriod,
|
||||||
|
gracePeriod: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "has negative grace period",
|
||||||
|
pod: &api.Pod{
|
||||||
|
Spec: api.PodSpec{
|
||||||
|
NodeName: "something",
|
||||||
|
TerminationGracePeriodSeconds: utilpointer.Int64(-1),
|
||||||
|
},
|
||||||
|
Status: api.PodStatus{},
|
||||||
|
},
|
||||||
|
gracePeriod: 1,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tc := range tcs {
|
for _, tc := range tcs {
|
||||||
out := &metav1.DeleteOptions{GracePeriodSeconds: &defaultGracePeriod}
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
Strategy.CheckGracefulDelete(genericapirequest.NewContext(), tc.in, out)
|
out := &metav1.DeleteOptions{}
|
||||||
if out.GracePeriodSeconds == nil {
|
if tc.deleteGracePeriod != nil {
|
||||||
t.Errorf("out grace period was nil but supposed to be %v", tc.gracePeriod)
|
out.GracePeriodSeconds = utilpointer.Int64(*tc.deleteGracePeriod)
|
||||||
}
|
}
|
||||||
if *(out.GracePeriodSeconds) != tc.gracePeriod {
|
Strategy.CheckGracefulDelete(genericapirequest.NewContext(), tc.pod, out)
|
||||||
t.Errorf("out grace period was %v but was expected to be %v", *out, tc.gracePeriod)
|
if out.GracePeriodSeconds == nil {
|
||||||
}
|
t.Errorf("out grace period was nil but supposed to be %v", tc.gracePeriod)
|
||||||
|
}
|
||||||
|
if *(out.GracePeriodSeconds) != tc.gracePeriod {
|
||||||
|
t.Errorf("out grace period was %v but was expected to be %v", *out, tc.gracePeriod)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user