Skip to content

Commit

Permalink
refs #53 Delete job before delete pods in cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
h3poteto committed Jun 16, 2020
1 parent 6ee9787 commit ec5c5de
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
8 changes: 4 additions & 4 deletions job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,13 @@ func containerIsCompleted(pod corev1.Pod, containerName string) bool {
// Cleanup removes the job from the kubernetes cluster.
func (j *Job) Cleanup() error {
ctx := context.Background()
err := j.removePods(ctx)
log.Infof("Removing the job: %s", j.CurrentJob.Name)
options := metav1.DeleteOptions{}
err := j.client.BatchV1().Jobs(j.CurrentJob.Namespace).Delete(ctx, j.CurrentJob.Name, options)
if err != nil {
return err
}
log.Infof("Removing the job: %s", j.CurrentJob.Name)
options := metav1.DeleteOptions{}
return j.client.BatchV1().Jobs(j.CurrentJob.Namespace).Delete(ctx, j.CurrentJob.Name, options)
return j.removePods(ctx)
}

func (j *Job) removePods(ctx context.Context) error {
Expand Down
14 changes: 7 additions & 7 deletions job/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,21 @@ func (j *Job) Run(ignoreSidecar bool) error {

err = j.WaitJob(ctx, running, ignoreSidecar)
time.Sleep(10 * time.Second)
cancel()
return err
}

// RunAndCleanup executes a command and clean up the job and pods.
func (j *Job) RunAndCleanup(cleanupType string, ignoreSidecar bool) error {
err := j.Run(ignoreSidecar)
if shouldCleanup(cleanupType, err) {
if e := j.Cleanup(); e != nil {
return e
}
if !shouldCleanup(cleanupType, err) {
log.Info("Job should no clean up")
}
if e := j.Cleanup(); e != nil {
return e
}
return err
}

func shouldCleanup(cleanupType string, err error) bool {
return cleanupType == All.String() || (cleanupType == Succeeded.String() && err == nil) || (cleanupType == Failed.String() && err != nil)
func shouldCleanup(cleanupType string, jobResult error) bool {
return cleanupType == All.String() || (cleanupType == Succeeded.String() && jobResult == nil) || (cleanupType == Failed.String() && jobResult != nil)
}

0 comments on commit ec5c5de

Please sign in to comment.