2016-05-27 21:03:54 +00:00
|
|
|
/*
|
2016-09-08 11:02:39 +00:00
|
|
|
Copyright 2016 The Kubernetes Authors.
|
2016-05-27 21:03:54 +00:00
|
|
|
|
|
|
|
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 ratelimit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-08-21 19:36:31 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2016-11-10 22:56:29 +00:00
|
|
|
|
2017-07-16 19:19:59 +00:00
|
|
|
extensions "k8s.io/api/extensions/v1beta1"
|
2016-11-16 18:24:26 +00:00
|
|
|
|
2017-08-23 00:47:29 +00:00
|
|
|
"k8s.io/ingress/core/pkg/base64"
|
2016-11-16 18:24:26 +00:00
|
|
|
"k8s.io/ingress/core/pkg/ingress/annotations/parser"
|
2017-08-13 06:52:20 +00:00
|
|
|
"k8s.io/ingress/core/pkg/ingress/resolver"
|
2017-08-21 19:36:31 +00:00
|
|
|
"k8s.io/ingress/core/pkg/net"
|
2016-05-27 21:03:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-08-13 06:52:20 +00:00
|
|
|
limitIP = "ingress.kubernetes.io/limit-connections"
|
|
|
|
limitRPS = "ingress.kubernetes.io/limit-rps"
|
|
|
|
limitRPM = "ingress.kubernetes.io/limit-rpm"
|
|
|
|
limitRATE = "ingress.kubernetes.io/limit-rate"
|
|
|
|
limitRATEAFTER = "ingress.kubernetes.io/limit-rate-after"
|
2017-08-21 19:36:31 +00:00
|
|
|
limitWhitelist = "ingress.kubernetes.io/limit-whitelist"
|
2016-05-27 21:03:54 +00:00
|
|
|
|
|
|
|
// allow 5 times the specified limit as burst
|
|
|
|
defBurst = 5
|
|
|
|
|
|
|
|
// 1MB -> 16 thousand 64-byte states or about 8 thousand 128-byte states
|
|
|
|
// default is 5MB
|
|
|
|
defSharedSize = 5
|
|
|
|
)
|
|
|
|
|
2017-03-07 14:41:05 +00:00
|
|
|
// RateLimit returns rate limit configuration for an Ingress rule limiting the
|
|
|
|
// number of connections per IP address and/or connections per second.
|
|
|
|
// If you both annotations are specified in a single Ingress rule, RPS limits
|
|
|
|
// takes precedence
|
2016-05-27 21:03:54 +00:00
|
|
|
type RateLimit struct {
|
|
|
|
// Connections indicates a limit with the number of connections per IP address
|
2016-11-16 18:24:26 +00:00
|
|
|
Connections Zone `json:"connections"`
|
2016-05-27 21:03:54 +00:00
|
|
|
// RPS indicates a limit with the number of connections per second
|
2016-11-16 18:24:26 +00:00
|
|
|
RPS Zone `json:"rps"`
|
2017-08-02 03:24:48 +00:00
|
|
|
|
|
|
|
RPM Zone `json:"rpm"`
|
2017-08-13 06:52:20 +00:00
|
|
|
|
|
|
|
LimitRate int `json:"limit-rate"`
|
|
|
|
|
|
|
|
LimitRateAfter int `json:"limit-rate-after"`
|
2017-08-21 19:36:31 +00:00
|
|
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
2017-08-23 00:47:29 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
|
2017-08-21 19:36:31 +00:00
|
|
|
Whitelist []string `json:"whitelist"`
|
2016-05-27 21:03:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-27 03:07:59 +00:00
|
|
|
// Equal tests for equality between two RateLimit types
|
2017-06-14 21:33:12 +00:00
|
|
|
func (rt1 *RateLimit) Equal(rt2 *RateLimit) bool {
|
|
|
|
if rt1 == rt2 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if rt1 == nil || rt2 == nil {
|
|
|
|
return false
|
|
|
|
}
|
2017-06-14 23:40:25 +00:00
|
|
|
if !(&rt1.Connections).Equal(&rt2.Connections) {
|
2017-06-14 21:33:12 +00:00
|
|
|
return false
|
|
|
|
}
|
2017-08-02 03:24:48 +00:00
|
|
|
if !(&rt1.RPM).Equal(&rt2.RPM) {
|
|
|
|
return false
|
|
|
|
}
|
2017-06-14 23:40:25 +00:00
|
|
|
if !(&rt1.RPS).Equal(&rt2.RPS) {
|
2017-06-14 21:33:12 +00:00
|
|
|
return false
|
|
|
|
}
|
2017-08-13 06:52:20 +00:00
|
|
|
if rt1.LimitRate != rt2.LimitRate {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if rt1.LimitRateAfter != rt2.LimitRateAfter {
|
|
|
|
return false
|
|
|
|
}
|
2017-08-22 20:16:59 +00:00
|
|
|
if rt1.ID != rt2.ID {
|
|
|
|
return false
|
|
|
|
}
|
2017-08-21 19:36:31 +00:00
|
|
|
if rt1.Name != rt2.Name {
|
|
|
|
return false
|
|
|
|
}
|
2017-08-22 20:16:59 +00:00
|
|
|
if len(rt1.Whitelist) != len(rt2.Whitelist) {
|
|
|
|
return false
|
|
|
|
}
|
2017-08-21 19:36:31 +00:00
|
|
|
|
|
|
|
for _, r1l := range rt1.Whitelist {
|
|
|
|
found := false
|
|
|
|
for _, rl2 := range rt2.Whitelist {
|
|
|
|
if r1l == rl2 {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2017-06-14 21:33:12 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-05-30 17:39:10 +00:00
|
|
|
// Zone returns information about the NGINX rate limit (limit_req_zone)
|
|
|
|
// http://nginx.org/en/docs/http/ngx_http_limit_req_module.html#limit_req_zone
|
2016-05-27 21:03:54 +00:00
|
|
|
type Zone struct {
|
2016-11-16 18:24:26 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Limit int `json:"limit"`
|
|
|
|
Burst int `json:"burst"`
|
2016-05-27 21:03:54 +00:00
|
|
|
// SharedSize amount of shared memory for the zone
|
2016-11-16 18:24:26 +00:00
|
|
|
SharedSize int `json:"sharedSize"`
|
2016-05-27 21:03:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-27 03:07:59 +00:00
|
|
|
// Equal tests for equality between two Zone types
|
2017-06-14 21:33:12 +00:00
|
|
|
func (z1 *Zone) Equal(z2 *Zone) bool {
|
|
|
|
if z1 == z2 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if z1 == nil || z2 == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if z1.Name != z2.Name {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if z1.Limit != z2.Limit {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if z1.Burst != z2.Burst {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if z1.SharedSize != z2.SharedSize {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-12-29 20:02:06 +00:00
|
|
|
type ratelimit struct {
|
2017-08-13 06:52:20 +00:00
|
|
|
backendResolver resolver.DefaultBackend
|
2016-12-29 20:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewParser creates a new ratelimit annotation parser
|
2017-08-13 06:52:20 +00:00
|
|
|
func NewParser(br resolver.DefaultBackend) parser.IngressAnnotation {
|
|
|
|
return ratelimit{br}
|
2016-12-29 20:02:06 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 21:03:54 +00:00
|
|
|
// ParseAnnotations parses the annotations contained in the ingress
|
|
|
|
// rule used to rewrite the defined paths
|
2016-12-29 20:02:06 +00:00
|
|
|
func (a ratelimit) Parse(ing *extensions.Ingress) (interface{}, error) {
|
2017-08-13 06:52:20 +00:00
|
|
|
defBackend := a.backendResolver.GetDefaultBackend()
|
|
|
|
lr, err := parser.GetIntAnnotation(limitRATE, ing)
|
|
|
|
if err != nil {
|
|
|
|
lr = defBackend.LimitRate
|
|
|
|
}
|
|
|
|
lra, err := parser.GetIntAnnotation(limitRATEAFTER, ing)
|
|
|
|
if err != nil {
|
|
|
|
lra = defBackend.LimitRateAfter
|
|
|
|
}
|
2016-05-27 21:03:54 +00:00
|
|
|
|
2017-08-02 03:24:48 +00:00
|
|
|
rpm, _ := parser.GetIntAnnotation(limitRPM, ing)
|
2016-11-10 22:56:29 +00:00
|
|
|
rps, _ := parser.GetIntAnnotation(limitRPS, ing)
|
|
|
|
conn, _ := parser.GetIntAnnotation(limitIP, ing)
|
2016-05-27 21:03:54 +00:00
|
|
|
|
2017-08-21 19:36:31 +00:00
|
|
|
val, _ := parser.GetStringAnnotation(limitWhitelist, ing)
|
|
|
|
|
|
|
|
cidrs, err := parseCIDRs(val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-08-02 03:24:48 +00:00
|
|
|
if rpm == 0 && rps == 0 && conn == 0 {
|
2016-05-27 21:03:54 +00:00
|
|
|
return &RateLimit{
|
2017-08-13 06:52:20 +00:00
|
|
|
Connections: Zone{},
|
|
|
|
RPS: Zone{},
|
|
|
|
RPM: Zone{},
|
|
|
|
LimitRate: lr,
|
|
|
|
LimitRateAfter: lra,
|
2016-12-29 20:02:06 +00:00
|
|
|
}, nil
|
2016-05-27 21:03:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
zoneName := fmt.Sprintf("%v_%v", ing.GetNamespace(), ing.GetName())
|
|
|
|
|
|
|
|
return &RateLimit{
|
|
|
|
Connections: Zone{
|
|
|
|
Name: fmt.Sprintf("%v_conn", zoneName),
|
|
|
|
Limit: conn,
|
|
|
|
Burst: conn * defBurst,
|
|
|
|
SharedSize: defSharedSize,
|
|
|
|
},
|
|
|
|
RPS: Zone{
|
|
|
|
Name: fmt.Sprintf("%v_rps", zoneName),
|
|
|
|
Limit: rps,
|
2017-01-23 02:01:51 +00:00
|
|
|
Burst: rps * defBurst,
|
2016-05-27 21:03:54 +00:00
|
|
|
SharedSize: defSharedSize,
|
|
|
|
},
|
2017-08-02 03:24:48 +00:00
|
|
|
RPM: Zone{
|
|
|
|
Name: fmt.Sprintf("%v_rpm", zoneName),
|
|
|
|
Limit: rpm,
|
|
|
|
Burst: rpm * defBurst,
|
|
|
|
SharedSize: defSharedSize,
|
|
|
|
},
|
2017-08-13 06:52:20 +00:00
|
|
|
LimitRate: lr,
|
|
|
|
LimitRateAfter: lra,
|
2017-08-21 19:36:31 +00:00
|
|
|
Name: zoneName,
|
2017-08-23 18:40:57 +00:00
|
|
|
ID: base64.Encode(zoneName),
|
2017-08-21 19:36:31 +00:00
|
|
|
Whitelist: cidrs,
|
2016-05-27 21:03:54 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2017-08-21 19:36:31 +00:00
|
|
|
|
|
|
|
func parseCIDRs(s string) ([]string, error) {
|
|
|
|
if s == "" {
|
|
|
|
return []string{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
values := strings.Split(s, ",")
|
|
|
|
|
|
|
|
ipnets, ips, err := net.ParseIPNets(values...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cidrs := []string{}
|
|
|
|
for k := range ipnets {
|
|
|
|
cidrs = append(cidrs, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
for k := range ips {
|
|
|
|
cidrs = append(cidrs, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(cidrs)
|
|
|
|
|
|
|
|
return cidrs, nil
|
|
|
|
}
|