Skip to content

Commit

Permalink
feat: support multiple backend refs (#53)
Browse files Browse the repository at this point in the history
Signed-off-by: Tommy Chen <[email protected]>
  • Loading branch information
tommy351 authored Apr 29, 2024
1 parent b2477b9 commit 47954c2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
12 changes: 8 additions & 4 deletions pkg/plugin/httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,25 @@ 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(canaryServiceName, routeRuleList)
canaryBackendRefs, err := getBackendRefs(canaryServiceName, routeRuleList)
if err != nil {
return pluginTypes.RpcError{
ErrorString: err.Error(),
}
}
canaryBackendRef.Weight = &desiredWeight
stableBackendRef, err := getBackendRef(stableServiceName, routeRuleList)
for _, ref := range canaryBackendRefs {
ref.Weight = &desiredWeight
}
stableBackendRefs, err := getBackendRefs(stableServiceName, routeRuleList)
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: 6 additions & 2 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,22 @@ 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)
}
}
}
if len(matchedRefs) > 0 {
return matchedRefs, nil
}
return nil, routeRuleList.Error()
}

Expand Down
12 changes: 8 additions & 4 deletions pkg/plugin/tcproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,25 @@ 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(canaryServiceName, routeRuleList)
canaryBackendRefs, err := getBackendRefs(canaryServiceName, routeRuleList)
if err != nil {
return pluginTypes.RpcError{
ErrorString: err.Error(),
}
}
canaryBackendRef.Weight = &desiredWeight
stableBackendRef, err := getBackendRef(stableServiceName, routeRuleList)
for _, ref := range canaryBackendRefs {
ref.Weight = &desiredWeight
}
stableBackendRefs, err := getBackendRefs(stableServiceName, routeRuleList)
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 47954c2

Please sign in to comment.