/
broadcast.go
400 lines (362 loc) · 10.2 KB
/
broadcast.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*This file is part of kuberpult.
Kuberpult is free software: you can redistribute it and/or modify
it under the terms of the Expat(MIT) License as published by
the Free Software Foundation.
Kuberpult is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
MIT License for more details.
You should have received a copy of the MIT License
along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
Copyright 2023 freiheit.com*/
package service
import (
"context"
"errors"
"sync"
"time"
api "github.com/freiheit-com/kuberpult/pkg/api/v1"
"github.com/freiheit-com/kuberpult/pkg/ptr"
"github.com/freiheit-com/kuberpult/services/rollout-service/pkg/versions"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/argoproj/gitops-engine/pkg/health"
"github.com/argoproj/gitops-engine/pkg/sync/common"
)
type Key struct {
Application string
Environment string
}
type appState struct {
argocdVersion *versions.VersionInfo
kuberpultVersion *versions.VersionInfo
rolloutStatus api.RolloutStatus
environmentGroup string
isProduction *bool
team string
}
func (a *appState) applyArgoEvent(ev *ArgoEvent) *BroadcastEvent {
status := rolloutStatus(ev)
if a.rolloutStatus != status || !a.argocdVersion.Equal(ev.Version) {
a.rolloutStatus = status
a.argocdVersion = ev.Version
return a.getEvent(ev.Application, ev.Environment)
}
return nil
}
func (a *appState) applyKuberpultEvent(ev *versions.KuberpultEvent) *BroadcastEvent {
if !a.argocdVersion.Equal(ev.Version) || a.isProduction == nil || *a.isProduction != ev.IsProduction {
a.kuberpultVersion = ev.Version
a.environmentGroup = ev.EnvironmentGroup
a.team = ev.Team
a.isProduction = ptr.Bool(ev.IsProduction)
return a.getEvent(ev.Application, ev.Environment)
}
return nil
}
func (a *appState) getEvent(application, environment string) *BroadcastEvent {
rs := a.rolloutStatus
if a.kuberpultVersion == nil || a.argocdVersion == nil {
if rs == api.RolloutStatus_ROLLOUT_STATUS_SUCCESFUL {
rs = api.RolloutStatus_ROLLOUT_STATUS_UNKNOWN
}
} else if a.kuberpultVersion.Version != a.argocdVersion.Version {
rs = api.RolloutStatus_ROLLOUT_STATUS_PENDING
}
return &BroadcastEvent{
Key: Key{
Environment: environment,
Application: application,
},
EnvironmentGroup: a.environmentGroup,
IsProduction: a.isProduction,
ArgocdVersion: a.argocdVersion,
RolloutStatus: rs,
Team: a.team,
KuberpultVersion: a.kuberpultVersion,
}
}
type Broadcast struct {
state map[Key]*appState
mx sync.Mutex
listener map[chan *BroadcastEvent]struct{}
// The waiting function is used in tests to trigger events after the subscription is set up.
waiting func()
}
func New() *Broadcast {
return &Broadcast{
state: map[Key]*appState{},
listener: map[chan *BroadcastEvent]struct{}{},
}
}
// ProcessArgoEvent implements service.EventProcessor
func (b *Broadcast) ProcessArgoEvent(ctx context.Context, ev ArgoEvent) {
b.mx.Lock()
defer b.mx.Unlock()
k := Key{
Application: ev.Application,
Environment: ev.Environment,
}
if b.state[k] == nil {
b.state[k] = &appState{}
}
msg := b.state[k].applyArgoEvent(&ev)
if msg == nil {
return
}
desub := []chan *BroadcastEvent{}
for l := range b.listener {
select {
case l <- msg:
default:
close(l)
desub = append(desub, l)
}
}
for _, l := range desub {
delete(b.listener, l)
}
}
func (b *Broadcast) ProcessKuberpultEvent(ctx context.Context, ev versions.KuberpultEvent) {
b.mx.Lock()
defer b.mx.Unlock()
k := Key{
Application: ev.Application,
Environment: ev.Environment,
}
if b.state[k] == nil {
b.state[k] = &appState{}
}
msg := b.state[k].applyKuberpultEvent(&ev)
if msg == nil {
return
}
desub := []chan *BroadcastEvent{}
for l := range b.listener {
select {
case l <- msg:
default:
close(l)
desub = append(desub, l)
}
}
for _, l := range desub {
delete(b.listener, l)
}
}
// Disconnects all listeners. This is used in tests to check wheter subscribers handle reconnects
func (b *Broadcast) DisconnectAll() {
b.mx.Lock()
defer b.mx.Unlock()
for l := range b.listener {
close(l)
}
b.listener = make(map[chan *BroadcastEvent]struct{})
}
func (b *Broadcast) StreamStatus(req *api.StreamStatusRequest, svc api.RolloutService_StreamStatusServer) error {
resp, ch, unsubscribe := b.Start()
defer unsubscribe()
for _, r := range resp {
svc.Send(streamStatus(r))
}
for {
select {
case r := <-ch:
if r == nil {
// closed
return nil
}
err := svc.Send(streamStatus(r))
if err != nil {
return err
}
case <-svc.Context().Done():
err := svc.Context().Err()
if errors.Is(err, context.Canceled) {
return nil
}
return err
}
}
}
func (b *Broadcast) GetStatus(ctx context.Context, req *api.GetStatusRequest) (*api.GetStatusResponse, error) {
var wait <-chan time.Time
if req.WaitSeconds > 0 {
wait = time.After(time.Duration(req.WaitSeconds) * time.Second)
}
resp, ch, unsubscribe := b.Start()
defer unsubscribe()
apps := map[Key]*api.GetStatusResponse_ApplicationStatus{}
for _, r := range resp {
s := filterApplication(req, r)
if s != nil {
apps[r.Key] = s
}
}
status := aggregateStatus(apps)
if wait != nil {
// The waiting function is used in testing to make sure, we are really processing delayed events.
if b.waiting != nil {
b.waiting()
}
waiting:
for {
status = aggregateStatus(apps)
if status == api.RolloutStatus_ROLLOUT_STATUS_SUCCESFUL || status == api.RolloutStatus_ROLLOUT_STATUS_ERROR {
break
}
select {
case r, ok := <-ch:
if !ok {
break waiting
}
s := filterApplication(req, r)
if s != nil {
apps[r.Key] = s
} else {
delete(apps, r.Key)
}
case <-ctx.Done():
break waiting
case <-wait:
break waiting
}
}
}
appList := make([]*api.GetStatusResponse_ApplicationStatus, 0, len(apps))
for _, app := range apps {
appList = append(appList, app)
}
return &api.GetStatusResponse{
Status: status,
Applications: appList,
}, nil
}
// Removes irrelevant app states from the list.
func filterApplication(req *api.GetStatusRequest, ev *BroadcastEvent) *api.GetStatusResponse_ApplicationStatus {
// Only apps that have the correct envgroup are considered
if ev.EnvironmentGroup != req.EnvironmentGroup {
return nil
}
// If it's filtered by team, then only apps with the correct team are considered.
if req.Team != "" && req.Team != ev.Team {
return nil
}
s := getStatus(ev)
// Successful apps are also irrelevant.
if s.RolloutStatus == api.RolloutStatus_ROLLOUT_STATUS_SUCCESFUL {
return nil
}
return s
}
// Calculates an aggregatted rollout status
func aggregateStatus(apps map[Key]*api.GetStatusResponse_ApplicationStatus) api.RolloutStatus {
status := api.RolloutStatus_ROLLOUT_STATUS_SUCCESFUL
for _, app := range apps {
status = mostRelevantStatus(app.RolloutStatus, status)
}
return status
}
type unsubscribe func()
func (b *Broadcast) Start() ([]*BroadcastEvent, <-chan *BroadcastEvent, unsubscribe) {
b.mx.Lock()
defer b.mx.Unlock()
result := make([]*BroadcastEvent, 0, len(b.state))
for key, app := range b.state {
result = append(result, app.getEvent(key.Application, key.Environment))
}
ch := make(chan *BroadcastEvent, 100)
b.listener[ch] = struct{}{}
return result, ch, func() {
b.mx.Lock()
defer b.mx.Unlock()
delete(b.listener, ch)
}
}
type BroadcastEvent struct {
Key
EnvironmentGroup string
Team string
IsProduction *bool
ArgocdVersion *versions.VersionInfo
KuberpultVersion *versions.VersionInfo
RolloutStatus api.RolloutStatus
}
func streamStatus(b *BroadcastEvent) *api.StreamStatusResponse {
version := uint64(0)
if b.ArgocdVersion != nil {
version = b.ArgocdVersion.Version
}
return &api.StreamStatusResponse{
Environment: b.Environment,
Application: b.Application,
Version: version,
RolloutStatus: b.RolloutStatus,
}
}
func getStatus(b *BroadcastEvent) *api.GetStatusResponse_ApplicationStatus {
return &api.GetStatusResponse_ApplicationStatus{
Environment: b.Environment,
Application: b.Application,
RolloutStatus: b.RolloutStatus,
}
}
func rolloutStatus(ev *ArgoEvent) api.RolloutStatus {
if ev.OperationState != nil {
switch ev.OperationState.Phase {
case common.OperationError, common.OperationFailed:
return api.RolloutStatus_ROLLOUT_STATUS_ERROR
}
}
switch ev.SyncStatusCode {
case v1alpha1.SyncStatusCodeOutOfSync:
return api.RolloutStatus_ROLLOUT_STATUS_PROGRESSING
}
switch ev.HealthStatusCode {
case health.HealthStatusDegraded, health.HealthStatusMissing:
return api.RolloutStatus_ROLLOUT_STATUS_UNHEALTHY
case health.HealthStatusProgressing, health.HealthStatusSuspended:
return api.RolloutStatus_ROLLOUT_STATUS_PROGRESSING
case health.HealthStatusHealthy:
if ev.Version == nil {
return api.RolloutStatus_ROLLOUT_STATUS_UNKNOWN
}
return api.RolloutStatus_ROLLOUT_STATUS_SUCCESFUL
}
return api.RolloutStatus_ROLLOUT_STATUS_UNKNOWN
}
// Depending on the rollout state, there are different things a user should do.
// 1. Nothing because everything is fine
// 2. Wait longer
// 3. Stop and call an operator
// The sorting is the same as in the UI.
var statusPriorities []api.RolloutStatus = []api.RolloutStatus{
// Error is not recoverable by waiting and requires manual intervention
api.RolloutStatus_ROLLOUT_STATUS_ERROR,
// These states may resolve by waiting longer
api.RolloutStatus_ROLLOUT_STATUS_PROGRESSING,
api.RolloutStatus_ROLLOUT_STATUS_UNHEALTHY,
api.RolloutStatus_ROLLOUT_STATUS_PENDING,
api.RolloutStatus_ROLLOUT_STATUS_UNKNOWN,
// This is the only successful state
api.RolloutStatus_ROLLOUT_STATUS_SUCCESFUL,
}
// 0 is the highest priority - (RolloutStatusSuccesful) is the lowest priority
func statusPriority(a api.RolloutStatus) int {
for i, p := range statusPriorities {
if p == a {
return i
}
}
return len(statusPriorities) - 1
}
func mostRelevantStatus(a, b api.RolloutStatus) api.RolloutStatus {
ap := statusPriority(a)
bp := statusPriority(b)
if ap < bp {
return a
} else {
return b
}
}
var _ ArgoEventProcessor = (*Broadcast)(nil)
var _ api.RolloutServiceServer = (*Broadcast)(nil)