forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoints.go
299 lines (276 loc) · 9.27 KB
/
endpoints.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
package endpoints
import (
"context"
"sort"
"encoding/json"
"fmt"
"reflect"
workloadUtil "github.com/rancher/rancher/pkg/controllers/user/workload"
managementv3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/apis/project.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
const (
allEndpoints = "_all_endpoints_"
endpointsAnnotation = "field.cattle.io/publicEndpoints"
)
func Register(ctx context.Context, workload *config.UserContext) {
isRKE := false
cluster, err := workload.Management.Management.Clusters("").Get(workload.ClusterName, metav1.GetOptions{})
if err != nil {
logrus.WithError(err).Warnf("Can not get cluster %s when registering endpoint controller", workload.ClusterName)
}
if cluster != nil {
//assume that cluster always has a spec
isRKE = cluster.Spec.RancherKubernetesEngineConfig != nil
}
n := &NodesController{
nodes: workload.Core.Nodes(""),
serviceLister: workload.Core.Services("").Controller().Lister(),
nodeLister: workload.Core.Nodes("").Controller().Lister(),
podLister: workload.Core.Pods("").Controller().Lister(),
machinesLister: workload.Management.Management.Nodes(workload.ClusterName).Controller().Lister(),
clusterName: workload.ClusterName,
}
workload.Core.Nodes("").AddHandler("nodesEndpointsController", n.sync)
s := &ServicesController{
services: workload.Core.Services(""),
serviceLister: workload.Core.Services("").Controller().Lister(),
nodeLister: workload.Core.Nodes("").Controller().Lister(),
nodeController: workload.Core.Nodes("").Controller(),
podLister: workload.Core.Pods("").Controller().Lister(),
podController: workload.Core.Pods("").Controller(),
workloadController: workloadUtil.NewWorkloadController(workload.UserOnlyContext(), nil),
machinesLister: workload.Management.Management.Nodes(workload.ClusterName).Controller().Lister(),
}
workload.Core.Services("").AddHandler("servicesEndpointsController", s.sync)
p := &PodsController{
nodeLister: workload.Core.Nodes("").Controller().Lister(),
nodeController: workload.Core.Nodes("").Controller(),
pods: workload.Core.Pods(""),
serviceLister: workload.Core.Services("").Controller().Lister(),
podLister: workload.Core.Pods("").Controller().Lister(),
workloadController: workloadUtil.NewWorkloadController(workload.UserOnlyContext(), nil),
machinesLister: workload.Management.Management.Nodes(workload.ClusterName).Controller().Lister(),
clusterName: workload.ClusterName,
}
workload.Core.Pods("").AddHandler("hostPortEndpointsController", p.sync)
w := &WorkloadEndpointsController{
ingressLister: workload.Extensions.Ingresses("").Controller().Lister(),
serviceLister: workload.Core.Services("").Controller().Lister(),
podLister: workload.Core.Pods("").Controller().Lister(),
machinesLister: workload.Management.Management.Nodes(workload.ClusterName).Controller().Lister(),
clusterName: workload.ClusterName,
isRKE: isRKE,
}
w.WorkloadController = workloadUtil.NewWorkloadController(workload.UserOnlyContext(), w.UpdateEndpoints)
i := &IngressEndpointsController{
workloadController: workloadUtil.NewWorkloadController(workload.UserOnlyContext(), nil),
ingressInterface: workload.Extensions.Ingresses(""),
isRKE: isRKE,
}
workload.Extensions.Ingresses("").AddHandler("ingressEndpointsController", i.sync)
}
func areEqualEndpoints(one []v3.PublicEndpoint, two []v3.PublicEndpoint) bool {
oneMap := make(map[string]bool)
twoMap := make(map[string]bool)
for _, value := range one {
oneMap[publicEndpointToString(value)] = true
}
for _, value := range two {
twoMap[publicEndpointToString(value)] = true
}
return reflect.DeepEqual(oneMap, twoMap)
}
func publicEndpointsToString(eps []v3.PublicEndpoint) (string, error) {
b, err := json.Marshal(eps)
if err != nil {
return "", err
}
return string(b), nil
}
func getPublicEndpointsFromAnnotations(annotations map[string]string) []v3.PublicEndpoint {
var eps []v3.PublicEndpoint
if annotations == nil {
return eps
}
if val, ok := annotations[endpointsAnnotation]; ok {
err := json.Unmarshal([]byte(val), &eps)
if err != nil {
logrus.Errorf("Failed to read public endpoints from annotation %v", err)
return eps
}
}
return eps
}
func getEndpointNodeIP(node *managementv3.Node) string {
externalIP := ""
internalIP := ""
for _, ip := range node.Status.InternalNodeStatus.Addresses {
if ip.Type == "ExternalIP" && ip.Address != "" {
externalIP = ip.Address
break
} else if ip.Type == "InternalIP" && ip.Address != "" {
internalIP = ip.Address
}
}
if externalIP != "" {
return externalIP
}
return internalIP
}
func convertServiceToPublicEndpoints(svc *corev1.Service, clusterName string, node *managementv3.Node) ([]v3.PublicEndpoint, error) {
var eps []v3.PublicEndpoint
if svc.DeletionTimestamp != nil {
return eps, nil
}
if !(svc.Spec.Type == "NodePort" || svc.Spec.Type == "LoadBalancer") {
return eps, nil
}
address := ""
nodeName := ""
if node != nil {
address = getEndpointNodeIP(node)
nodeName = fmt.Sprintf("%s:%s", clusterName, node.Name)
}
svcName := fmt.Sprintf("%s:%s", svc.Namespace, svc.Name)
if svc.Spec.Type == "NodePort" {
for _, port := range svc.Spec.Ports {
if port.NodePort == 0 {
continue
}
p := v3.PublicEndpoint{
NodeName: nodeName,
Port: port.NodePort,
Addresses: []string{},
Protocol: string(port.Protocol),
ServiceName: svcName,
AllNodes: true,
}
//for getting endpoints of specific node
if address != "" {
p.Addresses = append(p.Addresses, address)
}
eps = append(eps, p)
}
} else {
var addresses []string
for _, ingressEp := range svc.Status.LoadBalancer.Ingress {
address := ingressEp.Hostname
if address == "" {
address = ingressEp.IP
}
addresses = append(addresses, address)
}
for _, port := range svc.Spec.Ports {
p := v3.PublicEndpoint{
NodeName: "",
Addresses: addresses,
Port: port.Port,
Protocol: string(port.Protocol),
ServiceName: svcName,
AllNodes: false,
}
eps = append(eps, p)
}
}
return eps, nil
}
func convertHostPortToEndpoint(pod *corev1.Pod, clusterName string, node *managementv3.Node) ([]v3.PublicEndpoint, error) {
var eps []v3.PublicEndpoint
if pod.DeletionTimestamp != nil {
return eps, nil
}
if pod.Status.Phase != corev1.PodRunning {
return eps, nil
}
if node == nil {
return eps, nil
}
for _, c := range pod.Spec.Containers {
for _, p := range c.Ports {
if p.HostPort == 0 {
continue
}
address := getEndpointNodeIP(node)
p := v3.PublicEndpoint{
NodeName: fmt.Sprintf("%s:%s", clusterName, node.Name),
Addresses: []string{address},
Port: p.HostPort,
Protocol: string(p.Protocol),
PodName: fmt.Sprintf("%s:%s", pod.Namespace, pod.Name),
AllNodes: false,
}
eps = append(eps, p)
}
}
return eps, nil
}
func publicEndpointToString(p v3.PublicEndpoint) string {
sort.Strings(p.Addresses)
return fmt.Sprintf("%s_%v_%v_%s_%s_%s_%s_%s_%s", p.NodeName, p.Addresses, p.Port, p.Protocol, p.ServiceName, p.PodName, p.IngressName, p.Hostname, p.Path)
}
func getNodeNameToMachine(clusterName string, machineLister managementv3.NodeLister) (map[string]*managementv3.Node, error) {
machines, err := machineLister.List(clusterName, labels.NewSelector())
if err != nil {
return nil, err
}
machineMap := make(map[string]*managementv3.Node)
for _, machine := range machines {
if machine.Status.NodeName == "" {
if machine.Status.NodeConfig != nil {
if machine.Status.NodeConfig.HostnameOverride != "" {
machineMap[machine.Status.NodeConfig.HostnameOverride] = machine
}
}
} else {
machineMap[machine.Status.NodeName] = machine
}
}
return machineMap, nil
}
func convertIngressToServicePublicEndpointsMap(obj *extensionsv1beta1.Ingress, allNodes bool) (map[string][]v3.PublicEndpoint, error) {
var addresses []string
if !allNodes {
for _, address := range obj.Status.LoadBalancer.Ingress {
addresses = append(addresses, address.IP)
}
}
epsMap := make(map[string][]v3.PublicEndpoint)
ports := map[int32]string{80: "HTTP", 443: "HTTPS"}
for _, rule := range obj.Spec.Rules {
for _, path := range rule.HTTP.Paths {
for port, proto := range ports {
p := v3.PublicEndpoint{
Hostname: rule.Host,
Path: path.Path,
ServiceName: fmt.Sprintf("%s:%s", obj.Namespace, path.Backend.ServiceName),
Addresses: addresses,
Port: port,
Protocol: proto,
AllNodes: allNodes,
IngressName: fmt.Sprintf("%s:%s", obj.Namespace, obj.Name),
}
v := epsMap[path.Backend.ServiceName]
epsMap[path.Backend.ServiceName] = append(v, p)
}
}
}
return epsMap, nil
}
func convertIngressToPublicEndpoints(obj *extensionsv1beta1.Ingress, isRKE bool) ([]v3.PublicEndpoint, error) {
epsMap, err := convertIngressToServicePublicEndpointsMap(obj, isRKE)
if err != nil {
return nil, err
}
var eps []v3.PublicEndpoint
for _, v := range epsMap {
eps = append(eps, v...)
}
return eps, nil
}