Skip to content

Commit 29ea225

Browse files
sort rulegroups o iterate the map in a sorted order (#186)
* sort rulegroups o iterate the map in a sorted order * Update converter_test.go
1 parent c661417 commit 29ea225

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

pkg/i2gw/providers/common/converter.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ limitations under the License.
1717
package common
1818

1919
import (
20+
"cmp"
2021
"fmt"
22+
"slices"
2123
"strings"
2224

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

174-
for _, rg := range a.ruleGroups {
176+
// Sort the rulegroups to iterate the map in a sorted order.
177+
ruleGroupsKeys := make([]ruleGroupKey, 0, len(a.ruleGroups))
178+
for k := range a.ruleGroups {
179+
ruleGroupsKeys = append(ruleGroupsKeys, k)
180+
}
181+
182+
slices.SortFunc(ruleGroupsKeys, func(a, b ruleGroupKey) int {
183+
return cmp.Compare(a, b)
184+
})
185+
186+
for _, rgk := range ruleGroupsKeys {
187+
rg := a.ruleGroups[rgk]
175188
listener := gatewayv1.Listener{}
176189
if rg.host != "" {
177190
listener.Hostname = (*gatewayv1.Hostname)(&rg.host)

pkg/i2gw/providers/ingressnginx/converter_test.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -292,33 +292,33 @@ func Test_ToGateway(t *testing.T) {
292292
GatewayClassName: "nginx",
293293
Listeners: []gatewayv1.Listener{
294294
{
295-
Name: "foo-example-com-http",
295+
Name: "bar-example-com-http",
296296
Port: 80,
297297
Protocol: gatewayv1.HTTPProtocolType,
298-
Hostname: ptrTo(gatewayv1.Hostname("foo.example.com")),
298+
Hostname: ptrTo(gatewayv1.Hostname("bar.example.com")),
299299
},
300300
{
301-
Name: "foo-example-com-https",
301+
Name: "bar-example-com-https",
302302
Port: 443,
303303
Protocol: gatewayv1.HTTPSProtocolType,
304-
Hostname: ptrTo(gatewayv1.Hostname("foo.example.com")),
304+
Hostname: ptrTo(gatewayv1.Hostname("bar.example.com")),
305305
TLS: &gatewayv1.GatewayTLSConfig{
306306
CertificateRefs: []gatewayv1.SecretObjectReference{
307307
{Name: "example-com"},
308308
},
309309
},
310310
},
311311
{
312-
Name: "bar-example-com-http",
312+
Name: "foo-example-com-http",
313313
Port: 80,
314314
Protocol: gatewayv1.HTTPProtocolType,
315-
Hostname: ptrTo(gatewayv1.Hostname("bar.example.com")),
315+
Hostname: ptrTo(gatewayv1.Hostname("foo.example.com")),
316316
},
317317
{
318-
Name: "bar-example-com-https",
318+
Name: "foo-example-com-https",
319319
Port: 443,
320320
Protocol: gatewayv1.HTTPSProtocolType,
321-
Hostname: ptrTo(gatewayv1.Hostname("bar.example.com")),
321+
Hostname: ptrTo(gatewayv1.Hostname("foo.example.com")),
322322
TLS: &gatewayv1.GatewayTLSConfig{
323323
CertificateRefs: []gatewayv1.SecretObjectReference{
324324
{Name: "example-com"},
@@ -520,16 +520,16 @@ func Test_ToGateway(t *testing.T) {
520520
GatewayClassName: "nginx",
521521
Listeners: []gatewayv1.Listener{
522522
{
523-
Name: "foo-example-com-http",
523+
Name: "bar-example-com-http",
524524
Port: 80,
525525
Protocol: gatewayv1.HTTPProtocolType,
526-
Hostname: ptrTo(gatewayv1.Hostname("foo.example.com")),
526+
Hostname: ptrTo(gatewayv1.Hostname("bar.example.com")),
527527
},
528528
{
529-
Name: "bar-example-com-http",
529+
Name: "foo-example-com-http",
530530
Port: 80,
531531
Protocol: gatewayv1.HTTPProtocolType,
532-
Hostname: ptrTo(gatewayv1.Hostname("bar.example.com")),
532+
Hostname: ptrTo(gatewayv1.Hostname("foo.example.com")),
533533
},
534534
},
535535
},

0 commit comments

Comments
 (0)