Skip to content

Commit 54a10c9

Browse files
kkk777-7rudrakhp
authored andcommitted
feat: support both local and global ratelimit simultaneously (#7334)
* update rate limit type Signed-off-by: kkk777-7 <[email protected]> * feat: support both type rate limit Signed-off-by: kkk777-7 <[email protected]>
1 parent b2d0c6c commit 54a10c9

25 files changed

+846
-156
lines changed

api/v1alpha1/ratelimit_types.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import (
1010
)
1111

1212
// RateLimitSpec defines the desired state of RateLimitSpec.
13-
// +union
1413
type RateLimitSpec struct {
1514
// Type decides the scope for the RateLimits.
1615
// Valid RateLimitType values are "Global" or "Local".
1716
//
18-
// +unionDiscriminator
19-
Type RateLimitType `json:"type"`
17+
// Deprecated: Use Global and/or Local fields directly instead. Both can be specified simultaneously for combined rate limiting.
18+
//
19+
// +optional
20+
Type *RateLimitType `json:"type,omitempty"`
2021
// Global defines global rate limit configuration.
2122
//
2223
// +optional

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

charts/gateway-crds-helm/templates/generated/gateway.envoyproxy.io_backendtrafficpolicies.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,12 +1544,12 @@ spec:
15441544
description: |-
15451545
Type decides the scope for the RateLimits.
15461546
Valid RateLimitType values are "Global" or "Local".
1547+
1548+
Deprecated: Use Global and/or Local fields directly instead. Both can be specified simultaneously for combined rate limiting.
15471549
enum:
15481550
- Global
15491551
- Local
15501552
type: string
1551-
required:
1552-
- type
15531553
type: object
15541554
requestBuffer:
15551555
description: |-

charts/gateway-helm/crds/generated/gateway.envoyproxy.io_backendtrafficpolicies.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,12 +1543,12 @@ spec:
15431543
description: |-
15441544
Type decides the scope for the RateLimits.
15451545
Valid RateLimitType values are "Global" or "Local".
1546+
1547+
Deprecated: Use Global and/or Local fields directly instead. Both can be specified simultaneously for combined rate limiting.
15461548
enum:
15471549
- Global
15481550
- Local
15491551
type: string
1550-
required:
1551-
- type
15521552
type: object
15531553
requestBuffer:
15541554
description: |-

internal/gatewayapi/backendtrafficpolicy.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,14 +1037,18 @@ func (t *Translator) translateBackendTrafficPolicyForGateway(
10371037
}
10381038

10391039
func (t *Translator) buildRateLimit(policy *egv1a1.BackendTrafficPolicy) (*ir.RateLimit, error) {
1040-
switch policy.Spec.RateLimit.Type {
1041-
case egv1a1.GlobalRateLimitType:
1042-
return t.buildGlobalRateLimit(policy)
1043-
case egv1a1.LocalRateLimitType:
1044-
return t.buildLocalRateLimit(policy)
1040+
// For backward compatibility, process the deprecated Type field if specified.
1041+
if policy.Spec.RateLimit.Type != nil {
1042+
switch *policy.Spec.RateLimit.Type {
1043+
case egv1a1.GlobalRateLimitType:
1044+
return t.buildGlobalRateLimit(policy)
1045+
case egv1a1.LocalRateLimitType:
1046+
return t.buildLocalRateLimit(policy)
1047+
}
1048+
return nil, fmt.Errorf("invalid rateLimit type: %s", *policy.Spec.RateLimit.Type)
10451049
}
10461050

1047-
return nil, fmt.Errorf("invalid rateLimit type: %s", policy.Spec.RateLimit.Type)
1051+
return t.buildBothRateLimit(policy)
10481052
}
10491053

10501054
func (t *Translator) buildLocalRateLimit(policy *egv1a1.BackendTrafficPolicy) (*ir.RateLimit, error) {
@@ -1147,6 +1151,35 @@ func (t *Translator) buildGlobalRateLimit(policy *egv1a1.BackendTrafficPolicy) (
11471151
return rateLimit, nil
11481152
}
11491153

1154+
func (t *Translator) buildBothRateLimit(policy *egv1a1.BackendTrafficPolicy) (*ir.RateLimit, error) {
1155+
var (
1156+
localRateLimit *ir.RateLimit
1157+
globalRateLimit *ir.RateLimit
1158+
err error
1159+
)
1160+
1161+
if policy.Spec.RateLimit.Local != nil {
1162+
localRateLimit, err = t.buildLocalRateLimit(policy)
1163+
if err != nil {
1164+
return nil, err
1165+
}
1166+
}
1167+
if policy.Spec.RateLimit.Global != nil {
1168+
globalRateLimit, err = t.buildGlobalRateLimit(policy)
1169+
if err != nil {
1170+
return nil, err
1171+
}
1172+
}
1173+
rl := &ir.RateLimit{}
1174+
if localRateLimit != nil && localRateLimit.Local != nil {
1175+
rl.Local = localRateLimit.Local
1176+
}
1177+
if globalRateLimit != nil && globalRateLimit.Global != nil {
1178+
rl.Global = globalRateLimit.Global
1179+
}
1180+
return rl, nil
1181+
}
1182+
11501183
func buildRateLimitRule(rule egv1a1.RateLimitRule) (*ir.RateLimitRule, error) {
11511184
irRule := &ir.RateLimitRule{
11521185
Limit: ir.RateLimitValue{
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
gateways:
2+
- apiVersion: gateway.networking.k8s.io/v1
3+
kind: Gateway
4+
metadata:
5+
namespace: envoy-gateway
6+
name: gateway-1
7+
spec:
8+
gatewayClassName: envoy-gateway-class
9+
listeners:
10+
- name: http
11+
protocol: HTTP
12+
port: 80
13+
allowedRoutes:
14+
namespaces:
15+
from: All
16+
grpcRoutes:
17+
- apiVersion: gateway.networking.k8s.io/v1alpha2
18+
kind: GRPCRoute
19+
metadata:
20+
namespace: default
21+
name: grpcroute-1
22+
spec:
23+
parentRefs:
24+
- namespace: envoy-gateway
25+
name: gateway-1
26+
sectionName: http
27+
rules:
28+
- backendRefs:
29+
- name: service-1
30+
port: 8080
31+
backendTrafficPolicies:
32+
- apiVersion: gateway.envoyproxy.io/v1alpha1
33+
kind: BackendTrafficPolicy
34+
metadata:
35+
namespace: default
36+
name: policy-for-grcp-route
37+
spec:
38+
targetRef:
39+
group: gateway.networking.k8s.io
40+
kind: GRPCRoute
41+
name: grpcroute-1
42+
rateLimit:
43+
global:
44+
rules:
45+
- clientSelectors:
46+
- sourceCIDR:
47+
type: "Distinct"
48+
value: 192.168.0.0/16
49+
limit:
50+
requests: 20
51+
unit: Hour
52+
local:
53+
rules:
54+
- clientSelectors:
55+
- headers:
56+
- name: x-user-id
57+
value: one
58+
- name: x-org-id
59+
value: foo
60+
limit:
61+
requests: 10
62+
unit: Hour

0 commit comments

Comments
 (0)