mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 01:06:27 +00:00
Merge pull request #48335 from sakeven/fix/close_resp_Body
Automatic merge from submit-queue (batch tested with PRs 48402, 47203, 47460, 48335, 48322) HTTPExtender: should close resp.Body even when StatusCode not ok Signed-off-by: sakeven <jc5930@sina.cn> **What this PR does / why we need it**: close resp.Body even when StatusCode isn't ok **Special notes for your reviewer**: **Release note**: ``` NONE ```
This commit is contained in:
commit
33718a8fae
@ -20,8 +20,8 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
@ -229,7 +229,7 @@ func (h *HTTPExtender) send(action string, args interface{}, result interface{})
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
url := h.extenderURL + "/" + action
|
url := strings.TrimRight(h.extenderURL, "/") + "/" + action
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", url, bytes.NewReader(out))
|
req, err := http.NewRequest("POST", url, bytes.NewReader(out))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -242,19 +242,11 @@ func (h *HTTPExtender) send(action string, args interface{}, result interface{})
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return fmt.Errorf("Failed %v with extender at URL %v, code %v", action, h.extenderURL, resp.StatusCode)
|
return fmt.Errorf("Failed %v with extender at URL %v, code %v", action, h.extenderURL, resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
return json.NewDecoder(resp.Body).Decode(result)
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := json.Unmarshal(body, result); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user