-
Notifications
You must be signed in to change notification settings - Fork 0
/
event-sources.go
275 lines (244 loc) · 9.26 KB
/
event-sources.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
Copyright 2018 BlackRock, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gateways
import (
"context"
"fmt"
"io"
"time"
"github.com/argoproj/argo-events/pkg/apis/gateway"
"github.com/argoproj/argo-events/common"
"github.com/argoproj/argo-events/pkg/apis/gateway/v1alpha1"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
corev1 "k8s.io/api/core/v1"
)
// createInternalEventSources creates an internal representation of event source declared in the gateway configmap.
// returned event sources are map of hash of event source and event source itself.
// Creating a hash of event source makes it easy to check equality of two event sources.
func (gc *GatewayConfig) createInternalEventSources(cm *corev1.ConfigMap) (map[string]*EventSourceContext, error) {
configs := make(map[string]*EventSourceContext)
for configKey, configValue := range cm.Data {
hashKey := common.Hasher(configKey + configValue)
gc.Log.WithFields(
map[string]interface{}{
"config-key": configKey,
"config-value": configValue,
"hash": string(hashKey),
},
).Info("event source")
// create a connection to gateway server
ctx, cancel := context.WithCancel(context.Background())
conn, err := grpc.Dial(
fmt.Sprintf("localhost:%s", gc.serverPort),
grpc.WithBlock(),
grpc.WithInsecure(),
grpc.WithTimeout(common.ServerConnTimeout*time.Second))
if err != nil {
gc.Log.WithError(err).Panic("failed to connect to gateway server")
cancel()
return nil, err
}
gc.Log.WithField("state", conn.GetState().String()).Info("state of the connection")
configs[hashKey] = &EventSourceContext{
Source: &EventSource{
Id: hashKey,
Name: configKey,
Data: configValue,
Version: cm.Labels[common.LabelArgoEventsEventSourceVersion],
},
Cancel: cancel,
Ctx: ctx,
Client: NewEventingClient(conn),
Conn: conn,
}
}
return configs, nil
}
// diffConfig diffs currently registered event sources and the event sources in the gateway configmap
// It simply matches the event source strings. So, if event source string differs through some sequence of definition
// and although the event sources are actually same, this method will treat them as different event sources.
// retunrs staleConfig - event sources to be removed from gateway
// newConfig - new event sources to run
func (gc *GatewayConfig) diffEventSources(newConfigs map[string]*EventSourceContext) (staleConfigKeys []string, newConfigKeys []string) {
var currentConfigKeys []string
var updatedConfigKeys []string
for currentConfigKey := range gc.registeredConfigs {
currentConfigKeys = append(currentConfigKeys, currentConfigKey)
}
for updatedConfigKey := range newConfigs {
updatedConfigKeys = append(updatedConfigKeys, updatedConfigKey)
}
gc.Log.WithField("current-event-sources-keys", currentConfigKeys).Debug("event sources hashes")
gc.Log.WithField("updated-event-sources-keys", updatedConfigKeys).Debug("event sources hashes")
swapped := false
// iterates over current event sources and updated event sources
// and creates two arrays, first one containing event sources that need to removed
// and second containing new event sources that need to be added and run.
for i := 0; i < 2; i++ {
for _, cc := range currentConfigKeys {
found := false
for _, uc := range updatedConfigKeys {
if cc == uc {
found = true
break
}
}
if !found {
if swapped {
newConfigKeys = append(newConfigKeys, cc)
} else {
staleConfigKeys = append(staleConfigKeys, cc)
}
}
}
if i == 0 {
currentConfigKeys, updatedConfigKeys = updatedConfigKeys, currentConfigKeys
swapped = true
}
}
return
}
// startEventSources starts new event sources added to gateway
func (gc *GatewayConfig) startEventSources(eventSources map[string]*EventSourceContext, keys []string) {
for _, key := range keys {
eventSource := eventSources[key]
// register the event source
gc.registeredConfigs[key] = eventSource
log := gc.Log.WithField(common.LabelEventSource, eventSource.Source.Name)
log.Info("activating new event source")
go func() {
// conn should be in READY state
if eventSource.Conn.GetState() != connectivity.Ready {
gc.Log.Error("connection is not in ready state.")
gc.StatusCh <- EventSourceStatus{
Phase: v1alpha1.NodePhaseError,
Id: eventSource.Source.Id,
Message: "connection_is_not_in_ready_state",
Name: eventSource.Source.Name,
}
return
}
// validate event source
if valid, _ := eventSource.Client.ValidateEventSource(eventSource.Ctx, eventSource.Source); !valid.IsValid {
gc.Log.WithFields(
map[string]interface{}{
"validation-failure": valid.Reason,
},
).Error("event source is not valid")
if err := eventSource.Conn.Close(); err != nil {
gc.Log.WithError(err).Error("failed to close client connection")
}
gc.StatusCh <- EventSourceStatus{
Phase: v1alpha1.NodePhaseError,
Id: eventSource.Source.Id,
Message: "event_source_is_not_valid",
Name: eventSource.Source.Name,
}
return
}
gc.Log.Info("event source is valid")
// mark event source as running
gc.StatusCh <- EventSourceStatus{
Phase: v1alpha1.NodePhaseRunning,
Message: "event_source_is_running",
Id: eventSource.Source.Id,
Name: eventSource.Source.Name,
}
// listen to events from gateway server
eventStream, err := eventSource.Client.StartEventSource(eventSource.Ctx, eventSource.Source)
if err != nil {
gc.Log.WithError(err).Error("error occurred while starting event source")
gc.StatusCh <- EventSourceStatus{
Phase: v1alpha1.NodePhaseError,
Message: "failed_to_receive_event_stream",
Name: eventSource.Source.Name,
Id: eventSource.Source.Id,
}
return
}
gc.Log.Info("started listening to events from gateway server")
for {
event, err := eventStream.Recv()
if err != nil {
if err == io.EOF {
gc.Log.Info("event source has stopped")
gc.StatusCh <- EventSourceStatus{
Phase: v1alpha1.NodePhaseCompleted,
Message: "event_source_has_been_stopped",
Name: eventSource.Source.Name,
Id: eventSource.Source.Id,
}
return
}
gc.Log.WithError(err).Error("failed to receive event from stream")
gc.StatusCh <- EventSourceStatus{
Phase: v1alpha1.NodePhaseError,
Message: "failed_to_receive_event_from_event_source_stream",
Name: eventSource.Source.Name,
Id: eventSource.Source.Id,
}
return
}
err = gc.DispatchEvent(event)
if err != nil {
// escalate error through a K8s event
labels := map[string]string{
common.LabelEventType: string(common.EscalationEventType),
common.LabelGatewayEventSourceName: eventSource.Source.Name,
common.LabelGatewayName: gc.Name,
common.LabelGatewayEventSourceID: eventSource.Source.Id,
common.LabelOperation: "dispatch_event_to_watchers",
}
if err := common.GenerateK8sEvent(gc.Clientset, fmt.Sprintf("failed to dispatch event to watchers"), common.EscalationEventType, "event dispatch failed", gc.Name, gc.Namespace, gc.controllerInstanceID, gateway.Kind, labels); err != nil {
gc.Log.WithError(err).Error("failed to create K8s event to escalate event dispatch failure")
}
gc.Log.WithError(err).Error("failed to dispatch event to watchers")
}
}
}()
}
}
// stopEventSources stops an existing event sources
func (gc *GatewayConfig) stopEventSources(configs []string) {
for _, configKey := range configs {
eventSource := gc.registeredConfigs[configKey]
delete(gc.registeredConfigs, configKey)
gc.Log.WithField(common.LabelEventSource, eventSource.Source.Name).Info("removing the event source")
gc.StatusCh <- EventSourceStatus{
Phase: v1alpha1.NodePhaseRemove,
Id: eventSource.Source.Id,
Message: "event_source_is_removed",
Name: eventSource.Source.Name,
}
eventSource.Cancel()
if err := eventSource.Conn.Close(); err != nil {
gc.Log.WithField(common.LabelEventSource, eventSource.Source.Name).WithError(err).Error("failed to close client connection")
}
}
}
// manageEventSources syncs registered event sources and updated gateway configmap
func (gc *GatewayConfig) manageEventSources(cm *corev1.ConfigMap) error {
eventSources, err := gc.createInternalEventSources(cm)
if err != nil {
return err
}
staleEventSources, newEventSources := gc.diffEventSources(eventSources)
gc.Log.WithField(common.LabelEventSource, staleEventSources).Info("stale event sources")
gc.Log.WithField(common.LabelEventSource, newEventSources).Info("new event sources")
// stop existing event sources
gc.stopEventSources(staleEventSources)
// start new event sources
gc.startEventSources(eventSources, newEventSources)
return nil
}