Skip to content

Commit

Permalink
sort rulegroups o iterate the map in a sorted order (#186)
Browse files Browse the repository at this point in the history
* sort rulegroups o iterate the map in a sorted order

* Update converter_test.go
  • Loading branch information
LiorLieberman authored Aug 21, 2024
1 parent c661417 commit 29ea225
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
15 changes: 14 additions & 1 deletion pkg/i2gw/providers/common/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
package common

import (
"cmp"
"fmt"
"slices"
"strings"

"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
Expand Down Expand Up @@ -171,7 +173,18 @@ func (a *ingressAggregator) toHTTPRoutesAndGateways(options i2gw.ProviderImpleme
var errors field.ErrorList
listenersByNamespacedGateway := map[string][]gatewayv1.Listener{}

for _, rg := range a.ruleGroups {
// Sort the rulegroups to iterate the map in a sorted order.
ruleGroupsKeys := make([]ruleGroupKey, 0, len(a.ruleGroups))
for k := range a.ruleGroups {
ruleGroupsKeys = append(ruleGroupsKeys, k)
}

slices.SortFunc(ruleGroupsKeys, func(a, b ruleGroupKey) int {
return cmp.Compare(a, b)
})

for _, rgk := range ruleGroupsKeys {
rg := a.ruleGroups[rgk]
listener := gatewayv1.Listener{}
if rg.host != "" {
listener.Hostname = (*gatewayv1.Hostname)(&rg.host)
Expand Down
24 changes: 12 additions & 12 deletions pkg/i2gw/providers/ingressnginx/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,33 +292,33 @@ func Test_ToGateway(t *testing.T) {
GatewayClassName: "nginx",
Listeners: []gatewayv1.Listener{
{
Name: "foo-example-com-http",
Name: "bar-example-com-http",
Port: 80,
Protocol: gatewayv1.HTTPProtocolType,
Hostname: ptrTo(gatewayv1.Hostname("foo.example.com")),
Hostname: ptrTo(gatewayv1.Hostname("bar.example.com")),
},
{
Name: "foo-example-com-https",
Name: "bar-example-com-https",
Port: 443,
Protocol: gatewayv1.HTTPSProtocolType,
Hostname: ptrTo(gatewayv1.Hostname("foo.example.com")),
Hostname: ptrTo(gatewayv1.Hostname("bar.example.com")),
TLS: &gatewayv1.GatewayTLSConfig{
CertificateRefs: []gatewayv1.SecretObjectReference{
{Name: "example-com"},
},
},
},
{
Name: "bar-example-com-http",
Name: "foo-example-com-http",
Port: 80,
Protocol: gatewayv1.HTTPProtocolType,
Hostname: ptrTo(gatewayv1.Hostname("bar.example.com")),
Hostname: ptrTo(gatewayv1.Hostname("foo.example.com")),
},
{
Name: "bar-example-com-https",
Name: "foo-example-com-https",
Port: 443,
Protocol: gatewayv1.HTTPSProtocolType,
Hostname: ptrTo(gatewayv1.Hostname("bar.example.com")),
Hostname: ptrTo(gatewayv1.Hostname("foo.example.com")),
TLS: &gatewayv1.GatewayTLSConfig{
CertificateRefs: []gatewayv1.SecretObjectReference{
{Name: "example-com"},
Expand Down Expand Up @@ -520,16 +520,16 @@ func Test_ToGateway(t *testing.T) {
GatewayClassName: "nginx",
Listeners: []gatewayv1.Listener{
{
Name: "foo-example-com-http",
Name: "bar-example-com-http",
Port: 80,
Protocol: gatewayv1.HTTPProtocolType,
Hostname: ptrTo(gatewayv1.Hostname("foo.example.com")),
Hostname: ptrTo(gatewayv1.Hostname("bar.example.com")),
},
{
Name: "bar-example-com-http",
Name: "foo-example-com-http",
Port: 80,
Protocol: gatewayv1.HTTPProtocolType,
Hostname: ptrTo(gatewayv1.Hostname("bar.example.com")),
Hostname: ptrTo(gatewayv1.Hostname("foo.example.com")),
},
},
},
Expand Down

0 comments on commit 29ea225

Please sign in to comment.