HTTPExtender: shoud close resp.Body even when StatusCode not ok

Signed-off-by: sakeven <jc5930@sina.cn>
This commit is contained in:
sakeven 2017-06-30 18:33:17 +08:00
parent 0641386318
commit 82dff5fbd0

View File

@ -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
} }