mirror of
https://github.com/tinyauthapp/tinyauth.git
synced 2026-09-22 18:43:34 +08:00
tests: add tests for kubernetes service and extractors
Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
@@ -28,7 +28,7 @@ func (k *KubernetesGRPCRouteExtractor) getHosts(hostnames []gateway.Hostname) []
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return hosts
|
||||
}
|
||||
|
||||
func (k *KubernetesGRPCRouteExtractor) Extract(route *gateway.GRPCRoute) *ExtractionResult {
|
||||
|
||||
@@ -30,28 +30,31 @@ func (k *KubernetesHTTPRouteExtractor) getHosts(hostnames []gateway.Hostname) []
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return hosts
|
||||
}
|
||||
|
||||
func (k *KubernetesHTTPRouteExtractor) getRuleMatchers(matchers []gateway.HTTPRouteMatch) []string {
|
||||
var res []string
|
||||
|
||||
for _, m := range matchers {
|
||||
pathType := m.Path.Type
|
||||
if pathType == nil {
|
||||
pathType = new(gateway.PathMatchPathPrefix)
|
||||
}
|
||||
|
||||
pathValue := m.Path.Value
|
||||
if pathValue == nil {
|
||||
pathValue = new("/")
|
||||
}
|
||||
|
||||
if *pathType != gateway.PathMatchPathPrefix {
|
||||
if m.Path == nil {
|
||||
res = append(res, "/")
|
||||
continue
|
||||
}
|
||||
|
||||
res = append(res, *pathValue)
|
||||
pathType := gateway.PathMatchPathPrefix
|
||||
if m.Path.Type != nil {
|
||||
pathType = *m.Path.Type
|
||||
}
|
||||
if pathType != gateway.PathMatchPathPrefix {
|
||||
continue
|
||||
}
|
||||
|
||||
pathValue := "/"
|
||||
if m.Path.Value != nil {
|
||||
pathValue = *m.Path.Value
|
||||
}
|
||||
res = append(res, pathValue)
|
||||
}
|
||||
|
||||
return res
|
||||
@@ -61,6 +64,10 @@ func (k *KubernetesHTTPRouteExtractor) getPaths(rules []gateway.HTTPRouteRule) [
|
||||
var paths []string
|
||||
|
||||
for _, rule := range rules {
|
||||
if len(rule.Matches) == 0 {
|
||||
paths = append(paths, "/")
|
||||
continue
|
||||
}
|
||||
matchers := k.getRuleMatchers(rule.Matches)
|
||||
paths = append(paths, matchers...)
|
||||
}
|
||||
|
||||
@@ -24,6 +24,10 @@ func NewKubernetesIngressExtractor(i KubernetesIngressExtractorInput) *Kubernete
|
||||
func (k *KubernetesIngressExtractor) getPaths(rule networking.IngressRule) []string {
|
||||
var paths []string
|
||||
|
||||
if rule.HTTP == nil {
|
||||
return paths
|
||||
}
|
||||
|
||||
for _, path := range rule.HTTP.Paths {
|
||||
paths = append(paths, path.Path)
|
||||
}
|
||||
@@ -47,15 +51,11 @@ func (k *KubernetesIngressExtractor) getHosts(rules []networking.IngressRule) []
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return hosts
|
||||
}
|
||||
|
||||
func (k *KubernetesIngressExtractor) Extract(ingress *networking.Ingress) *ExtractionResult {
|
||||
annotations := ingress.GetAnnotations()
|
||||
if len(annotations) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
hosts := k.getHosts(ingress.Spec.Rules)
|
||||
|
||||
return &ExtractionResult{
|
||||
|
||||
@@ -69,7 +69,8 @@ var supportedResources = []watchedResource{
|
||||
}
|
||||
|
||||
func hostMatchesHostname(host string, hostname string) bool {
|
||||
host = strings.ToLower(host)
|
||||
host = normalizeDomain(host)
|
||||
hostname = normalizeDomain(hostname)
|
||||
if suffix, ok := strings.CutPrefix(host, "*."); ok {
|
||||
return strings.HasSuffix(hostname, "."+suffix)
|
||||
}
|
||||
@@ -284,21 +285,26 @@ func (k *KubernetesService) getEntry(domain string, locator func(name string, ap
|
||||
func (k *KubernetesService) updateFromItem(res watchedResource, typedItem *typedItem) {
|
||||
var result *ExtractionResult
|
||||
|
||||
if typedItem == nil {
|
||||
k.log.App.Warn().Str("res", res.pretty()).Msg("Resource is nil, skipping")
|
||||
return
|
||||
}
|
||||
|
||||
switch typedItem.typ {
|
||||
case ResourceTypeIngress:
|
||||
if typedItem.ingress != nil {
|
||||
if typedItem.ingress == nil {
|
||||
k.log.App.Warn().Str("res", res.pretty()).Msg("Ingress is nil, skipping")
|
||||
return
|
||||
}
|
||||
result = k.extractors.ingress.Extract(typedItem.ingress)
|
||||
case ResourceTypeHTTPRoute:
|
||||
if typedItem.route != nil {
|
||||
if typedItem.route == nil {
|
||||
k.log.App.Warn().Str("res", res.pretty()).Msg("HTTPRoute is nil, skipping")
|
||||
return
|
||||
}
|
||||
result = k.extractors.httproute.Extract(typedItem.route)
|
||||
case ResourceTypeGRPCRoute:
|
||||
if typedItem.grpc != nil {
|
||||
if typedItem.grpc == nil {
|
||||
k.log.App.Warn().Str("res", res.pretty()).Msg("GRPCRoute is nil, skipping")
|
||||
return
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user