forked from zalando/skipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kube.go
965 lines (826 loc) · 27.9 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
/*
Package kubernetes implements Kubernetes Ingress support for Skipper.
See: http://kubernetes.io/docs/user-guide/ingress/
The package provides a Skipper DataClient implementation that can be used to access the Kubernetes API for
ingress resources and generate routes based on them. The client polls for the ingress settings, and there is no
need for a separate controller. On the other hand, it doesn't provide a full Ingress solution alone, because it
doesn't do any load balancer configuration or DNS updates. For a full Ingress solution, it is possible to use
Skipper together with Kube-ingress-aws-controller, which targets AWS and takes care of the load balancer setup
for Kubernetes Ingress.
See: https://github.com/zalando-incubator/kube-ingress-aws-controller
Both Kube-ingress-aws-controller and Skipper Kubernetes are part of the larger project, Kubernetes On AWS:
https://github.com/zalando-incubator/kubernetes-on-aws/
Ingress shutdown by healthcheck
The Kubernetes ingress client catches TERM signals when the ProvideHealthcheck option is enabled, and reports
failing healthcheck after the signal was received. This means that, when the Ingress client is responsible for
the healthcheck of the cluster, and the Skipper process receives the TERM signal, it won't exit by itself
immediately, but will start reporting failures on healthcheck requests. Until it gets killed by the kubelet,
Skipper keeps serving the requests in this case.
Example - Ingress
A basic ingress specification:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: app
spec:
rules:
- host: app-default.example.org
http:
paths:
- backend:
serviceName: app-svc
servicePort: 80
Example - Ingress with ratelimiting
The example shows 50 calls per minute are allowed to each skipper
instance for the given ingress.
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
zalando.org/ratelimit: ratelimit(50, "1m")
name: app
spec:
rules:
- host: app-default.example.org
http:
paths:
- backend:
serviceName: app-svc
servicePort: 80
Example - Ingress with client based ratelimiting
The example shows 3 calls per minute per client, based on
X-Forwarded-For header or IP incase there is no X-Forwarded-For header
set, are allowed to each skipper instance for the given ingress.
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
zalando.org/ratelimit: localRatelimit(3, "1m")
name: app
spec:
rules:
- host: app-default.example.org
http:
paths:
- backend:
serviceName: app-svc
servicePort: 80
The example shows 500 calls per hour per client, based on
Authorization header set, are allowed to each skipper instance for the
given ingress.
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
zalando.org/ratelimit: localRatelimit(500, "1h", "auth")
name: app
spec:
rules:
- host: app-default.example.org
http:
paths:
- backend:
serviceName: app-svc
servicePort: 80
Example - Ingress with custom skipper filter configuration
The example shows the use of 2 filters from skipper for the implicitly
defined route in ingress.
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
zalando.org/skipper-filter: localRatelimit(50, "10m") -> requestCookie("test-session", "abc")
name: app
spec:
rules:
- host: app-default.example.org
http:
paths:
- backend:
serviceName: app-svc
servicePort: 80
Example - Ingress with custom skipper Predicate configuration
The example shows the use of a skipper predicates for the implicitly
defined route in ingress.
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
zalando.org/skipper-predicate: QueryParam("query", "^example$")
name: app
spec:
rules:
- host: app-default.example.org
http:
paths:
- backend:
serviceName: app-svc
servicePort: 80
Example - Ingress with shadow traffic
This will send production traffic to app-default.example.org and
copies incoming requests to https://app.shadow.example.org, but drops
responses from shadow URL. This is helpful to test your next
generation software with production workload. See also
https://godoc.org/github.com/zalando/skipper/filters/tee for details.
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
zalando.org/skipper-filter: tee("https://app.shadow.example.org")
name: app
spec:
rules:
- host: app-default.example.org
http:
paths:
- backend:
serviceName: app-svc
servicePort: 80
*/
package kubernetes
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"time"
log "github.com/sirupsen/logrus"
"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/filters/builtin"
"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"
ingressesURI = "/apis/extensions/v1beta1/ingresses"
ingressClassKey = "kubernetes.io/ingress.class"
defaultIngressClass = "skipper"
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"
)
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
// 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
}
// 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
provideHTTPSRedirect bool
token string
current map[string]*eskip.Route
termReceived bool
sigs chan os.Signal
ingressClass *regexp.Regexp
reverseSourcePredicate bool
}
var nonWord = regexp.MustCompile("\\W")
var (
errServiceNotFound = errors.New("service 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) {
httpClient, err := buildHTTPClient(serviceAccountDir+serviceAccountRootCAKey, o.KubernetesInCluster)
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", o.KubernetesInCluster, apiURL, o.ProvideHealthcheck, ingCls)
var sigs chan os.Signal
if o.ProvideHealthcheck {
log.Info("register sigterm handler")
sigs = make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM)
}
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,
}, nil
}
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) (*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
}
tlsConfig := &tls.Config{
MinVersion: tls.VersionTLS12,
RootCAs: certPool,
}
transport := &http.Transport{
TLSClientConfig: tlsConfig,
}
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) getServiceURL(namespace, name string, port backendPort) (string, 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 "", err
}
if s.Spec == nil {
log.Debug("invalid service datagram, missing spec")
return "", errServiceNotFound
}
if p, ok := port.number(); ok {
log.Debugf("service port as number: %d", p)
return fmt.Sprintf("http://%s:%d", s.Spec.ClusterIP, p), nil
}
pn, _ := port.name()
for _, pi := range s.Spec.Ports {
if pi.Name == pn {
log.Debugf("service port found by name: %s -> %d", pn, pi.Port)
return fmt.Sprintf("http://%s:%d", s.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)
}
// 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
}
address, err := c.getServiceURL(
i.Metadata.Namespace,
i.Spec.DefaultBackend.ServiceName,
i.Spec.DefaultBackend.ServicePort,
)
if err != nil {
return nil, false, err
}
r := &eskip.Route{
Id: routeID(i.Metadata.Namespace, i.Metadata.Name, "", "", ""),
Backend: address,
}
return r, true, nil
}
func (c *Client) convertPathRule(ns, name, host string, prule *pathRule, servicesURLs 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)
}
serviceKey := ns + prule.Backend.ServiceName + prule.Backend.ServicePort.String()
var (
address string
err error
)
if val, ok := servicesURLs[serviceKey]; !ok {
address, err = c.getServiceURL(ns, prule.Backend.ServiceName, prule.Backend.ServicePort)
if err != nil {
return nil, err
}
servicesURLs[serviceKey] = address
log.Debugf("New route for %s/%s/%s", ns, prule.Backend.ServiceName, prule.Backend.ServicePort)
} else {
address = val
log.Debugf("Route for %s/%s/%s already known", ns, prule.Backend.ServiceName, prule.Backend.ServicePort)
}
var pathExpressions []string
if prule.Path != "" {
pathExpressions = []string{"^" + prule.Path}
}
r := &eskip.Route{
Id: routeID(ns, name, host, prule.Path, prule.Backend.ServiceName),
PathRegexps: pathExpressions,
Backend: address,
}
// 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 = []*eskip.Predicate{{
Name: traffic.PredicateName,
Args: []interface{}{prule.Backend.Traffic},
}}
log.Debugf("Traffic weight %.2f for backend '%s'", prule.Backend.Traffic, prule.Backend.ServiceName)
}
return r, 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) {
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.Warn("invalid ingress item: missing metadata")
continue
}
if r, ok, err := c.convertDefaultBackend(i); ok {
routes = append(routes, r)
} else if err != nil {
log.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 backend-weihgts 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 {
log.Errorf("error while parsing backend-weights annotation: %v", err)
}
}
// We need this to avoid asking the k8s API for the same services
servicesURLs := make(map[string]string)
for _, rule := range i.Spec.Rules {
if rule.Http == nil {
log.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 {
if prule.Backend.Traffic > 0 {
r, err := c.convertPathRule(i.Metadata.Namespace, i.Metadata.Name, rule.Host, prule, servicesURLs)
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)
}
r.HostRegexps = host
if annotationFilter != "" {
annotationFilters, err := eskip.ParseFilters(annotationFilter)
if err != nil {
log.Errorf("Can not parse annotation filters: %v", err)
} else {
sav := r.Filters[:]
r.Filters = append(annotationFilters, sav...)
}
}
if annotationPredicate != "" {
predicates, err := eskip.ParsePredicates(annotationPredicate)
if err != nil {
log.Errorf("Can not parse annotation predicate: %v", err)
} else {
r.Predicates = predicates
}
}
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
}
// catchAllRoutes returns true if one of the routes in the list has a catchAll
// path expression.
func catchAllRoutes(routes []*eskip.Route) bool {
for _, route := range routes {
if len(route.PathRegexps) == 0 {
return true
}
for _, exp := range route.PathRegexps {
if exp == "^/" {
return true
}
}
}
return false
}
// computeBackendWeights computes and sets the backend traffic weights on the
// rule backends.
// The traffic is calculated based on the following rules:
//
// * if no weight is defined for a backend it will get weight 0.
// * if no weights are specified for all backends of a path, then traffic will
// be distributed equally.
//
// Each traffic weight is relative to the number of backends per path. If there
// are multiple backends per path the weight will be relative to the number of
// remaining backends for the path e.g. if the weight is specified as
//
// backend-1: 0.2
// backend-2: 0.6
// backend-3: 0.2
//
// then the weight will be calculated to:
//
// backend-1: 0.2
// backend-2: 0.75
// backend-3: 1.0
//
// where for a weight of 1.0 no Traffic predicate will be generated.
func computeBackendWeights(backendWeights map[string]float64, rule *rule) {
type sumCount struct {
sum float64
count int
}
// get backend weight sum and count of backends for all paths
pathSumCount := make(map[string]*sumCount)
for _, path := range rule.Http.Paths {
sc, ok := pathSumCount[path.Path]
if !ok {
sc = &sumCount{}
pathSumCount[path.Path] = sc
}
if weight, ok := backendWeights[path.Backend.ServiceName]; ok {
sc.sum += weight
} else {
sc.count++
}
}
// calculate traffic weight for each backend
for _, path := range rule.Http.Paths {
if sc, ok := pathSumCount[path.Path]; ok {
if weight, ok := backendWeights[path.Backend.ServiceName]; ok {
path.Backend.Traffic = weight / sc.sum
// subtract weight from the sum in order to
// give subsequent backends a higher relative
// weight.
sc.sum -= weight
} else if sc.sum == 0 && sc.count > 0 {
path.Backend.Traffic = 1.0 / float64(sc.count)
}
// reduce count by one in order to give subsequent
// backends for the path a higher relative weight.
sc.count--
}
}
}
func mapRoutes(r []*eskip.Route) map[string]*eskip.Route {
m := make(map[string]*eskip.Route)
for _, ri := range r {
m[ri.Id] = ri
}
return m
}
func (c *Client) listRoutes() []*eskip.Route {
l := make([]*eskip.Route, 0, len(c.current))
for _, r := range c.current {
l = append(l, r)
}
return l
}
// filterIngressesByClass will filter only the ingresses that have the valid class, these are
// the defined one, empty string class or not class at all
func (c *Client) filterIngressesByClass(items []*ingressItem) []*ingressItem {
validIngs := []*ingressItem{}
for _, ing := range items {
// No metadata is the same as no annotations for us
if ing.Metadata != nil {
cls, ok := ing.Metadata.Annotations[ingressClassKey]
// Skip loop iteration if not valid ingress (non defined, empty or non defined one)
if ok && cls != "" && !c.ingressClass.MatchString(cls) {
continue
}
}
validIngs = append(validIngs, ing)
}
return validIngs
}
func (c *Client) loadAndConvert() ([]*eskip.Route, error) {
var il ingressList
log.Debugf("requesting ingresses")
if err := c.getJSON(ingressesURI, &il); err != nil {
log.Debugf("requesting all ingresses failed: %v", err)
return nil, err
}
log.Debugf("all ingresses received: %d", len(il.Items))
fItems := c.filterIngressesByClass(il.Items)
log.Debugf("filtered ingresses by ingress class: %d", len(fItems))
r, err := c.ingressToRoutes(fItems)
if err != nil {
log.Debugf("converting ingresses to routes failed: %v", err)
return nil, err
}
log.Debugf("all routes created: %d", len(r))
return r, nil
}
func healthcheckRoute(healthy, reverseSourcePredicate bool) *eskip.Route {
status := http.StatusOK
if !healthy {
status = http.StatusServiceUnavailable
}
var p []*eskip.Predicate
if reverseSourcePredicate {
p = []*eskip.Predicate{{
Name: source.NameLast,
Args: internalIPs,
}}
} else {
p = []*eskip.Predicate{{
Name: source.Name,
Args: internalIPs,
}}
}
return &eskip.Route{
Id: healthcheckRouteID,
Predicates: p,
Path: healthcheckPath,
Filters: []*eskip.Filter{{
Name: builtin.StatusName,
Args: []interface{}{status}},
},
Shunt: true,
}
}
func httpRedirectRoute() *eskip.Route {
// the forwarded port and any-path (.*) is set to make sure that
// the redirect route has a higher priority during matching than
// the normal routes that may have max 2 predicates: path regexp
// and host.
return &eskip.Route{
Id: httpRedirectRouteID,
Headers: map[string]string{
"X-Forwarded-Proto": "http",
},
HeaderRegexps: map[string][]string{
"X-Forwarded-Port": {".*"},
},
PathRegexps: []string{".*"},
Filters: []*eskip.Filter{{
Name: "redirectTo",
Args: []interface{}{float64(301), "https:"},
}},
Shunt: true,
}
}
func (c *Client) hasReceivedTerm() bool {
select {
case s := <-c.sigs:
log.Infof("shutdown, caused by %s, set health check to be unhealthy", s)
c.termReceived = true
default:
}
return c.termReceived
}
func (c *Client) LoadAll() ([]*eskip.Route, error) {
log.Debug("loading all")
r, err := c.loadAndConvert()
if err != nil {
log.Errorf("failed to load all: %v", err)
return nil, err
}
// teardown handling: always healthy unless SIGTERM received
if c.provideHealthcheck {
healthy := !c.hasReceivedTerm()
r = append(r, healthcheckRoute(healthy, c.reverseSourcePredicate))
}
if c.provideHTTPSRedirect {
r = append(r, httpRedirectRoute())
}
c.current = mapRoutes(r)
log.Debugf("all routes loaded and mapped")
return r, nil
}
// TODO: implement a force reset after some time
func (c *Client) LoadUpdate() ([]*eskip.Route, []string, error) {
log.Debugf("polling for updates")
r, err := c.loadAndConvert()
if err != nil {
log.Errorf("polling for updates failed: %v", err)
return nil, nil, err
}
next := mapRoutes(r)
log.Debugf("next version of routes loaded and mapped")
var (
updatedRoutes []*eskip.Route
deletedIDs []string
)
for id := range c.current {
if r, ok := next[id]; ok && r.String() != c.current[id].String() {
updatedRoutes = append(updatedRoutes, r)
} else if !ok && id != healthcheckRouteID && id != httpRedirectRouteID {
deletedIDs = append(deletedIDs, id)
}
}
for id, r := range next {
if _, ok := c.current[id]; !ok {
updatedRoutes = append(updatedRoutes, r)
}
}
log.Debugf("diff taken, inserts/updates: %d, deletes: %d", len(updatedRoutes), len(deletedIDs))
// teardown handling: always healthy unless SIGTERM received
if c.provideHealthcheck {
healthy := !c.hasReceivedTerm()
hc := healthcheckRoute(healthy, c.reverseSourcePredicate)
next[healthcheckRouteID] = hc
updatedRoutes = append(updatedRoutes, hc)
}
c.current = next
return updatedRoutes, deletedIDs, nil
}