diff --git a/bcs-services/cluster-resources/cmd/http.go b/bcs-services/cluster-resources/cmd/http.go index 4d0a4b4bd9..fcc600333c 100644 --- a/bcs-services/cluster-resources/cmd/http.go +++ b/bcs-services/cluster-resources/cmd/http.go @@ -15,7 +15,6 @@ package cmd import ( "archive/tar" - "bytes" "compress/gzip" "encoding/json" "errors" @@ -25,7 +24,6 @@ import ( "net/url" "os" "path/filepath" - "strconv" "strings" "time" @@ -40,7 +38,6 @@ import ( "github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/util/contextx" httpUtil "github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/util/http" "github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/util/httpx" - "github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/util/stringx" ) // SpaceNames model template space name @@ -69,8 +66,8 @@ func NewAPIRouter(crs *clusterResourcesService) *mux.Router { r.Use(httpx.AuthorizationMiddleware) r.Use(httpx.AuditMiddleware) - // events接口反向代理,单独对cluster进行鉴权 - r.Methods("GET").Path("/clusterresources/api/v1/projects/{projectCode}/clusters/{clusterID}/storage/events"). + // events 接口代理 + r.Methods("GET").Path("/clusterresources/api/v1/projects/{projectCode}/clusters/{clusterID}/events"). Handler(httpx.ParseClusterIDMiddleware(http.HandlerFunc(StorageEvents(crs)))) // import template r.Methods("POST").Path("/clusterresources/api/v1/projects/{projectCode}/import/template"). @@ -84,53 +81,26 @@ func NewAPIRouter(crs *clusterResourcesService) *mux.Router { // StorageEvents reverse proxy events func StorageEvents(crs *clusterResourcesService) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { - targetURL := fmt.Sprintf("%s/bcsapi/v4/storage/events", config.G.BCSAPIGW.Host) + targetURLPath := fmt.Sprintf("%s/bcsstorage/v1/events", config.G.Component.BCSStorageHost) - targetURLParse, err := url.Parse(targetURL) + targetURL, err := url.Parse(targetURLPath) if err != nil { httpx.ResponseSystemError(w, r, err) return } clusterID := contextx.GetClusterIDFromCtx(r.Context()) + query := r.URL.Query() + query.Set("clusterId", clusterID) + targetURL.RawQuery = query.Encode() - postData := paramToPostData(r.URL) - postData["clusterId"] = clusterID - b, err := json.Marshal(postData) - if err != nil { - httpx.ResponseSystemError(w, r, err) - return - } - - proxy := httpUtil.NewHTTPReverseProxy(crs.clientTLSConfig, modifyRequest(targetURLParse, b)) + proxy := httpUtil.NewHTTPReverseProxy(crs.clientTLSConfig, func(request *http.Request) { + request.URL = targetURL + request.Method = http.MethodGet + }) proxy.ServeHTTP(w, r) } } -// GET参数转post data -func paramToPostData(reqUrl *url.URL) map[string]interface{} { - query := reqUrl.Query() - postData := make(map[string]interface{}) - for k := range query { - if k == "offset" || k == "length" { - postData[k] = stringx.GetIntOrDefault(query.Get(k)) - continue - } - postData[k] = query.Get(k) - } - return postData -} - -// 反向代理请求处理 -func modifyRequest(targetURL *url.URL, reqBody []byte) func(r *http.Request) { - return func(r *http.Request) { - r.URL = targetURL - r.Method = http.MethodPost - r.Header.Set("Content-Length", strconv.Itoa(len(reqBody))) - r.Body = http.NoBody - r.Body = io.NopCloser(bytes.NewReader(reqBody)) - } -} - // ImportTemplate import template func ImportTemplate(crs *clusterResourcesService) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { diff --git a/bcs-services/cluster-resources/pkg/config/config.go b/bcs-services/cluster-resources/pkg/config/config.go index cc0a76fbd2..7add87363c 100644 --- a/bcs-services/cluster-resources/pkg/config/config.go +++ b/bcs-services/cluster-resources/pkg/config/config.go @@ -98,6 +98,7 @@ func LoadConf(filePath string) (*ClusterResourcesConf, error) { conf.initJWTPubKey, // 初始化 iam conf.initIAM, + conf.initCompoment, } { if initErr := f(); initErr != nil { return nil, initErr @@ -223,6 +224,14 @@ func (c *ClusterResourcesConf) initIAM() error { return nil } +// initCompoment 初始化 compoment +func (c *ClusterResourcesConf) initCompoment() error { + if c.Global.Component.BCSStorageHost == "" { + c.Global.Component.BCSStorageHost = "https://bcs-storage:50024" + } + return nil +} + // EtcdConf Etcd 相关配置 type EtcdConf struct { EtcdEndpoints string `yaml:"endpoints" usage:"Etcd Endpoints"` @@ -307,11 +316,17 @@ type GlobalConf struct { Auth AuthConf `yaml:"auth"` Basic BasicConf `yaml:"basic"` BCSAPIGW BCSAPIGatewayConf `yaml:"bcsApiGW"` // nolint:tagliatelle + Component ComponentConf `yaml:"component"` IAM IAMConf `yaml:"iam"` SharedCluster SharedClusterConf `yaml:"sharedCluster"` MultiCluster MultiClusterConf `yaml:"multiCluster"` } +// ComponentConf 组件配置 +type ComponentConf struct { + BCSStorageHost string `yaml:"bcsStorageHost" usage:"BCS Storage Host"` +} + // AuthConf 认证相关配置 type AuthConf struct { Disabled bool `yaml:"disabled" usage:"是否禁用身份认证"`