Created
December 10, 2014 19:14
-
-
Save geekgonecrazy/eef4f2fa720d4d349176 to your computer and use it in GitHub Desktop.
App Container discovery via DNS TXT record
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/miekg/dns" | |
"log" | |
"net" | |
"os" | |
"strings" | |
) | |
type AcDiscovery struct { | |
Prefix string | |
UrlTmpl string | |
} | |
func main() { | |
config, _ := dns.ClientConfigFromFile("/etc/resolv.conf") | |
c := new(dns.Client) | |
m := new(dns.Msg) | |
m.SetQuestion(dns.Fqdn("_ac-discovery.example.com"), dns.TypeTXT) | |
m.RecursionDesired = true | |
r, _, err := c.Exchange(m, net.JoinHostPort(config.Servers[0], config.Port)) | |
if r == nil { | |
log.Fatalf("error: %s\n", err.Error()) | |
} | |
if r.Rcode != dns.RcodeSuccess { | |
log.Fatalf(" Invalid record returned") | |
} | |
for _, a := range r.Answer { | |
if t, ok := a.(*dns.TXT); ok { | |
log.Println(t.Txt) | |
sp := strings.Split(t.Txt[0], " ") | |
fmt.Printf("%#v\n", sp) | |
discovery := AcDiscovery{} | |
for _, s := range sp { | |
kv := strings.Split(s, "=") | |
if kv[0] == "prefix" { | |
discovery.Prefix = kv[1] | |
} | |
if kv[0] == "url-tmpl" { | |
discovery.UrlTmpl = kv[1] | |
} | |
} | |
fmt.Printf("%#v\n", discovery) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment