Skip to content

Commit 522705d

Browse files
committed
feat: add query parameter match support in ratelimit
Signed-off-by: sachin maurya <[email protected]>
1 parent b6a8d27 commit 522705d

23 files changed

+681
-1
lines changed

api/v1alpha1/ratelimit_types.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,19 @@ type RateLimitSelectCondition struct {
197197
//
198198
// +optional
199199
SourceCIDR *SourceMatch `json:"sourceCIDR,omitempty"`
200+
201+
// Rate limit on query parameters.
202+
// +optional
203+
QueryParameters *QueryParameters `json:"queryParameters,omitempty"`
204+
}
205+
206+
type QueryParameters struct {
207+
// The name of the query parameter to use for rate limiting.
208+
// Value of this query parameter is used to populate the value of the descriptor entry for the descriptor_key.
209+
QueryParameterName string `json:"queryParameterName,omitempty"`
210+
// The key to use when creating the rate limit descriptor entry.
211+
// This descriptor key will be used to identify the rate limit rule in the rate limiting service.
212+
DescriptorKey string `json:"descriptorKey,omitempty"`
200213
}
201214

202215
// +kubebuilder:validation:Enum=Exact;Distinct

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 20 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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,20 @@ spec:
959959
type: object
960960
maxItems: 16
961961
type: array
962+
queryParameters:
963+
description: Rate limit on query parameters.
964+
properties:
965+
descriptorKey:
966+
description: |-
967+
The key to use when creating the rate limit descriptor entry.
968+
This descriptor key will be used to identify the rate limit rule in the rate limiting service.
969+
type: string
970+
queryParameterName:
971+
description: |-
972+
The name of the query parameter to use for rate limiting.
973+
Value of this query parameter is used to populate the value of the descriptor entry for the descriptor_key.
974+
type: string
975+
type: object
962976
sourceCIDR:
963977
description: |-
964978
SourceCIDR is the client IP Address range to match on.
@@ -1210,6 +1224,20 @@ spec:
12101224
type: object
12111225
maxItems: 16
12121226
type: array
1227+
queryParameters:
1228+
description: Rate limit on query parameters.
1229+
properties:
1230+
descriptorKey:
1231+
description: |-
1232+
The key to use when creating the rate limit descriptor entry.
1233+
This descriptor key will be used to identify the rate limit rule in the rate limiting service.
1234+
type: string
1235+
queryParameterName:
1236+
description: |-
1237+
The name of the query parameter to use for rate limiting.
1238+
Value of this query parameter is used to populate the value of the descriptor entry for the descriptor_key.
1239+
type: string
1240+
type: object
12131241
sourceCIDR:
12141242
description: |-
12151243
SourceCIDR is the client IP Address range to match on.

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,20 @@ spec:
958958
type: object
959959
maxItems: 16
960960
type: array
961+
queryParameters:
962+
description: Rate limit on query parameters.
963+
properties:
964+
descriptorKey:
965+
description: |-
966+
The key to use when creating the rate limit descriptor entry.
967+
This descriptor key will be used to identify the rate limit rule in the rate limiting service.
968+
type: string
969+
queryParameterName:
970+
description: |-
971+
The name of the query parameter to use for rate limiting.
972+
Value of this query parameter is used to populate the value of the descriptor entry for the descriptor_key.
973+
type: string
974+
type: object
961975
sourceCIDR:
962976
description: |-
963977
SourceCIDR is the client IP Address range to match on.
@@ -1209,6 +1223,20 @@ spec:
12091223
type: object
12101224
maxItems: 16
12111225
type: array
1226+
queryParameters:
1227+
description: Rate limit on query parameters.
1228+
properties:
1229+
descriptorKey:
1230+
description: |-
1231+
The key to use when creating the rate limit descriptor entry.
1232+
This descriptor key will be used to identify the rate limit rule in the rate limiting service.
1233+
type: string
1234+
queryParameterName:
1235+
description: |-
1236+
The name of the query parameter to use for rate limiting.
1237+
Value of this query parameter is used to populate the value of the descriptor entry for the descriptor_key.
1238+
type: string
1239+
type: object
12121240
sourceCIDR:
12131241
description: |-
12141242
SourceCIDR is the client IP Address range to match on.

internal/gatewayapi/backendtrafficpolicy.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,17 @@ func buildRateLimitRule(rule egv1a1.RateLimitRule) (*ir.RateLimitRule, error) {
950950
cidrMatch.Distinct = distinct
951951
irRule.CIDRMatch = cidrMatch
952952
}
953+
954+
if match.QueryParameters != nil {
955+
// Validate QueryParameters
956+
if match.QueryParameters.QueryParameterName == "" {
957+
return nil, fmt.Errorf("queryParameterName is required when QueryParameters is specified")
958+
}
959+
if match.QueryParameters.DescriptorKey == "" {
960+
return nil, fmt.Errorf("descriptorKey is required when QueryParameters is specified")
961+
}
962+
irRule.QueryParameters = (*ir.QueryParameters)(match.QueryParameters)
963+
}
953964
}
954965

