creating Kubernetes POD with DeletionGracePeriodSeconds is not respected

10/20/2020

I am creating Kubernetes POD with Golang. Iam trying to set DeletionGracePeriodSeconds but after creating pod, the pod has 30 in this field while I am setting 25. Name of the pod is OK, so after creating the POD it has name that I assigned in code.

func setupPod(client *Client, ns string, name string, labels map[string]string) (*v1.Pod, error) {
	 seconds := func(i int64) *int64 { return &i }(25)
	 pod := &v1.Pod{}
	 pod.Name = name
	 pod.Namespace = ns
	 pod.SetDeletionGracePeriodSeconds(seconds) //it is 25 seconds under debugger
	 pod.DeletionGracePeriodSeconds = seconds
	 pod.Spec.Containers = []v1.Container{v1.Container{Name: "ubuntu", Image: "ubuntu", Command: []string{"sleep", "30"}}}
	 pod.Spec.NodeName = "node1"
	 if labels != nil {
		pod.Labels = labels
	 }
	 _, err := client.client.CoreV1().Pods(ns).Create(client.context, pod, metav1.CreateOptions{})
	 return pod, err
}
-- michealAtmi
go
kubernetes

1 Answer

10/20/2020

DeletionGracePeriodSeconds is read-only and hence you can not change it. You should instead set terminationGracePeriodSeconds and kubernetes will set the DeletionGracePeriodSeconds accordingly. You can verify that by getting the value and printing it.

From the API docs

Number of seconds allowed for this object to gracefully terminate before it will be removed from the system. Only set when deletionTimestamp is also set. May only be shortened. Read-only.

podSpec := &v1.Pod{
		Spec: v1.PodSpec{
			TerminationGracePeriodSeconds: <Your-Grace-Period>
		},
	}

	_, err = clientset.CoreV1().Pods("namespacename").Create(context.TODO(), podSpec, metav1.CreateOptions{})
-- Arghya Sadhu
Source: StackOverflow