Skip to content

Commit

Permalink
ttl
Browse files Browse the repository at this point in the history
  • Loading branch information
psvmcc committed Mar 7, 2024
1 parent 51130dd commit dd9dd1a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Usage of /disconter:
- **disconter.service.priority** - dns discovery priority, default 1
- **disconter.service.weight** - dns discovery weight, default 1
- **disconter.service.port** - dns discovery port, default 80
- **disconter.service.ttl** - dns discovery ttl in seconds, default 0

### run container with label `disconter.service`
```
Expand Down
9 changes: 9 additions & 0 deletions pkg/discovery/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type ContainerInfo struct {
Priority uint16
Weight uint16
Port uint16
TTL uint16
}
}

Expand Down Expand Up @@ -182,6 +183,7 @@ func ListContainers(socket string) (containers []ContainerInfo, err error) {
c.DisconterService.Priority = 1
c.DisconterService.Weight = 1
c.DisconterService.Port = 80
c.DisconterService.TTL = 0

for _, v := range resp.NetworkSettings.Networks {
if v.IPAddress != "" {
Expand Down Expand Up @@ -214,6 +216,13 @@ func ListContainers(socket string) (containers []ContainerInfo, err error) {
c.DisconterService.Port = uint16(port)
}
}

if resp.Labels["disconter.service.ttl"] != "" {
ttl, err := strconv.Atoi(resp.Labels["disconter.service.ttl"])
if err == nil {
c.DisconterService.TTL = uint16(ttl)
}
}
containers[i] = c
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/handlers/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func HandleDNSRequest(w dns.ResponseWriter, r *dns.Msg) {
if (dns.TypeToString[q.Qtype] == "A" || dns.TypeToString[q.Qtype] == "ANY") && strings.HasSuffix(q.Name, "container.disconter.") {
for _, c := range discovery.ServiceContainers {
if q.Name == fmt.Sprintf("%s.container.disconter.", c.Name) {
rr, err := dns.NewRR(fmt.Sprintf("%s 0 A %s", q.Name, c.IP))
rr, err := dns.NewRR(fmt.Sprintf("%s %d A %s", q.Name, c.DisconterService.TTL, c.IP))
if err == nil {
m.Answer = append(m.Answer, rr)
}
Expand All @@ -44,7 +44,7 @@ func HandleDNSRequest(w dns.ResponseWriter, r *dns.Msg) {
if (dns.TypeToString[q.Qtype] == "A" || dns.TypeToString[q.Qtype] == "ANY") && strings.HasSuffix(q.Name, "service.disconter.") {
for _, c := range discovery.ServiceContainers {
if q.Name == fmt.Sprintf("%s.service.disconter.", c.DisconterService.Name) {
rr, err := dns.NewRR(fmt.Sprintf("%s 0 A %s", q.Name, c.IP))
rr, err := dns.NewRR(fmt.Sprintf("%s %d A %s", q.Name, c.DisconterService.TTL, c.IP))
if err == nil {
m.Answer = append(m.Answer, rr)
}
Expand All @@ -54,11 +54,11 @@ func HandleDNSRequest(w dns.ResponseWriter, r *dns.Msg) {
if dns.TypeToString[q.Qtype] == "SRV" && strings.HasSuffix(q.Name, "service.disconter.") {
for _, c := range discovery.ServiceContainers {
if q.Name == fmt.Sprintf("%s.service.disconter.", c.DisconterService.Name) || q.Name == fmt.Sprintf("_%s._tcp.service.disconter.", c.DisconterService.Name) {
rr, err := dns.NewRR(fmt.Sprintf("%s 0 SRV %d %d %d %s.container.disconter", q.Name, c.DisconterService.Priority, c.DisconterService.Weight, c.DisconterService.Port, c.Name))
rr, err := dns.NewRR(fmt.Sprintf("%s %d SRV %d %d %d %s.container.disconter", q.Name, c.DisconterService.TTL, c.DisconterService.Priority, c.DisconterService.Weight, c.DisconterService.Port, c.Name))
if err == nil {
m.Answer = append(m.Answer, rr)
}
rrA, err := dns.NewRR(fmt.Sprintf("%s.container.disconter 0 A %s", c.Name, c.IP))
rrA, err := dns.NewRR(fmt.Sprintf("%s.container.disconter %d A %s", c.Name, c.DisconterService.TTL, c.IP))
if err == nil {
m.Extra = append(m.Extra, rrA)
}
Expand Down

0 comments on commit dd9dd1a

Please sign in to comment.