955966
if cost := rule.Cost; cost != nil {

internal/ir/xds.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,6 +2239,8 @@ type RateLimitRule struct {
22392239
HeaderMatches []*StringMatch `json:"headerMatches" yaml:"headerMatches"`
22402240
// CIDRMatch define the match conditions on the source IP's CIDR for this route.
22412241
CIDRMatch *CIDRMatch `json:"cidrMatch,omitempty" yaml:"cidrMatch,omitempty"`
2242+
// Rate limit on query parameters.
2243+
QueryParameters *QueryParameters `json:"queryParameters,omitempty" yaml:"queryParameters,omitempty"`
22422244
// Limit holds the rate limit values.
22432245
Limit RateLimitValue `json:"limit,omitempty" yaml:"limit,omitempty"`
22442246
// RequestCost specifies the cost of the request.
@@ -2256,6 +2258,12 @@ type RateLimitRule struct {
22562258
Name string `json:"name,omitempty" yaml:"name,omitempty"`
22572259
}
22582260

2261+
// +k8s:deepcopy-gen=true
2262+
type QueryParameters struct {
2263+
QueryParameterName string `json:"queryParameterName,omitempty" yaml:"queryParameterName,omitempty"`
2264+
DescriptorKey string `json:"descriptorKey,omitempty" yaml:"descriptorKey,omitempty"`
2265+
}
2266+
22592267
// RateLimitCost specifies the cost of the request or response.
22602268
// +k8s:deepcopy-gen=true
22612269
type RateLimitCost struct {

internal/ir/zz_generated.deepcopy.go

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

internal/xds/translator/local_ratelimit.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,17 @@ func buildRouteLocalRateLimits(local *ir.LocalRateLimit) (
304304
rlActions = append(rlActions, action)
305305
}
306306
}
307+
if rule.QueryParameters != nil {
308+
queryParam := &routev3.RateLimit_Action_QueryParameters{}
309+
queryParam.DescriptorKey = rule.QueryParameters.DescriptorKey
310+
queryParam.QueryParameterName = rule.QueryParameters.QueryParameterName
311+
action := &routev3.RateLimit_Action{
312+
ActionSpecifier: &routev3.RateLimit_Action_QueryParameters_{
313+
QueryParameters: queryParam,
314+
},
315+
}
316+
rlActions = append(rlActions, action)
317+
}
307318

308319
rateLimit := &routev3.RateLimit{Actions: rlActions}
309320
rateLimits = append(rateLimits, rateLimit)

internal/xds/translator/ratelimit.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,18 @@ func buildRouteRateLimits(route *ir.HTTPRoute) (rateLimits []*routev3.RateLimit,
336336
}
337337
}
338338

339+
if rule.QueryParameters != nil {
340+
queryParam := &routev3.RateLimit_Action_QueryParameters{}
341+
queryParam.DescriptorKey = rule.QueryParameters.DescriptorKey
342+
queryParam.QueryParameterName = rule.QueryParameters.QueryParameterName
343+
action := &routev3.RateLimit_Action{
344+
ActionSpecifier: &routev3.RateLimit_Action_QueryParameters_{
345+
QueryParameters: queryParam,
346+
},
347+
}
348+
rlActions = append(rlActions, action)
349+
}
350+
339351
// Case when both header and cidr match are not set and the ratelimit
340352
// will be applied to all traffic.
341353
// 3) No Match (apply to all traffic)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
http:
2+
- name: "first-listener"
3+
address: "0.0.0.0"
4+
port: 10080
5+
hostnames:
6+
- "*"
7+
path:
8+
mergeSlashes: true
9+
escapedSlashesAction: UnescapeAndRedirect
10+
routes:
11+
- name: "first-route"
12+
traffic:
13+
rateLimit:
14+
global:
15+
rules:
16+
- name: "test-namespace/test-policy-1/rule/0"
17+
headerMatches:
18+
- name: "x-user-id"
19+
exact: "one"
20+
queryParameters:
21+
queryParameterName: "user"
22+
descriptorKey: "user_param"
23+
limit:
24+
requests: 5
25+
unit: second
26+
shared: false
27+
pathMatch:
28+
exact: "foo/bar"
29+
destination:
30+
name: "first-route-dest"
31+
settings:
32+
- endpoints:
33+
- host: "1.2.3.4"
34+
port: 50000

0 commit comments

Comments
 (0)