Esempio n. 1
0
func NewTestEtcd(dir string, a ...string) (*TestEtcd, error) {
	dataDir := filepath.Join(dir, "etcd")

	// Hack to find a free tcp port
	ln, err := net.Listen("tcp", "localhost:0")
	if err != nil {
		return nil, err
	}
	defer ln.Close()
	ln2, err := net.Listen("tcp", "localhost:0")
	if err != nil {
		return nil, err
	}
	defer ln2.Close()

	listenAddress := ln.Addr().(*net.TCPAddr).IP.String()
	port := strconv.Itoa(ln.Addr().(*net.TCPAddr).Port)
	listenAddress2 := ln2.Addr().(*net.TCPAddr).IP.String()
	port2 := strconv.Itoa(ln2.Addr().(*net.TCPAddr).Port)

	args := []string{}
	args = append(args, fmt.Sprintf("--data-dir=%s", dataDir))
	args = append(args, fmt.Sprintf("--listen-client-urls=http://%s:%s", listenAddress, port))
	args = append(args, fmt.Sprintf("--advertise-client-urls=http://%s:%s", listenAddress, port))
	args = append(args, fmt.Sprintf("--listen-peer-urls=http://%s:%s", listenAddress2, port2))
	args = append(args, a...)

	etcdEndpoints := fmt.Sprintf("http://%s:%s", listenAddress, port)
	eCfg := etcd.Config{
		Transport: &http.Transport{},
		Endpoints: strings.Split(etcdEndpoints, ","),
	}
	eClient, err := etcd.New(eCfg)
	if err != nil {
		return nil, err
	}
	kAPI := etcd.NewKeysAPI(eClient)

	etcdBin := os.Getenv("ETCD_BIN")
	if etcdBin == "" {
		return nil, fmt.Errorf("missing ETCD_BIN env")
	}
	te := &TestEtcd{
		etcdBin:       etcdBin,
		args:          args,
		listenAddress: listenAddress,
		port:          port,
		eClient:       eClient,
		kAPI:          kAPI,
	}
	return te, nil
}
Esempio n. 2
0
func NewEtcdManager(etcdEndpoints string, path string, requestTimeout time.Duration) (*EtcdManager, error) {
	eCfg := etcd.Config{
		Transport: &http.Transport{},
		Endpoints: strings.Split(etcdEndpoints, ","),
	}
	eClient, err := etcd.New(eCfg)
	if err != nil {
		return nil, err
	}
	kAPI := etcd.NewKeysAPI(eClient)

	return &EtcdManager{
		etcdPath:       path,
		eCfg:           eCfg,
		kAPI:           kAPI,
		requestTimeout: requestTimeout,
	}, nil
}
Esempio n. 3
0
// New creates a new Etcd client given a list
// of endpoints and an optional tls config
func New(addrs []string, options *store.Config) (store.Store, error) {
	s := &Etcd{}

	var (
		entries []string
		err     error
	)

	entries = store.CreateEndpoints(addrs, "http")
	cfg := &etcd.Config{
		Endpoints:               entries,
		Transport:               etcd.DefaultTransport,
		HeaderTimeoutPerRequest: 3 * time.Second,
	}

	// Set options
	if options != nil {
		if options.TLS != nil {
			setTLS(cfg, options.TLS, addrs)
		}
		if options.ConnectionTimeout != 0 {
			setTimeout(cfg, options.ConnectionTimeout)
		}
	}

	c, err := etcd.New(*cfg)
	if err != nil {
		log.Fatal(err)
	}

	s.client = etcd.NewKeysAPI(c)

	// Periodic Cluster Sync
	go func() {
		for {
			if err := c.AutoSync(context.Background(), periodicSync); err != nil {
				return
			}
		}
	}()

	return s, nil
}
Esempio n. 4
0
func getClusters(etcdBasePath string) ([]string, error) {
	eCfg := etcd.Config{
		Transport: &http.Transport{},
		Endpoints: strings.Split(cfg.etcdEndpoints, ","),
	}
	eClient, err := etcd.New(eCfg)
	if err != nil {
		return nil, err
	}
	kAPI := etcd.NewKeysAPI(eClient)

	clusters := []string{}
	res, err := kAPI.Get(context.Background(), etcdBasePath, &etcd.GetOptions{Recursive: true, Quorum: true})
	if err != nil && !etcdm.IsEtcdNotFound(err) {
		return nil, err
	} else if !etcdm.IsEtcdNotFound(err) {
		for _, node := range res.Node.Nodes {
			clusters = append(clusters, filepath.Base(node.Key))
		}
	}
	sort.Strings(clusters)
	return clusters, nil
}