Use global HTTP client whenever applicable (#2682)

This commit is contained in:
Harshavardhana
2023-02-27 17:19:56 -08:00
committed by GitHub
parent 372852ee86
commit dd913decc6
9 changed files with 40 additions and 58 deletions

View File

@@ -19,11 +19,11 @@ package cluster
import (
"io/ioutil"
"net"
"net/http"
"strings"
"time"
xhttp "github.com/minio/console/pkg/http"
"github.com/minio/console/restapi"
"github.com/minio/console/pkg/utils"
@@ -69,11 +69,11 @@ func GetMinioImage() (*string, error) {
if image != "" {
return &image, nil
}
client := restapi.GetConsoleHTTPClient("")
client.Timeout = 5 * time.Second
latestMinIOImage, errLatestMinIOImage := utils.GetLatestMinIOImage(
&xhttp.Client{
Client: &http.Client{
Timeout: 5 * time.Second,
},
Client: client,
})
if errLatestMinIOImage != nil {

View File

@@ -32,6 +32,7 @@ import (
"github.com/minio/console/operatorapi/operations"
"github.com/minio/console/operatorapi/operations/operator_api"
"github.com/minio/console/pkg"
"github.com/minio/console/restapi"
errors "github.com/minio/console/restapi"
"github.com/minio/pkg/env"
corev1 "k8s.io/api/core/v1"
@@ -147,7 +148,8 @@ func makePostRequestToMP(url, email string) error {
if err != nil {
return err
}
client := &http.Client{Timeout: 3 * time.Second}
client := restapi.GetConsoleHTTPClient("")
client.Timeout = 3 * time.Second
if res, err := client.Do(request); err != nil {
return err
} else if res.StatusCode >= http.StatusBadRequest {

View File

@@ -19,7 +19,6 @@ package operatorapi
import (
"bytes"
"crypto/sha1"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
@@ -38,6 +37,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/minio/console/cluster"
"github.com/minio/console/restapi"
"github.com/minio/console/pkg/auth"
)
@@ -148,12 +148,8 @@ func serveProxy(responseWriter http.ResponseWriter, req *http.Request) {
}
loginReq.Header.Add("Content-Type", "application/json")
// FIXME: in the future we should use restapi.GetConsoleHTTPClient()
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
// use localhost to ensure 'InsecureSkipVerify'
client := restapi.GetConsoleHTTPClient("http://127.0.0.1")
loginResp, err := client.Do(loginReq)
if err != nil {
log.Println(err)
@@ -222,16 +218,11 @@ func serveProxy(responseWriter http.ResponseWriter, req *http.Request) {
}
func handleHTTPRequest(responseWriter http.ResponseWriter, req *http.Request, proxyCookieJar *cookiejar.Jar, tenantBase string, targetURL *url2.URL) {
tr := &http.Transport{
// FIXME: use restapi.GetConsoleHTTPClient()
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
Transport: tr,
Jar: proxyCookieJar,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
// use localhost to ensure 'InsecureSkipVerify'
client := restapi.GetConsoleHTTPClient("http://127.0.0.1")
client.Jar = proxyCookieJar
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
// are we proxying something with cp=y? (console proxy) then add cpb (console proxy base) so the console

View File

@@ -26,7 +26,6 @@ import (
"errors"
"fmt"
"net"
"net/http"
"sort"
"strconv"
"strings"
@@ -1440,10 +1439,10 @@ func getUpdateTenantResponse(session *models.Principal, params operator_api.Upda
opClient := &operatorClient{
client: opClientClientSet,
}
client := restapi.GetConsoleHTTPClient("")
client.Timeout = 4 * time.Second
httpC := &utils2.Client{
Client: &http.Client{
Timeout: 4 * time.Second,
},
Client: client,
}
if err := updateTenantAction(ctx, opClient, k8sClient, httpC, params.Namespace, params); err != nil {
return restapi.ErrorWithContext(ctx, err, errors.New("unable to update tenant"))

View File

@@ -124,11 +124,6 @@ func getListConfigResponse(session *models.Principal, params cfgApi.ListConfigPa
}
// getConfig gets the key values for a defined configuration.
//
// FIXME: This currently only returns config parameters in the default target
// `madmin.Default`. Some configuration sub-systems are multi-target and since
// this function does not accept a target argument, it ignores all non-default
// targets.
func getConfig(ctx context.Context, client MinioAdmin, name string) ([]*models.Configuration, error) {
configBytes, err := client.getConfigKV(ctx, name)
if err != nil {

View File

@@ -21,6 +21,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
"github.com/go-openapi/runtime/middleware"
@@ -78,20 +79,23 @@ func getReleaseServiceURL() string {
return fmt.Sprintf("%s/releases", host)
}
func getReleases(url, repo, currentRelease, search, filter string) (*models.ReleaseListResponse, error) {
func getReleases(endpoint, repo, currentRelease, search, filter string) (*models.ReleaseListResponse, error) {
rl := &models.ReleaseListResponse{}
client := &http.Client{Timeout: time.Second * 5}
req, err := http.NewRequest("GET", url, nil)
q := req.URL.Query()
q.Add("repo", repo)
q.Add("current", currentRelease)
q.Add("search", search)
q.Add("filter", filter)
req.URL.RawQuery = q.Encode()
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
q := &url.Values{}
q.Add("repo", repo)
q.Add("search", search)
q.Add("filter", filter)
q.Add("current", currentRelease)
req.URL.RawQuery = q.Encode()
req.Header.Set("Content-Type", "application/json")
client := GetConsoleHTTPClient("")
client.Timeout = time.Second * 5
resp, err := client.Do(req)
if err != nil {
return nil, err

View File

@@ -195,23 +195,17 @@ func SubnetLoginWithMFA(client xhttp.ClientI, username, mfaToken, otp string) (*
// GetSubnetHTTPClient will return a client with proxy if configured, otherwise will return the default console http client
func GetSubnetHTTPClient(ctx context.Context, minioClient MinioAdmin) (*xhttp.Client, error) {
var subnetHTTPClient *http.Client
var proxy string
envProxy := getSubnetProxy()
subnetHTTPClient := GetConsoleHTTPClient("")
subnetKey, err := GetSubnetKeyFromMinIOConfig(ctx, minioClient)
if err != nil {
return nil, err
}
proxy := getSubnetProxy()
if subnetKey.Proxy != "" {
proxy = subnetKey.Proxy
} else if envProxy != "" {
proxy = envProxy
}
if proxy != "" {
transport := PrepareSTSClientTransport(false)
subnetHTTPClient = &http.Client{
Transport: transport,
}
subnetProxyURL, err := url.Parse(proxy)
if err != nil {
return nil, err

View File

@@ -19,7 +19,6 @@ package restapi
import (
"os"
"github.com/minio/console/pkg/http"
"github.com/minio/console/pkg/subnet"
)
@@ -45,10 +44,7 @@ func (sp SubnetPlan) String() string {
var InstanceLicensePlan = PlanAGPL
func fetchLicensePlan() {
client := &http.Client{
Client: GetConsoleHTTPClient(""),
}
licenseInfo, err := subnet.ParseLicense(client, os.Getenv(EnvSubnetLicense))
licenseInfo, err := subnet.ParseLicense(GetConsoleHTTPClient(""), os.Getenv(EnvSubnetLicense))
if err != nil {
return
}

View File

@@ -18,7 +18,6 @@ package restapi
import (
"context"
"net/http"
"time"
xhttp "github.com/minio/console/pkg/http"
@@ -44,10 +43,12 @@ func registerVersionHandlers(api *operations.ConsoleAPI) {
func getVersionResponse(params systemApi.CheckMinIOVersionParams) (*models.CheckVersionResponse, *models.Error) {
ctx, cancel := context.WithCancel(params.HTTPRequest.Context())
defer cancel()
client := GetConsoleHTTPClient("")
client.Timeout = 15 * time.Second
ver, err := utils.GetLatestMinIOImage(&xhttp.Client{
Client: &http.Client{
Timeout: 15 * time.Second,
},
Client: client,
})
if err != nil {
return nil, ErrorWithContext(ctx, err)