Skip to content

Commit

Permalink
Fix non closed http responses (#5755) (#5770)
Browse files Browse the repository at this point in the history
Co-authored-by: jiuker <2818723467@qq.com>
Co-authored-by: guozhi.li <guozhi.li@daocloud.io>
  • Loading branch information
3 people committed Jun 14, 2022
1 parent afba329 commit a830566
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 13 deletions.
7 changes: 1 addition & 6 deletions hack/operatorhub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,7 @@ func makeRequest(url string) (io.Reader, error) {
if err != nil {
return nil, fmt.Errorf("failed to GET %s: %w", url, err)
}

defer func() {
if resp.Body != nil {
resp.Body.Close()
}
}()
defer resp.Body.Close()

if resp.StatusCode == http.StatusNotFound {
return nil, errNotFound
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/association/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ func (r UnmanagedAssociationConnectionInfo) Request(path string, jsonPath string
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", fmt.Errorf("error requesting %q, statusCode = %d", url, resp.StatusCode)
}

defer resp.Body.Close()
var obj interface{}
if err = json.NewDecoder(resp.Body).Decode(&obj); err != nil {
return "", err
Expand Down
6 changes: 5 additions & 1 deletion pkg/controller/common/stackmon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ func newBeatConfig(client k8s.Client, beatName string, resource monitoring.HasMo
}

configHash := fnv.New32a()
configHash.Write(configBytes)

_, err = configHash.Write(configBytes)
if err != nil {
return beatConfig{}, err
}

configSecret := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/test/enterprisesearch/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ func (e EnterpriseSearchClient) doRequest(request *http.Request) ([]byte, error)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, fmt.Errorf("http response status code is %d)", resp.StatusCode)
}

defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}

Expand Down
3 changes: 1 addition & 2 deletions test/e2e/test/kibana/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,13 @@ func DoRequest(k *test.K8sClient, kb kbv1.Kibana, password string, method string
if err != nil {
return nil, errors.Wrap(err, "while doing request")
}

defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, &APIError{
StatusCode: resp.StatusCode,
msg: fmt.Sprintf("fail to request %s, status is %d)", pathAndQuery, resp.StatusCode),
}
}

defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
3 changes: 1 addition & 2 deletions test/e2e/test/maps/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,12 @@ func DoRequest(client *http.Client, ems v1alpha1.ElasticMapsServer, method, path
if err != nil {
return nil, fmt.Errorf("while making request: %w", err)
}

defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, &APIError{
StatusCode: resp.StatusCode,
msg: fmt.Sprintf("fail to request %s, status is %d)", path, resp.StatusCode),
}
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}

0 comments on commit a830566

Please sign in to comment.