Skip to content

Commit

Permalink
Merge pull request #97 from mlavacca/fix-flaky-tests
Browse files Browse the repository at this point in the history
fix: ingress rules order is preserved
  • Loading branch information
k8s-ci-robot authored Dec 7, 2023
2 parents 6ff1a24 + eda2aed commit c55e274
Show file tree
Hide file tree
Showing 6 changed files with 574 additions and 19 deletions.
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
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)
}
}

ingressPathsByMatchKey := groupIngressPathsByMatchKey(rg.rules)
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 ingressPathsByMatchKey.keys {
paths := ingressPathsByMatchKey.data[key]
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 track the keys in an additional slice in order to preserve the rules order
ingressPathsByMatchKey := 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 := ingressPathsByMatchKey.data[pmKey]; !ok {
ingressPathsByMatchKey.keys = append(ingressPathsByMatchKey.keys, pmKey)
}
ingressPathsByMatchKey.data[pmKey] = append(ingressPathsByMatchKey.data[pmKey], ip)
}
}
return ingressPathsByMatchKey
}

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

0 comments on commit c55e274

Please sign in to comment.