From dc553d8f917c695c312f5a27a9730fa8503ccda0 Mon Sep 17 00:00:00 2001 From: Manuel Alejandro de Brito Fontes Date: Mon, 15 Oct 2018 16:29:32 -0300 Subject: [PATCH] Refactor probe url requests --- internal/ingress/controller/checker.go | 36 +++++++++++++++++++++----- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/internal/ingress/controller/checker.go b/internal/ingress/controller/checker.go index 118cbeb3fb..b595dd5325 100644 --- a/internal/ingress/controller/checker.go +++ b/internal/ingress/controller/checker.go @@ -21,6 +21,7 @@ import ( "net/http" "strconv" "strings" + "time" "github.com/ncabatoff/process-exporter/proc" "github.com/pkg/errors" @@ -35,21 +36,24 @@ func (n NGINXController) Name() string { // Check returns if the nginx healthz endpoint is returning ok (status code 200) func (n *NGINXController) Check(_ *http.Request) error { - res, err := http.Get(fmt.Sprintf("http://127.0.0.1:%v%v", n.cfg.ListenPorts.Status, ngxHealthPath)) + + url := fmt.Sprintf("http://127.0.0.1:%v%v", n.cfg.ListenPorts.Status, ngxHealthPath) + statusCode, err := simpleGet(url) if err != nil { return err } - defer res.Body.Close() - if res.StatusCode != 200 { + + if statusCode != 200 { return fmt.Errorf("ingress controller is not healthy") } - res, err = http.Get(fmt.Sprintf("http://127.0.0.1:%v/is-dynamic-lb-initialized", n.cfg.ListenPorts.Status)) + url = fmt.Sprintf("http://127.0.0.1:%v/is-dynamic-lb-initialized", n.cfg.ListenPorts.Status) + statusCode, err = simpleGet(url) if err != nil { return err } - defer res.Body.Close() - if res.StatusCode != 200 { + + if statusCode != 200 { return fmt.Errorf("dynamic load balancer not started") } @@ -70,3 +74,23 @@ func (n *NGINXController) Check(_ *http.Request) error { return err } + +func simpleGet(url string) (int, error) { + client := &http.Client{ + Timeout: 10 * time.Second, + Transport: &http.Transport{DisableKeepAlives: true}, + } + + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return -1, err + } + + res, err := client.Do(req) + if err != nil { + return -1, err + } + defer res.Body.Close() + + return res.StatusCode, nil +}