Skip to content

Commit

Permalink
enable rate limit for month and year
Browse files Browse the repository at this point in the history
Signed-off-by: Rico Pahlisch <[email protected]>
  • Loading branch information
rpahli committed Jan 13, 2025
1 parent 00ed0b8 commit 80431d7
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 7 deletions.
10 changes: 8 additions & 2 deletions api/v1alpha1/ratelimit_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,9 @@ type RateLimitValue struct {
}

// RateLimitUnit specifies the intervals for setting rate limits.
// Valid RateLimitUnit values are "Second", "Minute", "Hour", and "Day".
// Valid RateLimitUnit values are "Second", "Minute", "Hour", "Day", "Month" and "Year".
//
// +kubebuilder:validation:Enum=Second;Minute;Hour;Day
// +kubebuilder:validation:Enum=Second;Minute;Hour;Day;Month;Year
type RateLimitUnit string

// RateLimitUnit constants.
Expand All @@ -302,4 +302,10 @@ const (

// RateLimitUnitDay specifies the rate limit interval to be 1 day.
RateLimitUnitDay RateLimitUnit = "Day"

// RateLimitUnitMonth specifies the rate limit interval to be 1 month.
RateLimitUnitMonth RateLimitUnit = "Month"

// RateLimitUnitYear specifies the rate limit interval to be 1 year.
RateLimitUnitYear RateLimitUnit = "Year"
)
Original file line number Diff line number Diff line change
Expand Up @@ -872,12 +872,14 @@ spec:
unit:
description: |-
RateLimitUnit specifies the intervals for setting rate limits.
Valid RateLimitUnit values are "Second", "Minute", "Hour", and "Day".
Valid RateLimitUnit values are "Second", "Minute", "Hour", "Day", "Month" and "Year".
enum:
- Second
- Minute
- Hour
- Day
- Month
- Year
type: string
required:
- requests
Expand Down Expand Up @@ -1113,12 +1115,14 @@ spec:
unit:
description: |-
RateLimitUnit specifies the intervals for setting rate limits.
Valid RateLimitUnit values are "Second", "Minute", "Hour", and "Day".
Valid RateLimitUnit values are "Second", "Minute", "Hour", "Day", "Month" and "Year".
enum:
- Second
- Minute
- Hour
- Day
- Month
- Year
type: string
required:
- requests
Expand Down
4 changes: 4 additions & 0 deletions internal/utils/ratelimit/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func UnitToSeconds(unit egv1a1.RateLimitUnit) int64 {
seconds = 60 * 60
case egv1a1.RateLimitUnitDay:
seconds = 60 * 60 * 24
case egv1a1.RateLimitUnitMonth:
seconds = 60 * 60 * 24 * 30
case egv1a1.RateLimitUnitYear:
seconds = 60 * 60 * 24 * 365
}
return seconds
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: "first-listener"
address: "0.0.0.0"
port: 10080
hostnames:
- "*"
path:
mergeSlashes: true
escapedSlashesAction: UnescapeAndRedirect
routes:
- name: "first-route"
traffic:
rateLimit:
global:
rules:
- headerMatches:
- name: "x-user-id"
exact: "one"
limit:
requests: 5
unit: month
pathMatch:
exact: "foo/bar"
destination:
name: "first-route-dest"
settings:
- endpoints:
- host: "1.2.3.4"
port: 50000
- name: "second-route"
traffic:
rateLimit:
global:
rules:
- headerMatches:

Check failure on line 34 in internal/xds/translator/testdata/in/ratelimit-config/month-year-rule.yaml

View workflow job for this annotation

GitHub Actions / lint

34:11 [indentation] wrong indentation: expected 8 but found 10

Check failure on line 34 in internal/xds/translator/testdata/in/ratelimit-config/month-year-rule.yaml

View workflow job for this annotation

GitHub Actions / lint

34:11 [indentation] wrong indentation: expected 8 but found 10
- name: "x-user-id"

Check failure on line 35 in internal/xds/translator/testdata/in/ratelimit-config/month-year-rule.yaml

View workflow job for this annotation

GitHub Actions / lint

35:15 [indentation] wrong indentation: expected 12 but found 14

Check failure on line 35 in internal/xds/translator/testdata/in/ratelimit-config/month-year-rule.yaml

