From 655ab25eca9c36c9e976ebea21c768458080f328 Mon Sep 17 00:00:00 2001 From: Roman Zavodskikh Date: Thu, 4 Jan 2024 16:59:15 +0100 Subject: [PATCH] Write normalized scheme and host to routing.Route fields This change is needed for proxy to easily have normalized host and use it in endpointregistry for dynamic host-wide data to be easily fetchable. Signed-off-by: Roman Zavodskikh --- filters/fadein/fadein.go | 31 +------------------------------ loadbalancer/algorithm.go | 31 +------------------------------ routing/datasource.go | 4 ++++ routing/endpointregistry.go | 29 +++++++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 60 deletions(-) diff --git a/filters/fadein/fadein.go b/filters/fadein/fadein.go index 64105c3202..49d498511e 100644 --- a/filters/fadein/fadein.go +++ b/filters/fadein/fadein.go @@ -2,9 +2,7 @@ package fadein import ( "fmt" - "net" "net/url" - "strings" "time" log "github.com/sirupsen/logrus" @@ -101,40 +99,13 @@ func NewEndpointCreated() filters.Spec { func (endpointCreated) Name() string { return filters.EndpointCreatedName } -func normalizeSchemeHost(s, h string) (string, string, error) { - // endpoint address cannot contain path, the rest is not case sensitive - s, h = strings.ToLower(s), strings.ToLower(h) - - hh, p, err := net.SplitHostPort(h) - if err != nil { - // what is the actual right way of doing this, considering IPv6 addresses, too? - if !strings.Contains(err.Error(), "missing port") { - return "", "", err - } - - p = "" - } else { - h = hh - } - - switch { - case p == "" && s == "http": - p = "80" - case p == "" && s == "https": - p = "443" - } - - h = net.JoinHostPort(h, p) - return s, h, nil -} - func normalizeEndpoint(e string) (string, string, error) { u, err := url.Parse(e) if err != nil { return "", "", err } - return normalizeSchemeHost(u.Scheme, u.Host) + return routing.NormalizeSchemeHost(u.Scheme, u.Host) } func endpointKey(scheme, host string) string { diff --git a/loadbalancer/algorithm.go b/loadbalancer/algorithm.go index 37b897ef5c..125e617315 100644 --- a/loadbalancer/algorithm.go +++ b/loadbalancer/algorithm.go @@ -5,10 +5,8 @@ import ( "fmt" "math" "math/rand" - "net" "net/url" "sort" - "strings" "sync" "sync/atomic" "time" @@ -433,7 +431,7 @@ func parseEndpoints(r *routing.Route) error { return err } - scheme, host, err := normalizeSchemeHost(eu.Scheme, eu.Host) + scheme, host, err := routing.NormalizeSchemeHost(eu.Scheme, eu.Host) if err != nil { return err } @@ -463,33 +461,6 @@ func setAlgorithm(r *routing.Route) error { return nil } -func normalizeSchemeHost(s, h string) (string, string, error) { - // endpoint address cannot contain path, the rest is not case sensitive - s, h = strings.ToLower(s), strings.ToLower(h) - - hh, p, err := net.SplitHostPort(h) - if err != nil { - // what is the actual right way of doing this, considering IPv6 addresses, too? - if !strings.Contains(err.Error(), "missing port") { - return "", "", err - } - - p = "" - } else { - h = hh - } - - switch { - case p == "" && s == "http": - p = "80" - case p == "" && s == "https": - p = "443" - } - - h = net.JoinHostPort(h, p) - return s, h, nil -} - // Do implements routing.PostProcessor func (p *algorithmProvider) Do(r []*routing.Route) []*routing.Route { rr := make([]*routing.Route, 0, len(r)) diff --git a/routing/datasource.go b/routing/datasource.go index 68b3ad465d..978e4c6521 100644 --- a/routing/datasource.go +++ b/routing/datasource.go @@ -482,6 +482,10 @@ func processRouteDef(cpm map[string]PredicateSpec, fr filters.Registry, def *esk return nil, err } + scheme, host, err = NormalizeSchemeHost(scheme, host) + if err != nil { + return nil, err + } r := &Route{Route: *def, Scheme: scheme, Host: host, Predicates: cps, Filters: fs, weight: weight} if err := processTreePredicates(r, def.Predicates); err != nil { return nil, err diff --git a/routing/endpointregistry.go b/routing/endpointregistry.go index 5ea242c93a..bdfda571d8 100644 --- a/routing/endpointregistry.go +++ b/routing/endpointregistry.go @@ -1,6 +1,8 @@ package routing import ( + "net" + "strings" "sync" "sync/atomic" "time" @@ -81,6 +83,33 @@ type RegistryOptions struct { LastSeenTimeout time.Duration } +func NormalizeSchemeHost(s, h string) (string, string, error) { + // endpoint address cannot contain path, the rest is not case sensitive + s, h = strings.ToLower(s), strings.ToLower(h) + + hh, p, err := net.SplitHostPort(h) + if err != nil { + // what is the actual right way of doing this, considering IPv6 addresses, too? + if !strings.Contains(err.Error(), "missing port") { + return "", "", err + } + + p = "" + } else { + h = hh + } + + switch { + case p == "" && s == "http": + p = "80" + case p == "" && s == "https": + p = "443" + } + + h = net.JoinHostPort(h, p) + return s, h, nil +} + func (r *EndpointRegistry) Do(routes []*Route) []*Route { now := r.now()