Skip to content

Commit aa1e79b

Browse files
committed
Simplify matching
1 parent 03b4ed4 commit aa1e79b

File tree

2 files changed

+15
-29
lines changed

2 files changed

+15
-29
lines changed

internal/state/dataplane/configuration_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,22 +2027,22 @@ func TestHostnameMoreSpecific(t *testing.T) {
20272027
msg: "host1 has value; host2 empty",
20282028
},
20292029
{
2030-
host1: helpers.GetPointer(v1beta1.Hostname("foo.bar.example.com")),
2031-
host2: helpers.GetPointer(v1beta1.Hostname("foo.example.com")),
2032-
host1Wins: true,
2033-
msg: "host1 has more segments than host2",
2030+
host1: helpers.GetPointer(v1beta1.Hostname("")),
2031+
host2: helpers.GetPointer(v1beta1.Hostname("example.com")),
2032+
host1Wins: false,
2033+
msg: "host2 has value; host1 empty",
20342034
},
20352035
{
2036-
host1: helpers.GetPointer(v1beta1.Hostname("somelongname.example.com")),
2037-
host2: helpers.GetPointer(v1beta1.Hostname("foo.bar.example.com")),
2038-
host1Wins: false,
2039-
msg: "host2 has more segments than host1",
2036+
host1: helpers.GetPointer(v1beta1.Hostname("foo.example.com")),
2037+
host2: helpers.GetPointer(v1beta1.Hostname("*.example.com")),
2038+
host1Wins: true,
2039+
msg: "host1 more specific than host2",
20402040
},
20412041
{
2042-
host1: helpers.GetPointer(v1beta1.Hostname("example.com")),
2043-
host2: helpers.GetPointer(v1beta1.Hostname("longerexample.com")),
2042+
host1: helpers.GetPointer(v1beta1.Hostname("*.example.com")),
2043+
host2: helpers.GetPointer(v1beta1.Hostname("foo.example.com")),
20442044
host1Wins: false,
2045-
msg: "host2 longer than host1",
2045+
msg: "host2 more specific than host1",
20462046
},
20472047
}
20482048

internal/state/graph/httproute.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -441,15 +441,14 @@ func match(listenerHost, routeHost string) bool {
441441
// This function assumes that the two hostnames match each other, either:
442442
// - Exactly
443443
// - One as a substring of the other
444-
// - Both as substrings of some parent wildcard
445444
func GetMoreSpecificHostname(hostname1, hostname2 string) string {
446445
if hostname1 == hostname2 {
447446
return hostname1
448447
}
449-
450448
if hostname1 == "" {
451449
return hostname2
452-
} else if hostname2 == "" {
450+
}
451+
if hostname2 == "" {
453452
return hostname1
454453
}
455454

@@ -468,25 +467,12 @@ func GetMoreSpecificHostname(hostname1, hostname2 string) string {
468467
}
469468

470469
return hostname2
471-
} else if strings.HasPrefix(hostname2, "*.") {
472-
return hostname1
473470
}
474-
475-
subdomains1 := strings.Split(hostname1, ".")
476-
subdomains2 := strings.Split(hostname2, ".")
477-
478-
// Compare number of subdomains
479-
if len(subdomains1) > len(subdomains2) {
480-
return hostname1
481-
} else if len(subdomains1) < len(subdomains2) {
482-
return hostname2
483-
}
484-
485-
if len(hostname1) > len(hostname2) {
471+
if strings.HasPrefix(hostname2, "*.") {
486472
return hostname1
487473
}
488474

489-
return hostname2
475+
return ""
490476
}
491477

492478
func routeAllowedByListener(

0 commit comments

Comments
 (0)