Skip to content

Commit

Permalink
reuse discovered ip data (istio#22772)
Browse files Browse the repository at this point in the history
Signed-off-by: Rama Chavali <rama.rao@salesforce.com>
  • Loading branch information
ramaraochavali authored Apr 7, 2020
1 parent 8bcd05b commit 10c1bbd
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
6 changes: 6 additions & 0 deletions pilot/pkg/model/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ type Proxy struct {

// Indicates wheteher proxy supports IPv4 addresses
ipv4Support bool

// GlobalUnicastIP stores the globacl unicast IP if available, otherwise nil
GlobalUnicastIP string
}

var (
Expand Down Expand Up @@ -589,6 +592,9 @@ func (node *Proxy) DiscoverIPVersions() {
// skip it to prevent a panic.
continue
}
if addr.IsGlobalUnicast() {
node.GlobalUnicastIP = addr.String()
}
if addr.To4() != nil {
node.ipv4Support = true
} else {
Expand Down
1 change: 1 addition & 0 deletions pilot/pkg/networking/core/v1alpha3/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,7 @@ func TestBuildGatewayListeners(t *testing.T) {
env := buildEnv(t, []pilot_model.Config{{Spec: tt.gateway}}, []pilot_model.Config{})
proxyGateway.SetGatewaysForProxy(env.PushContext)
proxyGateway.ServiceInstances = tt.node.ServiceInstances
proxyGateway.DiscoverIPVersions()
builder := configgen.buildGatewayListeners(&proxyGateway, env.PushContext, &ListenerBuilder{})
var listeners []string
for _, l := range builder.gatewayListeners {
Expand Down
26 changes: 7 additions & 19 deletions pilot/pkg/networking/core/v1alpha3/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package v1alpha3
import (
"encoding/json"
"fmt"
"net"
"reflect"
"sort"
"strconv"
Expand Down Expand Up @@ -2096,7 +2095,6 @@ func buildListener(opts buildListenerOpts) *xdsapi.Listener {
match = chain.match
}
if len(chain.sniHosts) > 0 {
sort.Strings(chain.sniHosts)
fullWildcardFound := false
for _, h := range chain.sniHosts {
if h == "*" {
Expand All @@ -2107,6 +2105,7 @@ func buildListener(opts buildListenerOpts) *xdsapi.Listener {
}
}
if !fullWildcardFound {
sort.Strings(chain.sniHosts)
match.ServerNames = chain.sniHosts
}
}
Expand Down Expand Up @@ -2298,16 +2297,8 @@ func buildCompleteFilterChain(pluginParams *plugin.InputParams, mutable *istione
// and if there is at least one ipv4 address other than 127.0.0.1, it will use ipv4 address,
// if all addresses are ipv6 addresses then ipv6 address will be used to get wildcard and local host address.
func getActualWildcardAndLocalHost(node *model.Proxy) (string, string) {
for i := 0; i < len(node.IPAddresses); i++ {
addr := net.ParseIP(node.IPAddresses[i])
if addr == nil {
// Should not happen, invalid IP in proxy's IPAddresses slice should have been caught earlier,
// skip it to prevent a panic.
continue
}
if addr.To4() != nil {
return WildcardAddress, LocalhostAddress
}
if node.SupportsIPv4() {
return WildcardAddress, LocalhostAddress
}
return WildcardIPv6Address, LocalhostIPv6Address
}
Expand All @@ -2316,14 +2307,11 @@ func getActualWildcardAndLocalHost(node *model.Proxy) (string, string) {
// It looks for an unicast address, if none found, then the default wildcard address is used.
// This will make the inbound listener bind to instance_ip:port instead of 0.0.0.0:port where applicable.
func getSidecarInboundBindIP(node *model.Proxy) string {
defaultInboundIP, _ := getActualWildcardAndLocalHost(node)
for _, ipAddr := range node.IPAddresses {
ip := net.ParseIP(ipAddr)
// Return the IP if its a global unicast address.
if ip != nil && ip.IsGlobalUnicast() {
return ip.String()
}
// Return the IP if its a global unicast address.
if len(node.GlobalUnicastIP) > 0 {
return node.GlobalUnicastIP
}
defaultInboundIP, _ := getActualWildcardAndLocalHost(node)
return defaultInboundIP
}

Expand Down
2 changes: 2 additions & 0 deletions pilot/pkg/networking/core/v1alpha3/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@ func TestGetActualWildcardAndLocalHost(t *testing.T) {
},
}
for _, tt := range tests {
tt.proxy.DiscoverIPVersions()
wm, lh := getActualWildcardAndLocalHost(tt.proxy)
if wm != tt.expected[0] && lh != tt.expected[1] {
t.Errorf("Test %s failed, expected: %s / %s got: %s / %s", tt.name, tt.expected[0], tt.expected[1], wm, lh)
Expand Down Expand Up @@ -727,6 +728,7 @@ func testOutboundListenerConflict(t *testing.T, services ...*model.Service) {
t.Helper()
oldestService := getOldestService(services...)
p := &fakePlugin{}
proxy.DiscoverIPVersions()
listeners := buildOutboundListeners(t, p, &proxy, nil, nil, services...)
if len(listeners) != 1 {
t.Fatalf("expected %d listeners, found %d", 1, len(listeners))
Expand Down

0 comments on commit 10c1bbd

Please sign in to comment.