diff --git a/pkg/apis/controller/experiments/v1beta1/experiment_defaults.go b/pkg/apis/controller/experiments/v1beta1/experiment_defaults.go index b7888b22010..b418c80a136 100644 --- a/pkg/apis/controller/experiments/v1beta1/experiment_defaults.go +++ b/pkg/apis/controller/experiments/v1beta1/experiment_defaults.go @@ -50,47 +50,49 @@ func (e *Experiment) setDefaultResumePolicy() { func (e *Experiment) setDefaultObjective() { obj := e.Spec.Objective - if obj.MetricStrategies == nil { - obj.MetricStrategies = make([]common.MetricStrategy, 0) - } - objectiveHasDefault := false - metricsWithDefault := make(map[string]int) - for _, strategy := range obj.MetricStrategies { - if strategy.Name == obj.ObjectiveMetricName { - objectiveHasDefault = true - continue - } - metricsWithDefault[strategy.Name] = 1 - } - - // set default strategy of objective according to ObjectiveType - if !objectiveHasDefault { - var strategy common.MetricStrategy - switch e.Spec.Objective.Type { - case common.ObjectiveTypeMinimize: - strategy = common.MetricStrategy{Name: obj.ObjectiveMetricName, Value: common.ExtractByMin} - case common.ObjectiveTypeMaximize: - strategy = common.MetricStrategy{Name: obj.ObjectiveMetricName, Value: common.ExtractByMax} - default: - strategy = common.MetricStrategy{Name: obj.ObjectiveMetricName, Value: common.ExtractByLatest} - } - obj.MetricStrategies = append(obj.MetricStrategies, strategy) - } + if obj != nil { + if obj.MetricStrategies == nil { + obj.MetricStrategies = make([]common.MetricStrategy, 0) + } + objectiveHasDefault := false + metricsWithDefault := make(map[string]int) + for _, strategy := range obj.MetricStrategies { + if strategy.Name == obj.ObjectiveMetricName { + objectiveHasDefault = true + continue + } + metricsWithDefault[strategy.Name] = 1 + } - // set default strategy of additional metrics to ExtractByLatest - for _, metricName := range obj.AdditionalMetricNames { - if _, ok := metricsWithDefault[metricName]; !ok { + // Set default strategy of objective according to ObjectiveType. + if !objectiveHasDefault { var strategy common.MetricStrategy switch e.Spec.Objective.Type { case common.ObjectiveTypeMinimize: - strategy = common.MetricStrategy{Name: metricName, Value: common.ExtractByMin} + strategy = common.MetricStrategy{Name: obj.ObjectiveMetricName, Value: common.ExtractByMin} case common.ObjectiveTypeMaximize: - strategy = common.MetricStrategy{Name: metricName, Value: common.ExtractByMax} + strategy = common.MetricStrategy{Name: obj.ObjectiveMetricName, Value: common.ExtractByMax} default: - strategy = common.MetricStrategy{Name: metricName, Value: common.ExtractByLatest} + strategy = common.MetricStrategy{Name: obj.ObjectiveMetricName, Value: common.ExtractByLatest} } obj.MetricStrategies = append(obj.MetricStrategies, strategy) } + + // Set default strategy of additional metrics to ExtractByLatest. + for _, metricName := range obj.AdditionalMetricNames { + if _, ok := metricsWithDefault[metricName]; !ok { + var strategy common.MetricStrategy + switch e.Spec.Objective.Type { + case common.ObjectiveTypeMinimize: + strategy = common.MetricStrategy{Name: metricName, Value: common.ExtractByMin} + case common.ObjectiveTypeMaximize: + strategy = common.MetricStrategy{Name: metricName, Value: common.ExtractByMax} + default: + strategy = common.MetricStrategy{Name: metricName, Value: common.ExtractByLatest} + } + obj.MetricStrategies = append(obj.MetricStrategies, strategy) + } + } } }