-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathk8s_pod_tracker.go
87 lines (76 loc) · 1.97 KB
/
k8s_pod_tracker.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
package k8swatcher
import (
"net"
"reflect"
k8score "k8s.io/api/core/v1"
)
type podTracker struct {
lastStatus map[string]*k8score.Pod
lastVersion ResourceVersion
}
func (p *podTracker) recordEvent(ev PodEvent) {
p.lastVersion = ev.ResourceVersion()
switch pe := ev.(type) {
case *CreatePod:
p.lastStatus[ev.PodName()] = pe.Def
case *ModPod:
p.lastStatus[ev.PodName()] = pe.Def
case *DeletePod:
// We don't care about the state, we'll just delete any entry
// that's here for this pod.
delete(p.lastStatus, ev.PodName())
}
}
// Looks up the previous record (if any) and synthesizes an event for this pod.
// The event may be nil if no event should be generated.
func (p *podTracker) synthesizeEvent(pod *k8score.Pod) PodEvent {
podName := pod.ObjectMeta.Name
podIP := pod.Status.PodIP
ipaddr := net.ParseIP(podIP)
oldpod, present := p.lastStatus[podName]
// update our existing record
p.lastStatus[podName] = pod
if pod.ResourceVersion != "" {
p.lastVersion = ResourceVersion(pod.ResourceVersion)
}
if present {
// if nothing changed, just return nil
if reflect.DeepEqual(pod.Status, oldpod.Status) &&
reflect.DeepEqual(pod.ObjectMeta, oldpod.ObjectMeta) {
return nil
}
return &ModPod{
name: podName,
IP: &net.IPAddr{
IP: ipaddr,
Zone: "",
},
Def: pod,
}
}
return &CreatePod{
name: podName,
rv: ResourceVersion(p.lastVersion),
IP: &net.IPAddr{
IP: ipaddr,
Zone: "",
},
Def: pod,
}
}
// findRemoveDeadPods returns a set of dead pods
func (p *podTracker) findRemoveDeadPods(existingPodNames []string) map[string]struct{} {
oldPods := make(map[string]struct{}, len(p.lastStatus))
for podName := range p.lastStatus {
oldPods[podName] = struct{}{}
}
for _, pod := range existingPodNames {
delete(oldPods, pod)
}
// At this point the remaining pods in oldPods are dead
// now we update the existing state map to remove the dead pods.
for pn := range oldPods {
delete(p.lastStatus, pn)
}
return oldPods
}