-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
event_server.go
95 lines (77 loc) · 3.09 KB
/
event_server.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
package event
import (
"context"
"sync"
log "github.com/sirupsen/logrus"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
eventpkg "github.com/argoproj/argo-workflows/v3/pkg/apiclient/event"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo-workflows/v3/server/auth"
"github.com/argoproj/argo-workflows/v3/server/event/dispatch"
"github.com/argoproj/argo-workflows/v3/util/instanceid"
"github.com/argoproj/argo-workflows/v3/workflow/events"
)
type Controller struct {
instanceIDService instanceid.Service
eventRecorderManager events.EventRecorderManager
// a channel for operations to be executed async on
operationQueue chan dispatch.Operation
workerCount int
}
var _ eventpkg.EventServiceServer = &Controller{}
func NewController(instanceIDService instanceid.Service, eventRecorderManager events.EventRecorderManager, operationQueueSize, workerCount int) *Controller {
log.WithFields(log.Fields{"workerCount": workerCount, "operationQueueSize": operationQueueSize}).Info("Creating event controller")
return &Controller{
instanceIDService: instanceIDService,
eventRecorderManager: eventRecorderManager,
// so we can have `operationQueueSize` operations outstanding before we start putting back pressure on the senders
operationQueue: make(chan dispatch.Operation, operationQueueSize),
workerCount: workerCount,
}
}
func (s *Controller) Run(stopCh <-chan struct{}) {
// this `WaitGroup` allows us to wait for all events to dispatch before exiting
wg := sync.WaitGroup{}
for w := 0; w < s.workerCount; w++ {
go func() {
defer wg.Done()
for operation := range s.operationQueue {
ctx := context.Background()
operation.Dispatch(ctx)
}
}()
wg.Add(1)
}
<-stopCh
// stop accepting new events
close(s.operationQueue)
log.WithFields(log.Fields{"operations": len(s.operationQueue)}).Info("Waiting until all remaining events are processed")
// no more new events, process the existing events
wg.Wait()
}
func (s *Controller) ReceiveEvent(ctx context.Context, req *eventpkg.EventRequest) (*eventpkg.EventResponse, error) {
options := metav1.ListOptions{}
s.instanceIDService.With(&options)
list, err := auth.GetWfClient(ctx).ArgoprojV1alpha1().WorkflowEventBindings(req.Namespace).List(ctx, options)
if err != nil {
return nil, err
}
operation, err := dispatch.NewOperation(ctx, s.instanceIDService, s.eventRecorderManager.Get(req.Namespace), list.Items, req.Namespace, req.Discriminator, req.Payload)
if err != nil {
return nil, err
}
select {
case s.operationQueue <- *operation:
return &eventpkg.EventResponse{}, nil
default:
return nil, apierrors.NewServiceUnavailable("operation queue full")
}
}
func (s *Controller) ListWorkflowEventBindings(ctx context.Context, in *eventpkg.ListWorkflowEventBindingsRequest) (*wfv1.WorkflowEventBindingList, error) {
listOptions := metav1.ListOptions{}
if in.ListOptions != nil {
listOptions = *in.ListOptions
}
return auth.GetWfClient(ctx).ArgoprojV1alpha1().WorkflowEventBindings(in.Namespace).List(ctx, listOptions)
}