Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ingress rules order is preserved #97

Merged
merged 8 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.20
require (
github.com/google/go-cmp v0.6.0
github.com/spf13/cobra v1.8.0
istio.io/api v1.20.0
github.com/stretchr/testify v1.8.4
k8s.io/api v0.28.4
k8s.io/apimachinery v0.28.4
k8s.io/cli-runtime v0.28.4
Expand All @@ -15,6 +15,11 @@ require (
sigs.k8s.io/gateway-api v0.5.0
)

require (
github.com/pmezard/go-difflib v1.0.0 // indirect
istio.io/api v1.20.0 // indirect
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
Expand Down Expand Up @@ -63,7 +68,7 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
istio.io/client-go v1.19.0-alpha.1.0.20231130185426-9f1859c8ff42 // indirect
istio.io/client-go v1.19.0-alpha.1.0.20231130185426-9f1859c8ff42
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/kube-openapi v0.0.0-20231113174909-778a5567bc1e // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/xlab/treeprint v1.2.0 h1:HzHnuAF1plUN2zGlAFHbSQP2qJ0ZAD3XF5XD7OesXRQ=
github.com/xlab/treeprint v1.2.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd/WEJu0=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
16 changes: 4 additions & 12 deletions pkg/i2gw/providers/common/converter.go
mlavacca marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -269,17 +269,7 @@ func (a *ingressAggregator) toHTTPRoutesAndGateways(options i2gw.ProviderImpleme
}

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)
}
}

pathsByMatchGroup := groupIngressPathsByMatchKey(rg.rules)
mlavacca marked this conversation as resolved.
Show resolved Hide resolved
httpRoute := gatewayv1beta1.HTTPRoute{
ObjectMeta: metav1.ObjectMeta{
Name: RouteName(rg.name, rg.host),
Expand All @@ -301,7 +291,9 @@ func (rg *ingressRuleGroup) toHTTPRoute(options i2gw.ProviderImplementationSpeci
httpRoute.Spec.Hostnames = []gatewayv1beta1.Hostname{gatewayv1beta1.Hostname(rg.host)}
}

for _, paths := range pathsByMatchGroup {
var errors field.ErrorList
for _, key := range pathsByMatchGroup.keys {
paths := pathsByMatchGroup.data[key]
mlavacca marked this conversation as resolved.
Show resolved Hide resolved
path := 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)
Expand Down
25 changes: 25 additions & 0 deletions pkg/i2gw/providers/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,31 @@ func ToBackendRef(ib networkingv1.IngressBackend, path *field.Path) (*gatewayv1b
}, nil
}

type orderedIngressPathsByMatchKey struct {
keys []pathMatchKey
data map[pathMatchKey][]ingressPath
}

func groupIngressPathsByMatchKey(rules []ingressRule) orderedIngressPathsByMatchKey {
// we use a slice instead of a map to preserve rules order
mlavacca marked this conversation as resolved.
Show resolved Hide resolved
pathsByMatchGroup := orderedIngressPathsByMatchKey{
keys: []pathMatchKey{},
data: map[pathMatchKey][]ingressPath{},
}

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)
if _, ok := pathsByMatchGroup.data[pmKey]; !ok {
pathsByMatchGroup.keys = append(pathsByMatchGroup.keys, pmKey)
}
pathsByMatchGroup.data[pmKey] = append(pathsByMatchGroup.data[pmKey], ip)
}
}
return pathsByMatchGroup
}

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