-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
pod_endpoints.go
161 lines (145 loc) · 4.11 KB
/
pod_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
package endpoints
import (
"strings"
"fmt"
workloadutil "github.com/rancher/rancher/pkg/controllers/user/workload"
"github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
)
// This controller is responsible for monitoring pods
// and setting public endpoints on them based on HostPort pods
// and NodePort/LoadBalancer services backing up the pod
type PodsController struct {
nodeLister v1.NodeLister
pods v1.PodInterface
podLister v1.PodLister
serviceLister v1.ServiceLister
workloadController workloadutil.CommonController
machinesLister v3.NodeLister
clusterName string
}
func (c *PodsController) sync(key string, obj *corev1.Pod) error {
if obj == nil && !strings.HasSuffix(key, allEndpoints) {
return nil
}
var pods []*corev1.Pod
var services []*corev1.Service
var err error
if strings.HasSuffix(key, allEndpoints) {
namespace := ""
if !strings.EqualFold(key, allEndpoints) {
namespace = strings.TrimSuffix(key, fmt.Sprintf("/%s", allEndpoints))
}
pods, err = c.podLister.List(namespace, labels.NewSelector())
if err != nil {
return err
}
services, err = c.serviceLister.List(namespace, labels.NewSelector())
if err != nil {
return err
}
} else {
services, err = c.serviceLister.List(obj.Namespace, labels.NewSelector())
if err != nil {
return err
}
pods = append(pods, obj)
}
nodesToUpdate := map[string]bool{}
workloadsToUpdate := map[string]*workloadutil.Workload{}
nodeNameToMachine, err := getNodeNameToMachine(c.clusterName, c.machinesLister, c.nodeLister)
if err != nil {
return err
}
for _, pod := range pods {
updated, err := c.updatePodEndpoints(pod, services, nodeNameToMachine)
if err != nil {
return err
}
if updated {
if pod.Spec.NodeName != "" && podHasHostPort(pod) {
nodesToUpdate[pod.Spec.NodeName] = true
workloads, err := c.workloadController.GetWorkloadsMatchingLabels(pod.Namespace, pod.Labels)
if err != nil {
return err
}
for _, w := range workloads {
workloadsToUpdate[key] = w
}
}
}
}
// push changes to workload
for _, w := range workloadsToUpdate {
c.workloadController.EnqueueWorkload(w)
}
return nil
}
func podHasHostPort(obj *corev1.Pod) bool {
for _, c := range obj.Spec.Containers {
for _, p := range c.Ports {
if p.HostPort != 0 {
return true
}
}
}
return false
}
func (c *PodsController) updatePodEndpoints(obj *corev1.Pod, services []*corev1.Service, nodeNameToMachine map[string]*v3.Node) (updated bool, err error) {
if obj.Spec.NodeName == "" {
return false, nil
}
if obj.DeletionTimestamp != nil {
return true, nil
}
// 1. update pod with endpoints
// a) from HostPort
newPublicEps, err := convertHostPortToEndpoint(obj, c.clusterName, nodeNameToMachine[obj.Spec.NodeName])
if err != nil {
return false, err
}
allNodesIP, err := getAllNodesPublicEndpointIP(c.machinesLister, c.clusterName)
if err != nil {
return false, err
}
// b) from services
for _, svc := range services {
if svc.Namespace != obj.Namespace {
continue
}
set := labels.Set{}
for key, val := range svc.Spec.Selector {
set[key] = val
}
selector := labels.SelectorFromSet(set)
if selector.Matches(labels.Set(obj.Labels)) {
eps, err := convertServiceToPublicEndpoints(svc, "", nil, allNodesIP)
if err != nil {
return false, err
}
newPublicEps = append(newPublicEps, eps...)
}
}
existingPublicEps := getPublicEndpointsFromAnnotations(obj.Annotations)
if areEqualEndpoints(existingPublicEps, newPublicEps) {
return false, nil
}
toUpdate := obj.DeepCopy()
epsToUpdate, err := publicEndpointsToString(newPublicEps)
if err != nil {
return false, err
}
logrus.Infof("Updating pod [%s/%s] with public endpoints [%v]", obj.Namespace, obj.Name, epsToUpdate)
if toUpdate.Annotations == nil {
toUpdate.Annotations = make(map[string]string)
}
toUpdate.Annotations[endpointsAnnotation] = epsToUpdate
_, err = c.pods.Update(toUpdate)
if err != nil {
return false, err
}
return true, nil
}