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