Skip to content

Commit

Permalink
Merge pull request #616 from johnugeorge/metricfix
Browse files Browse the repository at this point in the history
Set trial completion status only after metric collection
  • Loading branch information
richardsliu authored May 31, 2019
2 parents 8056907 + df9f22c commit 48889cb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
25 changes: 21 additions & 4 deletions pkg/controller/v1alpha2/trial/trial_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
return err
}

// Watch for changes to Cronjob
err = c.Watch(
&source.Kind{Type: &batchv1beta.CronJob{}},
&handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &trialsv1alpha2.Trial{},
})

if err != nil {
log.Error(err, "CronJob watch error")
return err
}

for _, gvk := range commonv1alpha2.GetSupportedJobList() {
unstructuredJob := &unstructured.Unstructured{}
unstructuredJob.SetGroupVersionKind(gvk)
Expand Down Expand Up @@ -215,14 +228,18 @@ func (r *ReconcileTrial) reconcileTrial(instance *trialsv1alpha2.Trial) error {
//Job already exists
//TODO Can desired Spec differ from deployedSpec?
if deployedJob != nil {
if err = r.UpdateTrialStatusCondition(instance, deployedJob); err != nil {
logger.Error(err, "Update trial status condition error")
return err
}
if err = r.UpdateTrialStatusObservation(instance, deployedJob); err != nil {
logger.Error(err, "Update trial status observation error")
return err
}
// Update Trial job status only if observation field is available.
// This will ensure that trial is set to be complete only if metric is collected atleast once
if isTrialObservationAvailable(instance) {
if err = r.UpdateTrialStatusCondition(instance, deployedJob); err != nil {
logger.Error(err, "Update trial status condition error")
return err
}
}
}
return nil
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/controller/v1alpha2/trial/trial_controller_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ func (r *ReconcileTrial) UpdateTrialStatusObservation(instance *trialsv1alpha2.T
return nil
}

func isTrialObservationAvailable(instance *trialsv1alpha2.Trial) bool {
objectiveMetricName := instance.Spec.Objective.ObjectiveMetricName
if instance.Status.Observation != nil && instance.Status.Observation.Metrics != nil {
for _, metric := range instance.Status.Observation.Metrics {
if metric.Name == objectiveMetricName {
return true
}
}
}
return false
}

func getBestObjectiveMetricValue(metricLogs []*api_pb.MetricLog, objectiveType commonv1alpha2.ObjectiveType) *float64 {
metricLogSize := len(metricLogs)
if metricLogSize == 0 {
Expand Down

0 comments on commit 48889cb

Please sign in to comment.