// 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{} entries := store.CreateEndpoints(addrs, "http") s.client = etcd.NewClient(entries) // Set options if options != nil { if options.TLS != nil { s.setTLS(options.TLS) } if options.ConnectionTimeout != 0 { s.setTimeout(options.ConnectionTimeout) } } // Periodic SyncCluster go func() { for { s.client.SyncCluster() time.Sleep(periodicSync) } }() return s, nil }
// 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 ) // Create the etcd client if options != nil && options.ClientTLS != nil { entries = store.CreateEndpoints(addrs, "https") s.client, err = etcd.NewTLSClient(entries, options.ClientTLS.CertFile, options.ClientTLS.KeyFile, options.ClientTLS.CACertFile) if err != nil { return nil, err } } else { entries = store.CreateEndpoints(addrs, "http") s.client = etcd.NewClient(entries) } // Set options if options != nil { // Plain TLS config overrides ClientTLS if specified if options.TLS != nil { s.setTLS(options.TLS, addrs) } if options.ConnectionTimeout != 0 { s.setTimeout(options.ConnectionTimeout) } } // Periodic SyncCluster go func() { for { s.client.SyncCluster() time.Sleep(periodicSync) } }() return s, nil }
// SetTLS sets the tls configuration given a tls.Config scheme func (s *Etcd) setTLS(tls *tls.Config, addrs []string) { entries := store.CreateEndpoints(addrs, "https") s.client.SetCluster(entries) // Set transport t := http.Transport{ Dial: (&net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, }).Dial, TLSHandshakeTimeout: 10 * time.Second, TLSClientConfig: tls, } s.client.SetTransport(&t) }
// SetTLS sets the tls configuration given a tls.Config scheme func setTLS(cfg *etcd.Config, tls *tls.Config, addrs []string) { entries := store.CreateEndpoints(addrs, "https") cfg.Endpoints = entries // Set transport t := http.Transport{ Dial: (&net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, }).Dial, TLSHandshakeTimeout: 10 * time.Second, TLSClientConfig: tls, } cfg.Transport = &t }
// 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) } if options.Username != "" { setCredentials(cfg, options.Username, options.Password) } } 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 }