View workflow job for this annotation

GitHub Actions / lint

35:15 [indentation] wrong indentation: expected 12 but found 14
exact: "two"
limit:
requests: 1
unit: year
pathMatch:
exact: "foo/foo"
destination:
name: "second-route-dest"
settings:
- endpoints:

Check failure on line 45 in internal/xds/translator/testdata/in/ratelimit-config/month-year-rule.yaml

View workflow job for this annotation

GitHub Actions / lint

45:7 [indentation] wrong indentation: expected 4 but found 6

Check failure on line 45 in internal/xds/translator/testdata/in/ratelimit-config/month-year-rule.yaml

View workflow job for this annotation

GitHub Actions / lint

45:7 [indentation] wrong indentation: expected 4 but found 6
- host: "1.2.3.4"

Check failure on line 46 in internal/xds/translator/testdata/in/ratelimit-config/month-year-rule.yaml

View workflow job for this annotation

GitHub Actions / lint

46:11 [indentation] wrong indentation: expected 8 but found 10

Check failure on line 46 in internal/xds/translator/testdata/in/ratelimit-config/month-year-rule.yaml

View workflow job for this annotation

GitHub Actions / lint

46:11 [indentation] wrong indentation: expected 8 but found 10
port: 50000
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: first-listener
domain: first-listener
descriptors:
- key: first-route
value: first-route
rate_limit: null
descriptors:
- key: rule-0-match-0
value: rule-0-match-0
rate_limit:
requests_per_unit: 5
unit: MONTH
unlimited: false
name: ""
replaces: []
descriptors: []
shadow_mode: false
detailed_metric: false
shadow_mode: false
detailed_metric: false
- key: second-route
value: second-route
rate_limit: null
descriptors:
- key: rule-0-match-0
value: rule-0-match-0
rate_limit:
requests_per_unit: 1
unit: YEAR
unlimited: false
name: ""
replaces: []
descriptors: []
shadow_mode: false
detailed_metric: false
shadow_mode: false
detailed_metric: false
4 changes: 3 additions & 1 deletion site/content/en/latest/api/extension_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -3652,7 +3652,7 @@ _Appears in:_
_Underlying type:_ _string_

RateLimitUnit specifies the intervals for setting rate limits.
Valid RateLimitUnit values are "Second", "Minute", "Hour", and "Day".
Valid RateLimitUnit values are "Second", "Minute", "Hour", "Day", "Month" and "Year".

_Appears in:_
- [RateLimitValue](#ratelimitvalue)
Expand All @@ -3663,6 +3663,8 @@ _Appears in:_
| `Minute` | RateLimitUnitMinute specifies the rate limit interval to be 1 minute.<br /> |
| `Hour` | RateLimitUnitHour specifies the rate limit interval to be 1 hour.<br /> |
| `Day` | RateLimitUnitDay specifies the rate limit interval to be 1 day.<br /> |
| `Month` | RateLimitUnitDay specifies the rate limit interval to be 1 month.<br /> |
| `Year` | RateLimitUnitDay specifies the rate limit interval to be 1 year.<br /> |


#### RateLimitValue
Expand Down
5 changes: 3 additions & 2 deletions site/content/zh/latest/api/extension_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -3652,7 +3652,7 @@ _Appears in:_
_Underlying type:_ _string_

RateLimitUnit specifies the intervals for setting rate limits.
Valid RateLimitUnit values are "Second", "Minute", "Hour", and "Day".
Valid RateLimitUnit values are "Second", "Minute", "Hour", "Day", "Month" and "Year".

_Appears in:_
- [RateLimitValue](#ratelimitvalue)
Expand All @@ -3663,7 +3663,8 @@ _Appears in:_
| `Minute` | RateLimitUnitMinute specifies the rate limit interval to be 1 minute.<br /> |
| `Hour` | RateLimitUnitHour specifies the rate limit interval to be 1 hour.<br /> |
| `Day` | RateLimitUnitDay specifies the rate limit interval to be 1 day.<br /> |

| `Month` | RateLimitUnitDay specifies the rate limit interval to be 1 month.<br /> |
| `Year`| RateLimitUnitDay specifies the rate limit interval to be 1 year.<br /> |

#### RateLimitValue

Expand Down

0 comments on commit 80431d7

Please sign in to comment.