// ListenAndServe starts a DNS server that exposes services and values stored in etcd (if etcdclient // is not nil). It will block until the server exits. // TODO: hoist the service accessor out of this package so it can be reused. func ListenAndServe(config *server.Config, client *client.Client, etcdclient *etcd.Client) error { stop := make(chan struct{}) accessor := NewCachedServiceAccessor(client, stop) resolver := NewServiceResolver(config, accessor, client, openshiftFallback) resolvers := server.FirstBackend{resolver} if etcdclient != nil { resolvers = append(resolvers, backendetcd.NewBackend(etcdclient, &backendetcd.Config{ Ttl: config.Ttl, Priority: config.Priority, })) } s := server.New(resolvers, config) defer close(stop) return s.Run() }
func newTestServer(t *testing.T, c bool) *server { Port += 10 StrPort = strconv.Itoa(Port) s := new(server) client := etcd.NewClient([]string{"http://127.0.0.1:4001"}) // TODO(miek): why don't I use NewServer?? s.group = new(sync.WaitGroup) s.scache = cache.New(100, 0) s.rcache = cache.New(100, 0) if c { s.rcache = cache.New(100, 60) // 100 items, 60s ttl } s.config = new(Config) s.config.Domain = "skydns.test." s.config.DnsAddr = "127.0.0.1:" + StrPort s.config.Nameservers = []string{"8.8.4.4:53"} SetDefaults(s.config) s.config.Local = "104.server1.development.region1.skydns.test." s.config.Priority = 10 s.config.RCacheTtl = RCacheTtl s.config.Ttl = 3600 s.config.Ndots = 2 prometheusPort = "12300" prometheusSubsystem = "test" prometheusNamespace = "test" if !metricsDone { metricsDone = true Metrics() } s.dnsUDPclient = &dns.Client{Net: "udp", ReadTimeout: 2 * s.config.ReadTimeout, WriteTimeout: 2 * s.config.ReadTimeout, SingleInflight: true} s.dnsTCPclient = &dns.Client{Net: "tcp", ReadTimeout: 2 * s.config.ReadTimeout, WriteTimeout: 2 * s.config.ReadTimeout, SingleInflight: true} s.backend = backendetcd.NewBackend(client, &backendetcd.Config{ Ttl: s.config.Ttl, Priority: s.config.Priority, }) go s.Run() // Yeah, yeah, should do a proper fix. time.Sleep(500 * time.Millisecond) return s }
func newTestServer(t *testing.T, c bool) *server { Port += 10 StrPort = strconv.Itoa(Port) s := new(server) client, _ := etcd.New(etcd.Config{ Endpoints: []string{"http://127.0.0.1:2379/"}, Transport: etcd.DefaultTransport, }) kapi := etcd.NewKeysAPI(client) // TODO(miek): why don't I use NewServer?? s.group = new(sync.WaitGroup) s.scache = cache.New(100, 0) s.rcache = cache.New(100, 0) if c { s.rcache = cache.New(100, 60) // 100 items, 60s ttl } s.config = new(Config) s.config.Domain = "skydns.test." s.config.DnsAddr = "127.0.0.1:" + StrPort s.config.Nameservers = []string{"8.8.4.4:53"} SetDefaults(s.config) s.config.Local = "104.server1.development.region1.skydns.test." s.config.Priority = 10 s.config.RCacheTtl = RCacheTtl s.config.Ttl = 3600 s.config.Ndots = 2 s.dnsUDPclient = &dns.Client{Net: "udp", ReadTimeout: 2 * s.config.ReadTimeout, WriteTimeout: 2 * s.config.ReadTimeout, SingleInflight: true} s.dnsTCPclient = &dns.Client{Net: "tcp", ReadTimeout: 2 * s.config.ReadTimeout, WriteTimeout: 2 * s.config.ReadTimeout, SingleInflight: true} s.backend = backendetcd.NewBackend(kapi, ctx, &backendetcd.Config{ Ttl: s.config.Ttl, Priority: s.config.Priority, }) go s.Run() time.Sleep(500 * time.Millisecond) // Yeah, yeah, should do a proper fix return s }
func main() { flag.Parse() machines := strings.Split(machine, ",") client := newClient(machines, tlspem, tlskey, cacert) if nameserver != "" { for _, hostPort := range strings.Split(nameserver, ",") { if err := validateHostPort(hostPort); err != nil { log.Fatalf("skydns: nameserver is invalid: %s", err) } config.Nameservers = append(config.Nameservers, hostPort) } } if err := validateHostPort(config.DnsAddr); err != nil { log.Fatalf("skydns: addr is invalid: %s", err) } if err := loadConfig(client, config); err != nil { log.Fatalf("skydns: %s", err) } if err := server.SetDefaults(config); err != nil { log.Fatalf("skydns defaults could not be set from /etc/resolv.conf: %v", err) } if config.Local != "" { config.Local = dns.Fqdn(config.Local) } backend := backendetcd.NewBackend(client, &backendetcd.Config{ Ttl: config.Ttl, Priority: config.Priority, }) s := server.New(backend, config) if discover { go func() { recv := make(chan *etcd.Response) go client.Watch("/_etcd/machines/", 0, true, recv, nil) duration := 1 * time.Second for { select { case n := <-recv: if n != nil { if client := updateClient(n, tlskey, tlspem, cacert); client != nil { backend.UpdateClient(client) } duration = 1 * time.Second // reset } else { // we can see an n == nil, probably when we can't connect to etcd. log.Printf("skydns: etcd machine cluster update failed, sleeping %s + ~3s", duration) time.Sleep(duration + (time.Duration(rand.Float32() * 3e9))) // Add some random. duration *= 2 if duration > 32*time.Second { duration = 32 * time.Second } } } } }() } if stub { s.UpdateStubZones() go func() { recv := make(chan *etcd.Response) go client.Watch(msg.Path(config.Domain)+"/dns/stub/", 0, true, recv, nil) duration := 1 * time.Second for { select { case n := <-recv: if n != nil { s.UpdateStubZones() log.Printf("skydns: stubzone update") duration = 1 * time.Second // reset } else { // we can see an n == nil, probably when we can't connect to etcd. log.Printf("skydns: stubzone update failed, sleeping %s + ~3s", duration) time.Sleep(duration + (time.Duration(rand.Float32() * 3e9))) // Add some random. duration *= 2 if duration > 32*time.Second { duration = 32 * time.Second } } } } }() } stats.Collect() // Graphite server.Metrics() // Prometheus if err := s.Run(); err != nil { log.Fatalf("skydns: %s", err) } }
func (lk LocalkubeServer) NewDNSServer(rootDomain, clusterIP, kubeAPIServer string) (*DNSServer, error) { // setup backing etcd store peerURLs := []string{"http://localhost:9256"} DNSEtcdURLs := []string{"http://localhost:49090"} publicIP, err := lk.GetHostIP() if err != nil { return nil, err } serverAddress := fmt.Sprintf("%s:%d", publicIP.String(), 53) etcdServer, err := lk.NewEtcd(DNSEtcdURLs, peerURLs, DNSName, lk.GetDNSDataDirectory()) if err != nil { return nil, err } // setup skydns cfg := etcd.Config{ Endpoints: DNSEtcdURLs, Transport: etcd.DefaultTransport, } etcdClient, err := etcd.New(cfg) if err != nil { return nil, err } keysApi := etcd.NewKeysAPI(etcdClient) skyConfig := &skydns.Config{ DnsAddr: serverAddress, Domain: rootDomain, } dnsAddress, err := net.ResolveUDPAddr("udp", serverAddress) if err != nil { return nil, err } skydns.SetDefaults(skyConfig) backend := backendetcd.NewBackend( keysApi, context.Background(), &backendetcd.Config{ Ttl: skyConfig.Ttl, Priority: skyConfig.Priority, }) skyServer := skydns.New(backend, skyConfig) // setup so prometheus doesn't run into nil metrics.Metrics() // setup kube2sky k2s := kube2sky.NewKube2Sky(rootDomain, DNSEtcdURLs[0], "", kubeAPIServer, 10*time.Second, 8081) return &DNSServer{ etcd: etcdServer, sky: skyServer, kube2sky: k2s, dnsServerAddr: dnsAddress, clusterIP: clusterIP, }, nil }
func main() { flag.Parse() machines := strings.Split(machine, ",") client, err := newEtcdClient(machines, tlspem, tlskey, cacert) if err != nil { panic(err) } if nameserver != "" { for _, hostPort := range strings.Split(nameserver, ",") { if err := validateHostPort(hostPort); err != nil { log.Fatalf("skydns: nameserver is invalid: %s", err) } config.Nameservers = append(config.Nameservers, hostPort) } } if err := validateHostPort(config.DnsAddr); err != nil { log.Fatalf("skydns: addr is invalid: %s", err) } if err := loadConfig(client, config); err != nil { log.Fatalf("skydns: %s", err) } if err := server.SetDefaults(config); err != nil { log.Fatalf("skydns: defaults could not be set from /etc/resolv.conf: %v", err) } if config.Local != "" { config.Local = dns.Fqdn(config.Local) } backend := backendetcd.NewBackend(client, ctx, &backendetcd.Config{ Ttl: config.Ttl, Priority: config.Priority, }) s := server.New(backend, config) if stub { s.UpdateStubZones() go func() { duration := 1 * time.Second var watcher etcd.Watcher watcher = client.Watcher(msg.Path(config.Domain)+"/dns/stub/", &etcd.WatcherOptions{AfterIndex: 0, Recursive: true}) for { _, err := watcher.Next(ctx) if err != nil { // log.Printf("skydns: stubzone update failed, sleeping %s + ~3s", duration) time.Sleep(duration + (time.Duration(rand.Float32() * 3e9))) // Add some random. duration *= 2 if duration > 32*time.Second { duration = 32 * time.Second } } else { s.UpdateStubZones() log.Printf("skydns: stubzone update") duration = 1 * time.Second // reset } } }() } if err := metrics.Metrics(); err != nil { log.Fatalf("skydns: %s", err) } else { log.Printf("skydns: metrics enabled on :%s%s", metrics.Port, metrics.Path) } if err := s.Run(); err != nil { log.Fatalf("skydns: %s", err) } }
func main() { flag.Parse() if config.Version { fmt.Printf("skydns server version: %s\n", server.Version) os.Exit(0) } machines := strings.Split(machine, ",") var clientptr *etcdv3.Client var err error var clientv3 etcdv3.Client var clientv2 etcd.KeysAPI if config.Etcd3 { clientptr, err = newEtcdV3Client(machines, tlspem, tlskey, cacert) clientv3 = *clientptr } else { clientv2, err = newEtcdV2Client(machines, tlspem, tlskey, cacert, username, password) } if err != nil { panic(err) } if nameserver != "" { for _, hostPort := range strings.Split(nameserver, ",") { if err := validateHostPort(hostPort); err != nil { log.Fatalf("skydns: nameserver is invalid: %s", err) } config.Nameservers = append(config.Nameservers, hostPort) } } if err := validateHostPort(config.DnsAddr); err != nil { log.Fatalf("skydns: addr is invalid: %s", err) } if config.Etcd3 { if err := loadEtcdV3Config(clientv3, config); err != nil { log.Fatalf("skydns: %s", err) } } else { if err := loadEtcdV2Config(clientv2, config); err != nil { log.Fatalf("skydns: %s", err) } } if err := server.SetDefaults(config); err != nil { log.Fatalf("skydns: defaults could not be set from /etc/resolv.conf: %v", err) } if config.Local != "" { config.Local = dns.Fqdn(config.Local) } var backend server.Backend if config.Etcd3 { backend = backendetcdv3.NewBackendv3(clientv3, ctx, &backendetcdv3.Config{ Ttl: config.Ttl, Priority: config.Priority, }) } else { backend = backendetcd.NewBackend(clientv2, ctx, &backendetcd.Config{ Ttl: config.Ttl, Priority: config.Priority, }) } s := server.New(backend, config) if stub { s.UpdateStubZones() go func() { duration := 1 * time.Second if config.Etcd3 { var watcher etcdv3.WatchChan watcher = clientv3.Watch(ctx, msg.Path(config.Domain)+"/dns/stub/", etcdv3.WithPrefix()) for wresp := range watcher { if wresp.Err() != nil { log.Printf("skydns: stubzone update failed, sleeping %s + ~3s", duration) time.Sleep(duration + (time.Duration(rand.Float32() * 3e9))) duration *= 2 if duration > 32*time.Second { duration = 32 * time.Second } } else { s.UpdateStubZones() log.Printf("skydns: stubzone update") duration = 1 * time.Second //reset } } } else { var watcher etcd.Watcher watcher = clientv2.Watcher(msg.Path(config.Domain)+"/dns/stub/", &etcd.WatcherOptions{AfterIndex: 0, Recursive: true}) for { _, err := watcher.Next(ctx) if err != nil { // log.Printf("skydns: stubzone update failed, sleeping %s + ~3s", duration) time.Sleep(duration + (time.Duration(rand.Float32() * 3e9))) // Add some random. duration *= 2 if duration > 32*time.Second { duration = 32 * time.Second } } else { s.UpdateStubZones() log.Printf("skydns: stubzone update") duration = 1 * time.Second // reset } } } }() } if err := metrics.Metrics(); err != nil { log.Fatalf("skydns: %s", err) } else { log.Printf("skydns: metrics enabled on :%s%s", metrics.Port, metrics.Path) } if err := s.Run(); err != nil { log.Fatalf("skydns: %s", err) } }