I have a concept in my application to delete a JOB and then just recreated it. I have tried to delete the Job, then just sleep for a while and try to recreate it.
but I´m not convinced with this solution it´s not optimistic.
So, After something happened, from my code I will Run a function in a Goroutine.
here's the code inside the function.
func CreateGenerateJob() {
err = clientset.BatchV1().Jobs("default").Delete(jobName, nil)
if err != nil {
logrus.Errorf("could not delete job: %v", err)
}
time.Sleep(60*time.Second)
createdJob, err := clientset.BatchV1().Jobs("default").Create(job)
if err != nil {
logrus.Fatalf("could not create job: %v", err)
}
}
This is not Good at all. But, I had the idea to watch for the deleted resources.
So I added a watch function before deleting the Job,
func CreateGenerateJob() {
watchForTheJob(clientset)
err = clientset.BatchV1().Jobs("default").Delete(jobName, nil)
if err != nil {
logrus.Errorf("could not delete job: %v", err)
}
}
The content of the function is as follows:
func watchForTheJob(clientset kubernetes.Interface) {
kubeInformerFactory := kubeinformers.NewSharedInformerFactory(clientset, time.Second*30)
svcInformer := kubeInformerFactory.Batch().V1().Jobs().Informer()
svcInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
DeleteFunc: func(obj interface{}) {
logrus.Infof("service deleted: %s \n", obj)
// Now I can create the Job.
createdJob, err := clientset.BatchV1().Jobs("default").Create(job)
if err != nil {
logrus.Fatalf("could not create job: %v", err)
}
logrus.Infof("job created: %s", createdJob.Name)
},
},)
stop := make(chan struct{})
defer close(stop)
kubeInformerFactory.Start(stop)
for {
time.Sleep(time.Second)
}
}
The problem is that I´m not sure if the JOB is really available or not. (can the delete happens or not)
Since I´m running the Whole code of this package in a goroutine.
go adapter.CreateGenerateJob()
I would like to exit the Goroutine after successfully the job created, no matter if the job is deleted -> then recreated or just created only.
it might be a solution if on watching for the job, then after creating it, just exit the watch event.
Any, solution for that.