Beispiel #1
0
func newClient() (*client.Client, error) {
	protocol := os.Getenv("KUBERNETES_API_PROTOCOL")
	host := os.Getenv("KUBERNETES_RO_SERVICE_HOST")
	port := os.Getenv("KUBERNETES_RO_SERVICE_PORT")

	if len(protocol) == 0 {
		protocol = "http"
	}

	if len(host) == 0 {
		host = "127.0.0.1"
	}

	if len(port) == 0 {
		port = "8080"
	}

	return client.New(&client.Config{
		Host: protocol + "://" + host + ":" + port,
	})
}
func main() {
	clusterAddress := os.Getenv("CLUSTER_ADDRESS")
	rootDns := os.Getenv("ROOT_DNS_DOMAIN")
	if rootDns == "" {
		log.Fatalln("You need to precise your root DNS name with the `ROOT_DNS_DOMAIN` environment variable")
	}

	config := client.Config{
		Host:     clusterAddress,
		Insecure: os.Getenv("INSECURE_CLUSTER") == "true",
	}

	c, err := client.New(&config)
	if err != nil {
		log.Fatalln("Can't connect to Kubernetes API:", err)
	}

	w, err := c.Services(api.NamespaceAll).Watch(labels.Everything(), fields.Everything(), api.ListOptions{})
	if err != nil {
		log.Fatalln("Unable to watch services:", err)
	}

	log.Println("Watching services")
	for event := range w.ResultChan() {
		service, ok := event.Object.(*api.Service)
		if !ok {
			log.Println("Got a non-service object")

			continue
		}

		if event.Type == watch.Added || event.Type == watch.Modified {
			err := ReviewService(c, service, rootDns)

			if err != nil {
				log.Println("An error occured while updating service")
			}
		}
	}
}