Skip to content

Commit

Permalink
Have Sidecar reuse the same HTTP client and discard request body (#2213)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Valdivia <18384552+dvaldivia@users.noreply.github.com>
Co-authored-by: pjuarezd <pjuarezd@users.noreply.github.com>
  • Loading branch information
dvaldivia and pjuarezd committed Jul 16, 2024
1 parent 334c691 commit 3e8321f
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions sidecar/pkg/sidecar/webhook_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package sidecar
import (
"crypto/tls"
"fmt"
"io"
"net/http"
"time"

Expand Down Expand Up @@ -78,6 +79,14 @@ func configureProbesServer(c *Controller, tenantTLS bool) *http.Server {
return s
}

// we do insecure skip verify because we are checking against the local instance and don't care for the certificate
var probeHTTPClient = &http.Client{
Timeout: time.Millisecond * 500,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}

func readinessHandler(tenantTLS bool) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
schema := "https"
Expand All @@ -92,21 +101,13 @@ func readinessHandler(tenantTLS bool) func(w http.ResponseWriter, r *http.Reques
return
}

// we do insecure skip verify because we are checking against the local instance and don't care for the
// certificate
client := &http.Client{
Timeout: time.Millisecond * 500,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}

response, err := client.Do(request)
response, err := probeHTTPClient.Do(request)
if err != nil {
http.Error(w, fmt.Sprintf("HTTP request failed: %s", err), http.StatusInternalServerError)
return
}
defer response.Body.Close()
_, _ = io.Copy(io.Discard, response.Body) // Discard body to enable connection reuse

if response.StatusCode == 403 {
fmt.Fprintln(w, "Readiness probe succeeded.")
Expand Down

0 comments on commit 3e8321f

Please sign in to comment.