Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #208 from mesosphere/slave-srvs
Browse files Browse the repository at this point in the history
Generate slave SRV records
  • Loading branch information
tsenart committed Aug 1, 2015
2 parents 11ecb7c + 2b475ea commit 95e1cc3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion factories/fake.json
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@
},
"hostname": "1.2.3.12",
"id": "20140827-000744-3041283216-5050-2116-1",
"pid": "slave(1)@1.2.3:12",
"pid": "slave(1)@1.2.3.12:5051",
"registered_time": 1414913537.19731,
"reregistered_time": 1414913537.19735,
"resources": {
Expand Down
20 changes: 18 additions & 2 deletions records/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package records

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand All @@ -15,6 +16,8 @@ import (

"github.com/mesosphere/mesos-dns/logging"
"github.com/mesosphere/mesos-dns/records/labels"

"github.com/mesos/mesos-go/upid"
)

// Map host/service name to DNS answer
Expand Down Expand Up @@ -69,6 +72,16 @@ type Framework struct {
type Slave struct {
ID string `json:"id"`
Hostname string `json:"hostname"`
PID PID `json:"pid"`
}

// PID holds a Mesos PID and implements the json.Unmarshaler interface.
type PID struct{ *upid.UPID }

// UnmarshalJSON implements the json.Unmarshaler interface for PIDs.
func (p *PID) UnmarshalJSON(data []byte) (err error) {
p.UPID, err = upid.Parse(string(bytes.Trim(data, `" `)))
return err
}

// StateJSON holds the state defined in the /state.json Mesos HTTP endpoint.
Expand Down Expand Up @@ -264,9 +277,12 @@ func (rg *RecordGenerator) InsertState(sj StateJSON, domain string, ns string,
rg.As = make(rrs)

for _, slave := range sj.Slaves {
address, ok := hostToIP4(slave.Hostname)
address, ok := hostToIP4(slave.PID.Host)
if ok {
rg.insertRR("slave."+domain+".", address, "A")
a := "slave." + domain + "."
rg.insertRR(a, address, "A")
srv := net.JoinHostPort(a, slave.PID.Port)
rg.insertRR("_slave._tcp."+domain+".", srv, "SRV")
} else {
logging.VeryVerbose.Printf("string '%q' for slave with id %q is not a valid IP address", address, slave.ID)
address = labels.AsDomainFrag(address, spec)
Expand Down
22 changes: 22 additions & 0 deletions records/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"
"testing/quick"

"github.com/mesos/mesos-go/upid"
"github.com/mesosphere/mesos-dns/logging"
"github.com/mesosphere/mesos-dns/records/labels"
)
Expand Down Expand Up @@ -249,13 +250,34 @@ func TestInsertState(t *testing.T) {
"liquor-store-7581-1.marathon.mesos.:31737",
}},
{rg.SRVs, "SRV", "_liquor-store.marathon.mesos.", nil},
{rg.SRVs, "SRV", "_slave._tcp.mesos.", []string{"slave.mesos.:5051"}},
} {
if got := tt.rrs[tt.name]; !reflect.DeepEqual(got, tt.want) {
t.Errorf("test #%d: %s record for %q: got: %q, want: %q", i, tt.kind, tt.name, got, tt.want)
}
}
}

func TestPID_UnmarshalJSON(t *testing.T) {
for i, tt := range []struct {
data string
want PID
err error
}{
{`"slave(1)@127.0.0.1:5051"`, PID{&upid.UPID{"slave(1)", "127.0.0.1", "5051"}}, nil},
{` "slave(1)@127.0.0.1:5051" `, PID{&upid.UPID{"slave(1)", "127.0.0.1", "5051"}}, nil},
{`" slave(1)@127.0.0.1:5051 "`, PID{&upid.UPID{"slave(1)", "127.0.0.1", "5051"}}, nil},
} {
var pid PID
if err := json.Unmarshal([]byte(tt.data), &pid); !reflect.DeepEqual(err, tt.err) {
t.Errorf("test #%d: got err: %v, want: %v", i, err, tt.want)
}
if got := pid; !reflect.DeepEqual(got, tt.want) {
t.Errorf("test #%d: got: %v, want: %v", i, got, tt.want)
}
}
}

// ensure we only generate one A record for each host
func TestNTasks(t *testing.T) {
rg := &RecordGenerator{}
Expand Down

0 comments on commit 95e1cc3

Please sign in to comment.