Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean up all resources for job when job is suspended #16

Open
wants to merge 1 commit into
base: v1.0-aliyun-branch
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions pkg/controller.v1/tensorflow/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,13 @@ func (tc *TFController) processNextWorkItem() bool {
}

// Wait until queuing annotation is removed
if tfJob.Annotations != nil {
if suspend, exist := tfJob.Annotations[suspendInQueue]; exist && suspend == "true" {
infoMsg := fmt.Sprintf("Annotation %s is found, operator will not process until removed", suspendInQueue)
tflogger.LoggerForKey(key).Info(infoMsg)
return true
}
}
// if tfJob.Annotations != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using isSuspend to avoid creating resources like pod? If it's for ECI scenario, will it create pod and delete it after a while?

// if suspend, exist := tfJob.Annotations[suspendInQueue]; exist && suspend == "true" {
// infoMsg := fmt.Sprintf("Annotation %s is found, operator will not process until removed", suspendInQueue)
// tflogger.LoggerForKey(key).Info(infoMsg)
// return true
// }
// }

// Sync TFJob to match the actual state to this desired state.
forget, err := tc.syncHandler(key)
Expand Down Expand Up @@ -354,6 +354,15 @@ func (tc *TFController) syncTFJob(key string) (bool, error) {
return true, err
}

func isSuspend(tfjob *tfv1.TFJob) bool {
if tfjob.Annotations != nil {
if suspend, exist := tfjob.Annotations[suspendInQueue]; exist && suspend == "true" {
return true
}
}
return false
}

// reconcileTFJobs checks and updates replicas for each given TFReplicaSpec.
// It will requeue the tfjob in case of an error while creating/deleting pods/services.
func (tc *TFController) reconcileTFJobs(tfjob *tfv1.TFJob) error {
Expand Down Expand Up @@ -428,12 +437,12 @@ func (tc *TFController) reconcileTFJobs(tfjob *tfv1.TFJob) error {
}

// If the TFJob is terminated, delete all pods and services.
if isSucceeded(tfjob.Status) || isFailed(tfjob.Status) || tfJobExceedsLimit {
if isSucceeded(tfjob.Status) || isFailed(tfjob.Status) || tfJobExceedsLimit || isSuspend(tfjob) {

// If TTL is set, you need to wait until the TTL time before reclaiming resources.
ttlDuration, shouldWaitPodTTL := getPodTTL(tfjob)

if !shouldWaitPodTTL || isPodTTLReached(tfjob, ttlDuration) {
if !shouldWaitPodTTL || isPodTTLReached(tfjob, ttlDuration) || isSuspend(tfjob) {
if err := tc.deletePodsAndServices(tfjob, pods); err != nil {
return err
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/controller.v1/tensorflow/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package tensorflow

import (
"fmt"
"github.com/kubeflow/tf-operator/pkg/util"
"reflect"
"time"

"github.com/kubeflow/tf-operator/pkg/util"

log "github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1unstructured "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -182,8 +183,9 @@ func (tc *TFController) deletePodsAndServices(tfJob *tfv1.TFJob, pods []*v1.Pod)
return nil
}

isSuspend := isSuspend(tfJob)
// Delete nothing when the cleanPodPolicy is None.
if *tfJob.Spec.CleanPodPolicy == common.CleanPodPolicyNone {
if *tfJob.Spec.CleanPodPolicy == common.CleanPodPolicyNone && !isSuspend {
return nil
}

Expand Down