-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
endpoint.go
190 lines (157 loc) · 4.83 KB
/
endpoint.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package k8sclient // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/k8s/k8sclient"
import (
"context"
"fmt"
"sync"
"go.uber.org/zap"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/k8s/k8sutil"
)
const (
typePod = "Pod"
)
type Service struct {
ServiceName string
Namespace string
}
func NewService(name, namespace string) Service {
return Service{ServiceName: name, Namespace: namespace}
}
type EpClient interface {
// Get the mapping between pod key and the corresponding service names
PodKeyToServiceNames() map[string][]string
// Get the mapping between the service and the number of belonging pods
ServiceToPodNum() map[Service]int
}
type epClientOption func(*epClient)
func epSyncCheckerOption(checker initialSyncChecker) epClientOption {
return func(e *epClient) {
e.syncChecker = checker
}
}
type epClient struct {
stopChan chan struct{}
store *ObjStore
stopped bool
syncChecker initialSyncChecker
mu sync.RWMutex
podKeyToServiceNamesMap map[string][]string
serviceToPodNumMap map[Service]int // only running pods will show behind endpoints
}
func (c *epClient) PodKeyToServiceNames() map[string][]string {
if c.store.GetResetRefreshStatus() {
c.refresh()
}
c.mu.RLock()
defer c.mu.RUnlock()
return c.podKeyToServiceNamesMap
}
func (c *epClient) ServiceToPodNum() map[Service]int {
if c.store.GetResetRefreshStatus() {
c.refresh()
}
c.mu.RLock()
defer c.mu.RUnlock()
return c.serviceToPodNumMap
}
func (c *epClient) refresh() {
c.mu.Lock()
defer c.mu.Unlock()
objsList := c.store.List()
tmpMap := make(map[string]map[string]struct{}) // pod key to service names
serviceToPodNumMapNew := make(map[Service]int)
for _, obj := range objsList {
ep := obj.(*endpointInfo)
serviceName := ep.name
namespace := ep.namespace
// each obj should be a uniq service.
// ignore the service which has 0 pods.
if len(ep.podKeyList) > 0 {
serviceToPodNumMapNew[NewService(serviceName, namespace)] = len(ep.podKeyList)
}
for _, podKey := range ep.podKeyList {
var serviceNamesMap map[string]struct{}
var ok bool
if _, ok = tmpMap[podKey]; !ok {
tmpMap[podKey] = make(map[string]struct{})
}
serviceNamesMap = tmpMap[podKey]
serviceNamesMap[serviceName] = struct{}{}
}
}
podKeyToServiceNamesMapNew := make(map[string][]string)
for podKey, serviceNamesMap := range tmpMap {
serviceNamesList := make([]string, 0, len(serviceNamesMap))
for serviceName := range serviceNamesMap {
serviceNamesList = append(serviceNamesList, serviceName)
}
podKeyToServiceNamesMapNew[podKey] = serviceNamesList
}
c.podKeyToServiceNamesMap = podKeyToServiceNamesMapNew
c.serviceToPodNumMap = serviceToPodNumMapNew
}
func newEpClient(clientSet kubernetes.Interface, logger *zap.Logger, options ...epClientOption) *epClient {
c := &epClient{
stopChan: make(chan struct{}),
}
for _, option := range options {
option(c)
}
c.store = NewObjStore(transformFuncEndpoint, logger)
lw := c.createEndpointListWatch(clientSet, metav1.NamespaceAll)
reflector := cache.NewReflector(lw, &v1.Endpoints{}, c.store, 0)
go reflector.Run(c.stopChan)
if c.syncChecker != nil {
// check the init sync for potential connection issue
c.syncChecker.Check(reflector, "Endpoint initial sync timeout")
}
return c
}
func (c *epClient) shutdown() {
close(c.stopChan)
c.stopped = true
}
func transformFuncEndpoint(obj any) (any, error) {
endpoint, ok := obj.(*v1.Endpoints)
if !ok {
return nil, fmt.Errorf("input obj %v is not Endpoint type", obj)
}
info := new(endpointInfo)
info.name = endpoint.Name
info.namespace = endpoint.Namespace
info.podKeyList = []string{}
if subsets := endpoint.Subsets; subsets != nil {
for _, subset := range subsets {
if addresses := subset.Addresses; addresses != nil {
for _, address := range addresses {
if targetRef := address.TargetRef; targetRef != nil && targetRef.Kind == typePod {
podKey := k8sutil.CreatePodKey(targetRef.Namespace, targetRef.Name)
if podKey == "" {
continue
}
info.podKeyList = append(info.podKeyList, podKey)
}
}
}
}
}
return info, nil
}
func (c *epClient) createEndpointListWatch(client kubernetes.Interface, ns string) cache.ListerWatcher {
ctx := context.Background()
return &cache.ListWatch{
ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) {
return client.CoreV1().Endpoints(ns).List(ctx, opts)
},
WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) {
return client.CoreV1().Endpoints(ns).Watch(ctx, opts)
},
}
}