func (eb *Etcd2Bootstrapper) Run() {
	// sanity checks
	if eb.config.Me == "" {
		log.Fatal(fmt.Errorf("-me is a required parameters"))
	}

	meMember, err := parseMember(eb.config.Me)
	if err != nil {
		log.Fatal(err)
	}

	if eb.config.Members == "" {
		log.Fatal(fmt.Errorf("-members is a required parameters"))
	}

	clusterMembers, err := parseMembers(eb.config.Members)
	if err != nil {
		log.Fatal(err)
	}

	var tmplListen *template.Template = nil
	if eb.config.TmplListenUrl != "" {
		tmplListen, _ = template.New("tmpl-listen-url").Parse(eb.config.TmplListenUrl)
	}

	var tmplPeer *template.Template = nil
	if eb.config.TmplPeerUrl != "" {
		tmplPeer, _ = template.New("tmpl-listen-url").Parse(eb.config.TmplPeerUrl)
	}

	if _, err := os.Stat(eb.config.Out); err == nil {
		log.Printf("etcd-peers file %s already created, exiting.\n", eb.config.Out)
		return
	}

	// create client instantiator

	var newClientFn func(string) (*etcd.Client, error) = nil
	if eb.config.CertFile != "" && eb.config.KeyFile != "" && eb.config.CAFile != "" {
		newClientFn = func(url string) (*etcd.Client, error) {
			return etcd.NewTLSClient(url, eb.config.CertFile, eb.config.KeyFile, eb.config.CAFile)
		}
	} else {
		newClientFn = func(url string) (*etcd.Client, error) {
			return etcd.NewClient(url)
		}
	}

	kvs, err := syncEtcdCluster(*meMember, clusterMembers, eb.config.Force, tmplListen, tmplPeer, newClientFn)
	if err != nil {
		log.Fatal(err)
	}

	tempFilePath := eb.config.Out + ".tmp"
	tempFile, err := os.Create(tempFilePath)
	if err != nil {
		log.Fatal(err)
	}

	isClosed := false
	defer func() {
		if !isClosed {
			tempFile.Close()
		}
	}()

	if err := writeEnvironmentFile(kvs, tempFile); err != nil {
		log.Fatal(err)
	}

	if err := tempFile.Close(); err != nil {
		log.Fatal(err)
	}

	isClosed = true

	if err := os.Rename(tempFilePath, eb.config.Out); err != nil {
		log.Fatal(err)
	}

	return
}
示例#2
0
func (k2n *KubeToNginx) Run() {
	if k2n.config.IngressesData == "" {
		log.Fatal("no ingresses, no way")
	}

	id, err := base64.StdEncoding.DecodeString(k2n.config.IngressesData)
	if err != nil {
		log.Fatal(err)
	}

	k2n.ingressesData = make(map[string]string)
	if err := json.Unmarshal(id, &k2n.ingressesData); err != nil {
		log.Fatal(err)
	}

	// Get service account token
	serviceAccountToken, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/token")
	if err != nil {
		log.Fatal(err)
	}

	// Get CA certificate data
	caCertificate, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/ca.crt")
	if err != nil {
		log.Fatal(err)
	}

	// Create new k8s client
	kubeConfig := &kclient.ClientConfig{
		MasterURL:     k2n.config.KubeMasterURL,
		Auth:          &kclient.TokenAuth{string(serviceAccountToken)},
		CaCertificate: caCertificate,
	}
	kubeClient, err := kclient.NewClient(kubeConfig)
	if err != nil {
		log.Fatal(err)
	}

	// Flow control channels
	recvChan := make(chan interface{}, 100)
	stopChan := make(<-chan struct{})
	doneChan := make(chan bool)
	errChan := make(chan error, 10)

	// Create informer from client
	informerConfig := &kclient.InformerConfig{
		Namespace:      k2n.config.Namespace,
		Resource:       "services",
		Selector:       k2n.config.Selector,
		ResyncInterval: k2n.config.ResyncInterval,
	}
	i, err := kubeClient.NewInformer(
		informerConfig, recvChan,
		stopChan, doneChan, errChan,
	)
	if err != nil {
		log.Fatal(err)
	}

	tmplCfg := &core.TemplateConfig{
		SrcData:   core.NginxConf,
		Dest:      k2n.config.NginxDest,
		Uid:       k2n.config.NginxDestUid,
		Gid:       k2n.config.NginxDestGid,
		Mode:      k2n.config.NginxDestMode,
		Prefix:    "/lb",
		CheckCmd:  k2n.config.NginxCheckCmd,
		ReloadCmd: k2n.config.NginxReloadCmd,
	}

	if k2n.config.NginxSrc != "" {
		tmplCfg.Src = k2n.config.NginxSrc
	}

	k2n.tmpl = core.NewTemplate(tmplCfg, false, false, false)

	go i.Run()

	// Wait for signal
	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
	for {
		select {
		case v := <-recvChan:
			k2n.process(v)
		case err := <-errChan:
			log.Error(err)
		case s := <-signalChan:
			log.Infof("Captured %v. Exiting...", s)
			close(doneChan)
		case <-doneChan:
			os.Exit(0)
		}
	}
}
示例#3
0
func (kl *KubeListener) Run() {
	// Get service account token
	serviceAccountToken, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/token")
	if err != nil {
		log.Fatal(err)
	}

	// Get CA certificate data
	caCertificate, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/ca.crt")
	if err != nil {
		log.Fatal(err)
	}

	// Create new k8s client
	kubeConfig := &kclient.ClientConfig{
		MasterURL:     kl.config.KubeMasterURL,
		Auth:          &kclient.TokenAuth{string(serviceAccountToken)},
		CaCertificate: caCertificate,
	}
	kubeClient, err := kclient.NewClient(kubeConfig)
	if err != nil {
		log.Fatal(err)
	}

	// Flow control channels
	recvChan := make(chan interface{}, 100)
	stopChan := make(<-chan struct{})
	doneChan := make(chan bool)
	errChan := make(chan error, 10)

	// Create informer from client
	informerConfig := &kclient.InformerConfig{
		Namespace:      kl.config.Namespace,
		Resource:       kl.config.Resource,
		Selector:       kl.config.Selector,
		ResyncInterval: kl.config.ResyncInterval,
	}
	i, err := kubeClient.NewInformer(
		informerConfig, recvChan,
		stopChan, doneChan, errChan,
	)
	if err != nil {
		log.Fatal(err)
	}

	go i.Run()

	// Wait for signal
	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
	for {
		select {
		case v := <-recvChan:
			log.Infof("%v", v)
		case err := <-errChan:
			log.Error(err)
		case s := <-signalChan:
			log.Infof("Captured %v. Exiting...", s)
			close(doneChan)
		case <-doneChan:
			os.Exit(0)
		}
	}

	/*
		// create kubelistener instance
		kl := kubelistener{config: config}

		// open add events files
		if config.AddEventsFile == "" {
			glog.Warningf("Ignoring 'add' events because --add-events-file wasn't provided.")
		} else {
			if w, err := newWriter(config.AddEventsFile); err != nil {
				glog.Fatalf("Unable to open '%s' for writing due to: %s", config.AddEventsFile, err.Error())
			} else {
				kl.addWriter = w
			}
		}

		// open update events files
		if config.UpdateEventsFile == "" {
			glog.Warningf("Ignoring 'update' events because --update-events-file wasn't provided.")
		} else {
			if w, err := newWriter(config.UpdateEventsFile); err != nil {
				glog.Fatalf("Unable to open '%s' for writing due to: %s", config.UpdateEventsFile, err.Error())
			} else {
				kl.updateWriter = w
			}
		}

		// open delete events files
		if config.DeleteEventsFile == "" {
			glog.Warningf("Ignoring 'delete' events because --delete-events-file wasn't provided.")
		} else {
			if w, err := newWriter(config.DeleteEventsFile); err != nil {
				glog.Fatalf("Unable to open '%s' for writing due to: %s", config.DeleteEventsFile, err.Error())
			} else {
				kl.deleteWriter = w
			}
		}

		// choose which resources to watch
		if config.Resource == "" {
			glog.Fatalf("Unable to start kubelistener because --resources to watch wasn't provided.")
		}

		var resource = strings.ToLower(config.Resource)
		if resource == "all" {
			for _, watchFunc := range resources {
				watchFunc(&kl)
			}
		} else if watchFunc, ok := resources[resource]; ok {
			watchFunc(&kl)
		} else {
			glog.Fatal("Unknown resource to watch '%s':", resource)
		}
	*/
}