@ -15,6 +15,7 @@ package kubernetes
import (
"context"
"strings"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
@ -193,18 +194,16 @@ func (i *Ingress) buildIngress(ingress ingressAdaptor) *targetgroup.Group {
}
tg . Labels = ingressLabels ( ingress )
tlsHosts := make ( map [ string ] struct { } )
for _ , host := range ingress . tlsHosts ( ) {
tlsHosts [ host ] = struct { } { }
}
for _ , rule := range ingress . rules ( ) {
scheme := "http"
paths := pathsFromIngressPaths ( rule . paths ( ) )
scheme := "http"
_ , isTLS := tlsHosts [ rule . host ( ) ]
if isTLS {
out :
for _ , pattern := range ingress . tlsHosts ( ) {
if matchesHostnamePattern ( pattern , rule . host ( ) ) {
scheme = "https"
break out
}
}
for _ , path := range paths {
@ -219,3 +218,34 @@ func (i *Ingress) buildIngress(ingress ingressAdaptor) *targetgroup.Group {
return tg
}
// matchesHostnamePattern returns true if the host matches a wildcard DNS
// pattern or pattern and host are equal
func matchesHostnamePattern ( pattern , host string ) bool {
// check for exact match
if pattern == host {
return true
}
patternParts := strings . Split ( pattern , "." )
hostParts := strings . Split ( host , "." )
// if they are not equal, we cna check if we need to match
// on a wildcard or else give up
if len ( patternParts ) == 0 || patternParts [ 0 ] != "*" {
return false
}
// to get a valid wildcard match the parts will need to be the same length
if len ( patternParts ) != len ( hostParts ) {
return false
}
for i := 1 ; i < len ( patternParts ) ; i ++ {
if patternParts [ i ] != hostParts [ i ] {
return false
}
}
return true
}