删除一个作业并等待作业在client-go中删除完成

huangapple go评论76阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2021年10月1日 18:56:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/69404734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定