Changed to new URL scheme with container identifier

Implementation matches design.md better:
 - http action changes from POST /add to PUT /name
 - each record now has the container identifier
This commit is contained in:
Bryan Boreham committed 2014-10-14 11:55:45 +01:00
1 parent 50d343e2fb
commit 6bff651e87
4 files changed
+49 -20

No files matched your search

+31 -13
View File
@@ -1,30 +1,48 @@
package weavedns
import (
"errors"
"fmt"
"log"
"net"
"net/http"
"strings"
)
// Parse a URL of the form PUT /name/<identifier>/<ip-address>
func parseUrl(url string) (identifier string, ipaddr string, err error) {
parts := strings.Split(url, "/")
if len(parts) != 4 {
return "", "", errors.New("Unable to parse url: " + url)
}
return parts[2], parts[3], nil
}
func ListenHttp(db Zone, port int) {
http.HandleFunc("/add", func(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
addr := r.FormValue("ip")
weave_cidr := r.FormValue("weave_cidr")
if name != "" && addr != "" && weave_cidr != "" {
ip := net.ParseIP(addr)
http.HandleFunc("/name/", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "PUT":
identifier, weave_ipstr, err := parseUrl(r.URL.Path)
name := r.FormValue("fqdn")
prefix := r.FormValue("routing_prefix")
local_ip := r.FormValue("local_ip")
if identifier == "" || weave_ipstr == "" || name == "" || prefix == "" || local_ip == "" {
log.Printf("Invalid request: %s, %s", r.URL, r.Form)
return
}
ip := net.ParseIP(local_ip)
if ip == nil {
log.Printf("Invalid IP in request: %s", local_ip)
return
}
weave_cidr := weave_ipstr + "/" + prefix
weave_ip, subnet, err := net.ParseCIDR(weave_cidr)
if err == nil && ip != nil {
log.Printf("Adding %s (%s) -> %s", name, addr, weave_cidr)
db.AddRecord(name, ip, weave_ip, subnet)
if err == nil {
log.Printf("Adding %s (%s) -> %s", name, local_ip, weave_cidr)
db.AddRecord(identifier, name, ip, weave_ip, subnet)
} else if err != nil {
log.Printf("Invalid CIDR in request: %s", weave_cidr)
} else {
log.Printf("Invalid IP in request: %s", addr)
}
} else {
log.Printf("Invalid request: %s, %s, %s", name, addr, weave_cidr)
}
})
address := fmt.Sprintf(":%d", port)
+11 -2
View File
@@ -6,12 +6,20 @@ import (
"net"
"net/http"
"net/url"
"strings"
"testing"
"time"
)
func putForm(url string, data url.Values) (resp *http.Response, err error) {
req, err := http.NewRequest("PUT", url, strings.NewReader(data.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return http.DefaultClient.Do(req)
}
func TestHttp(t *testing.T) {
var (
container_id = "deadbeef"
success_test_name = "test1.weave."
test_addr1 = "10.0.2.1/24"
docker_ip = "9.8.7.6"
@@ -24,8 +32,9 @@ func TestHttp(t *testing.T) {
time.Sleep(100 * time.Millisecond) // Allow for http server to get going
resp, err := http.PostForm(fmt.Sprintf("http://localhost:%d/add", port),
url.Values{"name": {success_test_name}, "ip": {docker_ip}, "weave_cidr": {test_addr1}})
addr_parts := strings.Split(test_addr1, "/")
resp, err := putForm(fmt.Sprintf("http://localhost:%d/name/%s/%s", port, container_id, addr_parts[0]),
url.Values{"fqdn": {success_test_name}, "local_ip": {docker_ip}, "routing_prefix": {addr_parts[1]}})
if err != nil {
t.Fatal(err)
}
+3 -2
View File
@@ -9,7 +9,8 @@ import (
)
var (
test_addr1 = "10.0.2.1/24"
container_id = "deadbeef"
test_addr1 = "10.0.2.1/24"
)
func sendQuery(name string, querytype uint16) error {
@@ -33,7 +34,7 @@ func TestServerSimpleQuery(t *testing.T) {
var zone = new(ZoneDb)
docker_ip := net.ParseIP("9.8.7.6")
weave_ip, subnet, _ := net.ParseCIDR(test_addr1)
zone.AddRecord("test.weave.", docker_ip, weave_ip, subnet)
zone.AddRecord(container_id, "test.weave.", docker_ip, weave_ip, subnet)
mdnsServer, err := NewMDNSServer(zone)
if err != nil {
+4 -3
View File
@@ -8,11 +8,12 @@ import (
)
type Zone interface {
AddRecord(string, net.IP, net.IP, *net.IPNet) error
AddRecord(string, string, net.IP, net.IP, *net.IPNet) error
MatchLocal(string) (net.IP, error)
}
type Record struct {
Ident string
Name string
Ip net.IP
WeaveIp net.IP
@@ -47,9 +48,9 @@ func (zone *ZoneDb) MatchLocal(name string) (net.IP, error) {
return zone.match(name)
}
func (zone *ZoneDb) AddRecord(name string, ip net.IP, weave_ip net.IP, weave_subnet *net.IPNet) error {
func (zone *ZoneDb) AddRecord(identifier string, name string, ip net.IP, weave_ip net.IP, weave_subnet *net.IPNet) error {
zone.mx.Lock()
defer zone.mx.Unlock()
zone.recs = append(zone.recs, Record{dns.Fqdn(name), ip, weave_ip, weave_subnet})
zone.recs = append(zone.recs, Record{identifier, dns.Fqdn(name), ip, weave_ip, weave_subnet})
return nil
}