forked from zalando/skipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kube.go
1324 lines (1158 loc) · 37.5 KB
/
kube.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package kubernetes
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"os/signal"
"regexp"
"sort"
"strconv"
"strings"
"syscall"
"time"
log "github.com/sirupsen/logrus"
"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/filters/builtin"
"github.com/zalando/skipper/loadbalancer"
"github.com/zalando/skipper/predicates/source"
"github.com/zalando/skipper/predicates/traffic"
)
// FEATURE:
// - provide option to limit the used namespaces?
const (
defaultKubernetesURL = "http://localhost:8001"
ingressesClusterURI = "/apis/extensions/v1beta1/ingresses"
ingressesNamespaceFmt = "/apis/extensions/v1beta1/namespaces/%s/ingresses"
ingressClassKey = "kubernetes.io/ingress.class"
defaultIngressClass = "skipper"
endpointURIFmt = "/api/v1/namespaces/%s/endpoints/%s"
serviceURIFmt = "/api/v1/namespaces/%s/services/%s"
serviceAccountDir = "/var/run/secrets/kubernetes.io/serviceaccount/"
serviceAccountTokenKey = "token"
serviceAccountRootCAKey = "ca.crt"
serviceHostEnvVar = "KUBERNETES_SERVICE_HOST"
servicePortEnvVar = "KUBERNETES_SERVICE_PORT"
healthcheckRouteID = "kube__healthz"
httpRedirectRouteID = "kube__redirect"
healthcheckPath = "/kube-system/healthz"
backendWeightsAnnotationKey = "zalando.org/backend-weights"
ratelimitAnnotationKey = "zalando.org/ratelimit"
skipperfilterAnnotationKey = "zalando.org/skipper-filter"
skipperpredicateAnnotationKey = "zalando.org/skipper-predicate"
skipperRoutesAnnotationKey = "zalando.org/skipper-routes"
pathModeAnnotationKey = "zalando.org/skipper-ingress-path-mode"
)
// PathMode values are used to control the ingress path interpretation. The path mode can
// be set globally for all ingress paths, and it can be overruled by the individual ingress
// rules using the zalando.org/skipper-ingress-path-mode annotation. When path mode is not
// set, the Kubernetes ingress specification is used, accepting regular expressions with a
// mandatory leading "/", automatically prepended by the "^" control character.
//
// When PathPrefix is used, the path matching becomes deterministic when
// a request could match more than one ingress routes otherwise.
type PathMode int
const (
// KubernetesIngressMode is the default path mode. Expects regular expressions
// with a mandatory leading "/". The expressions are automatically prepended by
// the "^" control character.
KubernetesIngressMode PathMode = iota
// PathRegexp is like KubernetesIngressMode but is not prepended by the "^"
// control character.
PathRegexp
// PathPrefix is like the PathSubtree predicate. E.g. "/foo/bar" will match
// "/foo/bar" or "/foo/bar/baz", but won't match "/foo/barooz".
//
// In this mode, when a Path or a PathSubtree predicate is set in an annotation,
// the value from the annotation has precedence over the standard ingress path.
PathPrefix
)
const (
kubernetesIngressModeString = "kubernetes-ingress"
pathRegexpString = "path-regexp"
pathPrefixString = "path-prefix"
)
var internalIPs = []interface{}{
"10.0.0.0/8",
"192.168.0.0/16",
"172.16.0.0/12",
"127.0.0.1/8",
"fd00::/8",
"::1/128",
}
// Options is used to initialize the Kubernetes DataClient.
type Options struct {
// KubernetesInCluster defines if skipper is deployed and running in the kubernetes cluster
// this would make authentication with API server happen through the service account, rather than
// running along side kubectl proxy
KubernetesInCluster bool
// KubernetesURL is used as the base URL for Kubernetes API requests. Defaults to http://localhost:8001.
// (TBD: support in-cluster operation by taking the address and certificate from the standard Kubernetes
// environment variables.)
KubernetesURL string
// KubernetesNamespace is used to switch between finding ingresses in the cluster-scope or limit
// the ingresses to only those in the specified namespace. Defaults to "" which means monitor ingresses
// in the cluster-scope.
KubernetesNamespace string
// ProvideHealthcheck, when set, tells the data client to append a healthcheck route to the ingress
// routes in case of successfully receiving the ingress items from the API (even if individual ingress
// items may be invalid), or a failing healthcheck route when the API communication fails. The
// healthcheck endpoint can be accessed from internal IPs on any hostname, with the path
// /kube-system/healthz.
//
// When used in a custom configuration, the current filter registry needs to include the status()
// filter, and the available predicates need to include the Source() predicate.
ProvideHealthcheck bool
// ProvideHTTPSRedirect, when set, tells the data client to append an HTTPS redirect route to the
// ingress routes. This route will detect the X-Forwarded-Proto=http and respond with a 301 message
// to the HTTPS equivalent of the same request (using the redirectTo(301, "https:") filter). The
// X-Forwarded-Proto and X-Forwarded-Port is expected to be set by the load balancer.
//
// (See also https://github.com/zalando-incubator/kube-ingress-aws-controller as part of the
// https://github.com/zalando-incubator/kubernetes-on-aws project.)
ProvideHTTPSRedirect bool
// IngressClass is a regular expression to filter only those ingresses that match. If an ingress does
// not have a class annotation or the annotation is an empty string, skipper will load it. The default
// value for the ingress class is 'skipper'.
//
// For further information see:
// https://github.com/nginxinc/kubernetes-ingress/tree/master/examples/multiple-ingress-controllers
IngressClass string
// ReverseSourcePredicate set to true will do the Source IP
// whitelisting for the heartbeat endpoint correctly in AWS.
// Amazon's ALB writes the client IP to the last item of the
// string list of the X-Forwarded-For header, in this case you
// want to set this to true.
ReverseSourcePredicate bool
// Noop, WIP.
ForceFullUpdatePeriod time.Duration
// WhitelistedHealthcheckCIDR to be appended to the default iprange
WhitelistedHealthCheckCIDR []string
// PathMode controls the default interpretation of ingress paths in cases when the ingress doesn't
// specify it with an annotation.
PathMode PathMode
}
// Client is a Skipper DataClient implementation used to create routes based on Kubernetes Ingress settings.
type Client struct {
httpClient *http.Client
apiURL string
provideHealthcheck bool
healthy bool
provideHTTPSRedirect bool
token string
current map[string]*eskip.Route
termReceived bool
sigs chan os.Signal
ingressClass *regexp.Regexp
reverseSourcePredicate bool
pathMode PathMode
quit chan struct{}
namespace string
}
var nonWord = regexp.MustCompile("\\W")
var (
errServiceNotFound = errors.New("service not found")
errEndpointNotFound = errors.New("endpoint not found")
errAPIServerURLNotFound = errors.New("kubernetes API server URL could not be constructed from env vars")
errInvalidCertificate = errors.New("invalid CA")
)
// New creates and initializes a Kubernetes DataClient.
func New(o Options) (*Client, error) {
quit := make(chan struct{})
httpClient, err := buildHTTPClient(serviceAccountDir+serviceAccountRootCAKey, o.KubernetesInCluster, quit)
if err != nil {
return nil, err
}
apiURL, err := buildAPIURL(o)
if err != nil {
return nil, err
}
token, err := readServiceAccountToken(serviceAccountDir+serviceAccountTokenKey, o.KubernetesInCluster)
if err != nil {
return nil, err
}
ingCls := defaultIngressClass
if o.IngressClass != "" {
ingCls = o.IngressClass
}
ingClsRx, err := regexp.Compile(ingCls)
if err != nil {
return nil, err
}
log.Debugf(
"running in-cluster: %t. api server url: %s. provide health check: %t. ingress.class filter: %s. namespace: %s",
o.KubernetesInCluster, apiURL, o.ProvideHealthcheck, ingCls, o.KubernetesNamespace,
)
var sigs chan os.Signal
if o.ProvideHealthcheck {
log.Info("register sigterm handler")
sigs = make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM)
}
if len(o.WhitelistedHealthCheckCIDR) > 0 {
whitelistCIDRS := make([]interface{}, len(o.WhitelistedHealthCheckCIDR))
for i, v := range o.WhitelistedHealthCheckCIDR {
whitelistCIDRS[i] = v
}
internalIPs = append(internalIPs, whitelistCIDRS...)
log.Debugf("new internal ips are: %s", internalIPs)
}
return &Client{
httpClient: httpClient,
apiURL: apiURL,
provideHealthcheck: o.ProvideHealthcheck,
provideHTTPSRedirect: o.ProvideHTTPSRedirect,
current: make(map[string]*eskip.Route),
token: token,
sigs: sigs,
ingressClass: ingClsRx,
reverseSourcePredicate: o.ReverseSourcePredicate,
pathMode: o.PathMode,
quit: quit,
namespace: o.KubernetesNamespace,
}, nil
}
// String returns the string representation of the path mode, the same
// values that are used in the path mode annotation.
func (m PathMode) String() string {
switch m {
case PathRegexp:
return pathRegexpString
case PathPrefix:
return pathPrefixString
default:
return kubernetesIngressModeString
}
}
// ParsePathMode parses the string representations of the different
// path modes.
func ParsePathMode(s string) (PathMode, error) {
switch s {
case kubernetesIngressModeString:
return KubernetesIngressMode, nil
case pathRegexpString:
return PathRegexp, nil
case pathPrefixString:
return PathPrefix, nil
default:
return 0, fmt.Errorf("invalid path mode string: %s", s)
}
}
func readServiceAccountToken(tokenFilePath string, inCluster bool) (string, error) {
if !inCluster {
return "", nil
}
bToken, err := ioutil.ReadFile(tokenFilePath)
if err != nil {
return "", err
}
return string(bToken), nil
}
func buildHTTPClient(certFilePath string, inCluster bool, quit chan struct{}) (*http.Client, error) {
if !inCluster {
return http.DefaultClient, nil
}
rootCA, err := ioutil.ReadFile(certFilePath)
if err != nil {
return nil, err
}
certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM(rootCA) {
return nil, errInvalidCertificate
}
transport := &http.Transport{
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
ExpectContinueTimeout: 30 * time.Second,
MaxIdleConns: 5,
MaxIdleConnsPerHost: 5,
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
RootCAs: certPool,
},
}
// regularly force closing idle connections
go func() {
for {
select {
case <-time.After(10 * time.Second):
transport.CloseIdleConnections()
case <-quit:
return
}
}
}()
return &http.Client{
Transport: transport,
}, nil
}
func buildAPIURL(o Options) (string, error) {
if !o.KubernetesInCluster {
if o.KubernetesURL == "" {
return defaultKubernetesURL, nil
}
return o.KubernetesURL, nil
}
host, port := os.Getenv(serviceHostEnvVar), os.Getenv(servicePortEnvVar)
if host == "" || port == "" {
return "", errAPIServerURLNotFound
}
return "https://" + net.JoinHostPort(host, port), nil
}
func (c *Client) createRequest(method, url string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
if c.token != "" {
req.Header.Set("Authorization", "Bearer "+c.token)
}
return req, nil
}
func (c *Client) getJSON(uri string, a interface{}) error {
url := c.apiURL + uri
log.Debugf("making request to: %s", url)
req, err := c.createRequest("GET", url, nil)
if err != nil {
return err
}
rsp, err := c.httpClient.Do(req)
if err != nil {
log.Debugf("request to %s failed: %v", url, err)
return err
}
log.Debugf("request to %s succeeded", url)
defer rsp.Body.Close()
if rsp.StatusCode == http.StatusNotFound {
return errServiceNotFound
}
if rsp.StatusCode != http.StatusOK {
log.Debugf("request failed, status: %d, %s", rsp.StatusCode, rsp.Status)
return fmt.Errorf("request failed, status: %d, %s", rsp.StatusCode, rsp.Status)
}
b := bytes.NewBuffer(nil)
if _, err := io.Copy(b, rsp.Body); err != nil {
log.Debugf("reading response body failed: %v", err)
return err
}
err = json.Unmarshal(b.Bytes(), a)
if err != nil {
log.Debugf("invalid response format: %v", err)
}
return err
}
// TODO:
// - check if it can be batched
// - check the existing controllers for cases when hunting for cluster ip
func (c *Client) getService(namespace, name string) (*service, error) {
log.Debugf("requesting service: %s/%s", namespace, name)
url := fmt.Sprintf(serviceURIFmt, namespace, name)
var s service
if err := c.getJSON(url, &s); err != nil {
return nil, err
}
if s.Spec == nil {
log.Debug("invalid service datagram, missing spec")
return nil, errServiceNotFound
}
return &s, nil
}
func (c *Client) getServiceURL(svc *service, port backendPort) (string, error) {
if p, ok := port.number(); ok {
log.Debugf("service port as number: %d", p)
return fmt.Sprintf("http://%s:%d", svc.Spec.ClusterIP, p), nil
}
pn, _ := port.name()
for _, pi := range svc.Spec.Ports {
if pi.Name == pn {
log.Debugf("service port found by name: %s -> %d", pn, pi.Port)
return fmt.Sprintf("http://%s:%d", svc.Spec.ClusterIP, pi.Port), nil
}
}
log.Debugf("service port not found by name: %s", pn)
return "", errServiceNotFound
}
// TODO: find a nicer way to autogenerate route IDs
func routeID(namespace, name, host, path, backend string) string {
namespace = nonWord.ReplaceAllString(namespace, "_")
name = nonWord.ReplaceAllString(name, "_")
host = nonWord.ReplaceAllString(host, "_")
path = nonWord.ReplaceAllString(path, "_")
backend = nonWord.ReplaceAllString(backend, "_")
return fmt.Sprintf("kube_%s__%s__%s__%s__%s", namespace, name, host, path, backend)
}
// routeIDForCustom generates a route id for a custom route of an ingress
// resource.
func routeIDForCustom(namespace, name, id, host string, index int) string {
name = name + "_" + id + "_" + strconv.Itoa(index)
return routeID(namespace, name, host, "", "")
}
// converts the default backend if any
func (c *Client) convertDefaultBackend(i *ingressItem) ([]*eskip.Route, bool, error) {
// the usage of the default backend depends on what we want
// we can generate a hostname out of it based on shared rules
// and instructions in annotations, if there are no rules defined
// this is a flaw in the ingress API design, because it is not on the hosts' level, but the spec
// tells to match if no rule matches. This means that there is no matching rule on this ingress
// and if there are multiple ingress items, then there is a race between them.
if i.Spec.DefaultBackend == nil {
return nil, false, nil
}
var (
eps []string
err error
routes []*eskip.Route
ns = i.Metadata.Namespace
name = i.Metadata.Name
svcName = i.Spec.DefaultBackend.ServiceName
svcPort = i.Spec.DefaultBackend.ServicePort
)
svc, err := c.getService(ns, svcName)
if err != nil {
log.Errorf("convertDefaultBackend: Failed to get service %s, %s, %s", ns, svcName, svcPort)
return nil, false, err
}
targetPort, err := svc.GetTargetPort(svcPort)
if err != nil {
err = nil
log.Errorf("Failed to find target port %v, %s, fallback to service", svc.Spec.Ports, svcPort)
} else {
// TODO(aryszka): check docs that service name is always good for requesting the endpoints
log.Infof("Found target port %v, for service %s", targetPort, svcName)
eps, err = c.getEndpoints(
ns,
svcName,
svcPort.String(),
targetPort,
)
log.Infof("convertDefaultBackend: Found %d endpoints for %s: %v", len(eps), svcName, err)
}
if len(eps) == 0 || err == errEndpointNotFound {
// TODO(sszuecs): https://github.com/zalando/skipper/issues/549
// dispatch by service type to implement type externalname, which has no ServicePort (could be ignored from ingress).
// We should then implement a redirect route for that.
// Example spec:
//
// spec:
// type: ExternalName
// externalName: my.database.example.com
address, err2 := c.getServiceURL(svc, svcPort)
if err2 != nil {
return nil, false, err2
}
r := &eskip.Route{
Id: routeID(ns, name, "", "", ""),
Backend: address,
}
routes = append(routes, r)
} else if err != nil {
return nil, false, err
}
group := routeID(ns, name, "", "", "")
// TODO:
// - don't do load balancing if there's only a single endpoint
// - better: cleanup single route load balancer groups in the routing package before applying the next
// routing table
if len(eps) == 0 {
return routes, true, nil
}
if len(eps) == 1 {
r := &eskip.Route{
Id: routeID(ns, name, "", "", ""),
Backend: eps[0],
}
routes = append(routes, r)
return routes, true, nil
}
for idx, ep := range eps {
r := &eskip.Route{
Id: routeID(ns, name, "", "", strconv.Itoa(idx)),
Backend: ep,
Predicates: []*eskip.Predicate{{
Name: loadbalancer.MemberPredicateName,
Args: []interface{}{
group,
idx, // index within the group
},
}},
Filters: []*eskip.Filter{{
Name: builtin.DropRequestHeaderName,
Args: []interface{}{
loadbalancer.DecisionHeader,
},
}},
}
routes = append(routes, r)
}
decisionRoute := &eskip.Route{
Id: routeID(ns, name, "", "", "") + "__lb_group",
BackendType: eskip.LoopBackend,
Predicates: []*eskip.Predicate{{
Name: loadbalancer.GroupPredicateName,
Args: []interface{}{
group,
},
}},
Filters: []*eskip.Filter{{
Name: loadbalancer.DecideFilterName,
Args: []interface{}{
group,
len(eps), // number of member routes
},
}},
}
routes = append(routes, decisionRoute)
return routes, true, nil
}
func (c *Client) getEndpoints(ns, name, servicePort, targetPort string) ([]string, error) {
log.Debugf("requesting endpoint: %s/%s", ns, name)
url := fmt.Sprintf(endpointURIFmt, ns, name)
var ep endpoint
if err := c.getJSON(url, &ep); err != nil {
return nil, err
}
if ep.Subsets == nil {
return nil, errEndpointNotFound
}
targets := ep.Targets(servicePort, targetPort)
if len(targets) == 0 {
return nil, errEndpointNotFound
}
sort.Strings(targets)
return targets, nil
}
func setPath(m PathMode, r *eskip.Route, p string) {
if p == "" {
return
}
switch m {
case PathPrefix:
r.Predicates = append(r.Predicates, &eskip.Predicate{
Name: "PathSubtree",
Args: []interface{}{p},
})
case PathRegexp:
r.PathRegexps = []string{p}
default:
r.PathRegexps = []string{"^" + p}
}
}
func (c *Client) convertPathRule(
ns, name, host string,
prule *pathRule,
pathMode PathMode,
endpointsURLs map[string][]string,
) ([]*eskip.Route, error) {
if prule.Backend == nil {
return nil, fmt.Errorf("invalid path rule, missing backend in: %s/%s/%s", ns, name, host)
}
var (
eps []string
err error
routes []*eskip.Route
svc *service
)
svcPort := prule.Backend.ServicePort
svcName := prule.Backend.ServiceName
endpointKey := ns + svcName + svcPort.String()
if val, ok := endpointsURLs[endpointKey]; !ok {
svc, err = c.getService(ns, svcName)
if err != nil {
log.Errorf("convertPathRule: Failed to get service %s, %s, %s", ns, svcName, svcPort)
return nil, err
}
targetPort, err := svc.GetTargetPort(svcPort)
if err != nil {
// fallback to service, but service definition is wrong or no pods
log.Debugf("Failed to find target port for service %s, fallback to service: %v", svcName, err)
err = nil
} else {
// err handled below
eps, err = c.getEndpoints(ns, svcName, svcPort.String(), targetPort)
log.Debugf("convertPathRule: Found %d endpoints %s for %s", len(eps), targetPort, svcName)
}
if len(eps) == 0 || err == errEndpointNotFound {
// TODO(sszuecs): https://github.com/zalando/skipper/issues/549
// dispatch by service type to implement type externalname, which has no ServicePort (could be ignored from ingress).
// We should then implement a redirect route for that.
// Example spec:
//
// spec:
// type: ExternalName
// externalName: my.database.example.com
address, err2 := c.getServiceURL(svc, svcPort)
if err2 != nil {
return nil, err2
}
r := &eskip.Route{
Id: routeID(ns, name, host, prule.Path, svcName),
Backend: address,
}
setPath(pathMode, r, prule.Path)
if 0.0 < prule.Backend.Traffic && prule.Backend.Traffic < 1.0 {
r.Predicates = append([]*eskip.Predicate{{
Name: traffic.PredicateName,
Args: []interface{}{prule.Backend.Traffic},
}}, r.Predicates...)
log.Infof("Traffic weight %.2f for backend '%s'", prule.Backend.Traffic, svcName)
}
routes = append(routes, r)
} else if err != nil {
return nil, err
} else {
endpointsURLs[endpointKey] = eps
}
log.Debugf("%d new routes for %s/%s/%s", len(eps), ns, svcName, svcPort)
} else {
eps = val
log.Debugf("%d routes for %s/%s/%s already known", len(eps), ns, svcName, svcPort)
}
if len(eps) == 1 {
r := &eskip.Route{
Id: routeID(ns, name, host, prule.Path, svcName),
Backend: eps[0],
}
setPath(pathMode, r, prule.Path)
// add traffic predicate if traffic weight is between 0.0 and 1.0
if 0.0 < prule.Backend.Traffic && prule.Backend.Traffic < 1.0 {
r.Predicates = append([]*eskip.Predicate{{
Name: traffic.PredicateName,
Args: []interface{}{prule.Backend.Traffic},
}}, r.Predicates...)
log.Debugf("Traffic weight %.2f for backend '%s'", prule.Backend.Traffic, svcName)
}
routes = append(routes, r)
return routes, nil
}
if len(eps) == 0 {
return routes, nil
}
group := routeID(ns, name, host, prule.Path, prule.Backend.ServiceName)
for idx, ep := range eps {
r := &eskip.Route{
Id: routeID(ns, name, host, prule.Path, svcName+fmt.Sprintf("_%d", idx)),
Backend: ep,
Predicates: []*eskip.Predicate{{
Name: loadbalancer.MemberPredicateName,
Args: []interface{}{
group,
idx, // index within the group
},
}},
Filters: []*eskip.Filter{{
Name: builtin.DropRequestHeaderName,
Args: []interface{}{
loadbalancer.DecisionHeader,
},
}},
}
setPath(pathMode, r, prule.Path)
routes = append(routes, r)
}
decisionRoute := &eskip.Route{
Id: routeID(ns, name, host, prule.Path, svcName) + "__lb_group",
BackendType: eskip.LoopBackend,
Predicates: []*eskip.Predicate{{
Name: loadbalancer.GroupPredicateName,
Args: []interface{}{
group,
},
}},
Filters: []*eskip.Filter{{
Name: loadbalancer.DecideFilterName,
Args: []interface{}{
group,
len(eps), // number of member routes
},
}},
}
setPath(pathMode, decisionRoute, prule.Path)
// add traffic predicate if traffic weight is between 0.0 and 1.0
if 0.0 < prule.Backend.Traffic && prule.Backend.Traffic < 1.0 {
decisionRoute.Predicates = append([]*eskip.Predicate{{
Name: traffic.PredicateName,
Args: []interface{}{prule.Backend.Traffic},
}}, decisionRoute.Predicates...)
log.Debugf("Traffic weight %.2f for backend '%s'", prule.Backend.Traffic, svcName)
}
routes = append(routes, decisionRoute)
return routes, nil
}
func applyAnnotationPredicates(m PathMode, r *eskip.Route, annotation string) error {
if annotation == "" {
return nil
}
predicates, err := eskip.ParsePredicates(annotation)
if err != nil {
return err
}
// to avoid conflict, give precedence for those predicates that come
// from annotations
if m == PathPrefix {
for _, p := range predicates {
if p.Name != "Path" && p.Name != "PathSubtree" {
continue
}
r.Path = ""
for i, p := range r.Predicates {
if p.Name != "PathSubtree" && p.Name != "Path" {
continue
}
copy(r.Predicates[i:], r.Predicates[i+1:])
r.Predicates[len(r.Predicates)-1] = nil
r.Predicates = r.Predicates[:len(r.Predicates)-1]
break
}
}
}
r.Predicates = append(r.Predicates, predicates...)
return nil
}
// ingressToRoutes logs if an invalid found, but proceeds with the
// valid ones. Reporting failures in Ingress status is not possible,
// because Ingress status field is v1.LoadBalancerIngress that only
// supports IP and Hostname as string.
func (c *Client) ingressToRoutes(items []*ingressItem) ([]*eskip.Route, error) {
// TODO: apply the laod balancing by using the loadbalancer.BalanceRoute() function
routes := make([]*eskip.Route, 0, len(items))
hostRoutes := make(map[string][]*eskip.Route)
for _, i := range items {
if i.Metadata == nil || i.Metadata.Namespace == "" || i.Metadata.Name == "" ||
i.Spec == nil {
log.Error("invalid ingress item: missing metadata")
continue
}
logger := log.WithFields(log.Fields{
"ingress": fmt.Sprintf("%s/%s", i.Metadata.Namespace, i.Metadata.Name),
})
if r, ok, err := c.convertDefaultBackend(i); ok {
routes = append(routes, r...)
} else if err != nil {
logger.Errorf("error while converting default backend: %v", err)
}
// parse filter and ratelimit annotation
var annotationFilter string
if ratelimitAnnotationValue, ok := i.Metadata.Annotations[ratelimitAnnotationKey]; ok {
annotationFilter = ratelimitAnnotationValue
}
if val, ok := i.Metadata.Annotations[skipperfilterAnnotationKey]; ok {
if annotationFilter != "" {
annotationFilter = annotationFilter + " -> "
}
annotationFilter = annotationFilter + val
}
// parse predicate annotation
var annotationPredicate string
if val, ok := i.Metadata.Annotations[skipperpredicateAnnotationKey]; ok {
annotationPredicate = val
}
// parse routes annotation
var extraRoutes []*eskip.Route
annotationRoutes := i.Metadata.Annotations[skipperRoutesAnnotationKey]
if annotationRoutes != "" {
var err error
extraRoutes, err = eskip.Parse(annotationRoutes)
if err != nil {
logger.Errorf("failed to parse routes from %s, skipping: %v", skipperRoutesAnnotationKey, err)
}
}
// parse backend-weights annotation if it exists
var backendWeights map[string]float64
if backends, ok := i.Metadata.Annotations[backendWeightsAnnotationKey]; ok {
err := json.Unmarshal([]byte(backends), &backendWeights)
if err != nil {
logger.Errorf("error while parsing backend-weights annotation: %v", err)
}
}
// parse pathmode from annotation or fallback to global default
var pathMode PathMode = c.pathMode
if pathModeString, ok := i.Metadata.Annotations[pathModeAnnotationKey]; ok {
if p, err := ParsePathMode(pathModeString); err != nil {
log.Errorf("Failed to get path mode for ingress %s/%s: %v", i.Metadata.Namespace, i.Metadata.Name, err)
} else {
log.Infof("Set pathMode to %s", p)
pathMode = p
}
}
// We need this to avoid asking the k8s API for the same services
endpointsURLs := make(map[string][]string)
for _, rule := range i.Spec.Rules {
if rule.Http == nil {
logger.Warn("invalid ingress item: rule missing http definitions")
continue
}
// it is a regexp, would be better to have exact host, needs to be added in skipper
// this wrapping is temporary and escaping is not the right thing to do
// currently handled as mandatory
host := []string{"^" + strings.Replace(rule.Host, ".", "[.]", -1) + "$"}
// update Traffic field for each backend
computeBackendWeights(backendWeights, rule)
for _, prule := range rule.Http.Paths {
// add extra routes from optional annotation
for idx, r := range extraRoutes {
route := *r
route.HostRegexps = host
route.Id = routeIDForCustom(i.Metadata.Namespace, i.Metadata.Name, route.Id, rule.Host+strings.Replace(prule.Path, "/", "_", -1), idx)
setPath(pathMode, &route, prule.Path)
if i := countPathRoutes(&route); i <= 1 {
hostRoutes[rule.Host] = append(hostRoutes[rule.Host], &route)
} else {
log.Errorf("Failed to add route having %d path routes: %v", i, r)
}
}
if prule.Backend.Traffic > 0 {
endpoints, err := c.convertPathRule(
i.Metadata.Namespace,
i.Metadata.Name,
rule.Host,
prule,
pathMode,
endpointsURLs,
)
if err != nil {
// if the service is not found the route should be removed
if err == errServiceNotFound {
continue
}
// Ingress status field does not support errors
return nil, fmt.Errorf("error while getting service: %v", err)
}
for _, r := range endpoints {
r.HostRegexps = host
var isLBDecisionRoute bool
for _, p := range r.Predicates {
if p.Name == loadbalancer.GroupPredicateName {
isLBDecisionRoute = true
break
}
}
if !isLBDecisionRoute && annotationFilter != "" {
annotationFilters, err := eskip.ParseFilters(annotationFilter)
if err != nil {
logger.Errorf("Can not parse annotation filters: %v", err)
} else {
sav := r.Filters[:]
r.Filters = append(annotationFilters, sav...)
}
}
err := applyAnnotationPredicates(pathMode, r, annotationPredicate)
if err != nil {
logger.Errorf("failed to apply annotation predicates: %v", err)
}
hostRoutes[rule.Host] = append(hostRoutes[rule.Host], r)
}
}
}
}
}
for host, rs := range hostRoutes {
routes = append(routes, rs...)
// if routes were configured, but there is no catchall route
// defined for the host name, create a route which returns 404
if len(rs) > 0 && !catchAllRoutes(rs) {
catchAll := &eskip.Route{
Id: routeID("", "catchall", host, "", ""),
HostRegexps: rs[0].HostRegexps,
BackendType: eskip.ShuntBackend,
}
routes = append(routes, catchAll)
}
}
return routes, nil
}