mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-01 15:07:17 +08:00
Merge pull request #6221 from yousong/bugfix/yousong-proxysetting
multicloud: avoid changing transport of global default http client
This commit is contained in:
@@ -17,6 +17,8 @@ package cloudprovider
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
@@ -155,6 +157,18 @@ type ProviderConfig struct {
|
||||
ProxyFunc httputils.TransportProxyFunc
|
||||
}
|
||||
|
||||
func (cp *ProviderConfig) HttpClient() *http.Client {
|
||||
client := httputils.GetClient(true, 15*time.Second)
|
||||
httputils.SetClientProxyFunc(client, cp.ProxyFunc)
|
||||
return client
|
||||
}
|
||||
|
||||
func (cp *ProviderConfig) AdaptiveTimeoutHttpClient() *http.Client {
|
||||
client := httputils.GetAdaptiveTimeoutClient()
|
||||
httputils.SetClientProxyFunc(client, cp.ProxyFunc)
|
||||
return client
|
||||
}
|
||||
|
||||
type ICloudProviderFactory interface {
|
||||
GetProvider(cfg ProviderConfig) (ICloudProvider, error)
|
||||
|
||||
|
||||
@@ -276,8 +276,7 @@ func (client *SAliyunClient) getOssClient(regionId string) (*oss.Client, error)
|
||||
// which can be used to whitelist ips, domains from http_proxy,
|
||||
// https_proxy setting
|
||||
// oss use no timeout client so as to send/download large files
|
||||
httpClient := httputils.GetAdaptiveTimeoutClient()
|
||||
httputils.SetClientProxyFunc(httpClient, client.cpcfg.ProxyFunc)
|
||||
httpClient := client.cpcfg.AdaptiveTimeoutHttpClient()
|
||||
cliOpts := []oss.ClientOption{
|
||||
oss.HTTPClient(httpClient),
|
||||
}
|
||||
|
||||
@@ -29,7 +29,6 @@ import (
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/cloudprovider"
|
||||
"yunion.io/x/onecloud/pkg/util/httputils"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -189,8 +188,7 @@ func (self *SAwsClient) fetchRegions() error {
|
||||
}
|
||||
|
||||
func (client *SAwsClient) getAwsSession(regionId string) (*session.Session, error) {
|
||||
httpClient := httputils.GetDefaultClient()
|
||||
httputils.SetClientProxyFunc(httpClient, client.cpcfg.ProxyFunc)
|
||||
httpClient := client.cpcfg.HttpClient()
|
||||
return session.NewSession(&sdk.Config{
|
||||
Region: sdk.String(regionId),
|
||||
Credentials: credentials.NewStaticCredentials(
|
||||
|
||||
@@ -33,7 +33,6 @@ import (
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/cloudprovider"
|
||||
"yunion.io/x/onecloud/pkg/util/httputils"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -153,8 +152,7 @@ func (self *SAzureClient) getDefaultClient() (*autorest.Client, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
httpClient := httputils.GetDefaultClient()
|
||||
httputils.SetClientProxyFunc(httpClient, self.cpcfg.ProxyFunc)
|
||||
httpClient := self.cpcfg.HttpClient()
|
||||
client.Sender = httpClient
|
||||
|
||||
self.env = env
|
||||
|
||||
@@ -85,8 +85,7 @@ type SCtyunClient struct {
|
||||
}
|
||||
|
||||
func NewSCtyunClient(cfg *CtyunClientConfig) (*SCtyunClient, error) {
|
||||
httpClient := httputils.GetDefaultClient()
|
||||
httputils.SetClientProxyFunc(httpClient, cfg.cpcfg.ProxyFunc)
|
||||
httpClient := cfg.cpcfg.HttpClient()
|
||||
client := &SCtyunClient{
|
||||
CtyunClientConfig: cfg,
|
||||
httpClient: httpClient,
|
||||
|
||||
@@ -136,8 +136,7 @@ func NewGoogleClient(cfg *GoogleClientConfig) (*SGoogleClient, error) {
|
||||
TokenURL: google.JWTTokenURL,
|
||||
}
|
||||
|
||||
httpClient := httputils.GetDefaultClient()
|
||||
httputils.SetClientProxyFunc(httpClient, cfg.cpcfg.ProxyFunc)
|
||||
httpClient := cfg.cpcfg.HttpClient()
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient)
|
||||
|
||||
|
||||
@@ -27,7 +27,6 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/multicloud/huawei/client/auth"
|
||||
"yunion.io/x/onecloud/pkg/multicloud/huawei/client/auth/credentials"
|
||||
"yunion.io/x/onecloud/pkg/multicloud/huawei/obs"
|
||||
"yunion.io/x/onecloud/pkg/util/httputils"
|
||||
)
|
||||
|
||||
/*
|
||||
@@ -143,8 +142,7 @@ func (self *SHuaweiClient) newRegionAPIClient(regionId string) (*client.Client,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
httpClient := httputils.GetDefaultClient()
|
||||
httputils.SetClientProxyFunc(httpClient, self.cpcfg.ProxyFunc)
|
||||
httpClient := self.cpcfg.HttpClient()
|
||||
cli.SetHttpClient(httpClient)
|
||||
|
||||
return cli, nil
|
||||
@@ -156,8 +154,7 @@ func (self *SHuaweiClient) newGeneralAPIClient() (*client.Client, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
httpClient := httputils.GetDefaultClient()
|
||||
httputils.SetClientProxyFunc(httpClient, self.cpcfg.ProxyFunc)
|
||||
httpClient := self.cpcfg.HttpClient()
|
||||
cli.SetHttpClient(httpClient)
|
||||
|
||||
return cli, nil
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
|
||||
"yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/cloudprovider"
|
||||
"yunion.io/x/onecloud/pkg/util/httputils"
|
||||
)
|
||||
|
||||
func TestProxyFunc(t *testing.T) {
|
||||
@@ -39,6 +40,14 @@ func TestProxyFunc(t *testing.T) {
|
||||
}
|
||||
t.Errorf("vendor %s: proxyFunc not working", vendor)
|
||||
}
|
||||
t.Run("default client no proxy", func(t *testing.T) {
|
||||
proxied = false
|
||||
client := httputils.GetDefaultClient()
|
||||
client.Get("http://default-client-no-proxy.TestProxyFunc." + vendor + "/")
|
||||
if proxied {
|
||||
t.Errorf("%s: default client proxy changed", vendor)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
t.Parallel()
|
||||
|
||||
@@ -429,13 +429,11 @@ func (client *SQcloudClient) GetRegions() []SRegion {
|
||||
}
|
||||
|
||||
func (client *SQcloudClient) getDefaultClient() (*common.Client, error) {
|
||||
httpClient := httputils.GetDefaultClient()
|
||||
httputils.SetClientProxyFunc(httpClient, client.cpcfg.ProxyFunc)
|
||||
|
||||
cli, err := common.NewClientWithSecretId(client.secretId, client.secretKey, QCLOUD_DEFAULT_REGION)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
httpClient := client.cpcfg.HttpClient()
|
||||
cli.WithHttpTransport(httpClient.Transport)
|
||||
return cli, nil
|
||||
}
|
||||
|
||||
@@ -24,7 +24,6 @@ import (
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/cloudprovider"
|
||||
"yunion.io/x/onecloud/pkg/util/httputils"
|
||||
)
|
||||
|
||||
/*
|
||||
@@ -102,9 +101,7 @@ type SUcloudClient struct {
|
||||
// 进行资源操作时参数account 对应数据库cloudprovider表中的account字段,由accessKey和projectID两部分组成,通过"/"分割。
|
||||
// 初次导入Subaccount时,参数account对应cloudaccounts表中的account字段,即accesskey。此时projectID为空,只能进行同步子账号(项目)、查询region列表等projectId无关的操作。
|
||||
func NewUcloudClient(cfg *UcloudClientConfig) (*SUcloudClient, error) {
|
||||
httpClient := httputils.GetDefaultClient()
|
||||
httputils.SetClientProxyFunc(httpClient, cfg.cpcfg.ProxyFunc)
|
||||
|
||||
httpClient := cfg.cpcfg.HttpClient()
|
||||
client := SUcloudClient{
|
||||
UcloudClientConfig: cfg,
|
||||
httpClient: httpClient,
|
||||
|
||||
@@ -103,9 +103,7 @@ func getSignUrl(uri string) (string, error) {
|
||||
}
|
||||
|
||||
func NewZStackClient(cfg *ZstackClientConfig) (*SZStackClient, error) {
|
||||
httpClient := httputils.GetDefaultClient()
|
||||
httputils.SetClientProxyFunc(httpClient, cfg.cpcfg.ProxyFunc)
|
||||
|
||||
httpClient := cfg.cpcfg.HttpClient()
|
||||
cli := &SZStackClient{
|
||||
ZstackClientConfig: cfg,
|
||||
httpClient: httpClient,
|
||||
|
||||
Reference in New Issue
Block a user