Skip to content

Commit

Permalink
feat(experiment): Add a check before deletion (kubeflow#1223)
Browse files Browse the repository at this point in the history
* feat(experiment): Add a check before deletion

Signed-off-by: Ce Gao <gaoce@caicloud.io>

* fix: Delete all trials

Signed-off-by: Ce Gao <gaoce@caicloud.io>

* feat: Implement in v1beta1

Signed-off-by: Ce Gao <gaoce@caicloud.io>
  • Loading branch information
gaocegege authored and whalecold committed Sep 16, 2020
1 parent 8795d2e commit 6ada9a7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
12 changes: 10 additions & 2 deletions pkg/controller.v1alpha3/experiment/experiment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func (r *ReconcileExperiment) createTrials(instance *experimentsv1alpha3.Experim

func (r *ReconcileExperiment) deleteTrials(instance *experimentsv1alpha3.Experiment,
trials []trialsv1alpha3.Trial,
deleteCount int32) error {
expectedDeletions int32) error {
logger := log.WithValues("Experiment", types.NamespacedName{Name: instance.GetName(), Namespace: instance.GetNamespace()})

trialSlice := trials
Expand All @@ -366,7 +366,15 @@ func (r *ReconcileExperiment) deleteTrials(instance *experimentsv1alpha3.Experim
After(trialSlice[j].CreationTimestamp.Time)
})

for i := 0; i < int(deleteCount); i++ {
expected := int(expectedDeletions)
actual := len(trialSlice)
// If the number of trials < expected, we delete all we have.
if actual < expected {
logger.Info("deleteTrials does not find enough trials, we will delete all trials instead",
"expectedDeletions", expected, "trials", actual)
expected = actual
}
for i := 0; i < expected; i++ {
if err := r.Delete(context.TODO(), &trialSlice[i]); err != nil {
logger.Error(err, "Trial Delete error")
return err
Expand Down
12 changes: 10 additions & 2 deletions pkg/controller.v1beta1/experiment/experiment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func (r *ReconcileExperiment) createTrials(instance *experimentsv1beta1.Experime

func (r *ReconcileExperiment) deleteTrials(instance *experimentsv1beta1.Experiment,
trials []trialsv1beta1.Trial,
deleteCount int32) error {
expectedDeletions int32) error {
logger := log.WithValues("Experiment", types.NamespacedName{Name: instance.GetName(), Namespace: instance.GetNamespace()})

trialSlice := trials
Expand All @@ -381,7 +381,15 @@ func (r *ReconcileExperiment) deleteTrials(instance *experimentsv1beta1.Experime
After(trialSlice[j].CreationTimestamp.Time)
})

for i := 0; i < int(deleteCount); i++ {
expected := int(expectedDeletions)
actual := len(trialSlice)
// If the number of trials < expected, we delete all we have.
if actual < expected {
logger.Info("deleteTrials does not find enough trials, we will delete all trials instead",
"expectedDeletions", expected, "trials", actual)
expected = actual
}
for i := 0; i < expected; i++ {
if err := r.Delete(context.TODO(), &trialSlice[i]); err != nil {
logger.Error(err, "Trial Delete error")
return err
Expand Down

0 comments on commit 6ada9a7

Please sign in to comment.