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)
if err != nil {
log.Error(err)
@ -126,6 +133,34 @@ func (s *server) Get(dest *common.Destination, stream pb.Destination_GetServer)
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 {
listener := endpointListener{stream: stream}

View File

@ -2,8 +2,9 @@ package destination
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
"github.com/stretchr/testify/assert"
)
func TestLocalKubernetesServiceIdFromDNSName(t *testing.T) {
@ -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)
}
})
}
}