Пример #1
0
func (ksync *KubernetesSync) addDNS(service string, info *serviceInfo) error {
	// ADD to SkyDNS registry
	svc := msg.Service{
		Host:     info.portalIP.String(),
		Port:     info.portalPort,
		Priority: 10,
		Weight:   10,
		Ttl:      30,
	}
	b, err := json.Marshal(svc)

	//Set with no TTL, and hope that kubernetes events are accurate.

	log.Printf("setting dns record: %v\n", service)
	_, err = ksync.eclient.Set(msg.Path(service), string(b), uint64(0))
	return err
}
Пример #2
0
func (ksync *KubernetesSync) removeDNS(service string, info *serviceInfo) error {
	// Remove from SkyDNS registration
	log.Printf("removing %s from DNS", service)
	_, err := ksync.eclient.Delete(msg.Path(service), true)
	return err
}
Пример #3
0
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)
	}
	server.SetDefaults(config)

	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
						}
					}
				}
			}
		}()
	}

	if kubernetes {
		go kube.WatchKubernetes(client)
	}

	stats.Collect()  // Graphite
	server.Metrics() // Prometheus

	if err := s.Run(); err != nil {
		log.Fatalf("skydns: %s", err)
	}
}