-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathendpoint_resolver.go
337 lines (302 loc) · 12.4 KB
/
endpoint_resolver.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package backend
import (
"context"
"fmt"
awssdk "github.com/aws/aws-sdk-go-v2/aws"
"github.com/go-logr/logr"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
discovery "k8s.io/api/discovery/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/aws-load-balancer-controller/pkg/k8s"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var ErrNotFound = errors.New("backend not found")
// TODO: for pod endpoints, we currently rely on endpoints events, we might change to use pod events directly in the future.
// under current implementation with pod readinessGate enabled, an unready endpoint but not match our inclusionCriteria won't be registered,
// and it won't turn ready due to blocked by readinessGate, and no future endpoint events will trigger.
// We solve this by requeue the TGB if unready endpoints have the potential to be ready if reconcile in later time.
// EndpointResolver resolves the endpoints for specific service & service Port.
type EndpointResolver interface {
// ResolvePodEndpoints will resolve endpoints backed by pods directly.
// returns resolved podEndpoints and whether there are unready endpoints that can potentially turn ready in future reconciles.
ResolvePodEndpoints(ctx context.Context, svcKey types.NamespacedName, port intstr.IntOrString,
opts ...EndpointResolveOption) ([]PodEndpoint, bool, error)
// ResolveNodePortEndpoints will resolve endpoints backed by nodePort.
ResolveNodePortEndpoints(ctx context.Context, svcKey types.NamespacedName, port intstr.IntOrString,
opts ...EndpointResolveOption) ([]NodePortEndpoint, error)
}
// NewDefaultEndpointResolver constructs new defaultEndpointResolver
func NewDefaultEndpointResolver(k8sClient client.Client, podInfoRepo k8s.PodInfoRepo, failOpenEnabled bool, endpointSliceEnabled bool, logger logr.Logger) *defaultEndpointResolver {
return &defaultEndpointResolver{
k8sClient: k8sClient,
podInfoRepo: podInfoRepo,
failOpenEnabled: failOpenEnabled,
endpointSliceEnabled: endpointSliceEnabled,
logger: logger,
}
}
var _ EndpointResolver = &defaultEndpointResolver{}
// default implementation for EndpointResolver
type defaultEndpointResolver struct {
k8sClient client.Client
podInfoRepo k8s.PodInfoRepo
// [NodePort Endpoint] if fail-open enabled, then nodes that have `Unknown` ready condition will be included if there is no other node with `True` ready condition.
// [Pod Endpoint] if fail-open enabled, then containerRead pods on nodes that have `Unknown` ready condition will be included if there is no other pods that are ready.
failOpenEnabled bool
// [Pod Endpoint] whether to use endpointSlice instead of endpoints
endpointSliceEnabled bool
logger logr.Logger
}
func (r *defaultEndpointResolver) ResolvePodEndpoints(ctx context.Context, svcKey types.NamespacedName, port intstr.IntOrString, opts ...EndpointResolveOption) ([]PodEndpoint, bool, error) {
resolveOpts := defaultEndpointResolveOptions()
resolveOpts.ApplyOptions(opts)
_, svcPort, err := r.findServiceAndServicePort(ctx, svcKey, port)
if err != nil {
return nil, false, err
}
endpointsDataList, err := r.computeServiceEndpointsData(ctx, svcKey)
if err != nil {
return nil, false, err
}
return r.resolvePodEndpointsWithEndpointsData(ctx, svcKey, svcPort, endpointsDataList, resolveOpts.PodReadinessGates)
}
func (r *defaultEndpointResolver) ResolveNodePortEndpoints(ctx context.Context, svcKey types.NamespacedName, port intstr.IntOrString, opts ...EndpointResolveOption) ([]NodePortEndpoint, error) {
resolveOpts := defaultEndpointResolveOptions()
resolveOpts.ApplyOptions(opts)
svc, svcPort, err := r.findServiceAndServicePort(ctx, svcKey, port)
if err != nil {
return nil, err
}
if svc.Spec.Type != corev1.ServiceTypeNodePort && svc.Spec.Type != corev1.ServiceTypeLoadBalancer {
return nil, errors.Errorf("service type must be either 'NodePort' or 'LoadBalancer': %v", svcKey)
}
svcNodePort := svcPort.NodePort
nodeList := &corev1.NodeList{}
if err := r.k8sClient.List(ctx, nodeList, client.MatchingLabelsSelector{Selector: resolveOpts.NodeSelector}); err != nil {
return nil, err
}
var candidateNodes []*corev1.Node
for i := range nodeList.Items {
node := &nodeList.Items[i]
if IsNodeSuitableAsTrafficProxy(node) {
candidateNodes = append(candidateNodes, node)
}
}
targetNodes := filterNodesByReadyConditionStatus(candidateNodes, corev1.ConditionTrue)
if r.failOpenEnabled && len(targetNodes) == 0 {
targetNodes = filterNodesByReadyConditionStatus(candidateNodes, corev1.ConditionUnknown)
}
var endpoints []NodePortEndpoint
for _, node := range targetNodes {
instanceID, err := k8s.ExtractNodeInstanceID(node)
if err != nil {
return nil, err
}
endpoints = append(endpoints, buildNodePortEndpoint(node, instanceID, svcNodePort))
}
return endpoints, nil
}
func (r *defaultEndpointResolver) computeServiceEndpointsData(ctx context.Context, svcKey types.NamespacedName) ([]EndpointsData, error) {
var endpointsDataList []EndpointsData
if r.endpointSliceEnabled {
epSliceList := &discovery.EndpointSliceList{}
if err := r.k8sClient.List(ctx, epSliceList,
client.InNamespace(svcKey.Namespace),
client.MatchingLabels{discovery.LabelServiceName: svcKey.Name}); err != nil {
return nil, err
}
endpointsDataList = buildEndpointsDataFromEndpointSliceList(epSliceList)
} else {
eps := &corev1.Endpoints{}
if err := r.k8sClient.Get(ctx, svcKey, eps); err != nil {
if apierrors.IsNotFound(err) {
return nil, fmt.Errorf("%w: %v", ErrNotFound, err.Error())
}
return nil, err
}
endpointsDataList = buildEndpointsDataFromEndpoints(eps)
}
return endpointsDataList, nil
}
func (r *defaultEndpointResolver) resolvePodEndpointsWithEndpointsData(ctx context.Context, svcKey types.NamespacedName, svcPort corev1.ServicePort, endpointsDataList []EndpointsData, podReadinessGates []corev1.PodConditionType) ([]PodEndpoint, bool, error) {
var readyPodEndpoints []PodEndpoint
var unknownPodEndpoints []PodEndpoint
containsPotentialReadyEndpoints := false
for _, epsData := range endpointsDataList {
for _, port := range epsData.Ports {
if len(svcPort.Name) != 0 && svcPort.Name != awssdk.ToString(port.Name) {
continue
}
epPort := awssdk.ToInt32(port.Port)
for _, ep := range epsData.Endpoints {
if ep.TargetRef == nil || ep.TargetRef.Kind != "Pod" {
continue
}
if len(ep.Addresses) == 0 {
continue // this should never happen per specification.
}
epAddr := ep.Addresses[0]
podNamespace := svcKey.Namespace
if ep.TargetRef.Namespace != "" {
podNamespace = ep.TargetRef.Namespace
}
podKey := types.NamespacedName{Namespace: podNamespace, Name: ep.TargetRef.Name}
pod, exists, err := r.podInfoRepo.Get(ctx, podKey)
if err != nil {
return nil, false, err
}
if !exists {
r.logger.Info("the pod in endpoint is not found in pod cache yet, will keep retrying", "podKey", podKey.String())
containsPotentialReadyEndpoints = true
continue
}
podEndpoint := buildPodEndpoint(pod, epAddr, epPort)
// Recommendation from Kubernetes is to consider unknown ready status as ready (ready == nil)
if ep.Conditions.Ready == nil || *ep.Conditions.Ready {
readyPodEndpoints = append(readyPodEndpoints, podEndpoint)
continue
}
if !pod.IsContainersReady() {
if pod.HasAnyOfReadinessGates(podReadinessGates) {
containsPotentialReadyEndpoints = true
}
continue
}
node := &corev1.Node{}
if err := r.k8sClient.Get(ctx, types.NamespacedName{Name: pod.NodeName}, node); err != nil {
r.logger.Error(err, "ignore pod Endpoint without non-exist nodeInfo", "podKey", podKey.String())
continue
}
nodeReadyCondStatus := corev1.ConditionFalse
if readyCond := k8s.GetNodeCondition(node, corev1.NodeReady); readyCond != nil {
nodeReadyCondStatus = readyCond.Status
}
switch nodeReadyCondStatus {
case corev1.ConditionTrue:
// start from 1.22+, terminating pods are included in endpointSlices,
// and we don't want to include these pods if the node is known to be healthy.
if ep.Conditions.Terminating == nil || !*ep.Conditions.Terminating {
readyPodEndpoints = append(readyPodEndpoints, podEndpoint)
}
case corev1.ConditionUnknown:
unknownPodEndpoints = append(unknownPodEndpoints, podEndpoint)
}
}
}
}
podEndpoints := readyPodEndpoints
if r.failOpenEnabled && len(podEndpoints) == 0 {
podEndpoints = unknownPodEndpoints
}
return podEndpoints, containsPotentialReadyEndpoints, nil
}
func (r *defaultEndpointResolver) findServiceAndServicePort(ctx context.Context, svcKey types.NamespacedName, port intstr.IntOrString) (*corev1.Service, corev1.ServicePort, error) {
svc := &corev1.Service{}
if err := r.k8sClient.Get(ctx, svcKey, svc); err != nil {
if apierrors.IsNotFound(err) {
return nil, corev1.ServicePort{}, fmt.Errorf("%w: %v", ErrNotFound, err.Error())
}
return nil, corev1.ServicePort{}, err
}
svcPort, err := k8s.LookupServicePort(svc, port)
if err != nil {
return nil, corev1.ServicePort{}, fmt.Errorf("%w: %v", ErrNotFound, err.Error())
}
return svc, svcPort, nil
}
// filterNodesByReadyConditionStatus will filter out nodes that matches specified ready condition status
func filterNodesByReadyConditionStatus(nodes []*corev1.Node, readyCondStatus corev1.ConditionStatus) []*corev1.Node {
var nodesWithMatchingReadyStatus []*corev1.Node
for _, node := range nodes {
if readyCond := k8s.GetNodeCondition(node, corev1.NodeReady); readyCond != nil && readyCond.Status == readyCondStatus {
nodesWithMatchingReadyStatus = append(nodesWithMatchingReadyStatus, node)
}
}
return nodesWithMatchingReadyStatus
}
func buildEndpointsDataFromEndpoints(eps *corev1.Endpoints) []EndpointsData {
var endpointsDataList []EndpointsData
for _, epSubset := range eps.Subsets {
var endpointPorts []discovery.EndpointPort
for _, port := range epSubset.Ports {
endpointPort := convertCoreEndpointPortToDiscoveryEndpointPort(port)
endpointPorts = append(endpointPorts, endpointPort)
}
var endpoints []discovery.Endpoint
for _, set := range []struct {
ready bool
addresses []corev1.EndpointAddress
}{
{true, epSubset.Addresses},
{false, epSubset.NotReadyAddresses},
} {
for _, addr := range set.addresses {
endpoint := convertCoreEndpointAddressToDiscoveryEndpoint(addr, set.ready)
endpoints = append(endpoints, endpoint)
}
}
endpointsDataList = append(endpointsDataList, EndpointsData{
Ports: endpointPorts,
Endpoints: endpoints,
})
}
return endpointsDataList
}
func buildEndpointsDataFromEndpointSliceList(epsList *discovery.EndpointSliceList) []EndpointsData {
var endpointsDataList []EndpointsData
for _, epSlice := range epsList.Items {
endpointsDataList = append(endpointsDataList, EndpointsData{
Ports: epSlice.Ports,
Endpoints: epSlice.Endpoints,
})
}
return endpointsDataList
}
func buildPodEndpoint(pod k8s.PodInfo, epAddr string, port int32) PodEndpoint {
return PodEndpoint{
IP: epAddr,
Port: port,
Pod: pod,
}
}
func buildNodePortEndpoint(node *corev1.Node, instanceID string, nodePort int32) NodePortEndpoint {
return NodePortEndpoint{
InstanceID: instanceID,
Port: nodePort,
Node: node,
}
}
// convertCoreEndpointPortToDiscoveryEndpointPort converts a EndpointPort in core APIGroup into EndpointPort in discovery APIGroup
func convertCoreEndpointPortToDiscoveryEndpointPort(port corev1.EndpointPort) discovery.EndpointPort {
epPort := discovery.EndpointPort{
Port: awssdk.Int32(port.Port),
AppProtocol: port.AppProtocol,
}
if len(port.Protocol) != 0 {
epPort.Protocol = &port.Protocol
}
if len(port.Name) != 0 {
epPort.Name = awssdk.String(port.Name)
}
return epPort
}
// convertCoreEndpointAddressToDiscoveryEndpoint converts a EndpointAddress in core APIGroup into an Endpoint in discovery APIGroup along with its ready status.
func convertCoreEndpointAddressToDiscoveryEndpoint(endpoint corev1.EndpointAddress, ready bool) discovery.Endpoint {
ep := discovery.Endpoint{
Addresses: []string{endpoint.IP},
Conditions: discovery.EndpointConditions{
Ready: awssdk.Bool(ready),
Serving: awssdk.Bool(ready),
Terminating: awssdk.Bool(false),
},
TargetRef: endpoint.TargetRef,
NodeName: endpoint.NodeName,
}
if len(endpoint.Hostname) != 0 {
ep.Hostname = awssdk.String(endpoint.Hostname)
}
return ep
}