Skip to content

Commit

Permalink
Support multiple backend refs
Browse files Browse the repository at this point in the history
Signed-off-by: Tommy Chen <tommy351@gmail.com>
  • Loading branch information
tommy351 committed Apr 26, 2024
1 parent 0de0634 commit 1741588
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
16 changes: 12 additions & 4 deletions pkg/plugin/httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,29 @@ func (r *RpcPlugin) setHTTPRouteWeight(rollout *v1alpha1.Rollout, desiredWeight
canaryServiceName := rollout.Spec.Strategy.Canary.CanaryService
stableServiceName := rollout.Spec.Strategy.Canary.StableService
routeRuleList := HTTPRouteRuleList(httpRoute.Spec.Rules)
canaryBackendRef, err := getBackendRef[*HTTPBackendRef, *HTTPRouteRule](canaryServiceName, routeRuleList)
canaryBackendRefs, err := getBackendRefs(canaryServiceName, routeRuleList)

Check failure on line 35 in pkg/plugin/httproute.go

View workflow job for this annotation

GitHub Actions / Build creation

cannot infer T1 (/home/runner/work/rollouts-plugin-trafficrouter-gatewayapi/rollouts-plugin-trafficrouter-gatewayapi/pkg/plugin/plugin.go:204:21)

Check failure on line 35 in pkg/plugin/httproute.go

View workflow job for this annotation

GitHub Actions / Go code linting

cannot infer T1 (pkg/plugin/plugin.go:204:21)

Check failure on line 35 in pkg/plugin/httproute.go

View workflow job for this annotation

GitHub Actions / Go code linting

cannot infer T1 (pkg/plugin/plugin.go:204:21)

Check failure on line 35 in pkg/plugin/httproute.go

View workflow job for this annotation

GitHub Actions / Unit tests running

cannot infer T1 (/home/runner/work/rollouts-plugin-trafficrouter-gatewayapi/rollouts-plugin-trafficrouter-gatewayapi/pkg/plugin/plugin.go:204:21)
if err != nil {
return pluginTypes.RpcError{
ErrorString: err.Error(),
}
}
canaryBackendRef.Weight = &desiredWeight
stableBackendRef, err := getBackendRef[*HTTPBackendRef, *HTTPRouteRule](stableServiceName, routeRuleList)

for _, ref := range canaryBackendRefs {
ref.Weight = &desiredWeight
}

stableBackendRefs, err := getBackendRefs(stableServiceName, routeRuleList)

Check failure on line 46 in pkg/plugin/httproute.go

View workflow job for this annotation

GitHub Actions / Build creation

cannot infer T1 (/home/runner/work/rollouts-plugin-trafficrouter-gatewayapi/rollouts-plugin-trafficrouter-gatewayapi/pkg/plugin/plugin.go:204:21)

Check failure on line 46 in pkg/plugin/httproute.go

View workflow job for this annotation

GitHub Actions / Go code linting

cannot infer T1 (pkg/plugin/plugin.go:204:21)

Check failure on line 46 in pkg/plugin/httproute.go

View workflow job for this annotation

GitHub Actions / Go code linting

cannot infer T1 (pkg/plugin/plugin.go:204:21)

Check failure on line 46 in pkg/plugin/httproute.go

View workflow job for this annotation

GitHub Actions / Unit tests running

cannot infer T1 (/home/runner/work/rollouts-plugin-trafficrouter-gatewayapi/rollouts-plugin-trafficrouter-gatewayapi/pkg/plugin/plugin.go:204:21)
if err != nil {
return pluginTypes.RpcError{
ErrorString: err.Error(),
}
}
restWeight := 100 - desiredWeight
stableBackendRef.Weight = &restWeight

for _, ref := range stableBackendRefs {
ref.Weight = &restWeight
}

updatedHTTPRoute, err := httpRouteClient.Update(ctx, httpRoute, metav1.UpdateOptions{})
if r.IsTest {
r.UpdatedHTTPRouteMock = updatedHTTPRoute
Expand Down
8 changes: 5 additions & 3 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,21 @@ func getRouteRule[T1 GatewayAPIBackendRef, T2 GatewayAPIRouteRule[T1], T3 Gatewa
return nil, routeRuleList.Error()
}

func getBackendRef[T1 GatewayAPIBackendRef, T2 GatewayAPIRouteRule[T1], T3 GatewayAPIRouteRuleList[T1, T2]](backendRefName string, routeRuleList T3) (T1, error) {
func getBackendRefs[T1 GatewayAPIBackendRef, T2 GatewayAPIRouteRule[T1], T3 GatewayAPIRouteRuleList[T1, T2]](backendRefName string, routeRuleList T3) ([]T1, error) {
var backendRef T1
var routeRule T2
var matchedRefs []T1

for next, hasNext := routeRuleList.Iterator(); hasNext; {
routeRule, hasNext = next()
for next, hasNext := routeRule.Iterator(); hasNext; {
backendRef, hasNext = next()
if backendRefName == backendRef.GetName() {
return backendRef, nil
matchedRefs = append(matchedRefs, backendRef)
}
}
}
return nil, routeRuleList.Error()
return matchedRefs, routeRuleList.Error()
}

func removeManagedRouteEntry(managedRouteMap ManagedRouteMap, routeRuleList HTTPRouteRuleList, managedRouteName string, httpRouteName string) (HTTPRouteRuleList, error) {
Expand Down
16 changes: 12 additions & 4 deletions pkg/plugin/tcproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,29 @@ func (r *RpcPlugin) setTCPRouteWeight(rollout *v1alpha1.Rollout, desiredWeight i
canaryServiceName := rollout.Spec.Strategy.Canary.CanaryService
stableServiceName := rollout.Spec.Strategy.Canary.StableService
routeRuleList := TCPRouteRuleList(tcpRoute.Spec.Rules)
canaryBackendRef, err := getBackendRef[*TCPBackendRef, *TCPRouteRule](canaryServiceName, routeRuleList)
canaryBackendRefs, err := getBackendRefs(canaryServiceName, routeRuleList)

Check failure on line 29 in pkg/plugin/tcproute.go

View workflow job for this annotation

GitHub Actions / Build creation

cannot infer T1 (/home/runner/work/rollouts-plugin-trafficrouter-gatewayapi/rollouts-plugin-trafficrouter-gatewayapi/pkg/plugin/plugin.go:204:21)

Check failure on line 29 in pkg/plugin/tcproute.go

View workflow job for this annotation

GitHub Actions / Go code linting

cannot infer T1 (pkg/plugin/plugin.go:204:21)

Check failure on line 29 in pkg/plugin/tcproute.go

View workflow job for this annotation

GitHub Actions / Go code linting

cannot infer T1 (pkg/plugin/plugin.go:204:21)

Check failure on line 29 in pkg/plugin/tcproute.go

View workflow job for this annotation

GitHub Actions / Unit tests running

cannot infer T1 (/home/runner/work/rollouts-plugin-trafficrouter-gatewayapi/rollouts-plugin-trafficrouter-gatewayapi/pkg/plugin/plugin.go:204:21)
if err != nil {
return pluginTypes.RpcError{
ErrorString: err.Error(),
}
}
canaryBackendRef.Weight = &desiredWeight
stableBackendRef, err := getBackendRef[*TCPBackendRef, *TCPRouteRule](stableServiceName, routeRuleList)

for _, ref := range canaryBackendRefs {
ref.Weight = &desiredWeight
}

stableBackendRefs, err := getBackendRefs(stableServiceName, routeRuleList)

Check failure on line 40 in pkg/plugin/tcproute.go

View workflow job for this annotation

GitHub Actions / Build creation

cannot infer T1 (/home/runner/work/rollouts-plugin-trafficrouter-gatewayapi/rollouts-plugin-trafficrouter-gatewayapi/pkg/plugin/plugin.go:204:21)

Check failure on line 40 in pkg/plugin/tcproute.go

View workflow job for this annotation

GitHub Actions / Go code linting

cannot infer T1 (pkg/plugin/plugin.go:204:21)) (typecheck)

Check failure on line 40 in pkg/plugin/tcproute.go

View workflow job for this annotation

GitHub Actions / Go code linting

cannot infer T1 (pkg/plugin/plugin.go:204:21) (typecheck)

Check failure on line 40 in pkg/plugin/tcproute.go

View workflow job for this annotation

GitHub Actions / Unit tests running

cannot infer T1 (/home/runner/work/rollouts-plugin-trafficrouter-gatewayapi/rollouts-plugin-trafficrouter-gatewayapi/pkg/plugin/plugin.go:204:21)
if err != nil {
return pluginTypes.RpcError{
ErrorString: err.Error(),
}
}
restWeight := 100 - desiredWeight
stableBackendRef.Weight = &restWeight

for _, ref := range stableBackendRefs {
ref.Weight = &restWeight
}

updatedTCPRoute, err := tcpRouteClient.Update(ctx, tcpRoute, metav1.UpdateOptions{})
if r.IsTest {
r.UpdatedTCPRouteMock = updatedTCPRoute
Expand Down

0 comments on commit 1741588

Please sign in to comment.