mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-16 22:53:22 +00:00
Gracefully delete pods from the Kubelet
This commit wires together the graceful delete option for pods on the Kubelet. When a pod is deleted on the API server, a grace period is calculated that is based on the Pod.Spec.TerminationGracePeriodInSeconds, the user's provided grace period, or a default. The grace period can only shrink once set. The value provided by the user (or the default) is set onto metadata as DeletionGracePeriod. When the Kubelet sees a pod with DeletionTimestamp set, it uses the value of ObjectMeta.GracePeriodSeconds as the grace period sent to Docker. When updating status, if the pod has DeletionTimestamp set and all containers are terminated, the Kubelet will update the status one last time and then invoke Delete(pod, grace: 0) to clean up the pod immediately.
This commit is contained in:
@@ -54,6 +54,7 @@ func newStorage(t *testing.T) (*REST, *BindingREST, *StatusREST, *tools.FakeEtcd
|
||||
}
|
||||
|
||||
func validNewPod() *api.Pod {
|
||||
grace := int64(30)
|
||||
return &api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "foo",
|
||||
@@ -62,6 +63,8 @@ func validNewPod() *api.Pod {
|
||||
Spec: api.PodSpec{
|
||||
RestartPolicy: api.RestartPolicyAlways,
|
||||
DNSPolicy: api.DNSClusterFirst,
|
||||
|
||||
TerminationGracePeriodSeconds: &grace,
|
||||
Containers: []api.Container{
|
||||
{
|
||||
Name: "foo",
|
||||
@@ -132,9 +135,9 @@ func TestDelete(t *testing.T) {
|
||||
if fakeEtcdClient.Data[key].R.Node == nil {
|
||||
return false
|
||||
}
|
||||
return fakeEtcdClient.Data[key].R.Node.TTL == 30
|
||||
return fakeEtcdClient.Data[key].R.Node.TTL != 0
|
||||
}
|
||||
test.TestDelete(createFn, gracefulSetFn)
|
||||
test.TestDeleteGraceful(createFn, 30, gracefulSetFn)
|
||||
}
|
||||
|
||||
func expectPod(t *testing.T, out runtime.Object) (*api.Pod, bool) {
|
||||
@@ -1118,6 +1121,7 @@ func TestEtcdUpdateScheduled(t *testing.T) {
|
||||
},
|
||||
}), 1)
|
||||
|
||||
grace := int64(30)
|
||||
podIn := api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "foo",
|
||||
@@ -1139,6 +1143,8 @@ func TestEtcdUpdateScheduled(t *testing.T) {
|
||||
},
|
||||
RestartPolicy: api.RestartPolicyAlways,
|
||||
DNSPolicy: api.DNSClusterFirst,
|
||||
|
||||
TerminationGracePeriodSeconds: &grace,
|
||||
},
|
||||
}
|
||||
_, _, err := registry.Update(ctx, &podIn)
|
||||
@@ -1179,7 +1185,7 @@ func TestEtcdUpdateStatus(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &podStart), 1)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &podStart), 0)
|
||||
|
||||
podIn := api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
@@ -1208,6 +1214,8 @@ func TestEtcdUpdateStatus(t *testing.T) {
|
||||
|
||||
expected := podStart
|
||||
expected.ResourceVersion = "2"
|
||||
grace := int64(30)
|
||||
expected.Spec.TerminationGracePeriodSeconds = &grace
|
||||
expected.Spec.RestartPolicy = api.RestartPolicyAlways
|
||||
expected.Spec.DNSPolicy = api.DNSClusterFirst
|
||||
expected.Spec.Containers[0].ImagePullPolicy = api.PullIfNotPresent
|
||||
|
@@ -81,9 +81,26 @@ func (podStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) fiel
|
||||
return append(errorList, validation.ValidatePodUpdate(obj.(*api.Pod), old.(*api.Pod))...)
|
||||
}
|
||||
|
||||
// CheckGracefulDelete allows a pod to be gracefully deleted.
|
||||
// CheckGracefulDelete allows a pod to be gracefully deleted. It updates the DeleteOptions to
|
||||
// reflect the desired grace value.
|
||||
func (podStrategy) CheckGracefulDelete(obj runtime.Object, options *api.DeleteOptions) bool {
|
||||
return false
|
||||
if options == nil {
|
||||
return false
|
||||
}
|
||||
period := int64(0)
|
||||
// user has specified a value
|
||||
if options.GracePeriodSeconds != nil {
|
||||
period = *options.GracePeriodSeconds
|
||||
} else {
|
||||
// use the default value if set, or deletes the pod immediately (0)
|
||||
pod := obj.(*api.Pod)
|
||||
if pod.Spec.TerminationGracePeriodSeconds != nil {
|
||||
period = *pod.Spec.TerminationGracePeriodSeconds
|
||||
}
|
||||
}
|
||||
// ensure the options and the pod are in sync
|
||||
options.GracePeriodSeconds = &period
|
||||
return true
|
||||
}
|
||||
|
||||
type podStatusStrategy struct {
|
||||
@@ -96,6 +113,7 @@ func (podStatusStrategy) PrepareForUpdate(obj, old runtime.Object) {
|
||||
newPod := obj.(*api.Pod)
|
||||
oldPod := old.(*api.Pod)
|
||||
newPod.Spec = oldPod.Spec
|
||||
newPod.DeletionTimestamp = nil
|
||||
}
|
||||
|
||||
func (podStatusStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) fielderrors.ValidationErrorList {
|
||||
|
Reference in New Issue
Block a user