-
Notifications
You must be signed in to change notification settings - Fork 4
/
event_service.go
112 lines (95 loc) · 2.86 KB
/
event_service.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
package main
import (
"encoding/json"
"time"
v1 "k8s.io/api/core/v1"
)
func getServicePods(c *kubernetesClient, db Cachier, s *v1.Service) ([]v1.Pod, error) {
suid := string(s.GetUID())
// Find all PODS for this service so that a rerverse lookup is possible.
pods, err := c.getPods(db, s)
if err != nil {
return pods, err
}
// Save service -> pods
if err := db.Set(servicePodsTable, suid, pods); err != nil {
return pods, err
}
// Also save pod -> service denormalized for reverse Index lookup
for _, p := range pods {
// A pod may be behind multiple services.
// Get the existing array. append the new serviceID and set again
// Calls for race condition probably. So will need some mutex here.
if err := db.Set(
makeKey(podServicesTable, string(p.GetUID())), suid, true,
); err != nil {
return pods, err
}
}
return pods, nil
}
/*
func getServiceApps(c *kubernetesClient, db Cachier, s *v1.Service) ([]appsv1.Deployment, error) {
suid := string(s.GetUID())
// Find all Replication Controllers for this service
// so that a rerverse lookup is possible.
apps, err := c.getApps(db, s)
if err != nil {
return apps, err
}
// Save service -> ReplicationControllers
if err := db.Set(serviceAppsTable, suid, apps); err != nil {
return apps, err
}
// Also save rc -> service denormalized for reverse Index lookup
for _, r := range apps {
// A pod may be behind multiple services.
// Get the existing array. append the new serviceID and set again
// Calls for race condition probably. So will need some mutex here.
if err := db.Set(
makeKey(appServicesTable, string(r.GetUID())), suid, true,
); err != nil {
return apps, err
}
}
return apps, nil
}
*/
// eventID
func makeL9ServiceEvent(db Cachier, c *kubernetesClient, eventID string, s *v1.Service, eventType string) (*L9Event, error) {
suid := string(s.GetUID())
// Save service to database
// Maybe use s.SelfLink since UID is literally not exposed elsewhere for
// a service other than the service itself being aware of it.
// And a change in the service will change the UID afterall.
if err := db.Set(serviceTable, suid, s); err != nil {
return nil, err
}
pods, err := getServicePods(c, db, s)
if err != nil {
return nil, err
}
podMap := map[string]interface{}{}
for _, p := range pods {
b, err := json.Marshal(miniPodInfo(p))
if err != nil {
podMap[p.GetName()] = err.Error()
} else {
podMap[p.GetName()] = string(b)
}
}
return &L9Event{
ID: eventID,
Timestamp: time.Now().Unix(),
Component: s.GetName(),
Message: eventType,
Namespace: s.GetNamespace(),
Reason: eventType,
ReferenceVersion: s.GetResourceVersion(),
ObjectUid: string(s.GetUID()),
Labels: s.GetLabels(),
Annotations: s.GetAnnotations(),
Pod: podMap,
Version: VERSION,
}, nil
}