diff --git a/cmd/kube-apiserver/app/options/options_test.go b/cmd/kube-apiserver/app/options/options_test.go index 9cbfa44a3ed..a412f37e10f 100644 --- a/cmd/kube-apiserver/app/options/options_test.go +++ b/cmd/kube-apiserver/app/options/options_test.go @@ -160,6 +160,7 @@ func TestAddFlags(t *testing.T) { CompactionInterval: storagebackend.DefaultCompactInterval, CountMetricPollPeriod: time.Minute, DBMetricPollInterval: storagebackend.DefaultDBMetricPollInterval, + HealthcheckTimeout: storagebackend.DefaultHealthcheckTimeout, }, DefaultStorageMediaType: "application/vnd.kubernetes.protobuf", DeleteCollectionWorkers: 1, diff --git a/staging/src/k8s.io/apiserver/pkg/server/options/etcd.go b/staging/src/k8s.io/apiserver/pkg/server/options/etcd.go index 192edeb6b21..d9c6f9e54bb 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/options/etcd.go +++ b/staging/src/k8s.io/apiserver/pkg/server/options/etcd.go @@ -180,6 +180,9 @@ func (s *EtcdOptions) AddFlags(fs *pflag.FlagSet) { fs.DurationVar(&s.StorageConfig.DBMetricPollInterval, "etcd-db-metric-poll-interval", s.StorageConfig.DBMetricPollInterval, "The interval of requests to poll etcd and update metric. 0 disables the metric collection") + + fs.DurationVar(&s.StorageConfig.HealthcheckTimeout, "etcd-healthcheck-timeout", s.StorageConfig.HealthcheckTimeout, + "The timeout to use when checking etcd health.") } func (s *EtcdOptions) ApplyTo(c *server.Config) error { diff --git a/staging/src/k8s.io/apiserver/pkg/storage/storagebackend/config.go b/staging/src/k8s.io/apiserver/pkg/storage/storagebackend/config.go index 5dc0bbfb349..af94efcea88 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/storagebackend/config.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/storagebackend/config.go @@ -30,6 +30,7 @@ const ( DefaultCompactInterval = 5 * time.Minute DefaultDBMetricPollInterval = 30 * time.Second + DefaultHealthcheckTimeout = 2 * time.Second ) // TransportConfig holds all connection related info, i.e. equal TransportConfig means equal servers we talk to. @@ -74,6 +75,8 @@ type Config struct { CountMetricPollPeriod time.Duration // DBMetricPollInterval specifies how often should storage backend metric be updated. DBMetricPollInterval time.Duration + // HealthcheckTimeout specifies the timeout used when checking health + HealthcheckTimeout time.Duration } func NewDefaultConfig(prefix string, codec runtime.Codec) *Config { @@ -83,5 +86,6 @@ func NewDefaultConfig(prefix string, codec runtime.Codec) *Config { Codec: codec, CompactionInterval: DefaultCompactInterval, DBMetricPollInterval: DefaultDBMetricPollInterval, + HealthcheckTimeout: DefaultHealthcheckTimeout, } } diff --git a/staging/src/k8s.io/apiserver/pkg/storage/storagebackend/factory/etcd3.go b/staging/src/k8s.io/apiserver/pkg/storage/storagebackend/factory/etcd3.go index 3d1f2f150fa..9a1618df8dd 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/storagebackend/factory/etcd3.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/storagebackend/factory/etcd3.go @@ -92,7 +92,11 @@ func newETCD3HealthCheck(c storagebackend.Config) (func() error, error) { return fmt.Errorf(errMsg) } client := clientValue.Load().(*clientv3.Client) - ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + healthcheckTimeout := storagebackend.DefaultHealthcheckTimeout + if c.HealthcheckTimeout != time.Duration(0) { + healthcheckTimeout = c.HealthcheckTimeout + } + ctx, cancel := context.WithTimeout(context.Background(), healthcheckTimeout) defer cancel() // See https://github.com/etcd-io/etcd/blob/c57f8b3af865d1b531b979889c602ba14377420e/etcdctl/ctlv3/command/ep_command.go#L118 _, err := client.Get(ctx, path.Join("/", c.Prefix, "health"))