@@ -2,7 +2,7 @@ package event
2
2
3
3
import (
4
4
"context"
5
- "errors "
5
+ "encoding/json "
6
6
"fmt"
7
7
"os"
8
8
"strconv"
@@ -15,15 +15,22 @@ import (
15
15
16
16
"github.com/ovh/cds/engine/api/integration"
17
17
"github.com/ovh/cds/sdk"
18
+ "github.com/ovh/cds/sdk/event"
18
19
"github.com/ovh/cds/sdk/namesgenerator"
19
20
)
20
21
22
+ type Config struct {
23
+ GlobalKafka event.KafkaConfig `toml:"globalKafka" json:"globalKafka" mapstructure:"globalKafka"`
24
+ JobSummaryKafka event.KafkaConfig `toml:"jobSummaryKafka" json:"jobSummaryKafka" mapstructure:"jobSummaryKafka"`
25
+ }
26
+
21
27
// cache with go cache
22
28
var (
23
29
brokersConnectionCache = gocache .New (10 * time .Minute , 6 * time .Hour )
24
30
hostname , cdsname string
25
31
brokers []Broker
26
32
globalBroker Broker
33
+ jobSummaryBroker Broker
27
34
subscribers []chan <- sdk.Event
28
35
)
29
36
@@ -34,7 +41,7 @@ func init() {
34
41
// Broker event typed
35
42
type Broker interface {
36
43
initialize (ctx context.Context , options interface {}) (Broker , error )
37
- sendEvent (event * sdk. Event ) error
44
+ sendEvent (event interface {} ) error
38
45
status () string
39
46
close (ctx context.Context )
40
47
}
@@ -48,8 +55,8 @@ func getBroker(ctx context.Context, t string, option interface{}) (Broker, error
48
55
return nil , fmt .Errorf ("invalid Broker Type %s" , t )
49
56
}
50
57
51
- func getKafkaConfig (cfg sdk.IntegrationConfig ) KafkaConfig {
52
- kafkaCfg := KafkaConfig {
58
+ func getKafkaConfig (cfg sdk.IntegrationConfig ) event. KafkaConfig {
59
+ kafkaCfg := event. KafkaConfig {
53
60
Enabled : true ,
54
61
BrokerAddresses : cfg ["broker url" ].Value ,
55
62
Topic : cfg ["topic" ].Value ,
@@ -100,11 +107,7 @@ func ResetEventIntegration(ctx context.Context, db gorp.SqlExecutor, eventIntegr
100
107
}
101
108
102
109
// Initialize initializes event system
103
- func Initialize (ctx context.Context , db * gorp.DbMap , cache Store , glolbalKafkaConfigs ... KafkaConfig ) error {
104
- if len (glolbalKafkaConfigs ) > 1 {
105
- return errors .New ("only one global kafka global config is supported" )
106
- }
107
-
110
+ func Initialize (ctx context.Context , db * gorp.DbMap , cache Store , config * Config ) error {
108
111
store = cache
109
112
var err error
110
113
hostname , err = os .Hostname ()
@@ -121,11 +124,27 @@ func Initialize(ctx context.Context, db *gorp.DbMap, cache Store, glolbalKafkaCo
121
124
}
122
125
}
123
126
124
- if len (glolbalKafkaConfigs ) == 1 && glolbalKafkaConfigs [0 ].BrokerAddresses != "" {
125
- globalBroker , err = getBroker (ctx , "kafka" , glolbalKafkaConfigs [0 ])
127
+ if config == nil {
128
+ return nil
129
+ }
130
+
131
+ if config .GlobalKafka .BrokerAddresses != "" {
132
+ globalBroker , err = getBroker (ctx , "kafka" , config .GlobalKafka )
126
133
if err != nil {
127
134
ctx = log .ContextWithStackTrace (ctx , err )
128
135
log .Error (ctx , "unable to init builtin kafka broker from config: %v" , err )
136
+ } else {
137
+ log .Info (ctx , "client to broker %s:%s ready" , config .GlobalKafka .BrokerAddresses , config .GlobalKafka .Topic )
138
+ }
139
+ }
140
+
141
+ if config .JobSummaryKafka .BrokerAddresses != "" {
142
+ jobSummaryBroker , err = getBroker (ctx , "kafka" , config .JobSummaryKafka )
143
+ if err != nil {
144
+ ctx = log .ContextWithStackTrace (ctx , err )
145
+ log .Error (ctx , "unable to init builtin kafka broker from config: %v" , err )
146
+ } else {
147
+ log .Info (ctx , "client to broker %s:%s ready" , config .JobSummaryKafka .BrokerAddresses , config .GlobalKafka .Topic )
129
148
}
130
149
}
131
150
@@ -150,32 +169,47 @@ func DequeueEvent(ctx context.Context, db *gorp.DbMap) {
150
169
return
151
170
}
152
171
153
- for _ , s := range subscribers {
154
- s <- e
172
+ // Filter "EventJobSummary" for globalKafka Broker
173
+ if e .EventType != "sdk.EventJobSummary" {
174
+ for _ , s := range subscribers {
175
+ s <- e
176
+ }
177
+ if globalBroker != nil {
178
+ log .Info (ctx , "sending event %q to global broker" , e .EventType )
179
+ if err := globalBroker .sendEvent (& e ); err != nil {
180
+ log .Warn (ctx , "Error while sending message [%s: %s/%s/%s/%s/%s]: %s" , e .EventType , e .ProjectKey , e .WorkflowName , e .ApplicationName , e .PipelineName , e .EnvironmentName , err )
181
+ }
182
+ }
183
+ continue
184
+ // we don't send other events than EventJobSummary to users kafka
155
185
}
156
186
157
- if globalBroker != nil {
158
- log .Info (ctx , "sending event %q to global broker" , e .EventType )
159
- if err := globalBroker .sendEvent (& e ); err != nil {
160
- log .Warn (ctx , "Error while sending message [%s: %s/%s/%s/%s/%s]: %s" , e .EventType , e .ProjectKey , e .WorkflowName , e .ApplicationName , e .PipelineName , e .EnvironmentName , err )
187
+ // We now only send "EventJobSummary" in the jobSummary Broker in project integrations
188
+ // if the users send specific kafka integration on their workflows
189
+ var ejs sdk.EventJobSummary
190
+ if err := json .Unmarshal (e .Payload , & ejs ); err != nil {
191
+ ctx := log .ContextWithStackTrace (ctx , err )
192
+ log .Error (ctx , "unable to unmarshal EventJobSummary" )
193
+ continue
194
+ }
195
+ if jobSummaryBroker != nil {
196
+ log .Info (ctx , "sending event %+v to job summary broker" , ejs )
197
+ if err := jobSummaryBroker .sendEvent (ejs ); err != nil {
198
+ log .Error (ctx , "Error while sending message %s: %v" , string (e .Payload ), err )
161
199
}
162
200
}
163
201
164
202
for _ , eventIntegrationID := range e .EventIntegrationsID {
165
203
brokerConnectionKey := strconv .FormatInt (eventIntegrationID , 10 )
166
204
brokerConnection , ok := brokersConnectionCache .Get (brokerConnectionKey )
167
- var brokerConfig KafkaConfig
205
+ var brokerConfig event. KafkaConfig
168
206
if ! ok {
169
207
projInt , err := integration .LoadProjectIntegrationByIDWithClearPassword (ctx , db , eventIntegrationID )
170
208
if err != nil {
171
209
log .Error (ctx , "Event.DequeueEvent> Cannot load project integration for project %s and id %d and type event: %v" , e .ProjectKey , eventIntegrationID , err )
172
210
continue
173
211
}
174
212
175
- if projInt .Model .Public {
176
- continue
177
- }
178
-
179
213
kafkaCfg := getKafkaConfig (projInt .Config )
180
214
kafkaBroker , err := getBroker (ctx , "kafka" , kafkaCfg )
181
215
if err != nil {
@@ -197,9 +231,9 @@ func DequeueEvent(ctx context.Context, db *gorp.DbMap) {
197
231
}
198
232
199
233
// Send into external brokers
200
- log .Info (ctx , "sending event %q to %s" , e .EventType , brokerConfig .BrokerAddresses )
201
- if err := broker .sendEvent (& e ); err != nil {
202
- log .Warn (ctx , "Error while sending message [ %s: %s/%s/%s/%s/%s]: %s " , e . EventType , e . ProjectKey , e . WorkflowName , e . ApplicationName , e . PipelineName , e . EnvironmentName , err )
234
+ log .Info (ctx , "sending event %q to integration broker: %s" , e .EventType , brokerConfig .BrokerAddresses )
235
+ if err := broker .sendEvent (ejs ); err != nil {
236
+ log .Warn (ctx , "Error while sending message %s: %v " , string ( e . Payload ) , err )
203
237
}
204
238
}
205
239
}
0 commit comments