controller: echo ip address if destination service receives ip (#186)

Signed-off-by: Sean McArthur <sean@seanmonstar.com>
This commit is contained in:
Sean McArthur 2018-01-22 16:20:13 -08:00 committed by GitHub
parent beaea5540d
commit db913e3d18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 2 deletions

View File

@ -109,6 +109,13 @@ func (s *server) Get(dest *common.Destination, stream pb.Destination_GetServer)
} }
} }
// If this is an IP address, echo it back
isIP, ip := isIPAddress(host)
if isIP {
echoIPDestination(ip, port, stream)
return nil
}
id, err := s.localKubernetesServiceIdFromDNSName(host) id, err := s.localKubernetesServiceIdFromDNSName(host)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
@ -126,6 +133,34 @@ func (s *server) Get(dest *common.Destination, stream pb.Destination_GetServer)
return err return err
} }
func isIPAddress(host string) (bool, *common.IPAddress) {
ip, err := util.ParseIPV4(host)
return err == nil, ip
}
func echoIPDestination(ip *common.IPAddress, port int, stream pb.Destination_GetServer) bool {
update := &pb.Update{
Update: &pb.Update_Add{
Add: &pb.WeightedAddrSet{
Addrs: []*pb.WeightedAddr{
&pb.WeightedAddr{
Addr: &common.TcpAddress{
Ip: ip,
Port: uint32(port),
},
Weight: 1,
},
},
},
},
}
stream.Send(update)
<-stream.Context().Done()
return true
}
func (s *server) resolveKubernetesService(id string, port int, stream pb.Destination_GetServer) error { func (s *server) resolveKubernetesService(id string, port int, stream pb.Destination_GetServer) error {
listener := endpointListener{stream: stream} listener := endpointListener{stream: stream}

View File

@ -2,8 +2,9 @@ package destination
import ( import (
"fmt" "fmt"
"github.com/stretchr/testify/assert"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func TestLocalKubernetesServiceIdFromDNSName(t *testing.T) { func TestLocalKubernetesServiceIdFromDNSName(t *testing.T) {
@ -12,7 +13,7 @@ func TestLocalKubernetesServiceIdFromDNSName(t *testing.T) {
testCases := []struct { testCases := []struct {
k8sDNSZone string k8sDNSZone string
host string host string
result *string result *string
resultErr bool resultErr bool
}{ }{
{"cluster.local", "", nil, true}, {"cluster.local", "", nil, true},
@ -88,3 +89,22 @@ func TestSplitDNSName(t *testing.T) {
}) })
} }
} }
func TestIsIPAddress(t *testing.T) {
testCases := []struct {
host string
result bool
}{
{"8.8.8.8", true},
{"example.com", false},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("%d: %+v", i, tc.host), func(t *testing.T) {
isIP, _ := isIPAddress(tc.host)
if isIP != tc.result {
t.Fatalf("Unexpected result: %+v", isIP)
}
})
}
}