Merge pull request #243 from rade/240_dns_without_docker_net

LGTM.  Fixes #240
This commit is contained in:
Bryan Boreham committed 2014-11-28 12:16:40 +00:00
commit f2ef3dcfe7
6 files changed
+51 -64

No files matched your search

+14 -21
View File
@@ -45,29 +45,22 @@ func ListenHttp(domain string, db Zone, port int) {
switch r.Method {
case "PUT":
ident, weaveIPstr, err := parseUrl(r.URL.Path)
ident, ipStr, err := parseUrl(r.URL.Path)
name := r.FormValue("fqdn")
localIPstr := r.FormValue("local_ip")
if ident == "" || weaveIPstr == "" || name == "" || localIPstr == "" {
if ident == "" || ipStr == "" || name == "" {
reqError("Invalid request", "Invalid request: %s, %s", r.URL, r.Form)
return
}
localIP := net.ParseIP(localIPstr)
if localIP == nil {
reqError("Invalid IP in request", "Invalid IP in request: %s", localIPstr)
return
}
weaveIP := net.ParseIP(weaveIPstr)
if weaveIP == nil {
reqError("Invalid weave IP", "Invalid weave IP in request: %s", weaveIPstr)
ip := net.ParseIP(ipStr)
if ip == nil {
reqError("Invalid IP", "Invalid IP in request: %s", ipStr)
return
}
if dns.IsSubDomain(domain, name) {
Info.Printf("Adding %s (%s) -> %s", name, localIPstr, weaveIPstr)
if err = db.AddRecord(ident, name, localIP, weaveIP); err != nil {
Info.Printf("Adding %s -> %s", name, ipStr)
if err = db.AddRecord(ident, name, ip); err != nil {
if dup, ok := err.(DuplicateError); !ok {
httpErrorAndLog(
Error, w, "Internal error", http.StatusInternalServerError,
@@ -83,19 +76,19 @@ func ListenHttp(domain string, db Zone, port int) {
}
case "DELETE":
ident, weaveIPstr, err := parseUrl(r.URL.Path)
if ident == "" || weaveIPstr == "" {
ident, ipStr, err := parseUrl(r.URL.Path)
if ident == "" || ipStr == "" {
reqError("Invalid Request", "Invalid request: %s, %s", r.URL, r.Form)
return
}
weaveIP := net.ParseIP(weaveIPstr)
if weaveIP == nil {
reqError("Invalid IP in request", "Invalid IP in request: %s", weaveIPstr)
ip := net.ParseIP(ipStr)
if ip == nil {
reqError("Invalid IP in request", "Invalid IP in request: %s", ipStr)
return
}
Info.Printf("Deleting %s (%s)", ident, weaveIPstr)
if err = db.DeleteRecord(ident, weaveIP); err != nil {
Info.Printf("Deleting %s (%s)", ident, ipStr)
if err = db.DeleteRecord(ident, ip); err != nil {
if _, ok := err.(LookupError); !ok {
httpErrorAndLog(
Error, w, "Internal error", http.StatusInternalServerError,
+4 -4
View File
@@ -42,11 +42,11 @@ func TestHttp(t *testing.T) {
assertStatus(t, resp.StatusCode, http.StatusOK, "http response")
// Check that the address is now there.
ip, err := zone.MatchLocal(successTestName)
foundIP, err := zone.MatchLocal(successTestName)
assertNoErr(t, err)
weaveIP, _, _ := net.ParseCIDR(testAddr1)
if !ip.Equal(weaveIP) {
t.Fatal("Unexpected result for", successTestName, ip)
ip, _, _ := net.ParseCIDR(testAddr1)
if !foundIP.Equal(ip) {
t.Fatal("Unexpected result for", successTestName, foundIP)
}
// Adding exactly the same address should be OK
+3 -4
View File
@@ -32,9 +32,8 @@ func sendQuery(name string, querytype uint16) error {
func TestServerSimpleQuery(t *testing.T) {
log.Println("TestServerSimpleQuery starting")
var zone = new(ZoneDb)
dockerIP := net.ParseIP("9.8.7.6")
weaveIP, _, _ := net.ParseCIDR(testAddr1)
zone.AddRecord(containerID, "test.weave.", dockerIP, weaveIP)
ip, _, _ := net.ParseCIDR(testAddr1)
zone.AddRecord(containerID, "test.weave.", ip)
mdnsServer, err := NewMDNSServer(zone)
assertNoErr(t, err)
@@ -74,7 +73,7 @@ func TestServerSimpleQuery(t *testing.T) {
if receivedCount != 1 {
t.Fatal("Unexpected result count for test.weave", receivedCount)
}
if !receivedAddr.Equal(weaveIP) {
if !receivedAddr.Equal(ip) {
t.Fatal("Unexpected result for test.weave", receivedAddr)
}
+17 -18
View File
@@ -7,18 +7,17 @@ import (
)
type Zone interface {
AddRecord(ident string, name string, localIP net.IP, weaveIP net.IP) error
DeleteRecord(ident string, weaveIp net.IP) error
AddRecord(ident string, name string, ip net.IP) error
DeleteRecord(ident string, ip net.IP) error
DeleteRecordsFor(ident string) error
MatchLocal(name string) (net.IP, error)
MatchLocalIP(ip net.IP) (string, error)
}
type Record struct {
Ident string
Name string
LocalIP net.IP
WeaveIP net.IP
Ident string
Name string
IP net.IP
}
// Very simple data structure for now, with linear searching.
@@ -35,13 +34,13 @@ func (ops LookupError) Error() string {
}
type DuplicateError struct {
Name string
WeaveIP net.IP
Ident string
Name string
IP net.IP
Ident string
}
func (err DuplicateError) Error() string {
return "Duplicate " + err.Name + "," + err.WeaveIP.String() + " for identity " + err.Ident
return "Duplicate " + err.Name + "," + err.IP.String() + " for identity " + err.Ident
}
func (zone *ZoneDb) indexOf(match func(Record) bool) int {
@@ -58,7 +57,7 @@ func (zone *ZoneDb) MatchLocal(name string) (net.IP, error) {
defer zone.mx.RUnlock()
for _, r := range zone.recs {
if r.Name == name {
return r.WeaveIP, nil
return r.IP, nil
}
}
return nil, LookupError(name)
@@ -68,30 +67,30 @@ func (zone *ZoneDb) MatchLocalIP(ip net.IP) (string, error) {
zone.mx.RLock()
defer zone.mx.RUnlock()
for _, r := range zone.recs {
if r.WeaveIP.Equal(ip) {
if r.IP.Equal(ip) {
return r.Name, nil
}
}
return "", LookupError(ip.String())
}
func (zone *ZoneDb) AddRecord(ident string, name string, localIP net.IP, weaveIP net.IP) error {
func (zone *ZoneDb) AddRecord(ident string, name string, ip net.IP) error {
zone.mx.Lock()
defer zone.mx.Unlock()
fqdn := dns.Fqdn(name)
if index := zone.indexOf(
func(r Record) bool { return r.Name == fqdn && r.WeaveIP.Equal(weaveIP) }); index != -1 {
return DuplicateError{fqdn, weaveIP, zone.recs[index].Ident}
func(r Record) bool { return r.Name == fqdn && r.IP.Equal(ip) }); index != -1 {
return DuplicateError{fqdn, ip, zone.recs[index].Ident}
}
zone.recs = append(zone.recs, Record{ident, fqdn, localIP, weaveIP})
zone.recs = append(zone.recs, Record{ident, fqdn, ip})
return nil
}
func (zone *ZoneDb) DeleteRecord(ident string, weaveIP net.IP) error {
func (zone *ZoneDb) DeleteRecord(ident string, ip net.IP) error {
zone.mx.Lock()
defer zone.mx.Unlock()
if index := zone.indexOf(
func(r Record) bool { return r.Ident == ident && r.WeaveIP.Equal(weaveIP) }); index == -1 {
func(r Record) bool { return r.Ident == ident && r.IP.Equal(ip) }); index == -1 {
return LookupError(ident)
} else {
zone.recs = append(zone.recs[:index], zone.recs[index+1:]...)
+11 -15
View File
@@ -10,44 +10,42 @@ func TestZone(t *testing.T) {
containerID = "deadbeef"
successTestName = "test1.weave."
testAddr1 = "10.0.2.1/24"
dockerIP = "9.8.7.6"
)
var zone = new(ZoneDb)
ip := net.ParseIP(dockerIP)
weaveIP, _, _ := net.ParseCIDR(testAddr1)
err := zone.AddRecord(containerID, successTestName, ip, weaveIP)
ip, _, _ := net.ParseCIDR(testAddr1)
err := zone.AddRecord(containerID, successTestName, ip)
assertNoErr(t, err)
// Add a few more records to make the job harder
err = zone.AddRecord("abcdef0123", "adummy.weave.", net.ParseIP("1.1.1.1"), net.ParseIP("10.0.0.1"))
err = zone.AddRecord("abcdef0123", "adummy.weave.", net.ParseIP("10.0.0.1"))
assertNoErr(t, err)
err = zone.AddRecord("0123abcdef", "zdummy.weave.", net.ParseIP("2.2.2.2"), net.ParseIP("10.0.0.2"))
err = zone.AddRecord("0123abcdef", "zdummy.weave.", net.ParseIP("10.0.0.2"))
assertNoErr(t, err)
// Check that the address is now there.
foundIP, err := zone.MatchLocal(successTestName)
assertNoErr(t, err)
if !foundIP.Equal(weaveIP) {
if !foundIP.Equal(ip) {
t.Fatal("Unexpected result for", successTestName, foundIP)
}
// See if we can find the address by IP.
foundName, err := zone.MatchLocalIP(weaveIP)
foundName, err := zone.MatchLocalIP(ip)
assertNoErr(t, err)
if foundName != successTestName {
t.Fatal("Unexpected result for", weaveIP, foundName)
t.Fatal("Unexpected result for", ip, foundName)
}
// Now try to add the same address again
err = zone.AddRecord(containerID, successTestName, ip, weaveIP)
err = zone.AddRecord(containerID, successTestName, ip)
assertErrorType(t, err, (*DuplicateError)(nil), "duplicate add")
// Now delete the record
err = zone.DeleteRecord(containerID, weaveIP)
err = zone.DeleteRecord(containerID, ip)
assertNoErr(t, err)
// Check that the address is not there now.
@@ -65,13 +63,11 @@ func TestDeleteFor(t *testing.T) {
name = "foo.weave."
addr1 = "10.1.2.3/24"
addr2 = "10.2.7.8/24"
dockerIP = "172.16.0.4"
)
zone := new(ZoneDb)
ip := net.ParseIP(dockerIP)
for _, addr := range []string{addr1, addr2} {
weaveIP, _, _ := net.ParseCIDR(addr)
err := zone.AddRecord(id, name, ip, weaveIP)
ip, _, _ := net.ParseCIDR(addr)
err := zone.AddRecord(id, name, ip)
assertNoErr(t, err)
}
+2 -2
View File
@@ -316,7 +316,7 @@ tell_dns() {
CONTAINER=$(docker inspect --format '{{.Id}}' $2 2>/dev/null)
# extract IP address and routing prefix from CIDR
WEAVE_ADDR_IP=$(echo $3 | sed -e 's/\([^/]*\)\/\(.*\)/\1/')
MORE_ARGS=$(docker inspect --format '--data-urlencode local_ip={{ .NetworkSettings.IPAddress }} --data-urlencode fqdn={{ .Config.Hostname }}.{{ .Config.Domainname }}.' $CONTAINER 2>/dev/null) && true
MORE_ARGS=$(docker inspect --format '--data-urlencode fqdn={{ .Config.Hostname }}.{{ .Config.Domainname }}.' $CONTAINER 2>/dev/null) && true
http_call $DNS_CONTAINER_NAME $DNS_HTTP_PORT $1 /name/$CONTAINER/$WEAVE_ADDR_IP $MORE_ARGS
}
@@ -334,7 +334,7 @@ populate_dns() {
fi
for CONTAINER in $(docker ps -q); do
MORE_ARGS=$(docker inspect --format '--data-urlencode local_ip={{ .NetworkSettings.IPAddress }} --data-urlencode fqdn={{ .Config.Hostname }}.{{ .Config.Domainname }}.' $CONTAINER 2>/dev/null) && true
MORE_ARGS=$(docker inspect --format '--data-urlencode fqdn={{ .Config.Hostname }}.{{ .Config.Domainname }}.' $CONTAINER 2>/dev/null) && true
if CONTAINER_ADDRS=$(with_container_netns $CONTAINER container_weave_addrs 2>&1) ; then
CONTAINER_IPS=$(echo "$CONTAINER_ADDRS" | grep -o 'inet .*' | sed -e 's/inet \([^/]*\)\/\(.*\)/\1/')
for IP in $CONTAINER_IPS; do