/
pod_cache.go
412 lines (387 loc) · 11.1 KB
/
pod_cache.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package pod
import (
"crypto/sha256"
"fmt"
"sort"
"strings"
"sync"
"time"
"github.com/goodrain/rainbond/db"
"github.com/goodrain/rainbond/db/model"
"github.com/goodrain/rainbond/util"
"github.com/pquerna/ffjson/ffjson"
"github.com/Sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/pkg/api"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/tools/cache"
)
//CacheManager pod cache manager
type CacheManager struct {
caches map[string]*v1.Pod
lock sync.Mutex
kubeclient *kubernetes.Clientset
stop chan struct{}
cacheWatchs []*cacheWatch
oomInfos map[string]*AbnormalInfo
errorInfos map[string]*AbnormalInfo
rcController cache.Controller
}
//AbnormalInfo pod Abnormal info
//Record the container exception exit information in pod.
type AbnormalInfo struct {
ServiceID string `json:"service_id"`
ServiceAlias string `json:"service_alias"`
PodName string `json:"pod_name"`
ContainerName string `json:"container_name"`
Reason string `json:"reson"`
Message string `json:"message"`
CreateTime time.Time `json:"create_time"`
Count int `json:"count"`
}
//Hash get AbnormalInfo hash
func (a AbnormalInfo) Hash() string {
hash := sha256.New()
hash.Write([]byte(a.ServiceID + a.ServiceAlias + a.PodName + a.ContainerName))
return fmt.Sprintf("%x", hash.Sum(nil))
}
func (a AbnormalInfo) String() string {
return fmt.Sprintf("ServiceID: %s;ServiceAlias: %s;PodName: %s ; ContainerName: %s; Reason: %s; Message: %s",
a.ServiceID, a.ServiceAlias, a.PodName, a.ContainerName, a.Reason, a.Message)
}
//NewCacheManager create pod cache manager and start it
func NewCacheManager(kubeclient *kubernetes.Clientset) *CacheManager {
m := &CacheManager{
kubeclient: kubeclient,
stop: make(chan struct{}),
caches: make(map[string]*v1.Pod),
oomInfos: make(map[string]*AbnormalInfo),
errorInfos: make(map[string]*AbnormalInfo),
}
lw := NewListWatchPodFromClient(kubeclient.Core().RESTClient())
_, rcController := cache.NewInformer(
lw,
&v1.Pod{},
15*time.Minute,
cache.ResourceEventHandlerFuncs{
AddFunc: m.addCachePod(),
UpdateFunc: m.updateCachePod(),
DeleteFunc: m.deleteCachePod(),
},
)
m.rcController = rcController
return m
}
//Start start watch pod event
func (c *CacheManager) Start() {
logrus.Info("pod source watching started...")
go c.rcController.Run(c.stop)
}
//Stop stop
func (c *CacheManager) Stop() {
close(c.stop)
}
func (c *CacheManager) addCachePod() func(obj interface{}) {
return func(obj interface{}) {
pod, ok := obj.(*v1.Pod)
if !ok {
utilruntime.HandleError(fmt.Errorf("cannot convert to *v1.Pod: %v", obj))
return
}
c.lock.Lock()
defer c.lock.Unlock()
if err := c.savePod(pod); err != nil {
logrus.Errorf("save or update pod error :%s", err.Error())
}
for _, w := range c.cacheWatchs {
if w.satisfied(pod) {
w.send(watch.Event{Type: watch.Added, Object: pod})
}
}
}
}
// savePod save pod info to db
// From pod basic information.
func (c *CacheManager) savePod(pod *v1.Pod) error {
creater, err := c.getPodCreator(pod)
if err != nil {
logrus.Error("add cache pod error:", err.Error())
return err
}
var serviceID string
loop:
for _, c := range pod.Spec.Containers {
for _, env := range c.Env {
if env.Name == "SERVICE_ID" {
serviceID = env.Value
break loop
}
}
}
if serviceID == "" {
logrus.Warningf("Pod (%s) can not found SERVICE_ID", pod.Name)
return nil
}
dbPod := &model.K8sPod{
ServiceID: serviceID,
ReplicationID: creater.Reference.Name,
ReplicationType: strings.ToLower(creater.Reference.Kind),
PodName: pod.Name,
PodIP: pod.Status.PodIP,
}
dbPod.CreatedAt = time.Now()
if pod.Status.StartTime != nil && !pod.Status.StartTime.IsZero() {
dbPod.CreatedAt = pod.Status.StartTime.Time
}
if err := db.GetManager().K8sPodDao().AddModel(dbPod); err != nil {
if !strings.HasSuffix(err.Error(), "is exist") {
logrus.Error("save service pod relation error.", err.Error())
return err
}
}
//local scheduler pod host ip save
if v, ok := pod.Labels["local-scheduler"]; ok && v == "true" {
if pod.Status.HostIP != "" {
if err := db.GetManager().LocalSchedulerDao().AddModel(&model.LocalScheduler{
ServiceID: serviceID,
PodName: pod.Name,
NodeIP: pod.Status.HostIP,
}); err != nil {
if !strings.HasSuffix(err.Error(), "is exist") {
logrus.Error("save local scheduler info error.", err.Error())
return err
}
}
}
}
return nil
}
func (c *CacheManager) getPodCreator(pod *v1.Pod) (*api.SerializedReference, error) {
creatorRef, found := pod.ObjectMeta.Annotations[api.CreatedByAnnotation]
if !found {
return nil, fmt.Errorf("not found pod creator name")
}
sr := &api.SerializedReference{}
err := ffjson.Unmarshal([]byte(creatorRef), sr)
if err != nil {
return nil, err
}
return sr, nil
}
func (c *CacheManager) updateCachePod() func(oldObj, newObj interface{}) {
return func(_, obj interface{}) {
pod, ok := obj.(*v1.Pod)
if !ok {
utilruntime.HandleError(fmt.Errorf("cannot convert to *v1.Pod: %v", obj))
return
}
//Analyze the cause of pod update.
c.analyzePodStatus(pod)
c.lock.Lock()
defer c.lock.Unlock()
if err := c.savePod(pod); err != nil {
logrus.Errorf("save or update pod error :%s", err.Error())
}
for _, w := range c.cacheWatchs {
if w.satisfied(pod) {
w.send(watch.Event{Type: watch.Modified, Object: pod})
}
}
}
}
func getServiceInfoFromPod(pod *v1.Pod) AbnormalInfo {
var ai AbnormalInfo
if len(pod.Spec.Containers) > 0 {
var i = 0
container := pod.Spec.Containers[0]
for _, env := range container.Env {
if env.Name == "SERVICE_ID" {
ai.ServiceID = env.Value
i++
}
if env.Name == "SERVICE_NAME" {
ai.ServiceAlias = env.Value
i++
}
if i == 2 {
break
}
}
}
ai.PodName = pod.Name
return ai
}
func (c *CacheManager) analyzePodStatus(pod *v1.Pod) {
for _, containerStatus := range pod.Status.ContainerStatuses {
if containerStatus.LastTerminationState.Terminated != nil {
ai := getServiceInfoFromPod(pod)
ai.ContainerName = containerStatus.Name
ai.Reason = containerStatus.LastTerminationState.Terminated.Reason
ai.Message = containerStatus.LastTerminationState.Terminated.Message
ai.CreateTime = time.Now()
c.addAbnormalInfo(&ai)
}
}
}
func (c *CacheManager) addAbnormalInfo(ai *AbnormalInfo) {
c.lock.Lock()
defer c.lock.Unlock()
switch ai.Reason {
case "OOMKilled":
if oldai, ok := c.oomInfos[ai.Hash()]; ok {
oldai.Count++
} else {
ai.Count++
c.oomInfos[ai.Hash()] = ai
}
db.GetManager().NotificationEventDao().AddModel(&model.NotificationEvent{
Kind: "service",
KindID: c.oomInfos[ai.Hash()].ServiceID,
Hash: ai.Hash(),
Type: "UnNormal",
Message: c.oomInfos[ai.Hash()].Message,
Reason: "OOMKilled",
Count: c.oomInfos[ai.Hash()].Count,
})
default:
if oldai, ok := c.errorInfos[ai.Hash()]; ok && oldai != nil {
oldai.Count++
} else {
ai.Count++
c.errorInfos[ai.Hash()] = ai
}
db.GetManager().NotificationEventDao().AddModel(&model.NotificationEvent{
Kind: "service",
KindID: c.errorInfos[ai.Hash()].ServiceID,
Hash: ai.Hash(),
Type: "UnNormal",
Message: c.errorInfos[ai.Hash()].Message,
Reason: c.errorInfos[ai.Hash()].Reason,
Count: c.errorInfos[ai.Hash()].Count,
})
}
}
func (c *CacheManager) deleteCachePod() func(obj interface{}) {
return func(obj interface{}) {
pod, ok := obj.(*v1.Pod)
if !ok {
utilruntime.HandleError(fmt.Errorf("cannot convert to *v1.Pod: %v", obj))
return
}
if err := db.GetManager().K8sPodDao().DeleteK8sPodByName(pod.Name); err != nil {
logrus.Error("delete service pod relation error.", err.Error())
}
c.lock.Lock()
defer c.lock.Unlock()
if _, ok := c.caches[pod.Name]; ok {
delete(c.caches, pod.Name)
}
for _, w := range c.cacheWatchs {
if w.satisfied(pod) {
w.send(watch.Event{Type: watch.Deleted, Object: pod})
}
}
}
}
//Watch pod cache watch
func (c *CacheManager) Watch(labelSelector string) watch.Interface {
lbs := strings.Split(labelSelector, ",")
sort.Strings(lbs)
labelSelector = strings.Join(lbs, ",")
w := &cacheWatch{
id: util.NewUUID(),
ch: make(chan watch.Event, 3),
labelSelector: labelSelector,
m: c,
}
c.cacheWatchs = append(c.cacheWatchs, w)
go c.sendCache(w)
return w
}
//RemoveWatch remove watch
func (c *CacheManager) RemoveWatch(w *cacheWatch) {
if c.cacheWatchs == nil {
return
}
for i := range c.cacheWatchs {
if c.cacheWatchs[i].id == w.id {
c.cacheWatchs = append(c.cacheWatchs[:i], c.cacheWatchs[i+1:]...)
return
}
}
}
func (c *CacheManager) sendCache(w *cacheWatch) {
c.lock.Lock()
defer c.lock.Unlock()
for _, pod := range c.caches {
if w.satisfied(pod) {
w.send(watch.Event{Type: watch.Added, Object: pod})
}
}
}
type cacheWatch struct {
id string
ch chan watch.Event
labelSelector string
m *CacheManager
}
func (c *cacheWatch) satisfied(pod *v1.Pod) bool {
var lbs []string
for k, v := range pod.Labels {
lbs = append(lbs, fmt.Sprintf("%s=%s", k, v))
}
sort.Strings(lbs)
lbStr := strings.Join(lbs, ",")
return strings.Contains(lbStr, c.labelSelector)
}
func (c *cacheWatch) Stop() {
close(c.ch)
c.m.RemoveWatch(c)
}
func (c *cacheWatch) send(event watch.Event) {
select {
case c.ch <- event:
default:
}
}
func (c *cacheWatch) ResultChan() <-chan watch.Event {
return c.ch
}
// NewListWatchPodFromClient creates a new ListWatch from the specified client, resource, namespace and field selector.
func NewListWatchPodFromClient(c cache.Getter) *cache.ListWatch {
listFunc := func(options metav1.ListOptions) (runtime.Object, error) {
return c.Get().
Namespace(v1.NamespaceAll).
Resource("pods").
VersionedParams(&options, metav1.ParameterCodec).
Do().
Get()
}
watchFunc := func(options metav1.ListOptions) (watch.Interface, error) {
options.Watch = true
return c.Get().
Namespace(v1.NamespaceAll).
Resource("pods").
VersionedParams(&options, metav1.ParameterCodec).
Watch()
}
return &cache.ListWatch{ListFunc: listFunc, WatchFunc: watchFunc}
}