Skip to content

Commit

Permalink
Merge branch 'main' into fix-empty-conn-limit
Browse files Browse the repository at this point in the history
  • Loading branch information
guydc authored Oct 21, 2024
2 parents 96bc751 + deddeb4 commit 3055fe7
Show file tree
Hide file tree
Showing 19 changed files with 650 additions and 105 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ jobs:
- uses: ./tools/github-actions/setup-deps

- name: Initialize CodeQL
uses: github/codeql-action/init@c36620d31ac7c881962c3d9dd939c40ec9434f2b # v3.26.12
uses: github/codeql-action/init@f779452ac5af1c261dce0346a8f964149f49322b # v3.26.13
with:
languages: ${{ matrix.language }}

- name: Autobuild
uses: github/codeql-action/autobuild@c36620d31ac7c881962c3d9dd939c40ec9434f2b # v3.26.12
uses: github/codeql-action/autobuild@f779452ac5af1c261dce0346a8f964149f49322b # v3.26.13

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@c36620d31ac7c881962c3d9dd939c40ec9434f2b # v3.26.12
uses: github/codeql-action/analyze@f779452ac5af1c261dce0346a8f964149f49322b # v3.26.13
with:
category: "/language:${{matrix.language}}"
2 changes: 1 addition & 1 deletion .github/workflows/scorecard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ jobs:
retention-days: 5

