Skip to content

Commit

Permalink
Write normalized scheme and host to routing.Route fields
Browse files Browse the repository at this point in the history
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 <roman.zavodskikh@zalando.de>
  • Loading branch information
Roman Zavodskikh committed Jan 4, 2024
1 parent 07dee9e commit 655ab25
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 60 deletions.
31 changes: 1 addition & 30 deletions filters/fadein/fadein.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package fadein

import (
"fmt"
"net"
"net/url"
"strings"
"time"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -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 {
Expand Down
31 changes: 1 addition & 30 deletions loadbalancer/algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"fmt"
"math"
"math/rand"
"net"
"net/url"
"sort"
"strings"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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))
Expand Down
4 changes: 4 additions & 0 deletions routing/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions routing/endpointregistry.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package routing

import (
"net"
"strings"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 655ab25

Please sign in to comment.