forked from kyma-project/kyma
/
pod.go
71 lines (58 loc) · 1.58 KB
/
pod.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
package listener
import (
"fmt"
"github.com/golang/glog"
"github.com/kyma-project/kyma/components/console-backend-service/internal/gqlschema"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
)
//go:generate mockery -name=gqlPodConverter -output=automock -outpkg=automock -case=underscore
type gqlPodConverter interface {
ToGQL(in *v1.Pod) (*gqlschema.Pod, error)
}
type Pod struct {
channel chan<- gqlschema.PodEvent
filter func(pod *v1.Pod) bool
converter gqlPodConverter
}
func NewPod(channel chan<- gqlschema.PodEvent, filter func(pod *v1.Pod) bool, converter gqlPodConverter) *Pod {
return &Pod{
channel: channel,
filter: filter,
converter: converter,
}
}
func (l *Pod) OnAdd(object interface{}) {
l.onEvent(gqlschema.SubscriptionEventTypeAdd, object)
}
func (l *Pod) OnUpdate(oldObject, newObject interface{}) {
l.onEvent(gqlschema.SubscriptionEventTypeUpdate, newObject)
}
func (l *Pod) OnDelete(object interface{}) {
l.onEvent(gqlschema.SubscriptionEventTypeDelete, object)
}
func (l *Pod) onEvent(eventType gqlschema.SubscriptionEventType, object interface{}) {
pod, ok := object.(*v1.Pod)
if !ok {
glog.Error(fmt.Errorf("incorrect object type: %T, should be: *Pod", object))
return
}
if l.filter(pod) {
l.notify(eventType, pod)
}
}
func (l *Pod) notify(eventType gqlschema.SubscriptionEventType, pod *v1.Pod) {
gqlPod, err := l.converter.ToGQL(pod)
if err != nil {
glog.Error(errors.Wrapf(err, "while converting *Pod"))
return
}
if gqlPod == nil {
return
}
event := gqlschema.PodEvent{
Type: eventType,
Pod: *gqlPod,
}
l.channel <- event
}