michealAtmi
michealAtmi

Reputation: 1042

creating Kubernetes POD with DeletionGracePeriodSeconds is not respected

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
}

Upvotes: 0

Views: 507

Answers (1)

Arghya Sadhu
Arghya Sadhu

Reputation: 44559

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{})

Upvotes: 1

Related Questions