/
component.go
123 lines (106 loc) · 3.06 KB
/
component.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
package ek8s
import (
"context"
"fmt"
"sync"
"time"
"github.com/gotomicro/ego/core/elog"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
)
const PackageName = "component.ek8s"
const defaultRsync = 5 * time.Minute
const (
KindPods = "pods"
KindEndpoints = "endpoints"
)
type (
ListOptions = metav1.ListOptions
)
// Component ...
type Component struct {
name string
config *Config
*kubernetes.Clientset
logger *elog.Component
locker sync.RWMutex
}
type KubernetesEvent struct {
IPs []string
EventType watch.EventType
}
// New ...
func newComponent(name string, config *Config, logger *elog.Component) *Component {
client, err := kubernetes.NewForConfig(config.toRestConfig())
if err != nil {
logger.Panic("new component err", elog.FieldErr(err))
}
return &Component{
name: name,
config: config,
logger: logger,
Clientset: client,
}
}
func (c *Component) Config() Config {
return *c.config
}
func (c *Component) ListPods(option ListOptions) (pods []*v1.PodList, err error) {
pods = make([]*v1.PodList, 0)
for _, ns := range c.config.Namespaces {
v1Pods, err := c.CoreV1().Pods(ns).List(context.Background(), option)
if err != nil {
return nil, fmt.Errorf("list pods in namespace (%s), err: %w", ns, err)
}
pods = append(pods, v1Pods)
}
return
}
func (c *Component) ListEndpoints(option ListOptions) (endPoints []*v1.EndpointsList, err error) {
endPoints = make([]*v1.EndpointsList, 0)
for _, ns := range c.config.Namespaces {
v1EndPoints, err := c.CoreV1().Endpoints(ns).List(context.Background(), option)
if err != nil {
return nil, fmt.Errorf("list endpoints in namespace (%s), err: %w", ns, err)
}
endPoints = append(endPoints, v1EndPoints)
}
return
}
func (c *Component) ListPodsByName(name string) (pods []*v1.Pod, err error) {
pods = make([]*v1.Pod, 0)
for _, ns := range c.config.Namespaces {
v1Pods, err := c.CoreV1().Pods(ns).Get(context.Background(), c.getDeploymentName(name), metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("list pods in namespace (%s), err: %w", ns, err)
}
pods = append(pods, v1Pods)
}
return
}
func (c *Component) ListEndpointsByName(name string) (endPoints []*v1.Endpoints, err error) {
endPoints = make([]*v1.Endpoints, 0)
for _, ns := range c.config.Namespaces {
v1EndPoints, err := c.CoreV1().Endpoints(ns).Get(context.Background(), c.getDeploymentName(name), metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("list endpoints in namespace (%s), err: %w", ns, err)
}
endPoints = append(endPoints, v1EndPoints)
}
return
}
func (c *Component) NewWatcherApp(ctx context.Context, appName string, kind string) (app *WatcherApp, err error) {
app = newWatcherApp(c.Clientset, appName, kind, c.config.DeploymentPrefix, c.logger)
for _, ns := range c.config.Namespaces {
err = app.watch(ctx, ns)
if err != nil {
return app, err
}
}
return app, nil
}
func (c *Component) getDeploymentName(name string) string {
return c.config.DeploymentPrefix + name
}