forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addonsconfiguration.go
66 lines (53 loc) · 1.86 KB
/
addonsconfiguration.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
package listener
import (
"fmt"
"github.com/golang/glog"
"github.com/kyma-project/kyma/components/console-backend-service/internal/gqlschema"
v1 "k8s.io/api/core/v1"
)
//go:generate mockery -name=gqlAddonsConfigurationConverter -output=automock -outpkg=automock -case=underscore
type gqlAddonsConfigurationConverter interface {
ToGQL(item *v1.ConfigMap) *gqlschema.AddonsConfiguration
}
type AddonsConfiguration struct {
channel chan<- gqlschema.AddonsConfigurationEvent
filter func(entity *v1.ConfigMap) bool
converter gqlAddonsConfigurationConverter
}
func NewAddonsConfiguration(channel chan<- gqlschema.AddonsConfigurationEvent, filter func(entity *v1.ConfigMap) bool, converter gqlAddonsConfigurationConverter) *AddonsConfiguration {
return &AddonsConfiguration{
channel: channel,
filter: filter,
converter: converter,
}
}
func (l *AddonsConfiguration) OnAdd(object interface{}) {
l.onEvent(gqlschema.SubscriptionEventTypeAdd, object)
}
func (l *AddonsConfiguration) OnUpdate(oldObject, newObject interface{}) {
l.onEvent(gqlschema.SubscriptionEventTypeUpdate, newObject)
}
func (l *AddonsConfiguration) OnDelete(object interface{}) {
l.onEvent(gqlschema.SubscriptionEventTypeDelete, object)
}
func (l *AddonsConfiguration) onEvent(eventType gqlschema.SubscriptionEventType, object interface{}) {
entity, ok := object.(*v1.ConfigMap)
if !ok {
glog.Error(fmt.Errorf("incorrect object type: %T, should be: *ConfigMap", object))
return
}
if l.filter(entity) {
l.notify(eventType, entity)
}
}
func (l *AddonsConfiguration) notify(eventType gqlschema.SubscriptionEventType, entity *v1.ConfigMap) {
gqlAddonsConfiguration := l.converter.ToGQL(entity)
if gqlAddonsConfiguration == nil {
return
}
event := gqlschema.AddonsConfigurationEvent{
Type: eventType,
AddonsConfiguration: *gqlAddonsConfiguration,
}
l.channel <- event
}