/
service.go
459 lines (397 loc) · 13.9 KB
/
service.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// Copyright 2018-2020 Authors of Cilium
//
// 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 k8s
import (
"fmt"
"net"
"net/url"
"strings"
"time"
"github.com/cilium/cilium/pkg/annotation"
"github.com/cilium/cilium/pkg/comparator"
"github.com/cilium/cilium/pkg/datapath"
slim_corev1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/apis/core/v1"
"github.com/cilium/cilium/pkg/loadbalancer"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/option"
serviceStore "github.com/cilium/cilium/pkg/service/store"
"github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
)
func getAnnotationIncludeExternal(svc *slim_corev1.Service) bool {
if value, ok := svc.ObjectMeta.Annotations[annotation.GlobalService]; ok {
return strings.ToLower(value) == "true"
}
return false
}
func getAnnotationShared(svc *slim_corev1.Service) bool {
if value, ok := svc.ObjectMeta.Annotations[annotation.SharedService]; ok {
return strings.ToLower(value) == "true"
}
return getAnnotationIncludeExternal(svc)
}
// ParseServiceID parses a Kubernetes service and returns the ServiceID
func ParseServiceID(svc *slim_corev1.Service) ServiceID {
return ServiceID{
Name: svc.ObjectMeta.Name,
Namespace: svc.ObjectMeta.Namespace,
}
}
// ParseService parses a Kubernetes service and returns a Service
func ParseService(svc *slim_corev1.Service, nodeAddressing datapath.NodeAddressing) (ServiceID, *Service) {
scopedLog := log.WithFields(logrus.Fields{
logfields.K8sSvcName: svc.ObjectMeta.Name,
logfields.K8sNamespace: svc.ObjectMeta.Namespace,
logfields.K8sAPIVersion: svc.TypeMeta.APIVersion,
logfields.K8sSvcType: svc.Spec.Type,
})
var loadBalancerIPs []string
svcID := ParseServiceID(svc)
switch svc.Spec.Type {
case slim_corev1.ServiceTypeClusterIP, slim_corev1.ServiceTypeNodePort, slim_corev1.ServiceTypeLoadBalancer:
break
case slim_corev1.ServiceTypeExternalName:
// External-name services must be ignored
return ServiceID{}, nil
default:
scopedLog.Warn("Ignoring k8s service: unsupported type")
return ServiceID{}, nil
}
if svc.Spec.ClusterIP == "" && (!option.Config.EnableNodePort || len(svc.Spec.ExternalIPs) == 0) {
return ServiceID{}, nil
}
clusterIP := net.ParseIP(svc.Spec.ClusterIP)
headless := false
if strings.ToLower(svc.Spec.ClusterIP) == "none" {
headless = true
}
var trafficPolicy loadbalancer.SVCTrafficPolicy
switch svc.Spec.ExternalTrafficPolicy {
case slim_corev1.ServiceExternalTrafficPolicyTypeLocal:
trafficPolicy = loadbalancer.SVCTrafficPolicyLocal
default:
trafficPolicy = loadbalancer.SVCTrafficPolicyCluster
}
for _, ip := range svc.Status.LoadBalancer.Ingress {
if ip.IP != "" {
loadBalancerIPs = append(loadBalancerIPs, ip.IP)
}
}
svcInfo := NewService(clusterIP, svc.Spec.ExternalIPs, loadBalancerIPs, headless,
trafficPolicy, uint16(svc.Spec.HealthCheckNodePort), svc.Labels, svc.Spec.Selector)
svcInfo.IncludeExternal = getAnnotationIncludeExternal(svc)
svcInfo.Shared = getAnnotationShared(svc)
if svc.Spec.SessionAffinity == slim_corev1.ServiceAffinityClientIP {
svcInfo.SessionAffinity = true
if cfg := svc.Spec.SessionAffinityConfig; cfg != nil && cfg.ClientIP != nil && cfg.ClientIP.TimeoutSeconds != nil {
svcInfo.SessionAffinityTimeoutSec = uint32(*cfg.ClientIP.TimeoutSeconds)
}
if svcInfo.SessionAffinityTimeoutSec == 0 {
svcInfo.SessionAffinityTimeoutSec = uint32(v1.DefaultClientIPServiceAffinitySeconds)
}
}
for _, port := range svc.Spec.Ports {
p := loadbalancer.NewL4Addr(loadbalancer.L4Type(port.Protocol), uint16(port.Port))
portName := loadbalancer.FEPortName(port.Name)
if _, ok := svcInfo.Ports[portName]; !ok {
svcInfo.Ports[portName] = p
}
// TODO(brb) Get rid of this hack by moving the creation of surrogate
// frontends to pkg/service.
//
// This is a hack;-( In the case of NodePort service, we need to create
// surrogate frontends per IP protocol - one with a zero IP addr and
// one per each public iface IP addr.
if svc.Spec.Type == slim_corev1.ServiceTypeNodePort || svc.Spec.Type == slim_corev1.ServiceTypeLoadBalancer {
if option.Config.EnableNodePort && nodeAddressing != nil {
if _, ok := svcInfo.NodePorts[portName]; !ok {
svcInfo.NodePorts[portName] =
make(map[string]*loadbalancer.L3n4AddrID)
}
proto := loadbalancer.L4Type(port.Protocol)
port := uint16(port.NodePort)
id := loadbalancer.ID(0) // will be allocated by k8s_watcher
if option.Config.EnableIPv4 &&
clusterIP != nil && !strings.Contains(svc.Spec.ClusterIP, ":") {
for _, ip := range nodeAddressing.IPv4().LoadBalancerNodeAddresses() {
nodePortFE := loadbalancer.NewL3n4AddrID(proto, ip, port, id)
svcInfo.NodePorts[portName][nodePortFE.String()] = nodePortFE
}
}
if option.Config.EnableIPv6 &&
clusterIP != nil && strings.Contains(svc.Spec.ClusterIP, ":") {
for _, ip := range nodeAddressing.IPv6().LoadBalancerNodeAddresses() {
nodePortFE := loadbalancer.NewL3n4AddrID(proto, ip, port, id)
svcInfo.NodePorts[portName][nodePortFE.String()] = nodePortFE
}
}
}
}
}
return svcID, svcInfo
}
// ServiceID identities the Kubernetes service
type ServiceID struct {
Name string `json:"serviceName,omitempty"`
Namespace string `json:"namespace,omitempty"`
}
// String returns the string representation of a service ID
func (s ServiceID) String() string {
return fmt.Sprintf("%s/%s", s.Namespace, s.Name)
}
// ParseServiceIDFrom returns a ServiceID derived from the given kubernetes
// service FQDN.
func ParseServiceIDFrom(dn string) *ServiceID {
// typical service name "cilium-etcd-client.kube-system.svc"
idx1 := strings.IndexByte(dn, '.')
if idx1 >= 0 {
svc := ServiceID{
Name: dn[:idx1],
}
idx2 := strings.IndexByte(dn[idx1+1:], '.')
if idx2 >= 0 {
// "cilium-etcd-client.kube-system.svc"
// ^idx1+1 ^ idx1+1+idx2
svc.Namespace = dn[idx1+1 : idx1+1+idx2]
} else {
// "cilium-etcd-client.kube-system"
// ^idx1+1
svc.Namespace = dn[idx1+1:]
}
return &svc
}
return nil
}
// Service is an abstraction for a k8s service that is composed by the frontend IP
// address (FEIP) and the map of the frontend ports (Ports).
// +k8s:deepcopy-gen=true
type Service struct {
FrontendIP net.IP
IsHeadless bool
// IncludeExternal is true when external endpoints from other clusters
// should be included
IncludeExternal bool
// Shared is true when the service should be exposed/shared to other clusters
Shared bool
// TrafficPolicy controls how backends are selected. If set to "Local", only
// node-local backends are chosen
TrafficPolicy loadbalancer.SVCTrafficPolicy
// HealthCheckNodePort defines on which port the node runs a HTTP health
// check server which may be used by external loadbalancers to determine
// if a node has local backends. This will only have effect if both
// LoadBalancerIPs is not empty and TrafficPolicy is SVCTrafficPolicyLocal.
HealthCheckNodePort uint16
Ports map[loadbalancer.FEPortName]*loadbalancer.L4Addr
// NodePorts stores mapping for port name => NodePort frontend addr string =>
// NodePort fronted addr. The string addr => addr indirection is to avoid
// storing duplicates.
NodePorts map[loadbalancer.FEPortName]map[string]*loadbalancer.L3n4AddrID
// K8sExternalIPs stores mapping of the endpoint in a string format to the
// externalIP in net.IP format.
K8sExternalIPs map[string]net.IP
// LoadBalancerIPs stores LB IPs assigned to the service (string(IP) => IP).
LoadBalancerIPs map[string]net.IP
Labels map[string]string
Selector map[string]string
// SessionAffinity denotes whether service has the clientIP session affinity
SessionAffinity bool
// SessionAffinityTimeoutSeconds denotes session affinity timeout
SessionAffinityTimeoutSec uint32
}
// String returns the string representation of a service resource
func (s *Service) String() string {
if s == nil {
return "nil"
}
ports := make([]string, len(s.Ports))
i := 0
for p := range s.Ports {
ports[i] = string(p)
i++
}
return fmt.Sprintf("frontend:%s/ports=%s/selector=%v", s.FrontendIP.String(), ports, s.Selector)
}
// IsExternal returns true if the service is expected to serve out-of-cluster endpoints:
func (s Service) IsExternal() bool {
return len(s.Selector) == 0
}
// DeepEquals returns true if both services are equal
func (s *Service) DeepEquals(o *Service) bool {
switch {
case (s == nil) != (o == nil):
return false
case (s == nil) && (o == nil):
return true
}
if s.IsHeadless == o.IsHeadless &&
s.TrafficPolicy == o.TrafficPolicy &&
s.HealthCheckNodePort == o.HealthCheckNodePort &&
s.FrontendIP.Equal(o.FrontendIP) &&
comparator.MapStringEquals(s.Labels, o.Labels) &&
comparator.MapStringEquals(s.Selector, o.Selector) {
if ((s.Ports == nil) != (o.Ports == nil)) ||
len(s.Ports) != len(o.Ports) {
return false
}
for portName, port := range s.Ports {
oPort, ok := o.Ports[portName]
if !ok {
return false
}
if !port.Equals(oPort) {
return false
}
}
if ((s.NodePorts == nil) != (o.NodePorts == nil)) ||
len(s.NodePorts) != len(o.NodePorts) {
return false
}
for portName, nodePorts := range s.NodePorts {
oNodePorts, ok := o.NodePorts[portName]
if !ok {
return false
}
if ((nodePorts == nil) != (oNodePorts == nil)) ||
len(nodePorts) != len(oNodePorts) {
return false
}
for nodePortName, nodePort := range nodePorts {
oNodePort, ok := oNodePorts[nodePortName]
if !ok {
return false
}
if !nodePort.Equals(oNodePort) {
return false
}
}
}
if ((s.K8sExternalIPs == nil) != (o.K8sExternalIPs == nil)) ||
len(s.K8sExternalIPs) != len(o.K8sExternalIPs) {
return false
}
for k, v := range s.K8sExternalIPs {
vOther, ok := o.K8sExternalIPs[k]
if !ok || !v.Equal(vOther) {
return false
}
}
if ((s.LoadBalancerIPs == nil) != (o.LoadBalancerIPs == nil)) ||
len(s.LoadBalancerIPs) != len(o.LoadBalancerIPs) {
return false
}
for k, v := range s.LoadBalancerIPs {
vOther, ok := o.LoadBalancerIPs[k]
if !ok || !v.Equal(vOther) {
return false
}
}
return true
}
return false
}
func parseIPs(externalIPs []string) map[string]net.IP {
m := map[string]net.IP{}
for _, externalIP := range externalIPs {
ip := net.ParseIP(externalIP)
if ip != nil {
m[externalIP] = ip
}
}
return m
}
// NewService returns a new Service with the Ports map initialized.
func NewService(ip net.IP, externalIPs []string, loadBalancerIPs []string,
headless bool, trafficPolicy loadbalancer.SVCTrafficPolicy,
healthCheckNodePort uint16, labels, selector map[string]string) *Service {
var k8sExternalIPs map[string]net.IP
var k8sLoadBalancerIPs map[string]net.IP
if option.Config.EnableNodePort {
k8sExternalIPs = parseIPs(externalIPs)
k8sLoadBalancerIPs = parseIPs(loadBalancerIPs)
}
return &Service{
FrontendIP: ip,
IsHeadless: headless,
TrafficPolicy: trafficPolicy,
HealthCheckNodePort: healthCheckNodePort,
Ports: map[loadbalancer.FEPortName]*loadbalancer.L4Addr{},
NodePorts: map[loadbalancer.FEPortName]map[string]*loadbalancer.L3n4AddrID{},
K8sExternalIPs: k8sExternalIPs,
LoadBalancerIPs: k8sLoadBalancerIPs,
Labels: labels,
Selector: selector,
}
}
// UniquePorts returns a map of all unique ports configured in the service
func (s *Service) UniquePorts() map[uint16]bool {
// We are not discriminating the different L4 protocols on the same L4
// port so we create the number of unique sets of service IP + service
// port.
uniqPorts := map[uint16]bool{}
for _, p := range s.Ports {
uniqPorts[p.Port] = true
}
return uniqPorts
}
// NewClusterService returns the serviceStore.ClusterService representing a
// Kubernetes Service
func NewClusterService(id ServiceID, k8sService *Service, k8sEndpoints *Endpoints) serviceStore.ClusterService {
svc := serviceStore.NewClusterService(id.Name, id.Namespace)
for key, value := range k8sService.Labels {
svc.Labels[key] = value
}
for key, value := range k8sService.Selector {
svc.Selector[key] = value
}
portConfig := serviceStore.PortConfiguration{}
for portName, port := range k8sService.Ports {
portConfig[string(portName)] = port
}
svc.Frontends = map[string]serviceStore.PortConfiguration{}
svc.Frontends[k8sService.FrontendIP.String()] = portConfig
svc.Backends = map[string]serviceStore.PortConfiguration{}
for ipString, backend := range k8sEndpoints.Backends {
svc.Backends[ipString] = backend.Ports
}
return svc
}
type ServiceIPGetter interface {
GetServiceIP(svcID ServiceID) *loadbalancer.L3n4Addr
}
// CreateCustomDialer returns a custom dialer that picks the service IP,
// from the given ServiceIPGetter, if the address the used to dial is a k8s
// service.
func CreateCustomDialer(b ServiceIPGetter, log *logrus.Entry) func(s string, duration time.Duration) (conn net.Conn, e error) {
return func(s string, duration time.Duration) (conn net.Conn, e error) {
// If the service is available, do the service translation to
// the service IP. Otherwise dial with the original service
// name `s`.
u, err := url.Parse(s)
if err == nil {
svc := ParseServiceIDFrom(u.Host)
if svc != nil {
svcIP := b.GetServiceIP(*svc)
if svcIP != nil {
s = svcIP.String()
}
} else {
log.Debug("Service not found")
}
log.Debugf("custom dialer based on k8s service backend is dialing to %q", s)
} else {
log.Errorf("Unable to parse etcd service URL %s", err)
}
return net.Dial("tcp", s)
}
}