-
Notifications
You must be signed in to change notification settings - Fork 282
/
provider.go
129 lines (108 loc) · 3 KB
/
provider.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
package provider
import (
"context"
"github.com/keel-hq/keel/approvals"
"github.com/keel-hq/keel/types"
log "github.com/sirupsen/logrus"
)
// Provider - generic provider interface
type Provider interface {
Submit(event types.Event) error
TrackedImages() ([]*types.TrackedImage, error)
GetName() string
Stop()
}
// Providers - available providers
type Providers interface {
Submit(event types.Event) error
TrackedImages() ([]*types.TrackedImage, error)
List() []string // list all providers
Stop() // stop all providers
}
// New - new providers registry
func New(providers []Provider, approvalsManager approvals.Manager) *DefaultProviders {
pvs := make(map[string]Provider)
for _, p := range providers {
pvs[p.GetName()] = p
log.Infof("provider.defaultProviders: provider '%s' registered", p.GetName())
}
dp := &DefaultProviders{
providers: pvs,
approvalsManager: approvalsManager,
stopCh: make(chan struct{}),
}
// subscribing to approved events
// TODO: create Start() function for DefaultProviders
go dp.subscribeToApproved()
return dp
}
// DefaultProviders - default providers container
type DefaultProviders struct {
providers map[string]Provider
approvalsManager approvals.Manager
stopCh chan struct{}
}
func (p *DefaultProviders) subscribeToApproved() {
ctx, cancel := context.WithCancel(context.Background())
approvedCh, err := p.approvalsManager.SubscribeApproved(ctx)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Fatal("provider.subscribeToApproved: failed to subscribe for approved reqs")
}
for {
select {
case approval := <-approvedCh:
approval.Event.TriggerName = types.TriggerTypeApproval.String()
p.Submit(*approval.Event)
case <-p.stopCh:
cancel()
return
}
}
}
// Submit - submit event to all providers
func (p *DefaultProviders) Submit(event types.Event) error {
for _, provider := range p.providers {
err := provider.Submit(event)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"provider": provider.GetName(),
"event": event.Repository,
"trigger": event.TriggerName,
}).Error("provider.Submit: submit event failed")
}
}
return nil
}
// TrackedImages - get tracked images for provider
func (p *DefaultProviders) TrackedImages() ([]*types.TrackedImage, error) {
var trackedImages []*types.TrackedImage
for _, provider := range p.providers {
ti, err := provider.TrackedImages()
if err != nil {
log.WithFields(log.Fields{
"error": err,
"provider": provider.GetName(),
}).Error("provider.defaultProviders: failed to get tracked images")
continue
}
trackedImages = append(trackedImages, ti...)
}
return trackedImages, nil
}
// List - list available providers
func (p *DefaultProviders) List() []string {
list := []string{}
for name := range p.providers {
list = append(list, name)
}
return list
}
// Stop - stop all providers
func (p *DefaultProviders) Stop() {
for _, provider := range p.providers {
provider.Stop()
}
}