英文:
Delete a Job and wait until job is deleting in client-go
问题
// 通过名称删除批处理作业
func (k K8sClient) DeleteBatchJob(name string, namespace string) error {
err := k.K8sCS.BatchV1().Jobs(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{})
if err != nil {
return err
}
// 等待作业被删除
for {
_, err := k.K8sCS.BatchV1().Jobs(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
break
}
return err
}
time.Sleep(1 * time.Second)
}
return nil
}
在删除作业后,我们可以使用一个循环来等待作业被完全删除。在每次循环中,我们通过调用Get
方法来检查作业是否仍然存在。如果作业不存在,则退出循环。通过在每次循环之间添加适当的延迟,可以控制轮询的频率。这样,当作业被完全删除后,才会继续执行创建新作业的操作。
英文:
// 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?
答案1
得分: 1
如果给每个工作一个唯一的名称,你就不必等待异步删除来创建一个新的工作。这就是k8s中cron调度器的工作原理-它每次都会创建一个唯一命名的工作。
为了查找和管理这些工作,你可以使用标签而不是工作名称。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论