Delete a Job and wait until job is deleting in client-go

10/1/2021
// Delete a Batch Job by name
func (k K8sClient) DeleteBatchJob(name string, namespace string) error {
	return k.K8sCS.BatchV1().Jobs(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{})
}

I am deleting a job if already exists and then starting a new Job, but the operation here is asynchronous and job creation phase started when the job is being deleted, which I don't want. I want to successfully delete a job before creation of new one.

How can I implement this functionality using go?

-- Abhishek Kumar
client-go
go
jobs
kubernetes

1 Answer

10/4/2021

If you give every job a unique name, you won't have to wait for the asynchronous deletion to make a new one. This is how the cron scheduler works in k8s - it creates uniquely named jobs every time.

To find and manage the jobs, you can use labels instead of the job name.

-- Daniel Farrell
Source: StackOverflow