forked from kubeedge/kubeedge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pod.go
104 lines (89 loc) · 2.89 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
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
package manager
import (
"reflect"
"sync"
"github.com/kubeedge/kubeedge/cloud/pkg/controller/config"
"github.com/kubeedge/beehive/pkg/common/log"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
)
// CachePod is the struct save pod data for check pod is really changed
type CachePod struct {
metav1.ObjectMeta
Spec v1.PodSpec
}
// PodManager is a manager watch pod change event
type PodManager struct {
// events from watch kubernetes api server
realEvents chan watch.Event
// events merged
mergedEvents chan watch.Event
// pods, key is UID, value is *v1.Pod
pods sync.Map
}
func (pm *PodManager) isPodUpdated(old *CachePod, new *v1.Pod) bool {
// does not care fields
old.ObjectMeta.ResourceVersion = new.ObjectMeta.ResourceVersion
old.ObjectMeta.Generation = new.ObjectMeta.Generation
// return true if ObjectMeta or Spec changed, else false
return !reflect.DeepEqual(old.ObjectMeta, new.ObjectMeta) || !reflect.DeepEqual(old.Spec, new.Spec)
}
func (pm *PodManager) merge() {
for re := range pm.realEvents {
pod := re.Object.(*v1.Pod)
switch re.Type {
case watch.Added:
pm.pods.Store(pod.UID, &CachePod{ObjectMeta: pod.ObjectMeta, Spec: pod.Spec})
if pod.DeletionTimestamp == nil {
pm.mergedEvents <- re
} else {
re.Type = watch.Modified
pm.mergedEvents <- re
}
case watch.Deleted:
pm.pods.Delete(pod.UID)
pm.mergedEvents <- re
case watch.Modified:
value, ok := pm.pods.Load(pod.UID)
pm.pods.Store(pod.UID, &CachePod{ObjectMeta: pod.ObjectMeta, Spec: pod.Spec})
if ok {
cachedPod := value.(*CachePod)
if pm.isPodUpdated(cachedPod, pod) {
pm.mergedEvents <- re
}
} else {
pm.mergedEvents <- re
}
default:
log.LOGGER.Warnf("event type: %s unsupported", re.Type)
}
}
}
// Events return a channel, can receive all pod event
func (pm *PodManager) Events() chan watch.Event {
return pm.mergedEvents
}
// NewPodManager create PodManager from config
func NewPodManager(kubeClient *kubernetes.Clientset, namespace, nodeName string) (*PodManager, error) {
var lw *cache.ListWatch
if "" == nodeName {
lw = cache.NewListWatchFromClient(kubeClient.CoreV1().RESTClient(), "pods", namespace, fields.Everything())
} else {
selector := fields.OneTermEqualSelector("spec.nodeName", nodeName)
lw = cache.NewListWatchFromClient(kubeClient.CoreV1().RESTClient(), "pods", namespace, selector)
}
realEvents := make(chan watch.Event, config.PodEventBuffer)
mergedEvents := make(chan watch.Event, config.PodEventBuffer)
rh := NewCommonResourceEventHandler(realEvents)
si := cache.NewSharedInformer(lw, &v1.Pod{}, 0)
si.AddEventHandler(rh)
pm := &PodManager{realEvents: realEvents, mergedEvents: mergedEvents}
stopNever := make(chan struct{})
go si.Run(stopNever)
go pm.merge()
return pm, nil
}