Skip to content

Commit e97933b

Browse files
committed
fix(server): Add read and write timeouts
There are a few documented scenarios where `kube-state-metrics` will lock up(#995, #1028). I believe a much simpler solution to ensure `kube-state-metrics` doesn't lock up and require a restart to server `/metrics` requests is to add default read and write timeouts and to allow them to be configurable. At Grafana, we've experienced a few scenarios where `kube-state-metrics` running in larger clusters falls behind and starts getting scraped multiple times. When this occurs, `kube-state-metrics` becomes completely unresponsive and requires a reboot. This is somewhat easily reproduceable(I'll provide a script in an issue) and causes other critical workloads(KEDA, VPA) to fail in weird ways. Adds two flags: - `server-read-timeout` - `server-write-timeout` Updates the metrics http server to set the `ReadTimeout` and `WriteTimeout` to the configured values.
1 parent 7995d5f commit e97933b

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

docs/developer/cli-arguments.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ Flags:
6767
--pod-namespace string Name of the namespace of the pod specified by --pod. When set, it is expected that --pod and --pod-namespace are both set. Most likely this should be passed via the downward API. This is used for auto-detecting sharding. If set, this has preference over statically configured sharding. This is experimental, it may be removed without notice.
6868
--port int Port to expose metrics on. (default 8080)
6969
--resources string Comma-separated list of Resources to be enabled. Defaults to "certificatesigningrequests,configmaps,cronjobs,daemonsets,deployments,endpoints,horizontalpodautoscalers,ingresses,jobs,leases,limitranges,mutatingwebhookconfigurations,namespaces,networkpolicies,nodes,persistentvolumeclaims,persistentvolumes,poddisruptionbudgets,pods,replicasets,replicationcontrollers,resourcequotas,secrets,services,statefulsets,storageclasses,validatingwebhookconfigurations,volumeattachments"
70+
--server-read-timeout duration The maximum duration for reading the entire request, including the body. (default 30s)
71+
--server-write-timeout duration The maximum duration before timing out writes of the response. (default 1m0s)
7072
--shard int32 The instances shard nominal (zero indexed) within the total number of shards. (default 0)
7173
--skip_headers If true, avoid header prefixes in the log messages
7274
--skip_log_headers If true, avoid headers when opening log files (no effect when -logtostderr=true)

pkg/app/server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error {
326326
metricsServer := http.Server{
327327
Handler: metricsMux,
328328
ReadHeaderTimeout: 5 * time.Second,
329+
ReadTimeout: opts.ServerReadTimeout,
330+
WriteTimeout: opts.ServerWriteTimeout,
329331
}
330332
metricsFlags := web.FlagConfig{
331333
WebListenAddresses: &[]string{metricsServerListenAddress},
@@ -401,7 +403,6 @@ func buildMetricsServer(m *metricshandler.MetricsHandler, durationObserver prome
401403
mux.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
402404

403405
mux.Handle(metricsPath, promhttp.InstrumentHandlerDuration(durationObserver, m))
404-
405406
// Add healthzPath
406407
mux.HandleFunc(healthzPath, func(w http.ResponseWriter, _ *http.Request) {
407408
w.WriteHeader(http.StatusOK)

pkg/options/options.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"os"
2323
"strings"
24+
"time"
2425

2526
"github.com/prometheus/common/version"
2627
"github.com/spf13/cobra"
@@ -55,6 +56,8 @@ type Options struct {
5556
TelemetryPort int `yaml:"telemetry_port"`
5657
TotalShards int `yaml:"total_shards"`
5758
UseAPIServerCache bool `yaml:"use_api_server_cache"`
59+
ServerReadTimeout time.Duration `yaml:"server_read_timeout"`
60+
ServerWriteTimeout time.Duration `yaml:"server_write_timeout"`
5861

5962
Config string
6063

@@ -146,6 +149,8 @@ func (o *Options) AddFlags(cmd *cobra.Command) {
146149
o.cmd.Flags().Var(&o.Namespaces, "namespaces", fmt.Sprintf("Comma-separated list of namespaces to be enabled. Defaults to %q", &DefaultNamespaces))
147150
o.cmd.Flags().Var(&o.NamespacesDenylist, "namespaces-denylist", "Comma-separated list of namespaces not to be enabled. If namespaces and namespaces-denylist are both set, only namespaces that are excluded in namespaces-denylist will be used.")
148151
o.cmd.Flags().Var(&o.Resources, "resources", fmt.Sprintf("Comma-separated list of Resources to be enabled. Defaults to %q", &DefaultResources))
152+
o.cmd.Flags().DurationVar(&o.ServerReadTimeout, "server-read-timeout", 30*time.Second, "The maximum duration for reading the entire request, including the body.")
153+
o.cmd.Flags().DurationVar(&o.ServerWriteTimeout, "server-write-timeout", 60*time.Second, "The maximum duration before timing out writes of the response.")
149154
}
150155

151156
// Parse parses the flag definitions from the argument list.

0 commit comments

Comments
 (0)