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

Verify that Trials were successfully deleted #1288

Merged
Merged
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
14 changes: 14 additions & 0 deletions pkg/apis/controller/suggestions/v1beta1/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
// SuggestionRestartReason is the reason for suggestion status when experiment is restarting
SuggestionRestartReason = "Experiment is restarting"
)

func getCondition(suggestion *Suggestion, condType SuggestionConditionType) *SuggestionCondition {
if suggestion.Status.Conditions != nil {
for _, condition := range suggestion.Status.Conditions {
Expand Down Expand Up @@ -64,6 +69,15 @@ func (suggestion *Suggestion) IsRunning() bool {
return hasCondition(suggestion, SuggestionRunning)
}

// IsRestarting returns true if suggestion running status is false and reason = SuggestionRestartReason
func (suggestion *Suggestion) IsRestarting() bool {
cond := getCondition(suggestion, SuggestionRunning)
if cond != nil && cond.Status == v1.ConditionFalse && cond.Reason == SuggestionRestartReason {
return true
}
return false
}

func (suggestion *Suggestion) IsDeploymentReady() bool {
return hasCondition(suggestion, SuggestionDeploymentReady)
}
Expand Down
56 changes: 56 additions & 0 deletions pkg/controller.v1beta1/experiment/experiment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package experiment

import (
"context"
"fmt"
"sort"
"time"

"github.com/spf13/viper"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -385,12 +387,66 @@ func (r *ReconcileExperiment) deleteTrials(instance *experimentsv1beta1.Experime
"expectedDeletions", expected, "trials", actual)
expected = actual
}
deletedNames := []string{}
for i := 0; i < expected; i++ {
if err := r.Delete(context.TODO(), &trialSlice[i]); err != nil {
logger.Error(err, "Trial Delete error")
return err
}
deletedNames = append(deletedNames, trialSlice[i].Name)
}

// Check if trials were deleted
timeout := 60 * time.Second
endTime := time.Now().Add(timeout)

for _, name := range deletedNames {
var err error
for !errors.IsNotFound(err) && time.Now().Before(endTime) {
err = r.Get(context.TODO(), types.NamespacedName{Name: name, Namespace: instance.GetNamespace()}, &trialsv1beta1.Trial{})
}
// If trials were deleted, err == IsNotFound, return error if timeout is out
if !errors.IsNotFound(err) {
return fmt.Errorf("Unable to delete trials %v, error: %v", deletedNames, err)
}
}

// We have to delete trials from suggestion status and update SuggestionCount
suggestion := &suggestionsv1beta1.Suggestion{}
err := r.Get(context.TODO(), types.NamespacedName{Name: instance.GetName(), Namespace: instance.GetNamespace()}, suggestion)
if err != nil {
logger.Error(err, "Suggestion Get error")
return err
}

deletedNamesMap := make(map[string]bool)
for _, name := range deletedNames {
deletedNamesMap[name] = true
}
// Create new Trial Assignment without deleted trials
newTrialAssignment := []suggestionsv1beta1.TrialAssignment{}
for _, ta := range suggestion.Status.Suggestions {
if _, ok := deletedNamesMap[ta.Name]; !ok {
newTrialAssignment = append(newTrialAssignment, ta)
}
}

// Update suggestion spec first.
// If Requests <= SuggestionCount suggestion controller returns nil.
suggestion.Spec.Requests = int32(len(newTrialAssignment))
if err := r.UpdateSuggestion(suggestion); err != nil {
return err
}

// Update suggestion status
suggestion.Status.Suggestions = newTrialAssignment
suggestion.Status.SuggestionCount = int32(len(newTrialAssignment))
if err := r.UpdateSuggestionStatus(suggestion); err != nil {
return err
}

logger.Info("Trials were successfully deleted", "trialNames", deletedNames)

return nil
}

Expand Down
Loading