-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwatcher_app.go
128 lines (116 loc) · 4.03 KB
/
watcher_app.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
package ek8s
import (
"context"
"fmt"
"github.com/gotomicro/ego/core/elog"
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
)
type WatcherApp struct {
appName string
kind string
*kubernetes.Clientset
queue workqueue.Interface
logger *elog.Component
deploymentPrefix string
}
func newWatcherApp(clientSet *kubernetes.Clientset, appName string, kind string, deploymentPrefix string, logger *elog.Component) *WatcherApp {
return &WatcherApp{
Clientset: clientSet,
appName: appName,
kind: kind,
queue: workqueue.New(),
deploymentPrefix: deploymentPrefix,
logger: logger,
}
}
func (c *WatcherApp) watch(ctx context.Context, ns string) error {
switch c.kind {
case KindPods:
label, err := c.getDeploymentsSelector(ns, c.appName)
if err != nil {
return err
}
c.logger.Debug("watch prefix label", zap.String("appname", c.appName), zap.String("label", label))
informersFactory := informers.NewSharedInformerFactoryWithOptions(
c.Clientset,
defaultRsync,
informers.WithNamespace(ns),
informers.WithTweakListOptions(func(options *metav1.ListOptions) {
options.LabelSelector = label
//// todo
options.ResourceVersion = "0"
}),
)
informer := informersFactory.Core().V1().Pods()
c.logger.Debug("k8s watch pods", zap.String("appname", c.appName), zap.String("kind", c.kind), zap.String("kind", c.kind))
informer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: c.addPod,
UpdateFunc: c.updatePod,
DeleteFunc: c.deletePod,
})
// 启动该命名空间里监听
go informersFactory.Start(ctx.Done())
case KindEndpoints:
endPoints, err := c.CoreV1().Endpoints(ns).Get(context.Background(), c.getDeploymentName(c.appName), metav1.GetOptions{})
if err != nil {
return err
}
c.logger.Info("k8s watch endpoints", zap.String("appname", c.appName), zap.String("namespace", endPoints.Namespace), zap.String("endPointName", endPoints.Name), zap.String("kind", c.kind))
informersFactory := informers.NewSharedInformerFactoryWithOptions(
c.Clientset,
defaultRsync,
informers.WithNamespace(ns),
informers.WithTweakListOptions(func(options *metav1.ListOptions) {
options.FieldSelector = "metadata.name=" + endPoints.Name
// todo
options.ResourceVersion = "0"
}),
)
informer := informersFactory.Core().V1().Endpoints()
informer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: c.addEndpoints,
UpdateFunc: c.updateEndpoints,
DeleteFunc: c.deleteEndpoints,
})
// 启动该命名空间里监听
go informersFactory.Start(ctx.Done())
default:
c.logger.Error("k8s watch prefix error", zap.String("appname", c.appName), zap.String("kind", c.kind))
}
return nil
}
func (c *WatcherApp) ProcessWorkItem(f func(info *KubernetesEvent) error) bool {
item, shutdown := c.queue.Get()
if shutdown {
return false
}
defer c.queue.Done(item)
o := item.(*KubernetesEvent)
f(o)
return true
}
func (c *WatcherApp) getDeploymentsSelector(namespace string, appName string) (label string, err error) {
deployment, err := c.AppsV1().Deployments(namespace).Get(context.Background(), c.getDeploymentName(appName), metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("get deployments in namespace (%s), err: %w", namespace, err)
}
label = labels.SelectorFromSet(deployment.Labels).String()
return
}
func (c *WatcherApp) getServicesSelector(namespace string, appName string) (label string, err error) {
service, err := c.CoreV1().Services(namespace).Get(context.Background(), c.getDeploymentName(appName), metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("get services in namespace (%s), err: %w", namespace, err)
}
label = labels.SelectorFromSet(service.Labels).String()
return
}
func (c *WatcherApp) getDeploymentName(appName string) string {
return c.deploymentPrefix + appName
}