From f0a0bfb2dacd82ea59781e9e3abc26908b77e077 Mon Sep 17 00:00:00 2001 From: Andrey Velichkevich Date: Fri, 3 May 2019 13:47:43 -0700 Subject: [PATCH 1/3] Update util for experiment --- .../apis/experiment/v1alpha2/util.go | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pkg/api/operators/apis/experiment/v1alpha2/util.go b/pkg/api/operators/apis/experiment/v1alpha2/util.go index bc9a1440a09..168d98f8f94 100644 --- a/pkg/api/operators/apis/experiment/v1alpha2/util.go +++ b/pkg/api/operators/apis/experiment/v1alpha2/util.go @@ -73,10 +73,34 @@ func (exp *Experiment) IsFailed() bool { return hasCondition(exp, ExperimentFailed) } +func (exp *Experiment) IsRunning() bool { + return hasCondition(exp, ExperimentRunning) +} + +func (exp *Experiment) IsRestarting() bool { + return hasCondition(exp, ExperimentRestarting) +} + func (exp *Experiment) IsCompleted() bool { return exp.IsSucceeded() || exp.IsFailed() } +func (exp *Experiment) GetCurrentConditionType() ExperimentConditionType { + if exp.IsCreated() { + return ExperimentCreated + } + if exp.IsSucceeded() { + return ExperimentSucceeded + } + if exp.IsFailed() { + return ExperimentFailed + } + if exp.IsRunning() { + return ExperimentRunning + } + return ExperimentRestarting +} + func (exp *Experiment) setCondition(conditionType ExperimentConditionType, status v1.ConditionStatus, reason, message string) { newCond := newCondition(conditionType, status, reason, message) From 3ee921dd2a5036cace47e1d4df2c4b6fe20e44ce Mon Sep 17 00:00:00 2001 From: andreyvelich Date: Wed, 8 May 2019 13:00:59 -0700 Subject: [PATCH 2/3] Change GetLastConditionType func --- .../operators/apis/experiment/v1alpha2/util.go | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/pkg/api/operators/apis/experiment/v1alpha2/util.go b/pkg/api/operators/apis/experiment/v1alpha2/util.go index 168d98f8f94..a9fe69ca160 100644 --- a/pkg/api/operators/apis/experiment/v1alpha2/util.go +++ b/pkg/api/operators/apis/experiment/v1alpha2/util.go @@ -85,20 +85,8 @@ func (exp *Experiment) IsCompleted() bool { return exp.IsSucceeded() || exp.IsFailed() } -func (exp *Experiment) GetCurrentConditionType() ExperimentConditionType { - if exp.IsCreated() { - return ExperimentCreated - } - if exp.IsSucceeded() { - return ExperimentSucceeded - } - if exp.IsFailed() { - return ExperimentFailed - } - if exp.IsRunning() { - return ExperimentRunning - } - return ExperimentRestarting +func (exp *Experiment) GetLastConditionType() ExperimentConditionType { + return exp.Status.Conditions[len(exp.Status.Conditions)-1].Type } func (exp *Experiment) setCondition(conditionType ExperimentConditionType, status v1.ConditionStatus, reason, message string) { From f5dc40cc0c20a73500bc8c37df22f50b152674f9 Mon Sep 17 00:00:00 2001 From: andreyvelich Date: Wed, 8 May 2019 17:57:14 -0700 Subject: [PATCH 3/3] Add empty condition check --- pkg/api/operators/apis/experiment/v1alpha2/util.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pkg/api/operators/apis/experiment/v1alpha2/util.go b/pkg/api/operators/apis/experiment/v1alpha2/util.go index a9fe69ca160..573cc2ba823 100644 --- a/pkg/api/operators/apis/experiment/v1alpha2/util.go +++ b/pkg/api/operators/apis/experiment/v1alpha2/util.go @@ -16,6 +16,8 @@ limitations under the License. package v1alpha2 import ( + "errors" + v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -85,8 +87,11 @@ func (exp *Experiment) IsCompleted() bool { return exp.IsSucceeded() || exp.IsFailed() } -func (exp *Experiment) GetLastConditionType() ExperimentConditionType { - return exp.Status.Conditions[len(exp.Status.Conditions)-1].Type +func (exp *Experiment) GetLastConditionType() (ExperimentConditionType, error) { + if len(exp.Status.Conditions) > 0 { + return exp.Status.Conditions[len(exp.Status.Conditions)-1].Type, nil + } + return "", errors.New("Experiment doesn't have any condition") } func (exp *Experiment) setCondition(conditionType ExperimentConditionType, status v1.ConditionStatus, reason, message string) {