@@ -18,8 +18,10 @@ import (
18
18
"context"
19
19
"encoding/json"
20
20
"fmt"
21
+ scheduledworkflow "github.com/kubeflow/pipelines/backend/src/crd/pkg/apis/scheduledworkflow/v1beta1"
21
22
"io"
22
23
"net"
24
+ "reflect"
23
25
"strconv"
24
26
"time"
25
27
@@ -582,6 +584,77 @@ func (r *ResourceManager) CreateRun(ctx context.Context, run *model.Run) (*model
582
584
return newRun , nil
583
585
}
584
586
587
+ // ReconcileSwfCrs reconciles the ScheduledWorkflow CRs based on existing jobs.
588
+ func (r * ResourceManager ) ReconcileSwfCrs (ctx context.Context ) error {
589
+ filterContext := & model.FilterContext {
590
+ ReferenceKey : & model.ReferenceKey {Type : model .NamespaceResourceType , ID : common .GetPodNamespace ()},
591
+ }
592
+
593
+ opts := list .EmptyOptions ()
594
+
595
+ jobs , _ , _ , err := r .jobStore .ListJobs (filterContext , opts )
596
+
597
+ if err != nil {
598
+ return util .Wrap (err , "Failed to reconcile ScheduledWorkflow Kubernetes resources" )
599
+ }
600
+
601
+ for i := range jobs {
602
+ select {
603
+ case <- ctx .Done ():
604
+ return nil
605
+ default :
606
+ }
607
+
608
+ tmpl , _ , err := r .fetchTemplateFromPipelineSpec (& jobs [i ].PipelineSpec )
609
+ if err != nil {
610
+ return failedToReconcileSwfCrsError (err )
611
+ }
612
+
613
+ newScheduledWorkflow , err := tmpl .ScheduledWorkflow (jobs [i ], r .getOwnerReferences ())
614
+ if err != nil {
615
+ return failedToReconcileSwfCrsError (err )
616
+ }
617
+
618
+ for {
619
+ currentScheduledWorkflow , err := r .getScheduledWorkflowClient (jobs [i ].Namespace ).Get (ctx , jobs [i ].K8SName , v1.GetOptions {})
620
+ if err != nil {
621
+ if util .IsNotFound (err ) {
622
+ break
623
+ }
624
+ return failedToReconcileSwfCrsError (err )
625
+ }
626
+
627
+ if ! reflect .DeepEqual (currentScheduledWorkflow .Spec , newScheduledWorkflow .Spec ) {
628
+ currentScheduledWorkflow .Spec = newScheduledWorkflow .Spec
629
+ err = r .updateSwfCrSpec (ctx , jobs [i ].Namespace , currentScheduledWorkflow )
630
+ if err != nil {
631
+ if apierrors .IsConflict (errors .Unwrap (err )) {
632
+ continue
633
+ } else if util .IsNotFound (errors .Cause (err )) {
634
+ break
635
+ }
636
+ return failedToReconcileSwfCrsError (err )
637
+ }
638
+ }
639
+ break
640
+ }
641
+ }
642
+
643
+ return nil
644
+ }
645
+
646
+ func failedToReconcileSwfCrsError (err error ) error {
647
+ return util .Wrap (err , "Failed to reconcile ScheduledWorkflow Kubernetes resources" )
648
+ }
649
+
650
+ func (r * ResourceManager ) updateSwfCrSpec (ctx context.Context , k8sNamespace string , scheduledWorkflow * scheduledworkflow.ScheduledWorkflow ) error {
651
+ _ , err := r .getScheduledWorkflowClient (k8sNamespace ).Update (ctx , scheduledWorkflow )
652
+ if err != nil {
653
+ return util .Wrap (err , "Failed to update ScheduledWorkflow" )
654
+ }
655
+ return nil
656
+ }
657
+
585
658
// Fetches a run with a given id.
586
659
func (r * ResourceManager ) GetRun (runId string ) (* model.Run , error ) {
587
660
run , err := r .runStore .GetRun (runId )
0 commit comments