Skip to content

Commit

Permalink
build multiple weighted clusters
Browse files Browse the repository at this point in the history
Signed-off-by: Karol Szwaj <karol.szwaj@gmail.com>
  • Loading branch information
cnvergence committed May 8, 2024
1 parent 92d8a12 commit e984aa4
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ httpRoutes:
backendRefs:
- name: service-1
port: 8080
weight: 1
weight: 8
filters:
- type: RequestHeaderModifier
requestHeaderModifier:
Expand All @@ -39,16 +39,19 @@ httpRoutes:
value: "some-value"
- name: "add-header-2"
value: "some-value"
- type: RequestHeaderModifier
requestHeaderModifier:
add:
- name: "add-header-1"
value: "some-value"
- name: "add-header-3"
value: "some-value"
- name: service-2
port: 8080
weight: 2
- matches:
- path:

Check failure on line 46 in internal/gatewayapi/testdata/httproute-with-backendref-add-multiple-filters.in.yaml

View workflow job for this annotation

GitHub Actions / lint

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

Check failure on line 46 in internal/gatewayapi/testdata/httproute-with-backendref-add-multiple-filters.in.yaml

View workflow job for this annotation

GitHub Actions / lint

46:11 [indentation] wrong indentation: expected 12 but found 10
value: "/foo"
backendRefs:
- name: service-3
port: 8080
weight: 3
weight: 1
filters:
- type: RequestHeaderModifier
requestHeaderModifier:
set:
- name: "add-header-3"
value: "some-value"
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,30 @@ httpRoutes:
- name: add-header-2
value: some-value
type: RequestHeaderModifier
- requestHeaderModifier:
add:
- name: add-header-1
value: some-value
- name: add-header-3
value: some-value
type: RequestHeaderModifier
name: service-1
port: 8080
weight: 1
weight: 8
- name: service-2
port: 8080
weight: 2
- name: service-3
port: 8080
weight: 3
matches:
- path:
value: /
sessionPersistence: null
- backendRefs:
- filters:
- requestHeaderModifier:
set:
- name: add-header-3
value: some-value
type: RequestHeaderModifier
name: service-3
port: 8080
weight: 1
matches:
- path:
value: /foo
sessionPersistence: null
status:
parents:
- conditions:
Expand Down Expand Up @@ -131,41 +135,55 @@ xdsIR:
routes:
- backendWeights:
invalid: 0
valid: 6
valid: 1
destination:
name: httproute/default/httproute-1/rule/0
name: httproute/default/httproute-1/rule/1
settings:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8080
filters:
addRequestHeaders:
- append: true
name: add-header-1
value: some-value
- append: true
name: add-header-2
value: some-value
- append: true
- append: false
name: add-header-3
value: some-value
protocol: HTTP
weight: 1
hostname: '*'
isHTTP2: false
name: httproute/default/httproute-1/rule/1/match/0/*
pathMatch:
distinct: false
name: ""
prefix: /foo
- backendWeights:
invalid: 0
valid: 10
destination:
name: httproute/default/httproute-1/rule/0
settings:
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8080
filters: {}
filters:
addRequestHeaders:
- append: true
name: add-header-1
value: some-value
- append: true
name: add-header-2
value: some-value
protocol: HTTP
weight: 2
weight: 8
- addressType: IP
endpoints:
- host: 7.7.7.7
port: 8080
filters: {}
protocol: HTTP
weight: 3
weight: 2
hostname: '*'
isHTTP2: false
name: httproute/default/httproute-1/rule/0/match/0/*
Expand Down
14 changes: 12 additions & 2 deletions internal/xds/translator/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func buildXdsRoute(httpRoute *ir.HTTPRoute) (*routev3.Route, error) {
router.Action = &routev3.Route_Route{Route: routeAction}
default:
var routeAction *routev3.RouteAction
if httpRoute.BackendWeights.Invalid != 0 || httpRoute.BackendWeights.Valid > 1 {
if hasFiltersInSettings(httpRoute.Destination.Settings) || httpRoute.BackendWeights.Invalid != 0 {
// If there are backend weights stored then a weighted cluster is required for the route
routeAction = buildXdsWeightedRouteAction(httpRoute)
} else {
Expand Down Expand Up @@ -272,8 +272,8 @@ func buildXdsWeightedRouteAction(httpRoute *ir.HTTPRoute) *routev3.RouteAction {
validCluster.ResponseHeadersToRemove = append(validCluster.ResponseHeadersToRemove, destinationSetting.Filters.RemoveResponseHeaders...)
}
}
weightedClusters = append(weightedClusters, validCluster)
}
weightedClusters = append(weightedClusters, validCluster)

return &routev3.RouteAction{
// Intentionally route to a non-existent cluster and return a 500 error when it is not found
Expand Down Expand Up @@ -597,3 +597,13 @@ func buildRetryOn(triggers []ir.TriggerEnum) (string, error) {

return b.String(), nil
}

func hasFiltersInSettings(settings []*ir.DestinationSetting) bool {
for _, setting := range settings {
filters := setting.Filters
if filters != nil {
return true
}
}
return false
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,62 @@
http:
- name: "first-listener"
address: "0.0.0.0"
port: 10080
hostnames:
- "*"
path:
mergeSlashes: true
escapedSlashesAction: UnescapeAndRedirect
routes:
- name: "first-route"
backendWeights:
invalid: 0
valid: 20
hostname: "*"
destination:
name: "first-route-dest"
settings:
- endpoints:
- host: "1.1.1.1"
port: 50001
weight: 20
filters:
addRequestHeaders:
- append: true
name: add-header-1
value: some-value
- append: true
name: add-header-2
value: some-value
- append: true
name: add-header-3
value: some-value
- endpoints:
- host: "2.2.2.2"
port: 50002
weight: 40
- endpoints:
- host: "3.3.3.3"
port: 50003
weight: 20
- endpoints:
- host: "4.4.4.4"
port: 50004
weight: 20
- name: "first-listener"
address: "0.0.0.0"
hostnames:
- '*'
path:
escapedSlashesAction: UnescapeAndRedirect
mergeSlashes: true
port: 10080
routes:
- backendWeights:
invalid: 0
valid: 1
destination:
name: "first-route-dest"
settings:
- addressType: IP
endpoints:
- host: 1.1.1.1
port: 8080
filters:
addRequestHeaders:
- append: false
name: add-header-3
value: some-value
protocol: HTTP
weight: 1
hostname: '*'
name: "first-route"
pathMatch:
prefix: /foo
- backendWeights:
invalid: 0
valid: 10
destination:
name: "second-route-dest"
settings:
- addressType: IP

Check failure on line 38 in internal/xds/translator/testdata/in/xds-ir/http-route-weighted-backend-with-filters.yaml

View workflow job for this annotation

GitHub Actions / lint

38:11 [indentation] wrong indentation: expected 12 but found 10

Check failure on line 38 in internal/xds/translator/testdata/in/xds-ir/http-route-weighted-backend-with-filters.yaml

View workflow job for this annotation

GitHub Actions / lint

38:11 [indentation] wrong indentation: expected 12 but found 10
endpoints:
- host: 2.2.2.2

Check failure on line 40 in internal/xds/translator/testdata/in/xds-ir/http-route-weighted-backend-with-filters.yaml

View workflow job for this annotation

GitHub Actions / lint

40:13 [indentation] wrong indentation: expected 14 but found 12

Check failure on line 40 in internal/xds/translator/testdata/in/xds-ir/http-route-weighted-backend-with-filters.yaml

View workflow job for this annotation

GitHub Actions / lint

40:13 [indentation] wrong indentation: expected 14 but found 12
port: 8080
filters:
addRequestHeaders:
- append: true

Check failure on line 44 in internal/xds/translator/testdata/in/xds-ir/http-route-weighted-backend-with-filters.yaml

View workflow job for this annotation

GitHub Actions / lint

44:15 [indentation] wrong indentation: expected 16 but found 14

Check failure on line 44 in internal/xds/translator/testdata/in/xds-ir/http-route-weighted-backend-with-filters.yaml

View workflow job for this annotation

GitHub Actions / lint

44:15 [indentation] wrong indentation: expected 16 but found 14
name: add-header-1
value: some-value
- append: true
name: add-header-2
value: some-value
protocol: HTTP
weight: 8
- addressType: IP
endpoints:
- host: 3.3.3.3

Check failure on line 54 in internal/xds/translator/testdata/in/xds-ir/http-route-weighted-backend-with-filters.yaml

View workflow job for this annotation

GitHub Actions / lint

54:13 [indentation] wrong indentation: expected 14 but found 12

Check failure on line 54 in internal/xds/translator/testdata/in/xds-ir/http-route-weighted-backend-with-filters.yaml

View workflow job for this annotation

GitHub Actions / lint

54:13 [indentation] wrong indentation: expected 14 but found 12
port: 8080
filters: {}
protocol: HTTP
weight: 2
hostname: '*'
name: "second-route"
pathMatch:
prefix: /
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,20 @@
outlierDetection: {}
perConnectionBufferLimitBytes: 32768
type: EDS
- circuitBreakers:
thresholds:
- maxRetries: 1024
commonLbConfig:
localityWeightedLbConfig: {}
connectTimeout: 10s
dnsLookupFamily: V4_ONLY
edsClusterConfig:
edsConfig:
ads: {}
resourceApiVersion: V3
serviceName: second-route-dest
lbPolicy: LEAST_REQUEST
name: second-route-dest
outlierDetection: {}
perConnectionBufferLimitBytes: 32768
type: EDS
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,30 @@
address:
socketAddress:
address: 1.1.1.1
portValue: 50001
portValue: 8080
loadBalancingWeight: 1
loadBalancingWeight: 20
loadBalancingWeight: 1
locality:
region: first-route-dest/backend/0
- clusterName: second-route-dest
endpoints:
- lbEndpoints:
- endpoint:
address:
socketAddress:
address: 2.2.2.2
portValue: 50002
portValue: 8080
loadBalancingWeight: 1
loadBalancingWeight: 40
loadBalancingWeight: 8
locality:
region: first-route-dest/backend/1
region: second-route-dest/backend/0
- lbEndpoints:
- endpoint:
address:
socketAddress:
address: 3.3.3.3
portValue: 50003
loadBalancingWeight: 1
loadBalancingWeight: 20
locality:
region: first-route-dest/backend/2
- lbEndpoints:
- endpoint:
address:
socketAddress:
address: 4.4.4.4
portValue: 50004
portValue: 8080
loadBalancingWeight: 1
loadBalancingWeight: 20
loadBalancingWeight: 2
locality:
region: first-route-dest/backend/3
region: second-route-dest/backend/1
Loading

0 comments on commit e984aa4

Please sign in to comment.