/
endpoint_slice.go
327 lines (256 loc) · 10.1 KB
/
endpoint_slice.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
/*
SPDX-License-Identifier: Apache-2.0
Copyright Contributors to the Submariner project.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package resolver
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/submariner-io/admiral/pkg/resource"
"github.com/submariner-io/lighthouse/coredns/constants"
discovery "k8s.io/api/discovery/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/sets"
mcsv1a1 "sigs.k8s.io/mcs-api/pkg/apis/v1alpha1"
)
const maxRecordsToLog = 5
func (i *Interface) PutEndpointSlices(endpointSlices ...*discovery.EndpointSlice) bool {
if len(endpointSlices) == 0 {
return false
}
key, clusterID, ok := getKeyInfoFrom(endpointSlices[0])
if !ok {
return false
}
logger.Infof("Put EndpointSlices for %q on cluster %q", key, clusterID)
localClusterID := i.clusterStatus.GetLocalClusterID()
var (
localEndpointSliceErr error
localEndpointSlices []*discovery.EndpointSlice
)
if localClusterID != "" && clusterID == localClusterID && shouldRetrieveLocalEndpointSlicesFor(endpointSlices[0]) {
// The EndpointSlice is from the local cluster. With globalnet enabled, the local global endpoint IPs aren't
// routable in the local cluster so we retrieve the K8s EndpointSlice and use those endpoints. Note that this
// only applies to headless services.
localEndpointSlices, localEndpointSliceErr = i.getLocalEndpointSlices(endpointSlices[0])
}
i.mutex.Lock()
defer i.mutex.Unlock()
serviceInfo, found := i.serviceMap[key]
if !found {
// This means we haven't observed a ServiceImport yet for the service. Return true for the controller to re-queue it.
logger.Infof("Service not found for EndpointSlice %q - requeuing", key)
return true
}
if !serviceInfo.isHeadless {
return i.putClusterIPEndpointSlice(key, clusterID, endpointSlices[0], serviceInfo)
}
if localEndpointSliceErr != nil {
logger.Error(localEndpointSliceErr, "unable to retrieve local EndpointSlice - requeuing")
return true
}
if localEndpointSlices != nil {
endpointSlices = localEndpointSlices
}
i.putHeadlessEndpointSlices(key, clusterID, endpointSlices, serviceInfo)
return false
}
func (i *Interface) putClusterIPEndpointSlice(key, clusterID string, endpointSlice *discovery.EndpointSlice, serviceInfo *serviceInfo,
) bool {
_, found := endpointSlice.Labels[constants.LabelIsHeadless]
if !found {
// This is a legacy pre-0.15 EndpointSlice.
clusterInfo, found := serviceInfo.clusters[clusterID]
if !found {
logger.Infof("Cluster %q not found for EndpointSlice %q - requeuing", clusterID, key)
return true
}
// For a ClusterIPService we really only care if there are any backing endpoints.
clusterInfo.endpointsHealthy = len(endpointSlice.Endpoints) > 0
return false
}
if len(endpointSlice.Endpoints) == 0 {
// This shouldn't happen - we expect the service IP endpoint to always be present.
logger.Errorf(nil, "Missing service IP endpoint in EndpointSlice %q", key)
return false
}
clusterInfo := serviceInfo.ensureClusterInfo(clusterID)
clusterInfo.endpointRecords = []DNSRecord{{
IP: endpointSlice.Endpoints[0].Addresses[0],
Ports: mcsServicePortsFrom(endpointSlice.Ports),
ClusterName: clusterID,
}}
clusterInfo.endpointsHealthy = endpointSlice.Endpoints[0].Conditions.Ready == nil || *endpointSlice.Endpoints[0].Conditions.Ready
serviceInfo.mergePorts()
serviceInfo.resetLoadBalancing()
logger.Infof("Added DNSRecord with service IP %q for EndpointSlice %q on cluster %q, endpointsHealthy: %v, ports: %#v",
clusterInfo.endpointRecords[0].IP, key, clusterID, clusterInfo.endpointsHealthy, clusterInfo.endpointRecords[0].Ports)
return false
}
func (i *Interface) putHeadlessEndpointSlices(key, clusterID string, endpointSlices []*discovery.EndpointSlice, serviceInfo *serviceInfo) {
clusterInfo := &clusterInfo{
endpointRecordsByHost: make(map[string][]DNSRecord),
}
serviceInfo.clusters[clusterID] = clusterInfo
allAddresses := sets.New[string]()
for _, endpointSlice := range endpointSlices {
mcsPorts := mcsServicePortsFrom(endpointSlice.Ports)
publishNotReadyAddresses := endpointSlice.Annotations[constants.PublishNotReadyAddresses] == strconv.FormatBool(true)
for i := range endpointSlice.Endpoints {
endpoint := &endpointSlice.Endpoints[i]
// Skip if not ready and the user does not want to publish not-ready addresses. Note: we're treating nil as ready
// to be on the safe side as the EndpointConditions doc states "In most cases consumers should interpret this
// unknown state (ie nil) as ready".
if endpoint.Conditions.Ready != nil && !*endpoint.Conditions.Ready && !publishNotReadyAddresses {
continue
}
var (
records []DNSRecord
hostname string
)
switch {
case endpoint.Hostname != nil && *endpoint.Hostname != "":
hostname = *endpoint.Hostname
case endpoint.TargetRef != nil && strings.EqualFold(endpoint.TargetRef.Kind, "pod"):
hostname = endpoint.TargetRef.Name
}
for _, address := range endpoint.Addresses {
if allAddresses.Has(address) {
continue
}
allAddresses.Insert(address)
record := DNSRecord{
IP: address,
Ports: mcsPorts,
ClusterName: clusterID,
HostName: hostname,
}
records = append(records, record)
}
if hostname != "" {
clusterInfo.endpointRecordsByHost[hostname] = records
}
clusterInfo.endpointRecords = append(clusterInfo.endpointRecords, records...)
}
}
if len(clusterInfo.endpointRecords) <= maxRecordsToLog {
logger.Infof("Added records for headless EndpointSlice %q from cluster %q: %s",
key, clusterID, resource.ToJSON(clusterInfo.endpointRecords))
} else {
logger.Infof("Added records for headless EndpointSlice %q from cluster %q (showing %d/%d): %s",
key, clusterID, maxRecordsToLog, len(clusterInfo.endpointRecords),
resource.ToJSON(clusterInfo.endpointRecords[:maxRecordsToLog]))
}
}
func (i *Interface) getLocalEndpointSlices(forEPS *discovery.EndpointSlice) ([]*discovery.EndpointSlice, error) {
epsGVR := schema.GroupVersionResource{
Group: discovery.SchemeGroupVersion.Group,
Version: discovery.SchemeGroupVersion.Version,
Resource: "endpointslices",
}
list, err := i.client.Resource(epsGVR).Namespace(forEPS.Labels[constants.LabelSourceNamespace]).List(context.TODO(),
metav1.ListOptions{
LabelSelector: labels.Set(map[string]string{
discovery.LabelServiceName: forEPS.Labels[mcsv1a1.LabelServiceName],
}).String(),
})
if err != nil {
return nil, errors.Wrapf(err, "error retrieving the endpointslices in namespace %s", forEPS.Labels[constants.LabelSourceNamespace])
}
if len(list.Items) == 0 {
return nil, fmt.Errorf("local EndpointSlice not found for %s/%s", forEPS.Labels[constants.LabelSourceNamespace],
forEPS.Labels[mcsv1a1.LabelServiceName])
}
epSlices := make([]*discovery.EndpointSlice, len(list.Items))
for i := range list.Items {
epSlice := &discovery.EndpointSlice{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(list.Items[i].Object, epSlice)
utilruntime.Must(err)
epSlice.Labels = forEPS.Labels
epSlice.Annotations = forEPS.Annotations
epSlices[i] = epSlice
}
return epSlices, nil
}
func (i *Interface) RemoveEndpointSlice(endpointSlice *discovery.EndpointSlice) {
key, clusterID, ok := getKeyInfoFrom(endpointSlice)
if !ok {
return
}
logger.Infof("Remove EndpointSlice %q on cluster %q", key, clusterID)
i.mutex.Lock()
defer i.mutex.Unlock()
serviceInfo, found := i.serviceMap[key]
if !found {
return
}
delete(serviceInfo.clusters, clusterID)
if len(serviceInfo.clusters) == 0 && !serviceInfo.isExported {
delete(i.serviceMap, key)
} else if !serviceInfo.isHeadless {
serviceInfo.mergePorts()
serviceInfo.resetLoadBalancing()
}
}
func getKeyInfoFrom(es *discovery.EndpointSlice) (string, string, bool) {
name, ok := es.Labels[mcsv1a1.LabelServiceName]
if !ok {
logger.Warningf("EndpointSlice missing label %q: %#v", mcsv1a1.LabelServiceName, es.ObjectMeta)
return "", "", false
}
namespace, ok := es.Labels[constants.LabelSourceNamespace]
if !ok {
logger.Warningf("EndpointSlice missing label %q: %#v", constants.LabelSourceNamespace, es.ObjectMeta)
return "", "", false
}
clusterID, ok := es.Labels[constants.MCSLabelSourceCluster]
if !ok {
logger.Warningf("EndpointSlice missing label %q: %#v", constants.MCSLabelSourceCluster, es.ObjectMeta)
return "", "", false
}
return keyFunc(namespace, name), clusterID, true
}
func mcsServicePortsFrom(ports []discovery.EndpointPort) []mcsv1a1.ServicePort {
mcsPorts := make([]mcsv1a1.ServicePort, len(ports))
for i, port := range ports {
mcsPorts[i] = mcsv1a1.ServicePort{
Name: *port.Name,
Protocol: *port.Protocol,
AppProtocol: port.AppProtocol,
Port: *port.Port,
}
}
return mcsPorts
}
func isHeadless(endpointSlice *discovery.EndpointSlice) bool {
return endpointSlice.Labels[constants.LabelIsHeadless] == strconv.FormatBool(true)
}
func shouldRetrieveLocalEndpointSlicesFor(endpointSlice *discovery.EndpointSlice) bool {
_, found := endpointSlice.Labels[constants.LabelIsHeadless]
if !found {
// This is a legacy pre-0.15 EndpointSlice. We don't know if it's headless or if globalnet is enabled.
return true
}
globalnetEnabled, found := endpointSlice.Annotations[constants.GlobalnetEnabled]
if !found {
// This is a legacy 0.15 EndpointSlice. We don't know if globalnet is enabled.
return isHeadless(endpointSlice)
}
return isHeadless(endpointSlice) && globalnetEnabled == strconv.FormatBool(true)
}