diff --git a/cmd/common.go b/cmd/common.go index d10bb1e42..bde8d7560 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -18,6 +18,7 @@ import ( "fmt" "os" "path/filepath" + "time" "github.com/prometheus/client_golang/prometheus" "github.com/sorintlab/stolon/internal/cluster" @@ -50,12 +51,14 @@ type CommonConfig struct { KubeConfig string KubeContext string KubeNamespace string + StoreTimeout time.Duration } func AddCommonFlags(cmd *cobra.Command, cfg *CommonConfig) { cmd.PersistentFlags().StringVar(&cfg.ClusterName, "cluster-name", "", "cluster name") cmd.PersistentFlags().StringVar(&cfg.StoreBackend, "store-backend", "", "store backend type (etcdv2/etcd, etcdv3, consul or kubernetes)") cmd.PersistentFlags().StringVar(&cfg.StoreEndpoints, "store-endpoints", "", "a comma-delimited list of store endpoints (use https scheme for tls communication) (defaults: http://127.0.0.1:2379 for etcd, http://127.0.0.1:8500 for consul)") + cmd.PersistentFlags().DurationVar(&cfg.StoreTimeout, "store-timeout", cluster.DefaultStoreTimeout, "store request timeout") cmd.PersistentFlags().StringVar(&cfg.StorePrefix, "store-prefix", common.StorePrefix, "the store base prefix") cmd.PersistentFlags().StringVar(&cfg.StoreCertFile, "store-cert-file", "", "certificate file for client identification to the store") cmd.PersistentFlags().StringVar(&cfg.StoreKeyFile, "store-key", "", "private key file for client identification to the store") @@ -143,6 +146,7 @@ func NewKVStore(cfg *CommonConfig) (store.KVStore, error) { return store.NewKVStore(store.Config{ Backend: store.Backend(cfg.StoreBackend), Endpoints: cfg.StoreEndpoints, + Timeout: cfg.StoreTimeout, CertFile: cfg.StoreCertFile, KeyFile: cfg.StoreKeyFile, CAFile: cfg.StoreCAFile, @@ -195,7 +199,7 @@ func NewElection(cfg *CommonConfig, uid string) (store.Election, error) { if err != nil { return nil, fmt.Errorf("cannot create kv store: %v", err) } - election = store.NewKVBackedElection(kvstore, filepath.Join(storePath, common.SentinelLeaderKey), uid) + election = store.NewKVBackedElection(kvstore, filepath.Join(storePath, common.SentinelLeaderKey), uid, cfg.StoreTimeout) case "kubernetes": kubecli, podName, namespace, err := getKubeValues(cfg) if err != nil { @@ -216,7 +220,8 @@ func getKubeValues(cfg *CommonConfig) (*kubernetes.Clientset, string, string, er if err != nil { return nil, "", "", err } - kubecfg.Timeout = cluster.DefaultStoreTimeout + + kubecfg.Timeout = cfg.StoreTimeout kubecli, err := kubernetes.NewForConfig(kubecfg) if err != nil { return nil, "", "", fmt.Errorf("cannot create kubernetes client: %v", err) diff --git a/internal/store/kvbacked.go b/internal/store/kvbacked.go index 406b17217..4721c20e6 100644 --- a/internal/store/kvbacked.go +++ b/internal/store/kvbacked.go @@ -65,6 +65,7 @@ var URLSchemeRegexp = regexp.MustCompile(`^([a-zA-Z][a-zA-Z0-9+-.]*)://`) type Config struct { Backend Backend Endpoints string + Timeout time.Duration BasePath string CertFile string KeyFile string @@ -172,7 +173,7 @@ func NewKVStore(cfg Config) (KVStore, error) { case CONSUL, ETCDV2: config := &libkvstore.Config{ TLS: tlsConfig, - ConnectionTimeout: cluster.DefaultStoreTimeout, + ConnectionTimeout: cfg.Timeout, } store, err := libkv.NewStore(kvBackend, addrs, config) @@ -190,7 +191,7 @@ func NewKVStore(cfg Config) (KVStore, error) { if err != nil { return nil, err } - return &etcdV3Store{c: c, requestTimeout: cluster.DefaultStoreTimeout}, nil + return &etcdV3Store{c: c, requestTimeout: cfg.Timeout}, nil default: return nil, fmt.Errorf("Unknown store backend: %q", cfg.Backend) } @@ -344,7 +345,7 @@ func (s *KVBackedStore) GetProxiesInfo(ctx context.Context) (cluster.ProxiesInfo return psi, nil } -func NewKVBackedElection(kvStore KVStore, path, candidateUID string) Election { +func NewKVBackedElection(kvStore KVStore, path, candidateUID string, timeout time.Duration) Election { switch kvStore := kvStore.(type) { case *libKVStore: s := kvStore @@ -357,7 +358,7 @@ func NewKVBackedElection(kvStore KVStore, path, candidateUID string) Election { path: path, candidateUID: candidateUID, ttl: MinTTL, - requestTimeout: cluster.DefaultStoreTimeout, + requestTimeout: timeout, } default: panic("unknown kvstore")