-
Notifications
You must be signed in to change notification settings - Fork 438
/
endpoint.go
270 lines (241 loc) · 6.64 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
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
package controller
import (
"context"
"fmt"
"strings"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
"github.com/kubeovn/kube-ovn/pkg/util"
)
func (c *Controller) enqueueAddEndpoint(obj interface{}) {
if !c.isLeader() {
return
}
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
utilruntime.HandleError(err)
return
}
klog.V(3).Infof("enqueue add endpoint %s", key)
c.updateEndpointQueue.Add(key)
}
func (c *Controller) enqueueUpdateEndpoint(old, new interface{}) {
if !c.isLeader() {
return
}
oldEp := old.(*v1.Endpoints)
newEp := new.(*v1.Endpoints)
if oldEp.ResourceVersion == newEp.ResourceVersion {
return
}
if len(oldEp.Subsets) == 0 && len(newEp.Subsets) == 0 {
return
}
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(new); err != nil {
utilruntime.HandleError(err)
return
}
klog.V(3).Infof("enqueue update endpoint %s", key)
c.updateEndpointQueue.Add(key)
}
func (c *Controller) runUpdateEndpointWorker() {
for c.processNextUpdateEndpointWorkItem() {
}
}
func (c *Controller) processNextUpdateEndpointWorkItem() bool {
obj, shutdown := c.updateEndpointQueue.Get()
if shutdown {
return false
}
err := func(obj interface{}) error {
defer c.updateEndpointQueue.Done(obj)
var key string
var ok bool
if key, ok = obj.(string); !ok {
c.updateEndpointQueue.Forget(obj)
utilruntime.HandleError(fmt.Errorf("expected string in workqueue but got %#v", obj))
return nil
}
if err := c.handleUpdateEndpoint(key); err != nil {
c.updateEndpointQueue.AddRateLimited(key)
return fmt.Errorf("error syncing '%s': %s, requeuing", key, err.Error())
}
c.updateEndpointQueue.Forget(obj)
return nil
}(obj)
if err != nil {
utilruntime.HandleError(err)
return true
}
return true
}
func (c *Controller) handleUpdateEndpoint(key string) error {
namespace, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
utilruntime.HandleError(fmt.Errorf("invalid resource key: %s", key))
return nil
}
klog.Infof("update endpoint %s/%s", namespace, name)
ep, err := c.endpointsLister.Endpoints(namespace).Get(name)
if err != nil {
if errors.IsNotFound(err) {
return nil
}
return err
}
orisvc, err := c.servicesLister.Services(namespace).Get(name)
if err != nil {
if errors.IsNotFound(err) {
return nil
}
return err
}
svc := orisvc.DeepCopy()
clusterIPs := svc.Spec.ClusterIPs
if len(clusterIPs) == 0 && svc.Spec.ClusterIP != "" && svc.Spec.ClusterIP != v1.ClusterIPNone {
clusterIPs = []string{svc.Spec.ClusterIP}
}
if len(clusterIPs) == 0 || clusterIPs[0] == v1.ClusterIPNone {
return nil
}
pods, err := c.podsLister.Pods(namespace).List(labels.Set(svc.Spec.Selector).AsSelector())
if err != nil {
klog.Errorf("failed to get pods for service %s in namespace %s: %v", name, namespace, err)
return err
}
var vpcName string
for _, pod := range pods {
if len(pod.Annotations) == 0 {
continue
}
for _, subset := range ep.Subsets {
for _, addr := range subset.Addresses {
if addr.IP == pod.Status.PodIP {
if vpcName = pod.Annotations[util.LogicalRouterAnnotation]; vpcName != "" {
break
}
}
}
if vpcName != "" {
break
}
}
if vpcName != "" {
break
}
}
if vpcName == "" {
if vpcName = svc.Annotations[util.VpcAnnotation]; vpcName == "" {
vpcName = util.DefaultVpc
}
}
vpc, err := c.vpcsLister.Get(vpcName)
if err != nil {
klog.Errorf("failed to get vpc %s of lb, %v", vpcName, err)
return err
}
if svcVpc := svc.Annotations[util.VpcAnnotation]; svcVpc != vpcName {
if svc.Annotations == nil {
svc.Annotations = make(map[string]string, 1)
}
svc.Annotations[util.VpcAnnotation] = vpcName
if _, err = c.config.KubeClient.CoreV1().Services(namespace).Update(context.Background(), svc, metav1.UpdateOptions{}); err != nil {
klog.Errorf("failed to update service %s/%s: %v", namespace, svc.Name, err)
return err
}
}
tcpLb, udpLb := vpc.Status.TcpLoadBalancer, vpc.Status.UdpLoadBalancer
if svc.Spec.SessionAffinity == v1.ServiceAffinityClientIP {
tcpLb, udpLb = vpc.Status.TcpSessionLoadBalancer, vpc.Status.UdpSessionLoadBalancer
}
for _, clusterIP := range clusterIPs {
for _, port := range svc.Spec.Ports {
vip := util.JoinHostPort(clusterIP, port.Port)
backends := getServicePortBackends(ep, pods, port, clusterIP)
if port.Protocol == v1.ProtocolTCP {
// for performance reason delete lb with no backends
if len(backends) != 0 {
err = c.ovnClient.CreateLoadBalancerRule(tcpLb, vip, backends, string(port.Protocol))
if err != nil {
klog.Errorf("failed to update vip %s to tcp lb, %v", vip, err)
return err
}
} else {
err = c.ovnClient.DeleteLoadBalancerVip(vip, tcpLb)
if err != nil {
klog.Errorf("failed to delete vip %s at tcp lb, %v", vip, err)
return err
}
}
} else {
if len(backends) != 0 {
err = c.ovnClient.CreateLoadBalancerRule(udpLb, vip, backends, string(port.Protocol))
if err != nil {
klog.Errorf("failed to update vip %s to udp lb, %v", vip, err)
return err
}
} else {
err = c.ovnClient.DeleteLoadBalancerVip(vip, udpLb)
if err != nil {
klog.Errorf("failed to delete vip %s at udp lb, %v", vip, err)
return err
}
}
}
}
}
return nil
}
func getServicePortBackends(endpoints *v1.Endpoints, pods []*v1.Pod, servicePort v1.ServicePort, serviceIP string) string {
backends := []string{}
protocol := util.CheckProtocol(serviceIP)
for _, subset := range endpoints.Subsets {
var targetPort int32
for _, port := range subset.Ports {
if port.Name == servicePort.Name {
targetPort = port.Port
break
}
}
if targetPort == 0 {
continue
}
for _, address := range subset.Addresses {
if address.TargetRef == nil || address.TargetRef.Kind != "Pod" {
backends = append(backends, util.JoinHostPort(address.IP, targetPort))
continue
}
var ip string
for _, pod := range pods {
if pod.Name == address.TargetRef.Name {
podIPs := pod.Status.PodIPs
if len(podIPs) == 0 && pod.Status.PodIP != "" {
podIPs = []v1.PodIP{{IP: pod.Status.PodIP}}
}
for _, podIP := range podIPs {
if util.CheckProtocol(podIP.IP) == protocol {
ip = podIP.IP
break
}
}
break
}
}
if ip == "" {
ip = address.IP
}
if ip != "" {
backends = append(backends, util.JoinHostPort(ip, targetPort))
}
}
}
return strings.Join(backends, ",")
}