This repository has been archived by the owner on Mar 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
argoworkflow_status_agent_controller.go
100 lines (87 loc) · 3.22 KB
/
argoworkflow_status_agent_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package agent
import (
"context"
"fmt"
"reflect"
argov1alpha1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)
const (
AnnotationKeyHubWorkflowUID = "workflows.argoproj.io/ocm-hub-workflow-uid"
)
type ArgoWorkflowStatusController struct {
spokeClient client.Client
hubClient client.Client
log logr.Logger
clusterName string
}
var WorkflowPredicateFunctions = predicate.Funcs{
// only reconcile on status chagne
UpdateFunc: func(e event.UpdateEvent) bool {
newWf := e.ObjectNew.(*argov1alpha1.Workflow)
oldWf := e.ObjectOld.(*argov1alpha1.Workflow)
return containsValidOCMAnnotation(*newWf) && !reflect.DeepEqual(newWf.Status, oldWf.Status)
},
CreateFunc: func(e event.CreateEvent) bool {
workflow := e.Object.(*argov1alpha1.Workflow)
return containsValidOCMAnnotation(*workflow)
},
// do not reconcile on delete
DeleteFunc: func(e event.DeleteEvent) bool {
return false
},
}
func (c *ArgoWorkflowStatusController) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&argov1alpha1.Workflow{}).
WithEventFilter(WorkflowPredicateFunctions).
Complete(c)
}
// Reconcile Workflow status changes and create/update a Workflow CR in the hub cluster's managed cluster namespace
// This agent only has permission to create/update in that particular namespace so the hub cluster
// will need another controller that sync the Workflow status from hub's managed cluster namespace to the original dormant Workflow.
func (c *ArgoWorkflowStatusController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
c.log.Info(fmt.Sprintf("reconciling... %s", req))
defer c.log.Info(fmt.Sprintf("done reconcile %s", req))
workflow := argov1alpha1.Workflow{}
err := c.spokeClient.Get(ctx, req.NamespacedName, &workflow)
switch {
case errors.IsNotFound(err):
return ctrl.Result{}, nil
case err != nil:
c.log.Error(err, "unable to get Workflow")
return ctrl.Result{}, err
}
if !workflow.DeletionTimestamp.IsZero() {
return ctrl.Result{}, nil
}
// note: this is not the orginial dormant workflow but a new/updated one that helps bridge the status sync.
hubWorkflow := argov1alpha1.Workflow{}
hubWorkflow.Namespace = c.clusterName
hubWorkflow.Name = generateHubWorkflowStatusSyncName(workflow)
err = c.hubClient.Get(ctx, types.NamespacedName{Namespace: hubWorkflow.Namespace, Name: hubWorkflow.Name}, &hubWorkflow)
switch {
case errors.IsNotFound(err):
hubWorkflow.Spec.Entrypoint = "workflow-status-sync-only"
if err = c.hubClient.Create(ctx, &hubWorkflow); err != nil {
c.log.Error(err, "unable to create hub Workflow")
return ctrl.Result{}, err
}
return ctrl.Result{Requeue: true}, nil
case err != nil:
c.log.Error(err, "unable to get hub Workflow")
return ctrl.Result{}, err
}
hubWorkflow.Status = workflow.Status
err = c.hubClient.Update(ctx, &hubWorkflow)
if err != nil {
c.log.Error(err, "unable to update hub Workflow status")
}
return ctrl.Result{}, err
}