@@ -46,6 +46,7 @@ import (
46
46
kbatch "k8s.io/api/batch/v1"
47
47
kcore "k8s.io/api/core/v1"
48
48
kerrors "k8s.io/apimachinery/pkg/api/errors"
49
+ v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
49
50
ctrl "sigs.k8s.io/controller-runtime"
50
51
"sigs.k8s.io/controller-runtime/pkg/client"
51
52
)
@@ -462,6 +463,18 @@ func (r *BatchJobReconciler) updateStatus(ctx context.Context, batchJob *batch.B
462
463
if worker != nil {
463
464
batchJob .Status .EndTime = worker .Status .CompletionTime // assign right away, because it's a pointer
464
465
466
+ if batchJob .Status .EndTime == nil {
467
+ completedTimestampStr , completedTimestampExists := batchJob .Annotations [_completedTimestampAnnotation ]
468
+ if completedTimestampExists {
469
+ ts , err := time .Parse (time .RFC3339 , completedTimestampStr )
470
+ if err != nil {
471
+ return errors .Wrap (err , "failed to parse completed timestamp string" )
472
+ }
473
+ completedTime := v1 .NewTime (ts )
474
+ batchJob .Status .EndTime = & completedTime
475
+ }
476
+ }
477
+
465
478
if worker .Status .Failed == batchJob .Spec .Workers {
466
479
batchJobStatus := status .JobWorkerError
467
480
for _ , condition := range worker .Status .Conditions {
@@ -668,14 +681,20 @@ func getTotalBatchCount(r *BatchJobReconciler, batchJob batch.BatchJob) (int, er
668
681
}
669
682
670
683
func getMetrics (r * BatchJobReconciler , batchJob batch.BatchJob ) (metrics.BatchMetrics , error ) {
684
+ endTime := time .Now ()
685
+ if batchJob .Status .EndTime != nil {
686
+ endTime = batchJob .Status .EndTime .Time
687
+ }
688
+
671
689
jobMetrics , err := batch .GetMetrics (r .Prometheus , spec.JobKey {
672
690
ID : batchJob .Name ,
673
691
APIName : batchJob .Spec .APIName ,
674
692
Kind : userconfig .BatchAPIKind ,
675
- }, batchJob . Status . EndTime . Time )
693
+ }, endTime )
676
694
if err != nil {
677
695
return metrics.BatchMetrics {}, err
678
696
}
697
+
679
698
return jobMetrics , nil
680
699
}
681
700
0 commit comments