Skip to content

Commit

Permalink
fix: rules order is preserved
Browse files Browse the repository at this point in the history
The ingress rules order is kept during Ingress to Gateway conversion.

Signed-off-by: Mattia Lavacca <[email protected]>
  • Loading branch information
mlavacca committed Dec 7, 2023
1 parent 6ff1a24 commit 2c89577
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
22 changes: 9 additions & 13 deletions pkg/i2gw/providers/common/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,13 @@ func (a *ingressAggregator) toHTTPRoutesAndGateways(options i2gw.ProviderImpleme
return httpRoutes, gateways, errors
}

func (rg *ingressRuleGroup) toHTTPRoute(options i2gw.ProviderImplementationSpecificOptions) (gatewayv1beta1.HTTPRoute, field.ErrorList) {
pathsByMatchGroup := map[pathMatchKey][]ingressPath{}
var errors field.ErrorList

for i, ir := range rg.rules {
for j, path := range ir.rule.HTTP.Paths {
ip := ingressPath{ruleIdx: i, pathIdx: j, ruleType: "http", path: path}
pmKey := getPathMatchKey(ip)
pathsByMatchGroup[pmKey] = append(pathsByMatchGroup[pmKey], ip)
}
}
type pathsByMatchGroupType struct {
key string
paths []ingressPath
}

func (rg *ingressRuleGroup) toHTTPRoute(options i2gw.ProviderImplementationSpecificOptions) (gatewayv1beta1.HTTPRoute, field.ErrorList) {
pathsByMatchGroup := groupPaths(rg.rules)
httpRoute := gatewayv1beta1.HTTPRoute{
ObjectMeta: metav1.ObjectMeta{
Name: RouteName(rg.name, rg.host),
Expand All @@ -301,8 +296,9 @@ func (rg *ingressRuleGroup) toHTTPRoute(options i2gw.ProviderImplementationSpeci
httpRoute.Spec.Hostnames = []gatewayv1beta1.Hostname{gatewayv1beta1.Hostname(rg.host)}
}

var errors field.ErrorList
for _, paths := range pathsByMatchGroup {
path := paths[0]
path := paths.paths[0]
fieldPath := field.NewPath("spec", "rules").Index(path.ruleIdx).Child(path.ruleType).Child("paths").Index(path.pathIdx)
match, err := toHTTPRouteMatch(path.path, fieldPath, options.ToImplementationSpecificHTTPPathTypeMatch)
if err != nil {
Expand All @@ -313,7 +309,7 @@ func (rg *ingressRuleGroup) toHTTPRoute(options i2gw.ProviderImplementationSpeci
Matches: []gatewayv1beta1.HTTPRouteMatch{*match},
}

backendRefs, errs := rg.configureBackendRef(paths)
backendRefs, errs := rg.configureBackendRef(paths.paths)
errors = append(errors, errs...)
hrRule.BackendRefs = backendRefs

Expand Down
28 changes: 28 additions & 0 deletions pkg/i2gw/providers/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,34 @@ func ToBackendRef(ib networkingv1.IngressBackend, path *field.Path) (*gatewayv1b
}, nil
}

func groupPaths(rules []ingressRule) []pathsByMatchGroupType {
// we use a slice instead of a map to preserve rules order
pathsByMatchGroup := []pathsByMatchGroupType{}

for i, ir := range rules {
for j, path := range ir.rule.HTTP.Paths {
ip := ingressPath{ruleIdx: i, pathIdx: j, ruleType: "http", path: path}
pmKey := getPathMatchKey(ip)
var found bool
for k, paths := range pathsByMatchGroup {
if pmKey == pathMatchKey(paths.key) {
paths.paths = append(paths.paths, ip)
pathsByMatchGroup[k] = paths
found = true
break
}
}
if !found {
pathsByMatchGroup = append(pathsByMatchGroup, pathsByMatchGroupType{
key: string(pmKey),
paths: []ingressPath{ip},
})
}
}
}
return pathsByMatchGroup
}

func PtrTo[T any](a T) *T {
return &a
}

0 comments on commit 2c89577

Please sign in to comment.