Skip to content

Commit ee0582d

Browse files
committed
Enforce 5s timeout on all HTTP clients in kubelet-to-gcm
1 parent 929f72e commit ee0582d

3 files changed

Lines changed: 12 additions & 3 deletions

File tree

kubelet-to-gcm/monitor/config/initialize.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ func metaDataURI(resource string) string {
104104

105105
// getGCEMetaData hits the instance's MD server.
106106
func getGCEMetaData(uri string) ([]byte, error) {
107-
client := &http.Client{}
107+
client := &http.Client{
108+
Timeout: 5 * time.Second,
109+
}
108110
req, err := http.NewRequest("GET", uri, nil)
109111
if err != nil {
110112
return nil, fmt.Errorf("Failed to create request %q for GCE metadata: %v", uri, err)

kubelet-to-gcm/monitor/controller/source.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package controller
1919
import (
2020
"fmt"
2121
"net/http"
22+
"time"
2223

2324
v3 "google.golang.org/api/monitoring/v3"
2425

@@ -38,7 +39,9 @@ func NewSource(cfg *monitor.SourceConfig) (*Source, error) {
3839
trans := NewTranslator(cfg.Zone, cfg.Project, cfg.Cluster, cfg.Instance, cfg.Resolution)
3940

4041
// NewClient validates its own inputs.
41-
client, err := NewClient(cfg.Host, cfg.Port, &http.Client{})
42+
client, err := NewClient(cfg.Host, cfg.Port, &http.Client{
43+
Timeout: 5 * time.Second,
44+
})
4245
if err != nil {
4346
return nil, fmt.Errorf("Failed to create a controller client with config %v: %v", cfg, err)
4447
}

kubelet-to-gcm/monitor/kubelet/source.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"io/ioutil"
2424
"net/http"
25+
"time"
2526

2627
v3 "google.golang.org/api/monitoring/v3"
2728

@@ -41,7 +42,9 @@ func NewSource(cfg *monitor.SourceConfig) (*Source, error) {
4142
trans := NewTranslator(cfg.Zone, cfg.Project, cfg.Cluster, cfg.ClusterLocation, cfg.Instance, cfg.InstanceID, cfg.SchemaPrefix, cfg.MonitoredResourceLabels, cfg.Resolution)
4243

4344
// NewClient validates its own inputs.
44-
httpClient := &http.Client{}
45+
httpClient := &http.Client{
46+
Timeout: 5 * time.Second,
47+
}
4548
var err error
4649
useAuthPort := false
4750
if cfg.CertificateLocation != "" {
@@ -101,6 +104,7 @@ func getSecuredHttpClient(certLocation string) (*http.Client, error) {
101104
return nil, fmt.Errorf("failed to parse kubelet certificate")
102105
}
103106
return &http.Client{
107+
Timeout: 5 * time.Second,
104108
Transport: &http.Transport{
105109
TLSClientConfig: &tls.Config{
106110
RootCAs: certPool,

0 commit comments

Comments
 (0)