- name: "Upload to code-scanning"
uses: github/codeql-action/upload-sarif@c36620d31ac7c881962c3d9dd939c40ec9434f2b # v3.26.12
uses: github/codeql-action/upload-sarif@f779452ac5af1c261dce0346a8f964149f49322b # v3.26.13
with:
sarif_file: results.sarif
2 changes: 1 addition & 1 deletion .github/workflows/trivy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
IMAGE=envoy-proxy/gateway-dev TAG=${{ github.sha }} make image
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@5681af892cd0f4997658e2bacc62bd0a894cf564 # v0.27.0
uses: aquasecurity/trivy-action@915b19bbe73b92a6cf82a1bc12b087c9a19a5fe2 # v0.28.0
with:
image-ref: envoy-proxy/gateway-dev:${{ github.sha }}
exit-code: '1'
4 changes: 2 additions & 2 deletions examples/extension-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.23.1
require (
github.com/envoyproxy/gateway v1.0.2
github.com/envoyproxy/go-control-plane v0.13.1
github.com/urfave/cli/v2 v2.27.4
github.com/urfave/cli/v2 v2.27.5
google.golang.org/grpc v1.67.1
google.golang.org/protobuf v1.35.1
k8s.io/apimachinery v0.31.1
Expand All @@ -17,7 +17,7 @@ require (
cel.dev/expr v0.16.0 // indirect
github.com/census-instrumentation/opencensus-proto v0.4.1 // indirect
github.com/cncf/xds/go v0.0.0-20240723142845-024c85f92f20 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
github.com/envoyproxy/protoc-gen-validate v1.1.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
Expand Down
8 changes: 4 additions & 4 deletions examples/extension-server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/census-instrumentation/opencensus-proto v0.4.1 h1:iKLQ0xPNFxR/2hzXZMr
github.com/census-instrumentation/opencensus-proto v0.4.1/go.mod h1:4T9NM4+4Vw91VeyqjLS6ao50K5bOcLKN6Q42XnYaRYw=
github.com/cncf/xds/go v0.0.0-20240723142845-024c85f92f20 h1:N+3sFI5GUjRKBi+i0TxYVST9h4Ie192jJWpHvthBBgg=
github.com/cncf/xds/go v0.0.0-20240723142845-024c85f92f20/go.mod h1:W+zGtBO5Y1IgJhy4+A9GOqVhqLpfZi+vwmdNXUehLA8=
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -64,8 +64,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/urfave/cli/v2 v2.27.4 h1:o1owoI+02Eb+K107p27wEX9Bb8eqIoZCfLXloLUSWJ8=
github.com/urfave/cli/v2 v2.27.4/go.mod h1:m4QzxcD2qpra4z7WhzEGn74WZLViBnMpb1ToCAKdGRQ=
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w=
github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/hashicorp/go-multierror v1.1.1
github.com/miekg/dns v1.1.62
github.com/ohler55/ojg v1.24.1
github.com/prometheus/client_golang v1.20.4
github.com/prometheus/client_golang v1.20.5
github.com/prometheus/common v0.60.0
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,8 @@ github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXP
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
github.com/prometheus/client_golang v1.1.0/go.mod h1:I1FGZT9+L76gKKOs5djB6ezCbFQP1xR9D75/vuwEF3g=
github.com/prometheus/client_golang v1.20.4 h1:Tgh3Yr67PaOv/uTqloMsCEdeuFTatm5zIq5+qNN23vI=
github.com/prometheus/client_golang v1.20.4/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y=
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand Down
45 changes: 0 additions & 45 deletions internal/gatewayapi/helpers_v1alpha2.go

This file was deleted.

9 changes: 3 additions & 6 deletions internal/provider/kubernetes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ func (r *gatewayAPIReconciler) processTLSRoutes(ctx context.Context, gatewayName

for _, rule := range tlsRoute.Spec.Rules {
for _, backendRef := range rule.BackendRefs {
ref := gatewayapi.UpgradeBackendRef(backendRef)
if err := validateBackendRef(&ref); err != nil {
if err := validateBackendRef(&backendRef); err != nil {
r.log.Error(err, "invalid backendRef")
continue
}
Expand Down Expand Up @@ -467,8 +466,7 @@ func (r *gatewayAPIReconciler) processTCPRoutes(ctx context.Context, gatewayName

for _, rule := range tcpRoute.Spec.Rules {
for _, backendRef := range rule.BackendRefs {
ref := gatewayapi.UpgradeBackendRef(backendRef)
if err := validateBackendRef(&ref); err != nil {
if err := validateBackendRef(&backendRef); err != nil {
r.log.Error(err, "invalid backendRef")
continue
}
Expand Down Expand Up @@ -545,8 +543,7 @@ func (r *gatewayAPIReconciler) processUDPRoutes(ctx context.Context, gatewayName

for _, rule := range udpRoute.Spec.Rules {
for _, backendRef := range rule.BackendRefs {
ref := gatewayapi.UpgradeBackendRef(backendRef)
if err := validateBackendRef(&ref); err != nil {
if err := validateBackendRef(&backendRef); err != nil {
r.log.Error(err, "invalid backendRef")
continue
}
Expand Down
85 changes: 45 additions & 40 deletions internal/xds/translator/ratelimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,19 @@ func buildRouteRateLimits(descriptorPrefix string, global *ir.GlobalRateLimit) [
// Matches are ANDed
rlActions := []*routev3.RateLimit_Action{routeDescriptor}
for mIdx, match := range rule.HeaderMatches {
var action *routev3.RateLimit_Action
// Case for distinct match
if match.Distinct {
// Setup RequestHeader actions
descriptorKey := getRouteRuleDescriptor(rIdx, mIdx)
action := &routev3.RateLimit_Action{
action = &routev3.RateLimit_Action{
ActionSpecifier: &routev3.RateLimit_Action_RequestHeaders_{
RequestHeaders: &routev3.RateLimit_Action_RequestHeaders{
HeaderName: match.Name,
DescriptorKey: descriptorKey,
},
},
}
rlActions = append(rlActions, action)
} else {
// Setup HeaderValueMatch actions
descriptorKey := getRouteRuleDescriptor(rIdx, mIdx)
Expand All @@ -184,7 +184,7 @@ func buildRouteRateLimits(descriptorPrefix string, global *ir.GlobalRateLimit) [
if match.Invert != nil && *match.Invert {
expectMatch = false
}
action := &routev3.RateLimit_Action{
action = &routev3.RateLimit_Action{
ActionSpecifier: &routev3.RateLimit_Action_HeaderValueMatch_{
HeaderValueMatch: &routev3.RateLimit_Action_HeaderValueMatch{
DescriptorKey: descriptorKey,
Expand All @@ -196,8 +196,8 @@ func buildRouteRateLimits(descriptorPrefix string, global *ir.GlobalRateLimit) [
},
},
}
rlActions = append(rlActions, action)
}
rlActions = append(rlActions, action)
}

// To be able to rate limit each individual IP, we need to use a nested descriptors structure in the configuration
Expand Down Expand Up @@ -236,7 +236,7 @@ func buildRouteRateLimits(descriptorPrefix string, global *ir.GlobalRateLimit) [
// Setup RemoteAddress action if distinct match is set
if rule.CIDRMatch.Distinct {
// Setup RemoteAddress action
action := &routev3.RateLimit_Action{
action = &routev3.RateLimit_Action{
ActionSpecifier: &routev3.RateLimit_Action_RemoteAddress_{
RemoteAddress: &routev3.RateLimit_Action_RemoteAddress{},
},
Expand All @@ -245,8 +245,8 @@ func buildRouteRateLimits(descriptorPrefix string, global *ir.GlobalRateLimit) [
}
}

// Case when header match is not set and the rate limit is applied
// to all traffic.
// Case when both header and cidr match are not set and the ratelimit
// will be applied to all traffic.
if !rule.IsMatchSet() {
// Setup GenericKey action
action := &routev3.RateLimit_Action{
Expand Down Expand Up @@ -333,22 +333,21 @@ func BuildRateLimitServiceConfig(irListener *ir.HTTPListener) *rlsconfv3.RateLim
func buildRateLimitServiceDescriptors(global *ir.GlobalRateLimit) []*rlsconfv3.RateLimitDescriptor {
pbDescriptors := make([]*rlsconfv3.RateLimitDescriptor, 0, len(global.Rules))

// The order in which matching descriptors are built is consistent with
// the order in which ratelimit actions are built:
// 1) Header Matches
// 2) CIDR Match
// 3) No Match
for rIdx, rule := range global.Rules {
var head, cur *rlsconfv3.RateLimitDescriptor
if !rule.IsMatchSet() {
pbDesc := new(rlsconfv3.RateLimitDescriptor)
// GenericKey case
pbDesc.Key = getRouteRuleDescriptor(rIdx, -1)
pbDesc.Value = getRouteRuleDescriptor(rIdx, -1)
rateLimit := rlsconfv3.RateLimitPolicy{
RequestsPerUnit: uint32(rule.Limit.Requests),
Unit: rlsconfv3.RateLimitUnit(rlsconfv3.RateLimitUnit_value[strings.ToUpper(string(rule.Limit.Unit))]),
}
pbDesc.RateLimit = &rateLimit
head = pbDesc
cur = head
rateLimitPolicy := &rlsconfv3.RateLimitPolicy{
RequestsPerUnit: uint32(rule.Limit.Requests),
Unit: rlsconfv3.RateLimitUnit(rlsconfv3.RateLimitUnit_value[strings.ToUpper(string(rule.Limit.Unit))]),
}

// We use a chain structure to describe the matching descriptors for one rule.
// The RateLimitPolicy should be added to the last descriptor in the chain.
var head, cur *rlsconfv3.RateLimitDescriptor

for mIdx, match := range rule.HeaderMatches {
pbDesc := new(rlsconfv3.RateLimitDescriptor)
// Case for distinct match
Expand All @@ -361,22 +360,16 @@ func buildRateLimitServiceDescriptors(global *ir.GlobalRateLimit) []*rlsconfv3.R
pbDesc.Value = getRouteRuleDescriptor(rIdx, mIdx)
}

// Add the ratelimit values to the last descriptor
if mIdx == len(rule.HeaderMatches)-1 {
rateLimit := rlsconfv3.RateLimitPolicy{
RequestsPerUnit: uint32(rule.Limit.Requests),
Unit: rlsconfv3.RateLimitUnit(rlsconfv3.RateLimitUnit_value[strings.ToUpper(string(rule.Limit.Unit))]),
}
pbDesc.RateLimit = &rateLimit
}

if mIdx == 0 {
head = pbDesc
} else {
cur.Descriptors = []*rlsconfv3.RateLimitDescriptor{pbDesc}
}

cur = pbDesc

// Do not add the RateLimitPolicy to the last header match descriptor yet,
// as it is also possible that CIDR match descriptor also exist.
}

// EG supports two kinds of rate limit descriptors for the source IP: exact and distinct.
Expand Down Expand Up @@ -405,25 +398,37 @@ func buildRateLimitServiceDescriptors(global *ir.GlobalRateLimit) []*rlsconfv3.R
pbDesc := new(rlsconfv3.RateLimitDescriptor)
pbDesc.Key = "masked_remote_address"
pbDesc.Value = rule.CIDRMatch.CIDR
rateLimit := rlsconfv3.RateLimitPolicy{
RequestsPerUnit: uint32(rule.Limit.Requests),
Unit: rlsconfv3.RateLimitUnit(rlsconfv3.RateLimitUnit_value[strings.ToUpper(string(rule.Limit.Unit))]),

if cur != nil {
// The header match descriptor chain exist, add current
// descriptor to the chain.
cur.Descriptors = []*rlsconfv3.RateLimitDescriptor{pbDesc}
} else {
head = pbDesc
}
cur = pbDesc

if rule.CIDRMatch.Distinct {
pbDesc.Descriptors = []*rlsconfv3.RateLimitDescriptor{
{
Key: "remote_address",
RateLimit: &rateLimit,
},
}
} else {
pbDesc.RateLimit = &rateLimit
pbDesc := new(rlsconfv3.RateLimitDescriptor)
pbDesc.Key = "remote_address"
cur.Descriptors = []*rlsconfv3.RateLimitDescriptor{pbDesc}
cur = pbDesc
}
}

// Case when both header and cidr match are not set and the ratelimit
// will be applied to all traffic.
if !rule.IsMatchSet() {
pbDesc := new(rlsconfv3.RateLimitDescriptor)
// GenericKey case
pbDesc.Key = getRouteRuleDescriptor(rIdx, -1)
pbDesc.Value = getRouteRuleDescriptor(rIdx, -1)
head = pbDesc
cur = head
}

// Add the ratelimit policy to the last descriptor of chain.
cur.RateLimit = rateLimitPolicy
pbDescriptors = append(pbDescriptors, head)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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"
- name: "x-user-id"
exact: "two"
- name: "x-org-id"
exact: "three"
cidrMatch:
cidr: 0.0.0.0/0
ip: 0.0.0.0
maskLen: 0
isIPv6: false
distinct: false
limit:
requests: 5
unit: second
pathMatch:
exact: "foo/bar"
destination:
name: "first-route-dest"
settings:
- endpoints:
- host: "1.2.3.4"
port: 50000
Loading

0 comments on commit 3055fe7

Please sign in to comment.