Skip to content

Commit b3fa2a3

Browse files
committed
fix: r
Signed-off-by: ashing <[email protected]>
1 parent 7f9c769 commit b3fa2a3

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

internal/controller/utils.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -458,39 +458,51 @@ func HostnamesIntersect(a, b string) bool {
458458
return HostnamesMatch(a, b) || HostnamesMatch(b, a)
459459
}
460460

461+
// HostnamesMatch checks that the hostnameB matches the hostnameA. HostnameA is treated as mask
462+
// to be checked against the hostnameB.
461463
func HostnamesMatch(hostnameA, hostnameB string) bool {
462-
labelsA := strings.Split(hostnameA, ".")
463-
labelsB := strings.Split(hostnameB, ".")
464+
// the hostnames are in the form of "foo.bar.com"; split them
465+
// in a slice of substrings
466+
hostnameALabels := strings.Split(hostnameA, ".")
467+
hostnameBLabels := strings.Split(hostnameB, ".")
464468

465-
var i, j int
469+
var a, b int
466470
var wildcard bool
467471

468-
for i, j = 0, 0; i < len(labelsA) && j < len(labelsB); i, j = i+1, j+1 {
472+
// iterate over the parts of both the hostnames
473+
for a, b = 0, 0; a < len(hostnameALabels) && b < len(hostnameBLabels); a, b = a+1, b+1 {
474+
var matchFound bool
475+
476+
// if the current part of B is a wildcard, we need to find the first
477+
// A part that matches with the following B part
469478
if wildcard {
470-
for ; j < len(labelsB); j++ {
471-
if labelsA[i] == labelsB[j] {
479+
for ; b < len(hostnameBLabels); b++ {
480+
if hostnameALabels[a] == hostnameBLabels[b] {
481+
matchFound = true
472482
break
473483
}
474484
}
475-
if j == len(labelsB) {
476-
return false
477-
}
478485
}
479486

480-
if labelsA[i] == "*" {
487+
// if no match was found, the hostnames don't match
488+
if wildcard && !matchFound {
489+
return false
490+
}
491+
492+
// check if at least on of the current parts are a wildcard; if so, continue
493+
if hostnameALabels[a] == "*" {
481494
wildcard = true
482-
j--
483495
continue
484496
}
485-
497+
// reset the wildcard variables
486498
wildcard = false
487499

488-
if labelsA[i] != labelsB[j] {
500+
// if the current a part is different from the b part, the hostnames are incompatible
501+
if hostnameALabels[a] != hostnameBLabels[b] {
489502
return false
490503
}
491504
}
492-
493-
return len(labelsA)-i == len(labelsB)-j
505+
return len(hostnameBLabels)-b == len(hostnameALabels)-a
494506
}
495507

496508
func routeMatchesListenerAllowedRoutes(

0 commit comments

Comments
 (0)